diff --git a/pgloader.1 b/pgloader.1 index 0d140c3..d9bebb6 100644 --- a/pgloader.1 +++ b/pgloader.1 @@ -1,7 +1,7 @@ .\" generated with Ronn/v0.7.3 .\" http://github.com/rtomayko/ronn/tree/0.7.3 . -.TH "PGLOADER" "1" "February 2015" "ff" "" +.TH "PGLOADER" "1" "March 2015" "ff" "" . .SH "NAME" \fBpgloader\fR \- PostgreSQL data loader @@ -1720,6 +1720,18 @@ When this option is listed, pgloader gets the definitions of all the indexes fou When this option is listed, pgloader skips the creating indexes\. . .IP "\(bu" 4 +\fIuniquify index names\fR, \fIpreserve index names\fR +. +.IP +MySQL index names are unique per\-table whereas in PostgreSQL index names have to be unique per\-schema\. The default for pgloader is to change the index name by prefixing it with \fBidx_OID\fR where \fBOID\fR is the internal numeric identifier of the table the index is built against\. +. +.IP +In somes cases like when the DDL are entirely left to a framework it might be sensible for pgloader to refrain from handling index unique names, that is achieved by using the \fIpreserve index names\fR option\. +. +.IP +The default is to \fIuniquify index names\fR\. +. +.IP "\(bu" 4 \fIforeign keys\fR . .IP diff --git a/pgloader.1.md b/pgloader.1.md index 9135672..b21446b 100644 --- a/pgloader.1.md +++ b/pgloader.1.md @@ -1456,6 +1456,20 @@ The `database` command accepts the following clauses and options: When this option is listed, pgloader skips the creating indexes. + - *uniquify index names*, *preserve index names* + + MySQL index names are unique per-table whereas in PostgreSQL index + names have to be unique per-schema. The default for pgloader is to + change the index name by prefixing it with `idx_OID` where `OID` is + the internal numeric identifier of the table the index is built + against. + + In somes cases like when the DDL are entirely left to a framework it + might be sensible for pgloader to refrain from handling index unique + names, that is achieved by using the *preserve index names* option. + + The default is to *uniquify index names*. + - *foreign keys* When this option is listed, pgloader gets the definitions of all the diff --git a/src/parsers/command-keywords.lisp b/src/parsers/command-keywords.lisp index 953a8e9..f5e8fe0 100644 --- a/src/parsers/command-keywords.lisp +++ b/src/parsers/command-keywords.lisp @@ -103,7 +103,10 @@ (def-keyword-rule "names") (def-keyword-rule "tables") (def-keyword-rule "views") + (def-keyword-rule "index") (def-keyword-rule "indexes") + (def-keyword-rule "preserve") + (def-keyword-rule "uniquify") (def-keyword-rule "sequences") (def-keyword-rule "foreign") (def-keyword-rule "keys") diff --git a/src/parsers/command-options.lisp b/src/parsers/command-options.lisp index ef616af..28877d5 100644 --- a/src/parsers/command-options.lisp +++ b/src/parsers/command-options.lisp @@ -123,6 +123,11 @@ (bind (((action _) id-case)) (cons :identifier-case action)))) +(defrule option-index-names (and (or kw-preserve kw-uniquify) kw-index kw-names) + (:lambda (preserve-or-uniquify) + (bind (((action _ _) preserve-or-uniquify)) + (cons :index-names action)))) + (defrule mysql-option (or option-workers option-batch-rows option-batch-size @@ -134,6 +139,7 @@ option-include-drop option-create-tables option-create-indexes + option-index-names option-reset-sequences option-foreign-keys option-identifiers-case)) diff --git a/src/pgsql/queries.lisp b/src/pgsql/queries.lisp index 74a2c32..e565ecf 100644 --- a/src/pgsql/queries.lisp +++ b/src/pgsql/queries.lisp @@ -116,7 +116,8 @@ (multiple-value-bind (res secs) (timing (handler-case (pgsql-execute sql) - (cl-postgres:database-error () + (cl-postgres:database-error (e) + (log-message :error "~a" e) (pgstate-incf state label :errs 1 :rows (- count))))) (declare (ignore res)) (pgstate-incf state label :read count :rows count :secs secs))) diff --git a/src/pgsql/schema.lisp b/src/pgsql/schema.lisp index 3e4033d..7d8f5bf 100644 --- a/src/pgsql/schema.lisp +++ b/src/pgsql/schema.lisp @@ -271,9 +271,12 @@ (defmethod format-pgsql-create-index ((index pgsql-index)) "Generate the PostgreSQL statement list to rebuild a Foreign Key" - (let* ((index-name (format nil "idx_~a_~a" - (pgsql-index-table-oid index) - (pgsql-index-name index))) + (let* ((index-name (if (pgsql-index-table-oid index) + (format nil "idx_~a_~a" + (pgsql-index-table-oid index) + (pgsql-index-name index)) + ;; lacking the oid means we preserve the index name + (pgsql-index-name index))) (table-name (apply-identifier-case (pgsql-index-table-name index))) (index-name (apply-identifier-case index-name)) diff --git a/src/sources/mysql/mysql.lisp b/src/sources/mysql/mysql.lisp index 023f9cc..bb1e1e1 100644 --- a/src/sources/mysql/mysql.lisp +++ b/src/sources/mysql/mysql.lisp @@ -152,7 +152,8 @@ &key state foreign-keys - include-drop) + include-drop + preserve-index-names) "Prepare the target PostgreSQL database: create tables casting datatypes from the MySQL definitions, prepare index definitions and create target tables for materialized views. @@ -179,7 +180,8 @@ ;; MySQL allows the same index name being used against several ;; tables, so we add the PostgreSQL table OID in the index name, ;; to differenciate. Set the table oids now. - (set-table-oids all-indexes) + (unless preserve-index-names + (set-table-oids all-indexes)) ;; We might have to MATERIALIZE VIEWS (when materialize-views @@ -356,6 +358,7 @@ (create-tables t) (include-drop t) (create-indexes t) + (index-names :uniquify) (reset-sequences t) (foreign-keys t) only-tables @@ -406,7 +409,9 @@ view-columns :state state-before :foreign-keys foreign-keys - :include-drop include-drop)) + :include-drop include-drop + :preserve-index-names (eq :preserve + index-names))) (t (when truncate (truncate-tables (target-db mysql) (mapcar #'car all-columns))))) diff --git a/test/parse/hans.goeuro.load b/test/parse/hans.goeuro.load index d610e10..27ebb35 100644 --- a/test/parse/hans.goeuro.load +++ b/test/parse/hans.goeuro.load @@ -37,5 +37,6 @@ LOAD DATABASE WITH include drop, create tables, create indexes, + preserve index names, reset sequences, disable triggers;