Skip to content

Commit

Permalink
refactor: Deprecate sqlalchemy_url and dialect+driver settings
Browse files Browse the repository at this point in the history
  • Loading branch information
edgarrmondragon committed Nov 19, 2024
1 parent 41a9d99 commit 0e0a112
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 32 deletions.
3 changes: 3 additions & 0 deletions target_postgres/driver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""This module contains the Postgres driver name for SQLAlchemy."""

PSYCOPG3 = "postgresql+psycopg"
55 changes: 26 additions & 29 deletions target_postgres/target.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from singer_sdk import typing as th
from singer_sdk.target_base import SQLTarget

from target_postgres.driver import PSYCOPG3
from target_postgres.sinks import PostgresSink

if t.TYPE_CHECKING:
Expand Down Expand Up @@ -40,6 +41,22 @@ def __init__(
parse_env_config=parse_env_config,
validate_config=validate_config,
)
# sqlalchemy_url and dialect+driver are now deprecated in favor of the
# individual host, port, user, password, and dialect+driver fields.
if self.config.get("sqlalchemy_url"):
self.logger.warning(
"The `sqlalchemy_url` configuration option is deprecated. "
"Please use the `host`, `port`, `user`, `password` "
"configuration options instead."
)

if (driver := self.config.get("dialect+driver")) and driver != PSYCOPG3:
self.logger.warning(
"The `dialect+driver` configuration option is deprecated. "
f"Please set it to `{PSYCOPG3}`, as this will be the hard-coded "
"value in the future."
)

# There's a few ways to do this in JSON Schema but it is schema draft dependent.
# https://stackoverflow.com/questions/38717933/jsonschema-attribute-conditionally-required # noqa: E501
assert (self.config.get("sqlalchemy_url") is not None) or (
Expand Down Expand Up @@ -98,49 +115,34 @@ def __init__(
th.Property(
"host",
th.StringType,
description=(
"Hostname for postgres instance. "
+ "Note if sqlalchemy_url is set this will be ignored."
),
description="Hostname for postgres instance.",
),
th.Property(
"port",
th.IntegerType,
default=5432,
description=(
"The port on which postgres is awaiting connection. "
+ "Note if sqlalchemy_url is set this will be ignored."
),
description="The port on which postgres is awaiting connections.",
),
th.Property(
"user",
th.StringType,
description=(
"User name used to authenticate. "
+ "Note if sqlalchemy_url is set this will be ignored."
),
description="User name used to authenticate.",
),
th.Property(
"password",
th.StringType,
description=(
"Password used to authenticate. "
"Note if sqlalchemy_url is set this will be ignored."
),
description="Password used to authenticate.",
),
th.Property(
"database",
th.StringType,
description=(
"Database name. "
+ "Note if sqlalchemy_url is set this will be ignored."
),
description="Database name.",
),
th.Property(
"sqlalchemy_url",
th.StringType,
description=(
"SQLAlchemy connection string. "
"DEPRECATED. SQLAlchemy connection string. "
+ "This will override using host, user, password, port, "
+ "dialect, and all ssl settings. Note that you must escape password "
+ "special characters properly. See "
Expand All @@ -150,12 +152,11 @@ def __init__(
th.Property(
"dialect+driver",
th.StringType,
default="postgresql+psycopg",
default=PSYCOPG3,
description=(
"Dialect+driver see "
"DEPRECATED. Dialect+driver see "
+ "https://docs.sqlalchemy.org/en/20/core/engines.html. "
+ "Generally just leave this alone. "
+ "Note if sqlalchemy_url is set this will be ignored."
+ "Generally just leave this alone."
),
),
th.Property(
Expand Down Expand Up @@ -215,7 +216,6 @@ def __init__(
+ " ssl_certificate_authority and ssl_mode for further customization."
+ " To use a client certificate to authenticate yourself to the server,"
+ " use ssl_client_certificate_enable instead."
+ " Note if sqlalchemy_url is set this will be ignored."
),
),
th.Property(
Expand All @@ -227,7 +227,6 @@ def __init__(
+ " authentication to the server. Use ssl_client_certificate and"
+ " ssl_client_private_key for further customization. To use SSL to"
+ " verify the server's identity, use ssl_enable instead."
+ " Note if sqlalchemy_url is set this will be ignored."
),
),
th.Property(
Expand All @@ -249,7 +248,6 @@ def __init__(
"The certificate authority that should be used to verify the server's"
+ " identity. Can be provided either as the certificate itself (in"
+ " .env) or as a filepath to the certificate."
+ " Note if sqlalchemy_url is set this will be ignored."
),
),
th.Property(
Expand All @@ -271,7 +269,6 @@ def __init__(
"The private key for the certificate you provided. Can be provided"
+ " either as the certificate itself (in .env) or as a filepath to the"
+ " certificate."
+ " Note if sqlalchemy_url is set this will be ignored."
),
),
th.Property(
Expand Down
8 changes: 5 additions & 3 deletions target_postgres/tests/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

def postgres_config():
return {
"dialect+driver": "postgresql+psycopg",
"host": "localhost",
"user": "postgres",
"password": "postgres",
Expand All @@ -29,7 +28,6 @@ def postgres_config():

def postgres_config_no_ssl():
return {
"dialect+driver": "postgresql+psycopg",
"host": "localhost",
"user": "postgres",
"password": "postgres",
Expand All @@ -43,7 +41,11 @@ def postgres_config_no_ssl():

def postgres_config_ssh_tunnel():
return {
"sqlalchemy_url": "postgresql://postgres:[email protected]:5432/main",
"host": "10.5.0.5",
"user": "postgres",
"password": "postgres",
"database": "main",
"port": 5432,
"ssh_tunnel": {
"enable": True,
"host": "127.0.0.1",
Expand Down

0 comments on commit 0e0a112

Please sign in to comment.