Module: Gitlab::Database::SchemaHelpers

Included in:
PartitioningMigrationHelpers::ForeignKeyHelpers, PartitioningMigrationHelpers::TableManagementHelpers
Defined in:
lib/gitlab/database/schema_helpers.rb

Instance Method Summary collapse

Instance Method Details

#assert_not_in_transaction_block(scope:) ⇒ Object


77
78
79
80
81
82
83
# File 'lib/gitlab/database/schema_helpers.rb', line 77

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

#create_comment(type, name, text) ⇒ Object


53
54
55
# File 'lib/gitlab/database/schema_helpers.rb', line 53

def create_comment(type, name, text)
  execute("COMMENT ON #{type} #{name} IS '#{text}'")
end

#create_trigger(table_name, name, function_name, fires:) ⇒ Object


23
24
25
26
27
28
29
30
# File 'lib/gitlab/database/schema_helpers.rb', line 23

def create_trigger(table_name, name, function_name, fires:)
  execute(<<~SQL)
    CREATE TRIGGER #{name}
    #{fires} ON #{table_name}
    FOR EACH ROW
    EXECUTE FUNCTION #{function_name}()
  SQL
end

#create_trigger_function(name, replace: true) ⇒ Object


6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/gitlab/database/schema_helpers.rb', line 6

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

#drop_function(name, if_exists: true) ⇒ Object


43
44
45
46
# File 'lib/gitlab/database/schema_helpers.rb', line 43

def drop_function(name, if_exists: true)
  exists_clause = optional_clause(if_exists, "IF EXISTS")
  execute("DROP FUNCTION #{exists_clause} #{name}()")
end

#drop_trigger(table_name, name, if_exists: true) ⇒ Object


48
49
50
51
# File 'lib/gitlab/database/schema_helpers.rb', line 48

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

#function_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)

19
20
21
# File 'lib/gitlab/database/schema_helpers.rb', line 19

def function_exists?(name)
  connection.select_value("SELECT 1 FROM pg_proc WHERE proname = '#{name}'")
end

#object_name(table, type) ⇒ Object


63
64
65
66
67
68
# File 'lib/gitlab/database/schema_helpers.rb', line 63

def object_name(table, type)
  identifier = "#{table}_#{type}"
  hashed_identifier = Digest::SHA256.hexdigest(identifier).first(10)

  "#{type}_#{hashed_identifier}"
end

#tmp_table_name(base) ⇒ Object


57
58
59
60
61
# File 'lib/gitlab/database/schema_helpers.rb', line 57

def tmp_table_name(base)
  hashed_base = Digest::SHA256.hexdigest(base).first(10)

  "#{base}_#{hashed_base}"
end

#trigger_exists?(table_name, name) ⇒ Boolean

Returns:

  • (Boolean)

32
33
34
35
36
37
38
39
40
41
# File 'lib/gitlab/database/schema_helpers.rb', line 32

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

#with_lock_retries(&block) ⇒ Object


70
71
72
73
74
75
# File 'lib/gitlab/database/schema_helpers.rb', line 70

def with_lock_retries(&block)
  Gitlab::Database::WithLockRetries.new({
    klass: self.class,
    logger: Gitlab::BackgroundMigration::Logger
  }).run(&block)
end