Skip to content

Commit

Permalink
fix(mssql): only last column in foreign key definition kept
Browse files Browse the repository at this point in the history
  • Loading branch information
gnarlex committed Jul 5, 2024
1 parent 29afa9d commit f0bb82a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
30 changes: 13 additions & 17 deletions src/sources/mssql/mssql-schema.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@

(defmethod fetch-foreign-keys ((catalog catalog) (mssql copy-mssql)
&key including excluding)
"Get the list of MSSQL index definitions per table."
"Get the list of MSSQL foreign key definitions per table."
(loop
:with incl-where := (filter-list-to-where-clause
mssql including :not nil
Expand All @@ -130,8 +130,8 @@
mssql excluding :not t
:schema-col "kcu1.table_schema"
:table-col "kcu1.table_name")
:for (fkey-name schema-name table-name col
fschema-name ftable-name fcol
:for (fkey-name schema-name table-name cols
fschema-name ftable-name fcols
fk-update-rule fk-delete-rule)
:in (mssql-query (sql "/mssql/list-all-fkeys.sql"
(db-name *mssql-db*) (db-name *mssql-db*)
Expand All @@ -143,20 +143,16 @@
(table (find-table schema table-name))
(fschema (find-schema catalog fschema-name))
(ftable (find-table fschema ftable-name))
(col-name (apply-identifier-case col))
(fcol-name (apply-identifier-case fcol))
(pg-fkey
(make-fkey :name (apply-identifier-case fkey-name)
:table table
:columns nil
:foreign-table ftable
:foreign-columns nil
:update-rule fk-update-rule
:delete-rule fk-delete-rule))
(fkey
(maybe-add-fkey table fkey-name pg-fkey :key #'fkey-name)))
(push-to-end col-name (fkey-columns fkey))
(push-to-end fcol-name (fkey-foreign-columns fkey)))
(fkey (make-fkey :table table
:columns (mapcar #'apply-identifier-case
(sq:split-sequence #\, cols))
:foreign-table ftable
:foreign-columns (mapcar
#'apply-identifier-case
(sq:split-sequence #\, fcols))
:update-rule fk-update-rule
:delete-rule fk-delete-rule)))
(add-fkey table fkey)
:finally (return catalog)))


Expand Down
13 changes: 10 additions & 3 deletions src/sources/mssql/sql/list-all-fkeys.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
REPLACE(KCU1.CONSTRAINT_NAME, '.', '_') AS 'CONSTRAINT_NAME'
, KCU1.TABLE_SCHEMA AS 'TABLE_SCHEMA'
, KCU1.TABLE_NAME AS 'TABLE_NAME'
, KCU1.COLUMN_NAME AS 'COLUMN_NAME'
, STRING_AGG(KCU1.COLUMN_NAME, ',') AS 'COLUMNS'
, KCU2.TABLE_SCHEMA AS 'UNIQUE_TABLE_SCHEMA'
, KCU2.TABLE_NAME AS 'UNIQUE_TABLE_NAME'
, KCU2.COLUMN_NAME AS 'UNIQUE_COLUMN_NAME'
, STRING_AGG(KCU2.COLUMN_NAME, ',') AS 'UNIQUE_COLUMN_NAME'
, RC.UPDATE_RULE AS 'UPDATE_RULE'
, RC.DELETE_RULE AS 'DELETE_RULE'

Expand All @@ -34,4 +34,11 @@
~:[~*~;and (~{~a~^ or ~})~]
~:[~*~;and (~{~a~^ and ~})~]

ORDER BY KCU1.CONSTRAINT_NAME, KCU1.ORDINAL_POSITION;
GROUP BY
KCU1.CONSTRAINT_NAME
, KCU1.TABLE_SCHEMA
, KCU1.TABLE_NAME
, KCU2.TABLE_SCHEMA
, KCU2.TABLE_NAME
, RC.UPDATE_RULE
, RC.DELETE_RULE;

0 comments on commit f0bb82a

Please sign in to comment.