@@ -71,31 +71,33 @@ def connection
71
71
@connection ||= Gitlab ::Database . database_base_models [ database_name ] . connection
72
72
end
73
73
74
+ def remove_schema_name ( table_with_schema )
75
+ ActiveRecord ::ConnectionAdapters ::PostgreSQL ::Utils
76
+ . extract_schema_qualified_name ( table_with_schema )
77
+ . identifier
78
+ end
79
+
80
+ def disable_locks_on_table ( table )
81
+ sql_statement = "SELECT set_config('lock_writes.#{ table } ', 'false', false)"
82
+ logger &.info ( sql_statement )
83
+ connection . execute ( sql_statement ) unless dry_run
84
+ end
85
+
74
86
def truncate_tables_in_batches ( tables_sorted )
75
87
truncated_tables = [ ]
76
88
77
89
tables_sorted . flatten . each do |table |
78
- table_name_without_schema = ActiveRecord ::ConnectionAdapters ::PostgreSQL ::Utils
79
- . extract_schema_qualified_name ( table )
80
- . identifier
90
+ table_name_without_schema = remove_schema_name ( table )
81
91
82
- sql_statement = "SELECT set_config('lock_writes.#{ table_name_without_schema } ', 'false', false)"
83
- logger &.info ( sql_statement )
84
- connection . execute ( sql_statement ) unless dry_run
92
+ disable_locks_on_table ( table_name_without_schema )
85
93
86
94
# Temporarily unlocking writes on the attached partitions of the table.
87
95
# Because in some cases they might have been locked for writes as well, when they used to be
88
96
# normal tables before being converted into attached partitions.
89
97
Gitlab ::Database ::SharedModel . using_connection ( connection ) do
90
98
table_partitions = Gitlab ::Database ::PostgresPartition . for_parent_table ( table_name_without_schema )
91
99
table_partitions . each do |table_partition |
92
- partition_name_without_schema = ActiveRecord ::ConnectionAdapters ::PostgreSQL ::Utils
93
- . extract_schema_qualified_name ( table_partition . identifier )
94
- . identifier
95
-
96
- sql_statement = "SELECT set_config('lock_writes.#{ partition_name_without_schema } ', 'false', false)"
97
- logger &.info ( sql_statement )
98
- connection . execute ( sql_statement ) unless dry_run
100
+ disable_locks_on_table ( remove_schema_name ( table_partition . identifier ) )
99
101
end
100
102
end
101
103
end
0 commit comments