From 49316043611519c33ca074ba83028e46cd1d6f30 Mon Sep 17 00:00:00 2001 From: Dimitri Fontaine Date: Sun, 18 Dec 2016 19:25:16 +0100 Subject: [PATCH] Allow ALTER SCHEMA command for MySQL. This pgloader command allows to migrate tables while changing the schema they are found into in between their MySQL source database and their PostgreSQL target database. This changes the default behavior of pgloader with MySQL from always targetting the 'public' schema to targetting by default a schema named the same as the MySQL database. You can revert to the old behavior by adding a rule: ALTER SCHEMA 'dbname' RENAME TO 'public We might want to add a patch to re-install the default behavior later. Also see #489 where it used not to be possible to rename the schema at migration time, causing strange errors (you need to spot NIL as the schema name in the "failed to find target table" messages. --- src/parsers/command-mysql.lisp | 10 +++++++--- src/sources/mysql/mysql.lisp | 2 +- test/sakila-data.load | 13 ++++++++----- test/sakila.load | 9 ++++++--- 4 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/parsers/command-mysql.lisp b/src/parsers/command-mysql.lisp index 5f6fb72..c8f0bd7 100644 --- a/src/parsers/command-mysql.lisp +++ b/src/parsers/command-mysql.lisp @@ -68,6 +68,7 @@ gucs casts alter-table + alter-schema materialize-views including-matching excluding-matching @@ -139,7 +140,7 @@ (defun lisp-code-for-loading-from-mysql (my-db-conn pg-db-conn &key gucs casts views before after options - alter-table + alter-table alter-schema ((:including incl)) ((:excluding excl)) ((:decoding decoding-as))) @@ -162,6 +163,7 @@ :excluding ',excl :materialize-views ',views :alter-table ',alter-table + :alter-schema ',alter-schema :set-table-oids t ,@(remove-batch-control-option options)) @@ -172,8 +174,9 @@ (destructuring-bind (my-db-uri pg-db-uri &key - gucs casts views before after - options alter-table including excluding decoding) + gucs casts views before after options + alter-table alter-schema + including excluding decoding) source (cond (*dry-run* (lisp-code-for-mysql-dry-run my-db-uri pg-db-uri)) @@ -186,6 +189,7 @@ :after after :options options :alter-table alter-table + :alter-schema alter-schema :including including :excluding excluding :decoding decoding)))))) diff --git a/src/sources/mysql/mysql.lisp b/src/sources/mysql/mysql.lisp index cf195f5..1e04357 100644 --- a/src/sources/mysql/mysql.lisp +++ b/src/sources/mysql/mysql.lisp @@ -82,7 +82,7 @@ including excluding) "MySQL introspection to prepare the migration." - (let ((schema (add-schema catalog nil)) + (let ((schema (add-schema catalog (catalog-name catalog))) (view-names (unless (eq :all materialize-views) (mapcar #'car materialize-views)))) (with-stats-collection ("fetch meta data" diff --git a/test/sakila-data.load b/test/sakila-data.load index 5c8aafc..914025c 100644 --- a/test/sakila-data.load +++ b/test/sakila-data.load @@ -34,11 +34,14 @@ load database ALTER TABLE NAMES MATCHING 'sales_by_store' RENAME TO 'sales_by_store_list' ALTER TABLE NAMES MATCHING 'film' RENAME TO 'films' + ALTER SCHEMA 'sakila' RENAME TO 'pagila' + -- INCLUDING ONLY TABLE NAMES MATCHING ~/film/, 'actor' - EXCLUDING TABLE NAMES MATCHING ~ + EXCLUDING TABLE NAMES MATCHING ~ ; - BEFORE LOAD DO - $$ create schema if not exists sakila; $$, - $$ create schema if not exists mv; $$, - $$ alter database sakila set search_path to sakila, mv, public; $$; + + -- BEFORE LOAD DO + -- $$ create schema if not exists sakila; $$, + -- $$ create schema if not exists mv; $$, + -- $$ alter database sakila set search_path to sakila, mv, public; $$ diff --git a/test/sakila.load b/test/sakila.load index 6f0f8e6..441b169 100644 --- a/test/sakila.load +++ b/test/sakila.load @@ -8,7 +8,8 @@ load database -- WITH batch rows = 10000 WITH concurrency = 1, workers = 6, - max parallel create index = 4 + max parallel create index = 4-- , + -- quote identifiers SET maintenance_work_mem to '128MB', work_mem to '12MB', @@ -29,11 +30,13 @@ load database ALTER TABLE NAMES MATCHING 'sales_by_store' RENAME TO 'sales_by_store_list' ALTER TABLE NAMES MATCHING 'film' RENAME TO 'films' + ALTER SCHEMA 'sakila' RENAME TO 'pagila' + -- INCLUDING ONLY TABLE NAMES MATCHING ~/film/, 'actor' -- EXCLUDING TABLE NAMES MATCHING ~ BEFORE LOAD DO - $$ create schema if not exists sakila; $$, + $$ create schema if not exists pagila; $$, $$ create schema if not exists mv; $$, - $$ alter database sakila set search_path to sakila, mv, public; $$; + $$ alter database sakila set search_path to pagila, mv, public; $$;