diff --git a/src/package.lisp b/src/package.lisp index 726ec3f..d92e694 100644 --- a/src/package.lisp +++ b/src/package.lisp @@ -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 diff --git a/src/pgsql/pgsql-create-schema.lisp b/src/pgsql/pgsql-create-schema.lisp index 938049c..9781fdc 100644 --- a/src/pgsql/pgsql-create-schema.lisp +++ b/src/pgsql/pgsql-create-schema.lisp @@ -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 diff --git a/src/pgsql/pgsql-schema.lisp b/src/pgsql/pgsql-schema.lisp index 3fac42c..1e34f85 100644 --- a/src/pgsql/pgsql-schema.lisp +++ b/src/pgsql/pgsql-schema.lisp @@ -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." diff --git a/src/sources/common/db-methods.lisp b/src/sources/common/db-methods.lisp index 056623b..e3f4890 100644 --- a/src/sources/common/db-methods.lisp +++ b/src/sources/common/db-methods.lisp @@ -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. ;; diff --git a/src/sources/mysql/mysql.lisp b/src/sources/mysql/mysql.lisp index 3a35a03..db60044 100644 --- a/src/sources/mysql/mysql.lisp +++ b/src/sources/mysql/mysql.lisp @@ -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" diff --git a/src/utils/catalog.lisp b/src/utils/catalog.lisp index a1ed9b5..c81758f 100644 --- a/src/utils/catalog.lisp +++ b/src/utils/catalog.lisp @@ -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)