Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Create SQLAlchemy URL from config #20

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions meltano.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,19 @@ plugins:
namespace: "target_postgres"
pip_url: -e .
config:
sqlalchemy_url: "postgresql://postgres:postgres@localhost:5432/postgres"
dialect: postgresql
driver_type: psycopg2
host: localhost
user: postgres
password: postgres
database: postgres
settings:
- name: sqlalchemy_url
- name: dialect
- name: driver_type
- name: host
- name: port
kind: integer
- name: user
- name: password
kind: password
- name: database
35 changes: 35 additions & 0 deletions target_postgres/connector.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Connector class for target."""
import sqlalchemy
from sqlalchemy.engine import URL

from singer_sdk import SQLConnector


Expand All @@ -24,3 +26,36 @@ def create_sqlalchemy_connection(self) -> sqlalchemy.engine.Connection:
A newly created SQLAlchemy engine object.
"""
return self.create_sqlalchemy_engine().connect()

def get_sqlalchemy_url(self, config: dict) -> str:
"""Generates a SQLAlchemy URL for sqlbuzz.

Args:
config: The configuration for the connector.
"""
if config['dialect'] == "postgresql" :
url_drivername:str = config['dialect']
else:
self.logger.error("Invalid dialect given")
exit(1)

if config['driver_type'] in ["psycopg2","pg8000","asyncpg","psycopg2cffi","pypostgresql","pygresql"]:
url_drivername += f"+{config['driver_type']}"
else:
self.logger.error("Invalid driver_type given")
exit(1)

self.logger.info(url_drivername)
config_url = URL.create(
url_drivername,
config['user'],
config['password'],
host = config['host'],
database = config['database']
)

if 'port' in config:
config_url.set(port={config['port']})

return (config_url)

36 changes: 32 additions & 4 deletions target_postgres/target.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,39 @@ class TargetPostgres(Target):
name = "target-postgres"
config_jsonschema = th.PropertiesList(
th.Property(
"sqlalchemy_url",
"dialect",
th.StringType,
required=True,
description="SQLAlchemy connection string, example."
+ "`postgresql://postgres:postgres@localhost:5432/postgres`",
description="The Dialect of SQLAlchamey"
),
th.Property(
"driver_type",
th.StringType,
description="The Python Driver you will be using to connect to the SQL server"
),
th.Property(
"host",
th.StringType,
description="The FQDN of the Host serving out the SQL Instance"
),
th.Property(
"port",
th.IntegerType,
description="The port on which SQL awaiting connection"
),
th.Property(
"user",
th.StringType,
description="The User Account who has been granted access to the SQL Server"
),
th.Property(
"password",
th.StringType,
description="The Password for the User account"
),
th.Property(
"database",
th.StringType,
description="The Default database for this connection"
),
).to_dict()
default_sink_class = PostgresSink
9 changes: 8 additions & 1 deletion target_postgres/tests/test_standard_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@

@pytest.fixture()
def postgres_config():
return {"sqlalchemy_url": "postgresql://postgres:postgres@localhost:5432/postgres"}
return {
"dialect": "postgresql",
"driver_type": "psycopg2",
"host": "localhost",
"user": "postgres",
"password": "postgres",
"database": "postgres"
}

@pytest.fixture
def postgres_target(postgres_config) -> TargetPostgres:
Expand Down