From 6fc40c48445aba193710b47f673e5d359828f010 Mon Sep 17 00:00:00 2001 From: Dimitri Fontaine Date: Sat, 15 Aug 2015 16:10:15 +0200 Subject: [PATCH] Implement MS SQL option to skip creating schemas, fix #263. Allow the user to control whether pgloader should create the same set of schema as found on the MS SQL database. --- pgloader.1 | 19 +++++++++++++++-- pgloader.1.md | 14 ++++++++++++- src/parsers/command-keywords.lisp | 1 + src/parsers/command-mssql.lisp | 34 +++++++++++++++++++++++++++++-- src/sources/mssql/mssql.lisp | 8 +++++--- 5 files changed, 68 insertions(+), 8 deletions(-) diff --git a/pgloader.1 b/pgloader.1 index b14876d..bc31c6b 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" "July 2015" "ff" "" +.TH "PGLOADER" "1" "August 2015" "ff" "" . .SH "NAME" \fBpgloader\fR \- PostgreSQL data loader @@ -2516,7 +2516,22 @@ Connection string to an existing MS SQL database server that listens and welcome \fIWITH\fR . .IP -When loading from a \fBMS SQL\fR database, the same options as when loading a \fBMySQL\fR database are supported\. Please refer to the MySQL section\. +When loading from a \fBMS SQL\fR database, the same options as when loading a \fBMySQL\fR database are supported\. Please refer to the MySQL section\. The following options are added: +. +.IP "\(bu" 4 +\fIcreate schemas\fR +. +.IP +When this option is listed, pgloader creates the same schemas as found on the MS SQL instance\. This is the default\. +. +.IP "\(bu" 4 +\fIcreate no schemas\fR +. +.IP +When this option is listed, pgloader refrains from creating any schemas at all, you must then ensure that the target schema do exist\. +. +.IP "" 0 + . .IP "\(bu" 4 \fICAST\fR diff --git a/pgloader.1.md b/pgloader.1.md index 792b424..eeb65b8 100644 --- a/pgloader.1.md +++ b/pgloader.1.md @@ -2054,7 +2054,19 @@ The `mssql` command accepts the following clauses and options: - *WITH* When loading from a `MS SQL` database, the same options as when loading - a `MySQL` database are supported. Please refer to the MySQL section. + a `MySQL` database are supported. Please refer to the MySQL section. The + following options are added: + + - *create schemas* + + When this option is listed, pgloader creates the same schemas as + found on the MS SQL instance. This is the default. + + - *create no schemas* + + When this option is listed, pgloader refrains from creating any + schemas at all, you must then ensure that the target schema do + exist. - *CAST* diff --git a/src/parsers/command-keywords.lisp b/src/parsers/command-keywords.lisp index 3f49045..2271ed4 100644 --- a/src/parsers/command-keywords.lisp +++ b/src/parsers/command-keywords.lisp @@ -96,6 +96,7 @@ (def-keyword-rule "delimiter") ;; option for MySQL imports (def-keyword-rule "schema") + (def-keyword-rule "schemas") (def-keyword-rule "only") (def-keyword-rule "drop") (def-keyword-rule "create") diff --git a/src/parsers/command-mssql.lisp b/src/parsers/command-mssql.lisp index 622e2c5..ac663f8 100644 --- a/src/parsers/command-mssql.lisp +++ b/src/parsers/command-mssql.lisp @@ -12,6 +12,36 @@ ;;; ;;; http://msdn.microsoft.com/en-us/library/ms187489(SQL.90).aspx ;;; +(make-option-rule create-tables (and kw-create (? kw-no) kw-schemas)) + +(defrule mssql-option (or option-batch-rows + option-batch-size + option-batch-concurrency + option-truncate + option-disable-triggers + option-data-only + option-schema-only + option-include-drop + option-create-tables + option-create-schemas + option-create-indexes + option-reset-sequences + option-encoding)) + +(defrule another-mssql-option (and comma mssql-option) + (:lambda (source) + (bind (((_ option) source)) option))) + +(defrule mssql-option-list (and mssql-option (* another-mssql-option)) + (:lambda (source) + (destructuring-bind (opt1 opts) source + (alexandria:alist-plist (list* opt1 opts))))) + +(defrule mssql-options (and kw-with mssql-option-list) + (:lambda (source) + (bind (((_ opts) source)) + (cons :mssql-options opts)))) + (defrule like-expression (and "'" (+ (not "'")) "'") (:lambda (le) (bind (((_ like _) le)) (text like)))) @@ -53,7 +83,7 @@ ;;; ;;; Allow clauses to appear in any order ;;; -(defrule load-mssql-optional-clauses (* (or mysql-options +(defrule load-mssql-optional-clauses (* (or mssql-options gucs casts before-load @@ -156,7 +186,7 @@ (bind (((ms-db-uri pg-db-uri &key gucs casts before after including excluding - ((:mysql-options options))) + ((:mssql-options options))) source)) (lisp-code-for-loading-from-mssql ms-db-uri pg-db-uri :gucs gucs diff --git a/src/sources/mssql/mssql.lisp b/src/sources/mssql/mssql.lisp index 1cb2e92..6cc1ade 100644 --- a/src/sources/mssql/mssql.lisp +++ b/src/sources/mssql/mssql.lisp @@ -180,6 +180,7 @@ (data-only nil) (schema-only nil) (create-tables t) + (create-schemas t) (include-drop t) (create-indexes t) (reset-sequences t) @@ -226,9 +227,10 @@ (loop :for (schema . tables) :in all-columns :do (let ((schema (apply-identifier-case schema))) ;; create schema - (let ((sql (format nil "CREATE SCHEMA ~a;" schema))) - (log-message :notice "~a" sql) - (pgsql-execute sql)) + (when create-schemas + (let ((sql (format nil "CREATE SCHEMA ~a;" schema))) + (log-message :notice "~a" sql) + (pgsql-execute sql))) ;; set search_path to only that schema (pgsql-execute