Skip to content

Commit ae0e4f2

Browse files
authored
Merge pull request wintersrd#88 from s7clarke10/bug/fix_replicate_table_primary_keys
Bug/fix replicate table primary keys
2 parents b82bafb + a05e78b commit ae0e4f2

File tree

5 files changed

+80
-5
lines changed

5 files changed

+80
-5
lines changed

.bumpversion.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[bumpversion]
2-
current_version = 2.6.1
2+
current_version = 2.6.2
33
parse = (?P<major>\d+)
44
\.(?P<minor>\d+)
55
\.(?P<patch>\d+)

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# tap-mssql 2.6.2 2024-10-09
2+
* Resolving issue when a table has a primary key and unique key. Both unique and primary key
3+
columns were being identified as the primary key for the target table. Prioritising the
4+
primary key first, and unique key secondary if there is no primary key.
5+
16
# tap-mssql 2.6.1 2024-10-09
27
* Resolving issue with call get the prior LSN number (passing in unescaped table).
38

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "tap-mssql"
3-
version = "2.6.1"
3+
version = "2.6.2"
44
description = "A pipelinewise compatible tap for connecting Microsoft SQL Server"
55
authors = ["Rob Winters <[email protected]>"]
66
license = "GNU Affero"

tap_mssql/__init__.py

+16-3
Original file line numberDiff line numberDiff line change
@@ -230,18 +230,31 @@ def discover_catalog(mssql_conn, config):
230230
table_info[db][table] = {"row_count": None, "is_view": table_type == "VIEW"}
231231
LOGGER.info("Tables fetched, fetching columns")
232232
cur.execute(
233-
"""with constraint_columns as (
233+
""" with table_constraints as (
234+
select tc.TABLE_SCHEMA,
235+
tc.TABLE_NAME,
236+
tc.CONSTRAINT_NAME,
237+
tc.CONSTRAINT_TYPE,
238+
row_number() over (partition by tc.TABLE_SCHEMA, tc.TABLE_NAME
239+
order by tc.constraint_TYPE) as row_number_rank
240+
241+
from INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
242+
where tc.CONSTRAINT_TYPE in ('PRIMARY KEY', 'UNIQUE')
243+
)
244+
,constraint_columns as (
234245
select c.TABLE_SCHEMA
235246
, c.TABLE_NAME
236247
, c.COLUMN_NAME
248+
, c.CONSTRAINT_NAME
237249
238250
from INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE c
239251
240-
join INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
252+
join table_constraints tc
241253
on tc.TABLE_SCHEMA = c.TABLE_SCHEMA
242254
and tc.TABLE_NAME = c.TABLE_NAME
243255
and tc.CONSTRAINT_NAME = c.CONSTRAINT_NAME
244-
and tc.CONSTRAINT_TYPE in ('PRIMARY KEY', 'UNIQUE'))
256+
and tc.row_number_rank = 1
257+
)
245258
SELECT c.TABLE_SCHEMA,
246259
c.TABLE_NAME,
247260
c.COLUMN_NAME,

tests/test_tap_mssql.py

+57
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,63 @@ def test_do_not_discover_key_properties_for_view(self):
650650

651651
self.assertEqual(primary_keys, {"a_table": ["id"], "a_view": []})
652652

653+
class TestPrimaryKeyUniqueKey(unittest.TestCase):
654+
def setUp(self):
655+
self.conn = test_utils.get_test_connection()
656+
657+
with connect_with_backoff(self.conn) as open_conn:
658+
with open_conn.cursor() as cursor:
659+
try:
660+
cursor.execute("drop table uc_only_table")
661+
except:
662+
pass
663+
try:
664+
cursor.execute("drop table pk_only_table")
665+
except:
666+
pass
667+
try:
668+
cursor.execute("drop table pk_uc_table")
669+
except:
670+
pass
671+
cursor.execute(
672+
"""
673+
CREATE TABLE uc_only_table (
674+
pk int,
675+
uc_1 int,
676+
uc_2 int,
677+
CONSTRAINT constraint_uc_only_table UNIQUE(uc_1,uc_2) )
678+
"""
679+
)
680+
cursor.execute(
681+
"""
682+
CREATE TABLE pk_only_table (
683+
pk int PRIMARY KEY,
684+
uc_1 int,
685+
uc_2 int,
686+
)
687+
"""
688+
)
689+
cursor.execute(
690+
"""
691+
CREATE TABLE pk_uc_table (
692+
pk int PRIMARY KEY,
693+
uc_1 int,
694+
uc_2 int,
695+
CONSTRAINT constraint_pk_uc_table UNIQUE(uc_1,uc_2) )
696+
"""
697+
)
698+
699+
def test_only_primary_key(self):
700+
catalog = test_utils.discover_catalog(self.conn, {})
701+
primary_keys = {}
702+
for c in catalog.streams:
703+
primary_keys[c.table] = (
704+
singer.metadata.to_map(c.metadata).get((), {}).get("table-key-properties")
705+
)
706+
707+
self.assertEqual(primary_keys["uc_only_table"], ["uc_1","uc_2"])
708+
self.assertEqual(primary_keys["pk_only_table"], ["pk"])
709+
self.assertEqual(primary_keys["pk_uc_table"], ["pk"])
653710

654711
if __name__ == "__main__":
655712
# test1 = TestBinlogReplication()

0 commit comments

Comments
 (0)