+property :database, :kind_of => String, :name_property => true
+property :permissions, :kind_of => Hash, :default => {}
+
+action :create do
+ unless mysql_databases.include?(new_resource.database)
+ converge_by("create #{new_resource}") do
+ Chef::Log.info("Creating #{new_resource}")
+ mysql_execute(:command => "CREATE DATABASE `#{new_resource.database}`")
+ end
+ end
+
+ current_permissions = mysql_databases[new_resource.database]&.dig(:permissions) || {}
+
+ new_permissions = Hash[new_resource.permissions.collect do |user, privileges|
+ [mysql_canonicalise_user(user), privileges]
+ end]
+
+ current_permissions.each_key do |user|
+ next if new_permissions[user]
+
+ converge_by("revoke all for #{user} on #{new_resource}") do
+ Chef::Log.info("Revoking all for #{user} on #{new_resource}")
+ mysql_execute(:command => "REVOKE ALL ON `#{new_resource.database}`.* FROM #{user}")
+ end
+ end
+
+ new_permissions.each do |user, new_privileges|
+ current_privileges = current_permissions[user] || {}
+ new_privileges = Array(new_privileges)
+
+ if new_privileges.include?(:all)
+ new_privileges |= (OpenStreetMap::MySQL::DATABASE_PRIVILEGES - [:grant])
+ end
+
+ OpenStreetMap::MySQL::DATABASE_PRIVILEGES.each do |privilege|
+ if new_privileges.include?(privilege)
+ unless current_privileges.include?(privilege)
+ converge_by("grant #{privilege} for #{user} on mysql database #{new_resource}") do
+ Chef::Log.info("Granting #{privilege} for #{user} on mysql database #{new_resource}")
+ mysql_execute(:command => "GRANT #{mysql_privilege_name(privilege)} ON `#{new_resource.database}`.* TO #{user}")
+ end
+ end
+ elsif current_privileges.include?(privilege)
+ converge_by("revoke #{privilege} for #{user} on #{new_resource}") do
+ Chef::Log.info("Revoking #{privilege} for #{user} on #{new_resource}")
+ mysql_execute(:command => "REVOKE #{mysql_privilege_name(privilege)} ON `#{new_resource.database}`.* FROM #{user}")
+ end
+ end
+ end
+ end
+end
+
+action :drop do
+ if mysql_databases.include?(new_resource.database)
+ converge_by("drop #{new_resource}") do
+ Chef::Log.info("Dropping #{new_resource}")
+ mysql_execute(:command => "DROP DATABASE `#{new_resource.database}`")
+ end
+ end
+end
+
+action_class do
+ include OpenStreetMap::MySQL
+end