diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-05-20 14:34:42 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-05-20 14:34:42 +0000 |
commit | 9f46488805e86b1bc341ea1620b866016c2ce5ed (patch) | |
tree | f9748c7e287041e37d6da49e0a29c9511dc34768 /lib/gitlab/database/schema_helpers.rb | |
parent | dfc92d081ea0332d69c8aca2f0e745cb48ae5e6d (diff) | |
download | gitlab-ce-9f46488805e86b1bc341ea1620b866016c2ce5ed.tar.gz |
Add latest changes from gitlab-org/gitlab@13-0-stable-ee
Diffstat (limited to 'lib/gitlab/database/schema_helpers.rb')
-rw-r--r-- | lib/gitlab/database/schema_helpers.rb | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/lib/gitlab/database/schema_helpers.rb b/lib/gitlab/database/schema_helpers.rb new file mode 100644 index 00000000000..f8d01c78ae8 --- /dev/null +++ b/lib/gitlab/database/schema_helpers.rb @@ -0,0 +1,52 @@ +# frozen_string_literal: true + +module Gitlab + module Database + module SchemaHelpers + def create_trigger_function(name, replace: true) + replace_clause = optional_clause(replace, "OR REPLACE") + execute(<<~SQL) + CREATE #{replace_clause} FUNCTION #{name}() + RETURNS TRIGGER AS + $$ + BEGIN + #{yield} + END + $$ LANGUAGE PLPGSQL + SQL + end + + def create_function_trigger(name, fn_name, fires: nil) + execute(<<~SQL) + CREATE TRIGGER #{name} + #{fires} + FOR EACH ROW + EXECUTE PROCEDURE #{fn_name}() + SQL + end + + def drop_function(name, if_exists: true) + exists_clause = optional_clause(if_exists, "IF EXISTS") + execute("DROP FUNCTION #{exists_clause} #{name}()") + end + + def drop_trigger(table_name, name, if_exists: true) + exists_clause = optional_clause(if_exists, "IF EXISTS") + execute("DROP TRIGGER #{exists_clause} #{name} ON #{table_name}") + end + + def object_name(table, type) + identifier = "#{table}_#{type}" + hashed_identifier = Digest::SHA256.hexdigest(identifier).first(10) + + "#{type}_#{hashed_identifier}" + end + + private + + def optional_clause(flag, clause) + flag ? clause : "" + end + end + end +end |