Skip to content

Commit 6e6d4fc

Browse files
fix: Implement NOTYPE.python_type to fix adapting column types (#238)
Closes #237
1 parent d864106 commit 6e6d4fc

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

target_postgres/connector.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Handles Postgres interactions."""
2+
23
from __future__ import annotations
34

45
import atexit
@@ -813,3 +814,12 @@ def process_bind_param(self, value, dialect):
813814
if value is not None and isinstance(value, (dict, list)):
814815
value = simplejson.dumps(value, use_decimal=True)
815816
return value
817+
818+
@property
819+
def python_type(self):
820+
"""Return the Python type for this column."""
821+
return object
822+
823+
def as_generic(self, *args: t.Any, **kwargs: t.Any):
824+
"""Return the generic type for this column."""
825+
return TEXT()

target_postgres/sinks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def generate_temp_table_name(self):
115115
# exceeds maximum length of 63 characters
116116
# Is hit if we have a long table name, there is no limit on Temporary tables
117117
# in postgres, used a guid just in case we are using the same session
118-
return f"{str(uuid.uuid4()).replace('-','_')}"
118+
return f"{str(uuid.uuid4()).replace('-', '_')}"
119119

120120
def bulk_insert_records( # type: ignore[override]
121121
self,

target_postgres/tests/test_types.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""Test custom types and the type hierarchy."""
2+
3+
import pytest
4+
from sqlalchemy import Boolean, DateTime, Integer, String
5+
6+
from target_postgres.connector import NOTYPE, PostgresConnector
7+
8+
9+
@pytest.fixture
10+
def connector():
11+
"""Create a PostgresConnector instance."""
12+
return PostgresConnector(
13+
config={
14+
"dialect+driver": "postgresql+psycopg2",
15+
"host": "localhost",
16+
"port": "5432",
17+
"user": "postgres",
18+
"password": "postgres",
19+
"database": "postgres",
20+
"ssl_enable": False,
21+
"ssl_client_certificate_enable": False,
22+
},
23+
)
24+
25+
26+
@pytest.mark.parametrize(
27+
("types", "expected"),
28+
[
29+
pytest.param([Integer(), String()], String, id="int+str=str"),
30+
pytest.param([Boolean(), String()], String, id="bool+str=str"),
31+
pytest.param([Integer(), DateTime()], Integer, id="int+datetime=int"),
32+
pytest.param([NOTYPE(), String()], String, id="none+str=str"),
33+
pytest.param([NOTYPE(), Integer()], NOTYPE, id="none+int=none"),
34+
],
35+
)
36+
def test_type_hierarchy(connector, types, expected):
37+
"""Test that types are merged correctly."""
38+
assert type(connector.merge_sql_types(types)) is expected

0 commit comments

Comments
 (0)