Skip to content

Commit 5522181

Browse files
committed
Revert "Improved --defer-constraints - closes ankane#209"
This reverts commit cb55978.
1 parent cb55978 commit 5522181

File tree

4 files changed

+8
-41
lines changed

4 files changed

+8
-41
lines changed

CHANGELOG.md

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
## 1.0.0 (unreleased)
22

33
- Added Docker image for `linux/arm64`
4-
- Improved `--defer-constraints`
54
- Fixed warning with Ruby 3.3
65
- Dropped support for Ruby < 2.7
76

lib/pgsync/table_sync.rb

+8-33
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def show_notes
126126

127127
# for non-deferrable constraints
128128
if opts[:defer_constraints_v1]
129-
constraints = non_deferrable_constraints(destination, tasks.map(&:table))
129+
constraints = non_deferrable_constraints(destination)
130130
constraints = tasks.flat_map { |t| constraints[t.table] || [] }
131131
warning "Non-deferrable constraints: #{constraints.join(", ")}" if constraints.any?
132132
end
@@ -150,46 +150,21 @@ def columns(data_source)
150150
end.to_h
151151
end
152152

153-
def non_deferrable_constraints(data_source, tables)
153+
def non_deferrable_constraints(data_source)
154154
query = <<~SQL
155155
SELECT
156156
table_schema AS schema,
157157
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,
182158
constraint_name
183159
FROM
184160
information_schema.table_constraints
185161
WHERE
186162
constraint_type = 'FOREIGN KEY' AND
187163
is_deferrable = 'NO'
188164
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
193168
end
194169

195170
def run_tasks(tasks, &block)
@@ -266,7 +241,7 @@ def run_tasks(tasks, &block)
266241
options[:in_processes] = jobs if jobs
267242
end
268243

269-
maybe_defer_constraints(tasks.map(&:table)) do
244+
maybe_defer_constraints do
270245
# could try to use `raise Parallel::Kill` to fail faster with --fail-fast
271246
# see `fast_faster` branch
272247
# however, need to make sure connections are cleaned up properly
@@ -286,7 +261,7 @@ def run_tasks(tasks, &block)
286261
end
287262

288263
# 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
290265
if opts[:disable_integrity] || opts[:disable_integrity_v2]
291266
# create a transaction on the source
292267
# to ensure we get a consistent snapshot
@@ -296,7 +271,7 @@ def maybe_defer_constraints(tables)
296271
elsif opts[:defer_constraints_v1] || opts[:defer_constraints_v2]
297272
destination.transaction do
298273
if opts[:defer_constraints_v2]
299-
table_constraints = non_deferrable_constraints(destination, tables)
274+
table_constraints = non_deferrable_constraints(destination)
300275
table_constraints.each do |table, constraints|
301276
constraints.each do |constraint|
302277
destination.execute("ALTER TABLE #{quote_ident_full(table)} ALTER CONSTRAINT #{quote_ident(constraint)} DEFERRABLE")

test/sync_test.rb

-2
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,6 @@ def test_defer_constraints
136136
assert_works "comments,posts --defer-constraints --preserve", config: true
137137
assert_equal [{"id" => 1}], conn2.exec("SELECT id FROM posts ORDER BY id").to_a
138138
assert_equal [{"post_id" => 1}], conn2.exec("SELECT post_id FROM comments ORDER BY post_id").to_a
139-
assert_prints "ALTER CONSTRAINT", "comments,posts --defer-constraints --debug", config: true
140-
refute_prints "ALTER CONSTRAINT", "authors --defer-constraints --debug", config: true
141139
end
142140

143141
def test_defer_constraints_not_deferrable

test/test_helper.rb

-5
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,6 @@ def assert_prints(message, command, **options)
6565
assert_match message, output
6666
end
6767

68-
def refute_prints(message, command, **options)
69-
output, _ = run_command(command, **options)
70-
refute_match message, output
71-
end
72-
7368
def truncate(conn, table)
7469
conn.exec("TRUNCATE #{quote_ident(table)} CASCADE")
7570
end

0 commit comments

Comments
 (0)