Skip to content
This repository was archived by the owner on Sep 23, 2024. It is now read-only.

Commit b4d8239

Browse files
committed
Use underlying type of domain type when defined
This fixes issues with primary keys being defined using domain types, and deselected by default. E.g. for a domain type `email_domain` with base type `text`, the `data_type` column will now be `text` rather than `email_domain`.
1 parent c0eb14e commit b4d8239

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

tap_postgres/discovery_utils.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ def produce_table_info(conn, filter_schemas=None, tables: Optional[List[str]] =
6464
with conn.cursor(cursor_factory=psycopg2.extras.DictCursor, name='stitch_cursor') as cur:
6565
cur.itersize = post_db.CURSOR_ITER_SIZE
6666
table_info = {}
67-
# SELECT CASE WHEN $2.typtype = 'd' THEN $2.typbasetype ELSE $1.atttypid END
6867
sql = """
6968
SELECT
7069
pg_class.reltuples::BIGINT AS approximate_row_count,
@@ -73,7 +72,7 @@ def produce_table_info(conn, filter_schemas=None, tables: Optional[List[str]] =
7372
pg_class.relname AS table_name,
7473
attname AS column_name,
7574
i.indisprimary AS primary_key,
76-
format_type(a.atttypid, NULL::integer) AS data_type,
75+
format_type(CASE WHEN pgt.typtype = 'd' THEN pgt.typbasetype ELSE a.atttypid END, NULL::integer) AS data_type,
7776
information_schema._pg_char_max_length(CASE WHEN COALESCE(subpgt.typtype, pgt.typtype) = 'd'
7877
THEN COALESCE(subpgt.typbasetype, pgt.typbasetype) ELSE COALESCE(subpgt.oid, pgt.oid)
7978
END,

tests/test_discovery.py

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,51 @@ def test_catalog(self):
452452
'definitions' : BASE_RECURSIVE_SCHEMAS},
453453
stream_dict.get('schema'))
454454

455+
class TestDomainTable(unittest.TestCase):
456+
maxDiff = None
457+
table_name = 'CHICKEN TIMES'
458+
459+
def setUp(self):
460+
table_spec = {"columns": [{"name" : 'our_test_domain_pk', "type" : "test_domain", "primary_key" : True },
461+
{"name" : 'our_test_domain', "type" : "test_domain"},
462+
{"name" : 'our_test_integer_domain', "type" : "test_integer_domain" }],
463+
"name" : TestHStoreTable.table_name}
464+
with get_test_connection() as conn:
465+
cur = conn.cursor()
466+
cur.execute(""" DROP DOMAIN IF EXISTS test_domain CASCADE """)
467+
cur.execute(""" CREATE DOMAIN test_domain AS text; """)
468+
cur.execute(""" DROP DOMAIN IF EXISTS test_integerdomain CASCADE """)
469+
cur.execute(""" CREATE DOMAIN test_integerdomain AS text; """)
470+
471+
ensure_test_table(table_spec)
472+
473+
def test_catalog(self):
474+
conn_config = get_test_connection_config()
475+
streams = tap_postgres.do_discovery(conn_config)
476+
chicken_streams = [s for s in streams if s['tap_stream_id'] == 'public-CHICKEN TIMES']
477+
self.assertEqual(len(chicken_streams), 1)
478+
stream_dict = chicken_streams[0]
479+
stream_dict.get('metadata').sort(key=lambda md: md['breadcrumb'])
480+
481+
with get_test_connection() as conn:
482+
with conn.cursor(cursor_factory=psycopg2.extras.DictCursor) as cur:
483+
cur.execute("""INSERT INTO "CHICKEN TIMES" (our_test_domain_pk, our_test_domain, our_test_integer_domain) VALUES ('sad', 'happy', 3)""")
484+
cur.execute("""SELECT * FROM "CHICKEN TIMES" """)
485+
486+
self.assertEqual(metadata.to_map(stream_dict.get('metadata')),
487+
{() : {'table-key-properties': ['our_test_domain_pk'], 'database-name': 'postgres', 'schema-name': 'public', 'is-view': False, 'row-count': 0},
488+
('properties', 'our_test_domain_pk') : {'inclusion': 'automatic', 'sql-datatype' : 'text', 'selected-by-default' : True},
489+
('properties', 'our_test_domain') : {'inclusion': 'available', 'sql-datatype' : 'text', 'selected-by-default' : True},
490+
('properties', 'our_test_integer_domain') : {'inclusion': 'available', 'sql-datatype' : 'integer', 'selected-by-default' : True}})
491+
492+
493+
self.assertEqual({'properties': {'our_test__integer_domain': {'type': ['null', 'integer']},
494+
'our_test_domain': {'type': ['null', 'string']},
495+
'our_test_domain_pk': {'type': ['string']}},
496+
'type': 'object',
497+
'definitions' : BASE_RECURSIVE_SCHEMAS},
498+
stream_dict.get('schema'))
499+
455500
class TestArraysTable(unittest.TestCase):
456501
maxDiff = None
457502
table_name = 'CHICKEN TIMES'
@@ -536,7 +581,7 @@ class TestColumnGrants(unittest.TestCase):
536581
table_name = 'CHICKEN TIMES'
537582
user = 'tmp_user_for_grant_tests'
538583
password = 'password'
539-
584+
540585
def setUp(self):
541586
table_spec = {"columns": [{"name" : "id", "type" : "integer", "serial" : True},
542587
{"name" : 'size integer', "type" : "integer", "quoted" : True},
@@ -560,8 +605,8 @@ def setUp(self):
560605
LOGGER.info("running sql: {}".format(sql))
561606
cur.execute(sql)
562607

563-
564-
608+
609+
565610

566611
def test_catalog(self):
567612
conn_config = get_test_connection_config()
@@ -587,7 +632,7 @@ def test_catalog(self):
587632
('properties', 'id'): {'inclusion': 'available',
588633
'selected-by-default': True,
589634
'sql-datatype': 'integer'}})
590-
635+
591636
self.assertEqual({'definitions' : BASE_RECURSIVE_SCHEMAS,
592637
'type': 'object',
593638
'properties': {'id': {'type': ['null', 'integer'],

0 commit comments

Comments
 (0)