Skip to content

Commit 0e0a112

Browse files
refactor: Deprecate sqlalchemy_url and dialect+driver settings
1 parent 41a9d99 commit 0e0a112

File tree

3 files changed

+34
-32
lines changed

3 files changed

+34
-32
lines changed

target_postgres/driver.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"""This module contains the Postgres driver name for SQLAlchemy."""
2+
3+
PSYCOPG3 = "postgresql+psycopg"

target_postgres/target.py

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from singer_sdk import typing as th
88
from singer_sdk.target_base import SQLTarget
99

10+
from target_postgres.driver import PSYCOPG3
1011
from target_postgres.sinks import PostgresSink
1112

1213
if t.TYPE_CHECKING:
@@ -40,6 +41,22 @@ def __init__(
4041
parse_env_config=parse_env_config,
4142
validate_config=validate_config,
4243
)
44+
# sqlalchemy_url and dialect+driver are now deprecated in favor of the
45+
# individual host, port, user, password, and dialect+driver fields.
46+
if self.config.get("sqlalchemy_url"):
47+
self.logger.warning(
48+
"The `sqlalchemy_url` configuration option is deprecated. "
49+
"Please use the `host`, `port`, `user`, `password` "
50+
"configuration options instead."
51+
)
52+
53+
if (driver := self.config.get("dialect+driver")) and driver != PSYCOPG3:
54+
self.logger.warning(
55+
"The `dialect+driver` configuration option is deprecated. "
56+
f"Please set it to `{PSYCOPG3}`, as this will be the hard-coded "
57+
"value in the future."
58+
)
59+
4360
# There's a few ways to do this in JSON Schema but it is schema draft dependent.
4461
# https://stackoverflow.com/questions/38717933/jsonschema-attribute-conditionally-required # noqa: E501
4562
assert (self.config.get("sqlalchemy_url") is not None) or (
@@ -98,49 +115,34 @@ def __init__(
98115
th.Property(
99116
"host",
100117
th.StringType,
101-
description=(
102-
"Hostname for postgres instance. "
103-
+ "Note if sqlalchemy_url is set this will be ignored."
104-
),
118+
description="Hostname for postgres instance.",
105119
),
106120
th.Property(
107121
"port",
108122
th.IntegerType,
109123
default=5432,
110-
description=(
111-
"The port on which postgres is awaiting connection. "
112-
+ "Note if sqlalchemy_url is set this will be ignored."
113-
),
124+
description="The port on which postgres is awaiting connections.",
114125
),
115126
th.Property(
116127
"user",
117128
th.StringType,
118-
description=(
119-
"User name used to authenticate. "
120-
+ "Note if sqlalchemy_url is set this will be ignored."
121-
),
129+
description="User name used to authenticate.",
122130
),
123131
th.Property(
124132
"password",
125133
th.StringType,
126-
description=(
127-
"Password used to authenticate. "
128-
"Note if sqlalchemy_url is set this will be ignored."
129-
),
134+
description="Password used to authenticate.",
130135
),
131136
th.Property(
132137
"database",
133138
th.StringType,
134-
description=(
135-
"Database name. "
136-
+ "Note if sqlalchemy_url is set this will be ignored."
137-
),
139+
description="Database name.",
138140
),
139141
th.Property(
140142
"sqlalchemy_url",
141143
th.StringType,
142144
description=(
143-
"SQLAlchemy connection string. "
145+
"DEPRECATED. SQLAlchemy connection string. "
144146
+ "This will override using host, user, password, port, "
145147
+ "dialect, and all ssl settings. Note that you must escape password "
146148
+ "special characters properly. See "
@@ -150,12 +152,11 @@ def __init__(
150152
th.Property(
151153
"dialect+driver",
152154
th.StringType,
153-
default="postgresql+psycopg",
155+
default=PSYCOPG3,
154156
description=(
155-
"Dialect+driver see "
157+
"DEPRECATED. Dialect+driver see "
156158
+ "https://docs.sqlalchemy.org/en/20/core/engines.html. "
157-
+ "Generally just leave this alone. "
158-
+ "Note if sqlalchemy_url is set this will be ignored."
159+
+ "Generally just leave this alone."
159160
),
160161
),
161162
th.Property(
@@ -215,7 +216,6 @@ def __init__(
215216
+ " ssl_certificate_authority and ssl_mode for further customization."
216217
+ " To use a client certificate to authenticate yourself to the server,"
217218
+ " use ssl_client_certificate_enable instead."
218-
+ " Note if sqlalchemy_url is set this will be ignored."
219219
),
220220
),
221221
th.Property(
@@ -227,7 +227,6 @@ def __init__(
227227
+ " authentication to the server. Use ssl_client_certificate and"
228228
+ " ssl_client_private_key for further customization. To use SSL to"
229229
+ " verify the server's identity, use ssl_enable instead."
230-
+ " Note if sqlalchemy_url is set this will be ignored."
231230
),
232231
),
233232
th.Property(
@@ -249,7 +248,6 @@ def __init__(
249248
"The certificate authority that should be used to verify the server's"
250249
+ " identity. Can be provided either as the certificate itself (in"
251250
+ " .env) or as a filepath to the certificate."
252-
+ " Note if sqlalchemy_url is set this will be ignored."
253251
),
254252
),
255253
th.Property(
@@ -271,7 +269,6 @@ def __init__(
271269
"The private key for the certificate you provided. Can be provided"
272270
+ " either as the certificate itself (in .env) or as a filepath to the"
273271
+ " certificate."
274-
+ " Note if sqlalchemy_url is set this will be ignored."
275272
),
276273
),
277274
th.Property(

target_postgres/tests/core.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
def postgres_config():
1010
return {
11-
"dialect+driver": "postgresql+psycopg",
1211
"host": "localhost",
1312
"user": "postgres",
1413
"password": "postgres",
@@ -29,7 +28,6 @@ def postgres_config():
2928

3029
def postgres_config_no_ssl():
3130
return {
32-
"dialect+driver": "postgresql+psycopg",
3331
"host": "localhost",
3432
"user": "postgres",
3533
"password": "postgres",
@@ -43,7 +41,11 @@ def postgres_config_no_ssl():
4341

4442
def postgres_config_ssh_tunnel():
4543
return {
46-
"sqlalchemy_url": "postgresql://postgres:postgres@10.5.0.5:5432/main",
44+
"host": "10.5.0.5",
45+
"user": "postgres",
46+
"password": "postgres",
47+
"database": "main",
48+
"port": 5432,
4749
"ssh_tunnel": {
4850
"enable": True,
4951
"host": "127.0.0.1",

0 commit comments

Comments
 (0)