summaryrefslogtreecommitdiff
path: root/lib/gitlab/database/grant.rb
diff options
context:
space:
mode:
authorYorick Peterse <yorickpeterse@gmail.com>2017-08-18 14:26:23 +0200
committerYorick Peterse <yorickpeterse@gmail.com>2017-08-29 13:02:44 +0200
commit5eab624d3ce7500b3cc19230b5ce86125b88015b (patch)
treecbfd2c470c9009fce1449a837d60058bed026aa0 /lib/gitlab/database/grant.rb
parent9e7e0496ff639d1eec65dcbf1b51edb2262456e2 (diff)
downloadgitlab-ce-5eab624d3ce7500b3cc19230b5ce86125b88015b.tar.gz
Improve migrations using triggerscheck-trigger-permissions-mysql
This adds a bunch of checks to migrations that may create or drop triggers. Dropping triggers/functions is done using "IF EXISTS" so we don't throw an error if the object in question has already been dropped. We now also raise a custom error (message) when the user does not have TRIGGER privileges. This should prevent the schema from entering an inconsistent state while also providing the user with enough information on how to solve the problem. The recommendation of using SUPERUSER permissions is a bit extreme but we require this anyway (Omnibus also configures users with this permission). Fixes https://gitlab.com/gitlab-org/gitlab-ce/issues/36633
Diffstat (limited to 'lib/gitlab/database/grant.rb')
-rw-r--r--lib/gitlab/database/grant.rb34
1 files changed, 34 insertions, 0 deletions
diff --git a/lib/gitlab/database/grant.rb b/lib/gitlab/database/grant.rb
new file mode 100644
index 00000000000..aee3981e79a
--- /dev/null
+++ b/lib/gitlab/database/grant.rb
@@ -0,0 +1,34 @@
+module Gitlab
+ module Database
+ # Model that can be used for querying permissions of a SQL user.
+ class Grant < ActiveRecord::Base
+ self.table_name =
+ if Database.postgresql?
+ 'information_schema.role_table_grants'
+ else
+ 'mysql.user'
+ end
+
+ def self.scope_to_current_user
+ if Database.postgresql?
+ where('grantee = user')
+ else
+ where("CONCAT(User, '@', Host) = current_user()")
+ end
+ end
+
+ # Returns true if the current user can create and execute triggers on the
+ # given table.
+ def self.create_and_execute_trigger?(table)
+ priv =
+ if Database.postgresql?
+ where(privilege_type: 'TRIGGER', table_name: table)
+ else
+ where(Trigger_priv: 'Y')
+ end
+
+ priv.scope_to_current_user.any?
+ end
+ end
+ end
+end