From b1ba09a21bc0a1701b764ed07b88107e51fa1c5b Mon Sep 17 00:00:00 2001 From: Dimitri Fontaine Date: Thu, 22 May 2014 12:35:54 +0200 Subject: [PATCH] Handle MySQL FK column names idenfier case, fix #62. The code forgot completely that MySQL column name references in foreign key definitions have to follow the identifier case rules, this patch fix that. To be able to do that, we need to parse the GROUP_CONCAT() result that lists the FK columns, as there's apparently no arrays in MySQL. The problem here is that about any character is allowed in column names when `quoted`, so using a comma here might reveal to be fragile later. --- src/pgsql/schema.lisp | 16 ++++++++++++---- src/sources/mysql-schema.lisp | 11 ++++++----- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/pgsql/schema.lisp b/src/pgsql/schema.lisp index c0e8198..35dd42c 100644 --- a/src/pgsql/schema.lisp +++ b/src/pgsql/schema.lisp @@ -75,15 +75,23 @@ (apply-identifier-case (pgsql-fkey-name fk) identifier-case)) (table-name (apply-identifier-case (pgsql-fkey-table-name fk) identifier-case)) + (fkey-columns + (mapcar (lambda (column-name) + (apply-identifier-case column-name identifier-case)) + (pgsql-fkey-columns fk))) (foreign-table - (apply-identifier-case (pgsql-fkey-foreign-table fk) identifier-case))) + (apply-identifier-case (pgsql-fkey-foreign-table fk) identifier-case)) + (foreign-columns + (mapcar (lambda (column-name) + (apply-identifier-case column-name identifier-case)) + (pgsql-fkey-foreign-columns fk)))) (format nil - "ALTER TABLE ~a ADD CONSTRAINT ~a FOREIGN KEY(~a) REFERENCES ~a(~a)" + "ALTER TABLE ~a ADD CONSTRAINT ~a FOREIGN KEY(~{~a~^,~}) REFERENCES ~a(~{~a~^,~})" table-name constraint-name - (pgsql-fkey-columns fk) + fkey-columns foreign-table - (pgsql-fkey-foreign-columns fk)))) + foreign-columns))) (defmethod format-pgsql-drop-fkey ((fk pgsql-fkey) &key all-pgsql-fkeys identifier-case) diff --git a/src/sources/mysql-schema.lisp b/src/sources/mysql-schema.lisp index cd39f4d..039158c 100644 --- a/src/sources/mysql-schema.lisp +++ b/src/sources/mysql-schema.lisp @@ -261,11 +261,12 @@ GROUP BY table_name, index_name;" dbname)) GROUP BY table_name, constraint_name, ft;" dbname dbname)) do (let ((entry (assoc table-name schema :test 'equal)) - (fk (make-pgsql-fkey :name name - :table-name table-name - :columns cols - :foreign-table ftable - :foreign-columns fcols))) + (fk + (make-pgsql-fkey :name name + :table-name table-name + :columns (sq:split-sequence #\, cols) + :foreign-table ftable + :foreign-columns (sq:split-sequence #\, fcols)))) (if entry (push fk (cdr entry)) (push (cons table-name (list fk)) schema)))