7
7
commands mysql_raw : 'mysql'
8
8
9
9
def self . instances
10
- instances = [ ]
10
+ instance_configs = { }
11
11
users . map do |user |
12
12
user_string = cmd_user ( user )
13
13
query = "SHOW GRANTS FOR #{ user_string } ;"
@@ -45,13 +45,6 @@ def self.instances
45
45
( priv == 'ALL PRIVILEGES' ) ? 'ALL' : priv . strip
46
46
end
47
47
end
48
- sorted_privileges = stripped_privileges . sort
49
- if newer_than ( 'mysql' => '8.0.0' ) && sorted_privileges == [ 'ALTER' , 'ALTER ROUTINE' , 'CREATE' , 'CREATE ROLE' , 'CREATE ROUTINE' , 'CREATE TABLESPACE' , 'CREATE TEMPORARY TABLES' , 'CREATE USER' ,
50
- 'CREATE VIEW' , 'DELETE' , 'DROP' , 'DROP ROLE' , 'EVENT' , 'EXECUTE' , 'FILE' , 'INDEX' , 'INSERT' , 'LOCK TABLES' , 'PROCESS' , 'REFERENCES' ,
51
- 'RELOAD' , 'REPLICATION CLIENT' , 'REPLICATION SLAVE' , 'SELECT' , 'SHOW DATABASES' , 'SHOW VIEW' , 'SHUTDOWN' , 'SUPER' , 'TRIGGER' ,
52
- 'UPDATE' ]
53
- sorted_privileges = [ 'ALL' ]
54
- end
55
48
# Same here, but to remove OPTION leaving just GRANT.
56
49
options = if %r{WITH\s GRANT\s OPTION} . match? ( rest )
57
50
[ 'GRANT' ]
@@ -61,16 +54,40 @@ def self.instances
61
54
# fix double backslash that MySQL prints, so resources match
62
55
table . gsub! ( '\\\\' , '\\' )
63
56
# We need to return an array of instances so capture these
64
- instances << new (
65
- name : "#{ user } @#{ host } /#{ table } " ,
66
- ensure : :present ,
57
+ name = "#{ user } @#{ host } /#{ table } "
58
+ if instance_configs . key? ( name )
59
+ instance_config = instance_configs [ name ]
60
+ stripped_privileges . concat instance_config [ :privileges ]
61
+ options . concat instance_config [ :options ]
62
+ end
63
+
64
+ sorted_privileges = stripped_privileges . uniq . sort
65
+ if newer_than ( 'mysql' => '8.0.0' ) && sorted_privileges == [ 'ALTER' , 'ALTER ROUTINE' , 'CREATE' , 'CREATE ROLE' , 'CREATE ROUTINE' , 'CREATE TABLESPACE' , 'CREATE TEMPORARY TABLES' , 'CREATE USER' ,
66
+ 'CREATE VIEW' , 'DELETE' , 'DROP' , 'DROP ROLE' , 'EVENT' , 'EXECUTE' , 'FILE' , 'INDEX' , 'INSERT' , 'LOCK TABLES' , 'PROCESS' , 'REFERENCES' ,
67
+ 'RELOAD' , 'REPLICATION CLIENT' , 'REPLICATION SLAVE' , 'SELECT' , 'SHOW DATABASES' , 'SHOW VIEW' , 'SHUTDOWN' , 'SUPER' , 'TRIGGER' ,
68
+ 'UPDATE' ]
69
+ sorted_privileges = [ 'ALL' ]
70
+ end
71
+
72
+ instance_configs [ name ] = {
67
73
privileges : sorted_privileges ,
68
74
table : table ,
69
75
user : "#{ user } @#{ host } " ,
70
- options : options ,
71
- )
76
+ options : options . uniq ,
77
+ }
72
78
end
73
79
end
80
+ instances = [ ]
81
+ instance_configs . map do |name , config |
82
+ instances << new (
83
+ name : name ,
84
+ ensure : :present ,
85
+ privileges : config [ :privileges ] ,
86
+ table : config [ :table ] ,
87
+ user : config [ :user ] ,
88
+ options : config [ :options ] ,
89
+ )
90
+ end
74
91
instances
75
92
end
76
93
0 commit comments