diff --git a/pgloader.1 b/pgloader.1 index dcc28f7..387c706 100644 --- a/pgloader.1 +++ b/pgloader.1 @@ -149,6 +149,18 @@ Allows forcing the source type, in case when the \fISOURCE\fR parsing isn\'t sat .IP Set the encoding of the source file to load data from\. . +.IP "\(bu" 4 +\fB\-\-before \fR +. +.IP +Parse given filename for SQL queries and run them against the target database before loading the data from the source\. The queries are parsed by pgloader itself: they need to be terminated by a semi\-colon (;) and the file may include \fB\ei\fR or \fB\eir\fR commands to \fIinclude\fR another file\. +. +.IP "\(bu" 4 +\fB\-\-after \fR +. +.IP +Parse given filename for SQL queries and run them against the target database after having loaded the data from the source\. The queries are parsed in the same way as with the \fB\-\-before\fR option, see above\. +. .IP "" 0 . .SS "MORE DEBUG INFORMATION" diff --git a/pgloader.1.md b/pgloader.1.md index 5909dba..52b1d5a 100644 --- a/pgloader.1.md +++ b/pgloader.1.md @@ -126,6 +126,19 @@ options: * `--encoding ` Set the encoding of the source file to load data from. + + * `--before ` + + Parse given filename for SQL queries and run them against the target + database before loading the data from the source. The queries are parsed + by pgloader itself: they need to be terminated by a semi-colon (;) and + the file may include `\i` or `\ir` commands to *include* another file. + + * `--after ` + + Parse given filename for SQL queries and run them against the target + database after having loaded the data from the source. The queries are + parsed in the same way as with the `--before` option, see above. ### MORE DEBUG INFORMATION diff --git a/src/main.lisp b/src/main.lisp index ca72f74..1cb86a5 100644 --- a/src/main.lisp +++ b/src/main.lisp @@ -60,6 +60,12 @@ (("encoding") :type string :optional t :documentation "Source expected encoding") + (("before") :type string :optional t + :documentation "SQL script to run before loading the data") + + (("after") :type string :optional t + :documentation "SQL script to run after loading the data") + ("self-upgrade" :type string :optional t :documentation "Path to pgloader newer sources"))) @@ -170,7 +176,7 @@ ((:load-lisp-file load)) client-min-messages log-min-messages summary root-dir self-upgrade - with set field cast type encoding) + with set field cast type encoding before after) options ;; First thing: Self Upgrade? @@ -303,7 +309,9 @@ :gucs (parse-cli-gucs set) :type type :fields (parse-cli-fields type field) - :casts (parse-cli-casts cast))))) + :casts (parse-cli-casts cast) + :before (parse-sql-file before) + :after (parse-sql-file after))))) ;; process the files (mapcar #'process-command-file arguments))) @@ -329,7 +337,8 @@ ;;; (defun load-data (&key ((:from source)) ((:into target)) (type (getf source :type)) - encoding fields options gucs casts start-logger) + encoding fields options gucs casts before after + start-logger) "Load data from SOURCE into TARGET." (with-monitor (:start-logger start-logger) ;; some preliminary checks @@ -353,20 +362,28 @@ (:csv (lisp-code-for-loading-from-csv source fields target :encoding encoding :gucs gucs - :csv-options options)) + :csv-options options + :before before + :after after)) (:fixed (lisp-code-for-loading-from-fixed source fields target :encoding encoding :gucs gucs - :fixed-options options)) + :fixed-options options + :before before + :after after)) (:db3 (lisp-code-for-loading-from-dbf source target :gucs gucs - :dbf-options options)) + :dbf-options options + :before before + :after after)) (:ixf (lisp-code-for-loading-from-ixf source target :gucs gucs - :ixf-options options)) + :ixf-options options + :before before + :after after)) (:sqlite (lisp-code-for-loading-from-sqlite source target :gucs gucs @@ -376,10 +393,14 @@ (:mysql (lisp-code-for-loading-from-mysql source target :gucs gucs :casts casts - :mysql-options options)) + :mysql-options options + :before before + :after after)) (:mssql (lisp-code-for-loading-from-mssql source target :gucs gucs :casts casts - :mssql-options options)))) + :mssql-options options + :before before + :after after)))) :start-logger start-logger))) diff --git a/src/package.lisp b/src/package.lisp index f3d495e..8d2be26 100644 --- a/src/package.lisp +++ b/src/package.lisp @@ -393,6 +393,7 @@ #:parse-cli-encoding #:parse-cli-fields #:parse-cli-casts + #:parse-sql-file ;; functions to generate lisp code from parameters #:lisp-code-for-loading-from-mysql diff --git a/src/parsers/command-parser.lisp b/src/parsers/command-parser.lisp index 229fb44..7f08e59 100644 --- a/src/parsers/command-parser.lisp +++ b/src/parsers/command-parser.lisp @@ -283,3 +283,9 @@ "Parse additional CAST rules when we get them from the CLI." (loop :for cast :in casts :collect (parse 'cast-rule cast))) + +(defun parse-sql-file (filename) + "Parse FILENAME for SQL statements" + (when filename + (log-message :notice "reading SQL queries from ~s" filename) + (pgloader.sql:read-queries (probe-file filename))))