From 0caa9c30ce8335c70c29eb8ba414b9ca75a90f13 Mon Sep 17 00:00:00 2001 From: Dimitri Fontaine Date: Thu, 14 Feb 2019 18:24:50 +0100 Subject: [PATCH] Fix Postgres to Postgres migrations with schema name change. When migrating from PostgreSQL, pgloader takes the index and foreign key definitions from the server directly, using pg_get_indexdef() and other catalog functions. That's very useful in that it embeds all the necessary quoting of the objects, and schema-qualify them. Of course we can't use the SQL definition as on the source system when we target a schema name that is different from the source system, which the code didn't realize before this patch. Here we simply invalidate the pre-computed SQL statement and resort to using the classic machinery to build the statement from pieces again. Fixes #903. --- src/utils/alter-table.lisp | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/utils/alter-table.lisp b/src/utils/alter-table.lisp index c5da02b..341b1b5 100644 --- a/src/utils/alter-table.lisp +++ b/src/utils/alter-table.lisp @@ -65,7 +65,10 @@ "Alter the schema of TABLE, set SCHEMA-NAME instead." (let* ((catalog (schema-catalog (table-schema table))) (schema (maybe-add-schema catalog schema-name))) - (setf (table-schema table) schema))) + (setf (table-schema table) schema) + + ;; this voids any index definition extracted from the source database... + (reset-sql-definitions table))) (defun alter-table-rename (table new-name) "Alter the name of TABLE to NEW-NAME." @@ -130,4 +133,26 @@ (defun alter-schema-rename (schema new-name) "Alter the name fo the given schema to new-name." - (setf (schema-name schema) new-name)) + (setf (schema-name schema) new-name) + + ;; this voids any index definition extracted from the source database... + (loop :for table :in (schema-table-list schema) + :do (reset-sql-definitions table))) + + + +;;; +;;; When a table targets a new schema, we need to refrain from using its +;;; index SQL definition when we got it from the source system, such as with +;;; pg_get_indexdef() on PostgreSQL. +;;; +(defun reset-sql-definitions (table) + "Reset source database given wholesale SQL definition for table's indexes + and foreign keys." + (loop :for index :in (table-index-list table) + :do (when (index-sql index) + (setf (index-sql index) nil))) + + (loop :for fkey :in (table-fkey-list table) + :do (when (fkey-condef fkey) + (setf (fkey-condef fkey) nil))))