-
Notifications
You must be signed in to change notification settings - Fork 794
/
Copy pathmysql.rb
63 lines (50 loc) · 2.03 KB
/
mysql.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# frozen_string_literal: true
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'mysql'))
Puppet::Type.type(:mysql_database).provide(:mysql, parent: Puppet::Provider::Mysql) do
desc 'Manages MySQL databases.'
commands mysql_raw: 'mysql'
def self.instances
mysql_caller('SELECT SCHEMA_NAME, DEFAULT_CHARACTER_SET_NAME, DEFAULT_COLLATION_NAME FROM information_schema.SCHEMATA', 'regular').split("\n").map do |line|
name, charset, collate = line.split(%r{\s})
new(name: name,
ensure: :present,
charset: charset,
collate: collate)
end
end
# We iterate over each mysql_database entry in the catalog and compare it against
# the contents of the property_hash generated by self.instances
def self.prefetch(resources)
databases = instances
resources.each_key do |database|
provider = databases.find { |db| db.name == database }
resources[database].provider = provider if provider
end
end
def create
self.class.mysql_caller("create database if not exists `#{@resource[:name]}` character set `#{@resource[:charset]}` collate `#{@resource[:collate]}`", 'regular')
@property_hash[:ensure] = :present
@property_hash[:charset] = @resource[:charset]
@property_hash[:collate] = @resource[:collate]
exists? ? (return true) : (return false)
end
def destroy
self.class.mysql_caller("drop database if exists `#{@resource[:name]}`", 'regular')
@property_hash.clear
exists? ? (return false) : (return true)
end
def exists?
@property_hash[:ensure] == :present || false
end
mk_resource_methods
def charset=(value)
self.class.mysql_caller("alter database `#{resource[:name]}` CHARACTER SET #{value}", 'regular')
@property_hash[:charset] = value
(charset == value) ? (return true) : (return false)
end
def collate=(value)
self.class.mysql_caller("alter database `#{resource[:name]}` COLLATE #{value}", 'regular')
@property_hash[:collate] = value
(collate == value) ? (return true) : (return false)
end
end