Skip to content

Commit e11b84d

Browse files
committed
fix: Avoid dumping unique_constraint when attisdropped
Fixes #347
1 parent 356e7b4 commit e11b84d

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

lib/active_record/connection_adapters/cockroachdb/schema_statements.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,38 @@ def foreign_keys(table_name)
222222
end
223223
end
224224

225+
# OVERRIDE: Verify that the related attribute to the unique_constraint is not dropped.
226+
# See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/347.
227+
def unique_constraints(table_name)
228+
scope = quoted_scope(table_name)
229+
230+
unique_info = internal_exec_query(<<~SQL, "SCHEMA", allow_retry: true, materialize_transactions: false)
231+
SELECT c.conname, c.conrelid, c.conkey, c.condeferrable, c.condeferred
232+
FROM pg_constraint c
233+
JOIN pg_class t ON c.conrelid = t.oid
234+
JOIN pg_namespace n ON n.oid = c.connamespace
235+
JOIN pg_attribute a ON a.attrelid = c.conrelid AND a.attnum = c.conkey[1] -- OVERRIDE
236+
WHERE c.contype = 'u'
237+
AND t.relname = #{scope[:name]}
238+
AND n.nspname = #{scope[:schema]}
239+
AND not a.attisdropped -- OVERRIDE
240+
SQL
241+
242+
unique_info.map do |row|
243+
conkey = row["conkey"].delete("{}").split(",").map(&:to_i)
244+
columns = column_names_from_column_numbers(row["conrelid"], conkey)
245+
246+
deferrable = extract_constraint_deferrable(row["condeferrable"], row["condeferred"])
247+
248+
options = {
249+
name: row["conname"],
250+
deferrable: deferrable
251+
}
252+
253+
UniqueConstraintDefinition.new(table_name, columns, options)
254+
end
255+
end
256+
225257
# CockroachDB uses unique_rowid() for primary keys, not sequences. It's
226258
# possible to force a table to use sequences, but since it's not the
227259
# default behavior we'll always return nil for default_sequence_name.

test/cases/schema_dumper_test.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,27 @@ def test_schema_dumps_unique_constraints
3434
assert_match 't.unique_constraint ["position_3"], name: "test_unique_constraints_position_3"', output
3535
end
3636

37+
# See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/347
38+
def test_dump_index_when_attisdropped_is_true
39+
ActiveRecord::Base.with_connection do |conn|
40+
conn.create_table :payments, force: true do |t|
41+
t.text "name"
42+
t.index "lower(name::STRING) ASC", name: "lower_name_index_on_payments", unique: true
43+
end
44+
end
45+
46+
stream = StringIO.new
47+
ActiveRecord::Base.connection_pool.with_connection do |conn|
48+
dumper = conn.create_schema_dumper({})
49+
dumper.send(:table, "payments", stream)
50+
end
51+
stream.rewind
52+
index_line = stream.each_line.find { _1[/lower_name_index_on_payments/] }
53+
assert_match /t\.index/, index_line
54+
ensure
55+
ActiveRecord::Base.with_connection { _1.drop_table :payments, if_exists: true }
56+
end
57+
3758
def test_schema_dump_with_timestamptz_datetime_format
3859
migration, original, $stdout = nil, $stdout, StringIO.new
3960

0 commit comments

Comments
 (0)