@@ -126,7 +126,7 @@ def show_notes
126
126
127
127
# for non-deferrable constraints
128
128
if opts [ :defer_constraints_v1 ]
129
- constraints = non_deferrable_constraints ( destination , tasks . map ( & :table ) )
129
+ constraints = non_deferrable_constraints ( destination )
130
130
constraints = tasks . flat_map { |t | constraints [ t . table ] || [ ] }
131
131
warning "Non-deferrable constraints: #{ constraints . join ( ", " ) } " if constraints . any?
132
132
end
@@ -150,46 +150,21 @@ def columns(data_source)
150
150
end . to_h
151
151
end
152
152
153
- def non_deferrable_constraints ( data_source , tables )
153
+ def non_deferrable_constraints ( data_source )
154
154
query = <<~SQL
155
155
SELECT
156
156
table_schema AS schema,
157
157
table_name AS table,
158
- constraint_schema,
159
- constraint_name
160
- FROM
161
- information_schema.key_column_usage
162
- UNION ALL
163
- SELECT
164
- table_schema AS schema,
165
- table_name AS table,
166
- constraint_schema,
167
- constraint_name
168
- FROM
169
- information_schema.constraint_column_usage
170
- SQL
171
- constraints_by_table =
172
- data_source . execute ( query )
173
- . group_by { |r | Table . new ( r [ "schema" ] , r [ "table" ] ) }
174
- . to_h { |k , v | [ k , v . map { |r | [ r [ "constraint_schema" ] , r [ "constraint_name" ] ] } ] }
175
- matching_constraints = Set . new ( tables . flat_map { |t | constraints_by_table [ t ] || [ ] } )
176
-
177
- query = <<~SQL
178
- SELECT
179
- table_schema AS schema,
180
- table_name AS table,
181
- constraint_schema,
182
158
constraint_name
183
159
FROM
184
160
information_schema.table_constraints
185
161
WHERE
186
162
constraint_type = 'FOREIGN KEY' AND
187
163
is_deferrable = 'NO'
188
164
SQL
189
- data_source . execute ( query )
190
- . select { |r | matching_constraints . include? ( [ r [ "constraint_schema" ] , r [ "constraint_name" ] ] ) }
191
- . group_by { |r | Table . new ( r [ "schema" ] , r [ "table" ] ) }
192
- . to_h { |k , v | [ k , v . map { |r | r [ "constraint_name" ] } ] }
165
+ data_source . execute ( query ) . group_by { |r | Table . new ( r [ "schema" ] , r [ "table" ] ) } . map do |k , v |
166
+ [ k , v . map { |r | r [ "constraint_name" ] } ]
167
+ end . to_h
193
168
end
194
169
195
170
def run_tasks ( tasks , &block )
@@ -266,7 +241,7 @@ def run_tasks(tasks, &block)
266
241
options [ :in_processes ] = jobs if jobs
267
242
end
268
243
269
- maybe_defer_constraints ( tasks . map ( & :table ) ) do
244
+ maybe_defer_constraints do
270
245
# could try to use `raise Parallel::Kill` to fail faster with --fail-fast
271
246
# see `fast_faster` branch
272
247
# however, need to make sure connections are cleaned up properly
@@ -286,7 +261,7 @@ def run_tasks(tasks, &block)
286
261
end
287
262
288
263
# TODO add option to open transaction on source when manually specifying order of tables
289
- def maybe_defer_constraints ( tables )
264
+ def maybe_defer_constraints
290
265
if opts [ :disable_integrity ] || opts [ :disable_integrity_v2 ]
291
266
# create a transaction on the source
292
267
# to ensure we get a consistent snapshot
@@ -296,7 +271,7 @@ def maybe_defer_constraints(tables)
296
271
elsif opts [ :defer_constraints_v1 ] || opts [ :defer_constraints_v2 ]
297
272
destination . transaction do
298
273
if opts [ :defer_constraints_v2 ]
299
- table_constraints = non_deferrable_constraints ( destination , tables )
274
+ table_constraints = non_deferrable_constraints ( destination )
300
275
table_constraints . each do |table , constraints |
301
276
constraints . each do |constraint |
302
277
destination . execute ( "ALTER TABLE #{ quote_ident_full ( table ) } ALTER CONSTRAINT #{ quote_ident ( constraint ) } DEFERRABLE" )
0 commit comments