Skip to content

Commit 114a4e5

Browse files
committed
fix: ignore unique constraints in favor of indexes
1 parent e11b84d commit 114a4e5

File tree

2 files changed

+15
-33
lines changed

2 files changed

+15
-33
lines changed

lib/active_record/connection_adapters/cockroachdb/schema_statements.rb

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -222,36 +222,12 @@ 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.
225+
# OVERRIDE: UNIQUE CONSTRAINTS will create indexes anyway, so we only consider
226+
# then as indexes.
227+
# See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/347.
228+
# See https://www.cockroachlabs.com/docs/stable/unique
227229
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
230+
[]
255231
end
256232

257233
# CockroachDB uses unique_rowid() for primary keys, not sequences. It's

test/cases/schema_dumper_test.rb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,14 @@ def test_schema_dumps_unique_constraints
3535
end
3636

3737
# See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/347
38-
def test_dump_index_when_attisdropped_is_true
38+
def test_dump_index_rather_than_unique_constraints
3939
ActiveRecord::Base.with_connection do |conn|
4040
conn.create_table :payments, force: true do |t|
4141
t.text "name"
42-
t.index "lower(name::STRING) ASC", name: "lower_name_index_on_payments", unique: true
42+
t.integer "value"
43+
t.unique_constraint ["name", "value"], name: "as_unique_constraint"
44+
t.index "lower(name::STRING) ASC", name: "simple_unique", unique: true
45+
t.index "name", name: "unique_with_where", where: "name IS NOT NULL", unique: true
4346
end
4447
end
4548

@@ -49,8 +52,11 @@ def test_dump_index_when_attisdropped_is_true
4952
dumper.send(:table, "payments", stream)
5053
end
5154
stream.rewind
52-
index_line = stream.each_line.find { _1[/lower_name_index_on_payments/] }
53-
assert_match /t\.index/, index_line
55+
index_lines = stream.each_line.select { _1[/simple_unique|unique_with_where|as_unique_constraint/] }
56+
assert_equal 3, index_lines.size
57+
index_lines.each do |line|
58+
assert_match /t.index/, line
59+
end
5460
ensure
5561
ActiveRecord::Base.with_connection { _1.drop_table :payments, if_exists: true }
5662
end

0 commit comments

Comments
 (0)