Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,38 @@ def foreign_keys(table_name)
end
end

# OVERRIDE: Verify that the related attribute to the unique_constraint is not dropped.
# See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/347.
def unique_constraints(table_name)
scope = quoted_scope(table_name)

unique_info = internal_exec_query(<<~SQL, "SCHEMA", allow_retry: true, materialize_transactions: false)
SELECT c.conname, c.conrelid, c.conkey, c.condeferrable, c.condeferred
FROM pg_constraint c
JOIN pg_class t ON c.conrelid = t.oid
JOIN pg_namespace n ON n.oid = c.connamespace
JOIN pg_attribute a ON a.attrelid = c.conrelid AND a.attnum = c.conkey[1] -- OVERRIDE
WHERE c.contype = 'u'
AND t.relname = #{scope[:name]}
AND n.nspname = #{scope[:schema]}
AND not a.attisdropped -- OVERRIDE
SQL

unique_info.map do |row|
conkey = row["conkey"].delete("{}").split(",").map(&:to_i)
columns = column_names_from_column_numbers(row["conrelid"], conkey)

deferrable = extract_constraint_deferrable(row["condeferrable"], row["condeferred"])

options = {
name: row["conname"],
deferrable: deferrable
}

UniqueConstraintDefinition.new(table_name, columns, options)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be:

Suggested change
UniqueConstraintDefinition.new(table_name, columns, options)
ActiveRecord::ConnectionAdapters::PostgreSQL::UniqueConstraintDefinition.new(table_name, columns, options)

end
end

# CockroachDB uses unique_rowid() for primary keys, not sequences. It's
# possible to force a table to use sequences, but since it's not the
# default behavior we'll always return nil for default_sequence_name.
Expand Down
21 changes: 21 additions & 0 deletions test/cases/schema_dumper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,27 @@ def test_schema_dumps_unique_constraints
assert_match 't.unique_constraint ["position_3"], name: "test_unique_constraints_position_3"', output
end

# See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/347
def test_dump_index_when_attisdropped_is_true
ActiveRecord::Base.with_connection do |conn|
conn.create_table :payments, force: true do |t|
t.text "name"
t.index "lower(name::STRING) ASC", name: "lower_name_index_on_payments", unique: true
end
end

stream = StringIO.new
ActiveRecord::Base.connection_pool.with_connection do |conn|
dumper = conn.create_schema_dumper({})
dumper.send(:table, "payments", stream)
end
stream.rewind
index_line = stream.each_line.find { _1[/lower_name_index_on_payments/] }
assert_match /t\.index/, index_line
ensure
ActiveRecord::Base.with_connection { _1.drop_table :payments, if_exists: true }
end

def test_schema_dump_with_timestamptz_datetime_format
migration, original, $stdout = nil, $stdout, StringIO.new

Expand Down
Loading