Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions clojure/src/pgloader/source/mssql.clj
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@

(deftype MSSQLSource
[^Connection conn
^String source-name-str]
^String source-name-str
table-filter]

Source
(source-name [_] source-name-str)
Expand All @@ -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]
Expand Down Expand Up @@ -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))))
25 changes: 24 additions & 1 deletion clojure/test/pgloader/source/mssql_test.clj
Original file line number Diff line number Diff line change
@@ -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))
Expand Down Expand Up @@ -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"))))))
2 changes: 2 additions & 0 deletions clojure/tests/mssql/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 21 additions & 0 deletions clojure/tests/mssql/expected/14-including-only.out
Original file line number Diff line number Diff line change
@@ -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)

43 changes: 43 additions & 0 deletions clojure/tests/mssql/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 10 additions & 0 deletions clojure/tests/mssql/mssql-including.load
Original file line number Diff line number Diff line change
@@ -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';
15 changes: 15 additions & 0 deletions clojure/tests/mssql/sql/14-including-only.sql
Original file line number Diff line number Diff line change
@@ -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';