diff --git a/clojure/src/pgloader/source/mssql.clj b/clojure/src/pgloader/source/mssql.clj index 41e4cf06..fa2c0bb6 100644 --- a/clojure/src/pgloader/source/mssql.clj +++ b/clojure/src/pgloader/source/mssql.clj @@ -172,7 +172,8 @@ (deftype MSSQLSource [^Connection conn - ^String source-name-str] + ^String source-name-str + table-filter] Source (source-name [_] source-name-str) @@ -183,7 +184,12 @@ (tables conn) (catch Exception e (log/error (str "Failed to list tables: " (.getMessage e))) - []))] + [])) + ;; Apply INCLUDING/EXCLUDING before the per-table metadata queries, + ;; so filtered-out tables cost no round trips. + ts (if table-filter + (filterv #(table-filter (:table_name %)) ts) + ts)] (log/debug (str "Fetched schema for " (count ts) " tables")) (->> ts (mapv (fn [t] @@ -377,6 +383,6 @@ :cache (:cache_size s)})))))) (defn create-source - [uri-map _table-spec] + [uri-map table-spec] (let [conn (connection uri-map)] - (->MSSQLSource conn (plog/redact-uri (:raw uri-map))))) + (->MSSQLSource conn (plog/redact-uri (:raw uri-map)) (:table-filter table-spec)))) diff --git a/clojure/test/pgloader/source/mssql_test.clj b/clojure/test/pgloader/source/mssql_test.clj index f6f46a4a..6cdb4c6c 100644 --- a/clojure/test/pgloader/source/mssql_test.clj +++ b/clojure/test/pgloader/source/mssql_test.clj @@ -1,6 +1,7 @@ (ns pgloader.source.mssql-test (:require [clojure.test :refer [deftest is testing]] - [pgloader.source.mssql :as mssql])) + [pgloader.source.mssql :as mssql] + [pgloader.source.protocol :as proto])) (defn- sanitize [default pg-type] (#'mssql/sanitize-default default pg-type)) @@ -28,3 +29,25 @@ (is (= "nextval('public.order_seq')" (sanitize "NEXT VALUE FOR [dbo].[order_seq]" "bigint"))) (is (nil? (sanitize "convert(datetime,'1753-01-01',0)" "timestamptz"))) (is (nil? (sanitize nil "text")))) + +(deftest test-catalog-prefilters-tables + (testing "INCLUDING/EXCLUDING is applied before the per-table metadata queries" + (let [queried (atom #{})] + (with-redefs [mssql/connection (fn [_] nil) + mssql/tables (fn [_] + [{:table_schema "dbo" :table_name "keep_orders"} + {:table_schema "dbo" :table_name "keep_items"} + {:table_schema "dbo" :table_name "drop_audit"}]) + mssql/table-columns (fn [_ {:keys [table]}] + (swap! queried conj table) + []) + mssql/table-pkeys (fn [_ _] []) + mssql/table-indexes (fn [_ _] []) + mssql/table-fkeys (fn [_ _] []) + mssql/table-extended-props (fn [_ _] [])] + (let [src (mssql/create-source {:raw "mssql://sa@localhost/db"} + {:table-filter #(re-find #"keep.*" %)}) + cat (proto/catalog src)] + (is (= ["keep_items" "keep_orders"] (mapv :table-name cat))) + (is (= #{"keep_items" "keep_orders"} @queried) + "no metadata query for filtered-out tables")))))) diff --git a/clojure/tests/mssql/Makefile b/clojure/tests/mssql/Makefile index 095bab87..dc368c0c 100644 --- a/clojure/tests/mssql/Makefile +++ b/clojure/tests/mssql/Makefile @@ -23,12 +23,14 @@ all: run run: $(PGLOADER_CMD) /suite/mssql.load $(PGLOADER_CMD) /suite/mssql-filter-views.load + $(PGLOADER_CMD) /suite/mssql-including.load $(_V4_ONLY_LOAD_CMD) java -jar $(JAR) regress $(REGRESS_FLAGS) $(_REGRESS_EXTRA) /suite update-expected: $(PGLOADER_CMD) /suite/mssql.load $(PGLOADER_CMD) /suite/mssql-filter-views.load + $(PGLOADER_CMD) /suite/mssql-including.load $(_V4_ONLY_LOAD_CMD) java -jar $(JAR) regress --update /suite diff --git a/clojure/tests/mssql/expected/14-including-only.out b/clojure/tests/mssql/expected/14-including-only.out new file mode 100644 index 00000000..d1458d0d --- /dev/null +++ b/clojure/tests/mssql/expected/14-including-only.out @@ -0,0 +1,21 @@ + table_name +------------- + keep_items + keep_orders +(2 rows) + + keep_orders +------------- + 2 +(1 row) + + keep_items +------------ + 3 +(1 row) + + fkeys +------- + 1 +(1 row) + diff --git a/clojure/tests/mssql/init.sql b/clojure/tests/mssql/init.sql index 879940c7..4608fe36 100644 --- a/clojure/tests/mssql/init.sql +++ b/clojure/tests/mssql/init.sql @@ -233,3 +233,46 @@ INSERT INTO shop.tsql_defaults DEFAULT VALUES; INSERT INTO shop.tsql_defaults DEFAULT VALUES; INSERT INTO shop.tsql_defaults DEFAULT VALUES; GO + +-- includetest: separate database for INCLUDING ONLY TABLE NAMES, loaded by +-- mssql-including.load. Only the keep_* tables must be migrated; drop_audit +-- must be left out, catalog queries included. +CREATE DATABASE includetest; +GO + +USE includetest; +GO + +CREATE SCHEMA inc; +GO + +CREATE TABLE inc.keep_orders ( + id INT IDENTITY(1,1) PRIMARY KEY, + label NVARCHAR(50) NOT NULL +); +GO + +CREATE TABLE inc.keep_items ( + id INT IDENTITY(1,1) PRIMARY KEY, + order_id INT NOT NULL, + qty INT NOT NULL, + CONSTRAINT fk_keep_items_order FOREIGN KEY (order_id) REFERENCES inc.keep_orders(id) +); +GO + +CREATE TABLE inc.drop_audit ( + id INT IDENTITY(1,1) PRIMARY KEY, + order_id INT NOT NULL, + note NVARCHAR(100), + CONSTRAINT fk_drop_audit_order FOREIGN KEY (order_id) REFERENCES inc.keep_orders(id) +); +GO + +INSERT INTO inc.keep_orders (label) VALUES ('first'), ('second'); +GO + +INSERT INTO inc.keep_items (order_id, qty) VALUES (1, 3), (1, 1), (2, 5); +GO + +INSERT INTO inc.drop_audit (order_id, note) VALUES (1, 'created'), (2, 'created'); +GO diff --git a/clojure/tests/mssql/mssql-including.load b/clojure/tests/mssql/mssql-including.load new file mode 100644 index 00000000..d4488ec9 --- /dev/null +++ b/clojure/tests/mssql/mssql-including.load @@ -0,0 +1,10 @@ +-- INCLUDING ONLY TABLE NAMES must restrict the migration to the matching +-- tables: keep_orders and keep_items are loaded, drop_audit is not. +LOAD DATABASE + FROM mssql://sa:pgloaderTest1!@mssql:1433/includetest + INTO postgresql://pgloader:pgloader@postgres:5432/target + + WITH include drop, create tables, create indexes, reset sequences, + foreign keys + + INCLUDING ONLY TABLE NAMES LIKE 'keep%' IN SCHEMA 'inc'; diff --git a/clojure/tests/mssql/sql/14-including-only.sql b/clojure/tests/mssql/sql/14-including-only.sql new file mode 100644 index 00000000..75572873 --- /dev/null +++ b/clojure/tests/mssql/sql/14-including-only.sql @@ -0,0 +1,15 @@ +-- INCLUDING ONLY TABLE NAMES LIKE 'keep%': only the matching tables are +-- migrated (drop_audit is absent), with their rows and the foreign key +-- between them. +SELECT table_name +FROM information_schema.tables +WHERE table_schema = 'inc' +ORDER BY table_name; + +SELECT count(*) AS keep_orders FROM inc.keep_orders; +SELECT count(*) AS keep_items FROM inc.keep_items; + +SELECT count(*) AS fkeys +FROM information_schema.table_constraints +WHERE table_schema = 'inc' + AND constraint_type = 'FOREIGN KEY';