Class: Gitlab::Database::LockWritesManager
- Inherits:
-
Object
- Object
- Gitlab::Database::LockWritesManager
- Defined in:
- lib/gitlab/database/lock_writes_manager.rb
Constant Summary collapse
- TRIGGER_FUNCTION_NAME =
'gitlab_schema_prevent_write'
- EXPECTED_TRIGGER_RECORD_COUNT =
Triggers to block INSERT / UPDATE / DELETE Triggers on TRUNCATE are not added to the information_schema.triggers See www.postgresql.org/message-id/16934.1568989957%40sss.pgh.pa.us
3
Instance Method Summary collapse
-
#initialize(table_name:, connection:, database_name:, with_retries: true, logger: nil, dry_run: false) ⇒ LockWritesManager
constructor
table_name can include schema name as a prefix.
- #lock_writes ⇒ Object
- #table_locked_for_writes? ⇒ Boolean
- #unlock_writes ⇒ Object
Constructor Details
#initialize(table_name:, connection:, database_name:, with_retries: true, logger: nil, dry_run: false) ⇒ LockWritesManager
table_name can include schema name as a prefix. For example: ‘gitlab_partitions_static.events_03’, otherwise, it will default to current used schema, for example ‘public’.
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/gitlab/database/lock_writes_manager.rb', line 15 def initialize(table_name:, connection:, database_name:, with_retries: true, logger: nil, dry_run: false) @table_name = table_name @connection = connection @database_name = database_name @logger = logger @dry_run = dry_run @with_retries = with_retries @table_name_without_schema = ActiveRecord::ConnectionAdapters::PostgreSQL::Utils .extract_schema_qualified_name(table_name.to_s) .identifier end |
Instance Method Details
#lock_writes ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/gitlab/database/lock_writes_manager.rb', line 38 def lock_writes if table_locked_for_writes? logger&.info "Skipping lock_writes, because #{table_name} is already locked for writes" return result_hash(action: 'skipped') end logger&.info "Database: '#{database_name}', Table: '#{table_name}': Lock Writes".color(:yellow) sql_statement = <<~SQL CREATE TRIGGER #{write_trigger_name} BEFORE INSERT OR UPDATE OR DELETE OR TRUNCATE ON #{table_name} FOR EACH STATEMENT EXECUTE FUNCTION #{TRIGGER_FUNCTION_NAME}(); SQL execute_sql_statement(sql_statement) result_hash(action: dry_run ? 'needs_lock' : 'locked') end |
#table_locked_for_writes? ⇒ Boolean
28 29 30 31 32 33 34 35 36 |
# File 'lib/gitlab/database/lock_writes_manager.rb', line 28 def table_locked_for_writes? query = <<~SQL SELECT COUNT(*) from information_schema.triggers WHERE event_object_table = '#{table_name_without_schema}' AND trigger_name = '#{write_trigger_name}' SQL connection.select_value(query) == EXPECTED_TRIGGER_RECORD_COUNT end |
#unlock_writes ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/gitlab/database/lock_writes_manager.rb', line 57 def unlock_writes unless table_locked_for_writes? logger&.info "Skipping unlock_writes, because #{table_name} is already unlocked for writes" return result_hash(action: 'skipped') end logger&.info "Database: '#{database_name}', Table: '#{table_name}': Allow Writes".color(:green) sql_statement = <<~SQL DROP TRIGGER IF EXISTS #{write_trigger_name} ON #{table_name}; SQL execute_sql_statement(sql_statement) result_hash(action: dry_run ? 'needs_unlock' : 'unlocked') end |