Skip to content
This repository was archived by the owner on Mar 13, 2020. It is now read-only.

Commit c65d3a3

Browse files
authored
Merge pull request #52 from pageuppeople-opensource/fix/compound-primary-keys
Fixed bug with handling batches of data with compound PKs
2 parents b956a04 + d86d3cb commit c65d3a3

File tree

6 files changed

+38
-3
lines changed

6 files changed

+38
-3
lines changed

rdl/data_sources/MsSqlDataSource.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,17 +231,22 @@ def __build_where_clause(batch_key_tracker, table_alias):
231231

232232
try:
233233
sql_builder = io.StringIO()
234+
where_stack = io.StringIO()
235+
where_stack.write("1 = 1")
234236
for primary_key in batch_key_tracker.bookmarks:
235237
if has_value:
236-
sql_builder.write(" AND ")
238+
sql_builder.write(" OR")
237239

238240
sql_builder.write(
239-
f" {table_alias}.{primary_key} > {batch_key_tracker.bookmarks[primary_key]}")
241+
f" ({where_stack.getvalue()} AND {table_alias}.{primary_key} > {batch_key_tracker.bookmarks[primary_key]})")
240242
has_value = True
243+
where_stack.write(
244+
f" AND {table_alias}.{primary_key} = {batch_key_tracker.bookmarks[primary_key]}")
241245

242246
return sql_builder.getvalue()
243247
finally:
244248
sql_builder.close()
249+
where_stack.close()
245250

246251
@staticmethod
247252
def __build_change_table_on_clause(batch_key_tracker):
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
SET client_encoding
2+
TO 'UTF8';
3+
4+
DO $$
5+
BEGIN
6+
IF ((SELECT COUNT(*)
7+
FROM rdl_integration_tests.load_compound_pk ) = 4)
8+
OR
9+
((SELECT COUNT(*)
10+
FROM rdl_integration_tests.load_compound_pk ) = 10) THEN
11+
RAISE NOTICE '[COMPOUND KEY MSSQL IMPORT TEST] PASS';
12+
ELSE
13+
RAISE EXCEPTION '[COMPOUND KEY MSSQL IMPORT TEST] FAIL: Did not find the required 4 (or 10) rows.';
14+
END
15+
IF;
16+
END $$;

tests/integration_tests/mssql_source/config/CompoundPkTest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"load_table": "load_compound_pk",
1010

1111
"batch": {
12-
"size": 100000
12+
"size": 2
1313
},
1414
"columns": [
1515
{

tests/integration_tests/mssql_source/source_database_setup/change_compound_pk.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,13 @@ INSERT CompoundPk
44
Id2
55
)
66
SELECT 3, 5
7+
UNION ALL
8+
SELECT 3, 6
9+
UNION ALL
10+
SELECT 3, 7
11+
UNION ALL
12+
SELECT 4, 3
13+
UNION ALL
14+
SELECT 4, 4
715
UNION ALL
816
SELECT 7, 8

tests/integration_tests/test_full_refresh_from_mssql.cmd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ if %errorlevel% neq 0 exit /b %errorlevel%
77

88
psql -U postgres -d rdl_integration_test_target_db -a -v ON_ERROR_STOP=1 -f ./tests/integration_tests/mssql_source/assertions/large_table_test_full_refresh_assertions.sql
99
if %errorlevel% neq 0 exit /b %errorlevel%
10+
11+
psql -U postgres -d rdl_integration_test_target_db -a -v ON_ERROR_STOP=1 -f ./tests/integration_tests/mssql_source/assertions/compound_pk_test_full_refresh_assertions.sql
12+
if %errorlevel% neq 0 exit /b %errorlevel%

tests/integration_tests/test_incremental_refresh_from_mssql.cmd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@ if %errorlevel% neq 0 exit /b %errorlevel%
44

55
psql -U postgres -d rdl_integration_test_target_db -a -v ON_ERROR_STOP=1 -f ./tests/integration_tests/mssql_source/assertions/large_table_test_full_refresh_assertions.sql
66
if %errorlevel% neq 0 exit /b %errorlevel%
7+
8+
psql -U postgres -d rdl_integration_test_target_db -a -v ON_ERROR_STOP=1 -f ./tests/integration_tests/mssql_source/assertions/compound_pk_test_full_refresh_assertions.sql
9+
if %errorlevel% neq 0 exit /b %errorlevel%

0 commit comments

Comments
 (0)