You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix(schema): render schema name when dumping foreign keys
In PostgreSQL, `oid::regclass::text` renders the schema
name if it is not the current_schema. In CockroachDB, this is not
the case, so we need to append the schema name manually.
Copy file name to clipboardExpand all lines: CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,6 +3,7 @@
3
3
## Ongoing
4
4
5
5
- Add support for [AOST](cockroachlabs.com/docs/stable/as-of-system-time) queries ([#284](https://github.com/cockroachdb/activerecord-cockroachdb-adapter/pull/284))
6
+
- Dump schema name in foreign keys ([#289](https://github.com/cockroachdb/activerecord-cockroachdb-adapter/pull/289))
Copy file name to clipboardExpand all lines: lib/active_record/connection_adapters/cockroachdb/schema_statements.rb
+14-3Lines changed: 14 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -36,16 +36,24 @@ def primary_key(table_name)
36
36
# Modified version of the postgresql foreign_keys method.
37
37
# Replaces t2.oid::regclass::text with t2.relname since this is
38
38
# more efficient in CockroachDB.
39
+
# Also, CockroachDB does not append the schema name in relname,
40
+
# so we append it manually.
39
41
defforeign_keys(table_name)
40
42
scope=quoted_scope(table_name)
41
43
fk_info=exec_query(<<~SQL,"SCHEMA")
42
-
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
44
+
SELECT CASE
45
+
WHEN n2.nspname = current_schema()
46
+
THEN ''
47
+
ELSE n2.nspname || '.'
48
+
END || t2.relname AS to_table,
49
+
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
43
50
FROM pg_constraint c
44
51
JOIN pg_class t1 ON c.conrelid = t1.oid
45
52
JOIN pg_class t2 ON c.confrelid = t2.oid
46
53
JOIN pg_attribute a1 ON a1.attnum = c.conkey[1] AND a1.attrelid = t1.oid
47
54
JOIN pg_attribute a2 ON a2.attnum = c.confkey[1] AND a2.attrelid = t2.oid
0 commit comments