2020-05-24 23:13:21 +05:30
|
|
|
# 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
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
def function_exists?(name)
|
|
|
|
connection.select_value("SELECT 1 FROM pg_proc WHERE proname = '#{name}'")
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_trigger(table_name, name, function_name, fires:)
|
2020-05-24 23:13:21 +05:30
|
|
|
execute(<<~SQL)
|
|
|
|
CREATE TRIGGER #{name}
|
2020-07-28 23:09:34 +05:30
|
|
|
#{fires} ON #{table_name}
|
2020-05-24 23:13:21 +05:30
|
|
|
FOR EACH ROW
|
2020-10-24 23:57:45 +05:30
|
|
|
EXECUTE FUNCTION #{function_name}()
|
2020-05-24 23:13:21 +05:30
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
def trigger_exists?(table_name, name)
|
|
|
|
connection.select_value(<<~SQL)
|
|
|
|
SELECT 1
|
|
|
|
FROM pg_trigger
|
|
|
|
INNER JOIN pg_class
|
|
|
|
ON pg_trigger.tgrelid = pg_class.oid
|
|
|
|
WHERE pg_class.relname = '#{table_name}'
|
|
|
|
AND pg_trigger.tgname = '#{name}'
|
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
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
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
def create_comment(type, name, text)
|
|
|
|
execute("COMMENT ON #{type} #{name} IS '#{text}'")
|
|
|
|
end
|
|
|
|
|
|
|
|
def tmp_table_name(base)
|
|
|
|
hashed_base = Digest::SHA256.hexdigest(base).first(10)
|
|
|
|
|
|
|
|
"#{base}_#{hashed_base}"
|
|
|
|
end
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
def object_name(table, type)
|
|
|
|
identifier = "#{table}_#{type}"
|
|
|
|
hashed_identifier = Digest::SHA256.hexdigest(identifier).first(10)
|
|
|
|
|
|
|
|
"#{type}_#{hashed_identifier}"
|
|
|
|
end
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
def with_lock_retries(&block)
|
|
|
|
Gitlab::Database::WithLockRetries.new({
|
|
|
|
klass: self.class,
|
|
|
|
logger: Gitlab::BackgroundMigration::Logger
|
|
|
|
}).run(&block)
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_not_in_transaction_block(scope:)
|
|
|
|
return unless transaction_open?
|
|
|
|
|
|
|
|
raise "#{scope} operations can not be run inside a transaction block, " \
|
|
|
|
"you can disable transaction blocks by calling disable_ddl_transaction! " \
|
|
|
|
"in the body of your migration class"
|
|
|
|
end
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
private
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
def table_for_range_partition(partition_name)
|
|
|
|
"#{Gitlab::Database::DYNAMIC_PARTITIONS_SCHEMA}.#{partition_name}"
|
|
|
|
end
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
def create_range_partition(partition_name, table_name, lower_bound, upper_bound)
|
|
|
|
execute(<<~SQL)
|
2020-07-28 23:09:34 +05:30
|
|
|
CREATE TABLE #{table_for_range_partition(partition_name)} PARTITION OF #{table_name}
|
2020-06-23 00:09:42 +05:30
|
|
|
FOR VALUES FROM (#{lower_bound}) TO (#{upper_bound})
|
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
def optional_clause(flag, clause)
|
|
|
|
flag ? clause : ""
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|