Skip to content

Commit

Permalink
fix(schema): render schema name when dumping foreign keys
Browse files Browse the repository at this point in the history
In PostgreSQL, `oid::regclass::text` renders the schema
name if it is not `public`. In CockroachDB, this is not
the case, so we need to append the schema name manually.
  • Loading branch information
BuonOmo committed Sep 22, 2023
1 parent 4bf5499 commit 6b56ba9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Ongoing

- Add support for [AOST](cockroachlabs.com/docs/stable/as-of-system-time) queries ([#284](https://github.com/cockroachdb/activerecord-cockroachdb-adapter/pull/284))
- Dump schema name in foreign keys ([#289](https://github.com/cockroachdb/activerecord-cockroachdb-adapter/pull/289))

## 7.0.3 - 2023-08-23

Expand Down
6 changes: 1 addition & 5 deletions bin/start-cockroachdb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ cockroach start-single-node \
--insecure --store=type=mem,size=0.25 --advertise-addr=localhost --pid-file "$pid_file" \
&> "$log_file" &

cockroach_pid=$!

until [[ -f "$pid_file" ]]; do
sleep 1
done
Expand All @@ -43,6 +41,4 @@ CREATE DATABASE activerecord_unittest;
CREATE DATABASE activerecord_unittest2;
SQL

tail -f "$log_file"

trap "kill $cockroach_pid" EXIT
echo "CockroachDB started. PID: $(cat "$pid_file"). log: $log_file"
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,14 @@ def primary_key(table_name)
# Modified version of the postgresql foreign_keys method.
# Replaces t2.oid::regclass::text with t2.relname since this is
# more efficient in CockroachDB.
# Also, CockroachDB does not append the schema name in relname,
# so we append it manually.
def foreign_keys(table_name)
Debug.say "foreign_keys(#{table_name})"
scope = quoted_scope(table_name)
fk_info = exec_query(<<~SQL, "SCHEMA")
SELECT t2.relname AS to_table, a1.attname AS column, a2.attname AS primary_key, c.conname AS name, c.confupdtype AS on_update, c.confdeltype AS on_delete, c.convalidated AS valid
SELECT CASE WHEN t2.relnamespace::regnamespace::text = 'public' THEN '' ELSE t2.relnamespace::regnamespace::text || '.' END || t2.relname AS to_table,
a1.attname AS column, a2.attname AS primary_key, c.conname AS name, c.confupdtype AS on_update, c.confdeltype AS on_delete, c.convalidated AS valid, c.condeferrable AS deferrable, c.condeferred AS deferred
FROM pg_constraint c
JOIN pg_class t1 ON c.conrelid = t1.oid
JOIN pg_class t2 ON c.confrelid = t2.oid
Expand All @@ -54,16 +58,19 @@ def foreign_keys(table_name)

fk_info.map do |row|
options = {
column: row["column"],
column: PostgreSQL::Utils.unquote_identifier(row["column"]),
name: row["name"],
primary_key: row["primary_key"]
}

options[:on_delete] = extract_foreign_key_action(row["on_delete"])
options[:on_update] = extract_foreign_key_action(row["on_update"])
options[:deferrable] = extract_foreign_key_deferrable(row["deferrable"], row["deferred"])

options[:validate] = row["valid"]
to_table = PostgreSQL::Utils.unquote_identifier(row["to_table"])

ForeignKeyDefinition.new(table_name, row["to_table"], options)
ForeignKeyDefinition.new(table_name, to_table, options)
end
end

Expand Down

0 comments on commit 6b56ba9

Please sign in to comment.