Add the MySQL target schema to the search_path.

In the next release, pgloader defaults to targetting a new schema named the
same as the MySQL database, because that's what makes more sense. But people
are used to having 'public' in the search_path and everything in there.

So when creating our target schema, when migrating from MySQL, arrange it so
that the new schema is in the search_path by issuing a command like:

  ALTER DATABASE plop SET search_path TO public, f1db;

And make this command visible in verbose (NOTICE) mode too, so that user can
see what happens.

Fix #654. I think.
This commit is contained in:
Dimitri Fontaine 2017-11-02 12:40:21 +01:00
parent 6b6c1c7d34
commit db7a91d6c4
6 changed files with 53 additions and 4 deletions

View File

@ -80,6 +80,7 @@
#:schema-source-name
#:schema-table-list
#:schema-view-list
#:schema-in-search-path
#:table-name
#:table-source-name
@ -402,6 +403,7 @@
#:create-sqltypes
#:create-schemas
#:add-to-search-path
#:create-tables
#:create-views
#:drop-pgsql-fkeys

View File

@ -88,6 +88,32 @@
:do (let ((sql (format nil "CREATE SCHEMA ~a;" (schema-name schema))))
(pgsql-execute sql :client-min-messages client-min-messages)))))
(defun add-to-search-path (catalog
&key
label
(section :post)
(log-level :notice)
(client-min-messages :notice))
"Add catalog schemas in the database search_path."
(let* ((dbname (get-current-database))
(search-path (list-search-path))
(missing-schemas
(loop :for schema :in (catalog-schema-list catalog)
:for schema-name := (schema-name schema)
:when (and (schema-in-search-path schema)
(not (member schema-name search-path :test #'string=)))
:collect schema-name)))
(when missing-schemas
(let ((sql (format nil
"ALTER DATABASE ~a SET search_path TO ~{~a~^, ~};"
dbname
(append search-path missing-schemas))))
(pgsql-execute-with-timing section
label
sql
:log-level log-level
:client-min-messages client-min-messages)))))
(defun create-tables (catalog
&key
if-not-exists

View File

@ -312,6 +312,17 @@
PostgreSQL connection."
(pomo:query "SELECT nspname FROM pg_catalog.pg_namespace;" :column))
(defun list-search-path ()
"Return the current list of schemas in the Search Path"
(pomo:query
"SELECT name FROM unnest(pg_catalog.current_schemas(false)) as t(name);"
:column))
(defun get-current-database ()
"Get the current database name. The catalog name and the connection string
name may be different, so just ask PostgreSQL here."
(pomo:query "select current_database();" :single))
(defun list-table-oids (table-names)
"Return an hash table mapping TABLE-NAME to its OID for all table in the
TABLE-NAMES list. A PostgreSQL connection must be established already."

View File

@ -170,6 +170,14 @@
:section :post
:label "Create Triggers")))
;;
;; Add schemas that needs to be in the search_path to the database
;; search_path
;;
(add-to-search-path catalog
:section :post
:label "Set Search Path")
;;
;; And now, comments on tables and columns.
;;

View File

@ -156,7 +156,8 @@
including
excluding)
"MySQL introspection to prepare the migration."
(let ((schema (add-schema catalog (catalog-name catalog)))
(let ((schema (add-schema catalog (catalog-name catalog)
:in-search-path t))
(view-names (unless (eq :all materialize-views)
(mapcar #'car materialize-views))))
(with-stats-collection ("fetch meta data"

View File

@ -43,7 +43,7 @@
;;; implemented in each source separately.
;;;
(defstruct catalog name schema-list types-without-btree)
(defstruct schema source-name name catalog table-list view-list)
(defstruct schema source-name name catalog table-list view-list in-search-path)
(defstruct table source-name name schema oid comment storage-parameter-list
;; field is for SOURCE
;; column is for TARGET
@ -203,12 +203,13 @@
;; already cooked table structure, try it and see...
(table maybe-qualified-name)))
(defmethod add-schema ((catalog catalog) schema-name &key)
(defmethod add-schema ((catalog catalog) schema-name &key in-search-path)
"Add SCHEMA-NAME to CATALOG and return the new schema instance."
(let ((schema (make-schema :catalog catalog
:source-name schema-name
:name (when schema-name
(apply-identifier-case schema-name)))))
(apply-identifier-case schema-name))
:in-search-path in-search-path)))
(push-to-end schema (catalog-schema-list catalog))))
(defmethod add-table ((schema schema) table-name &key comment oid)