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: raise exception if database doesnt exist #96

Merged
merged 5 commits into from
Jul 24, 2023
Merged
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
8 changes: 7 additions & 1 deletion target_snowflake/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def create_engine(self) -> Engine:
Returns:
A new SQLAlchemy Engine.
"""
return sqlalchemy.create_engine(
engine = sqlalchemy.create_engine(
self.sqlalchemy_url,
connect_args={
"session_parameters": {
Expand All @@ -143,6 +143,12 @@ def create_engine(self) -> Engine:
},
echo=False,
)
connection = engine.connect()
db_names = [db[1] for db in connection.execute(text("SHOW DATABASES;")).fetchall()]
if self.config["database"] not in db_names:
raise Exception(f"Database '{self.config['database']}' does not exist or the user/role doesn't have access to it.")
return engine


def prepare_column(
self,
Expand Down
11 changes: 11 additions & 0 deletions tests/test_target_snowflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,14 @@ class TestTargetSnowflake(BaseSnowflakeTargetTests, StandardTargetTests): # typ

class TestTargetSnowflakeBatch(BaseSnowflakeTargetTests, BatchTargetTests): # type: ignore[misc, valid-type] # noqa: E501
"""Batch Target Tests."""

def test_invalid_database():
INVALID_TEST_CONFIG = copy.deepcopy(SAMPLE_CONFIG)
INVALID_TEST_CONFIG["database"] = "FOO_BAR_DOESNT_EXIST"
runner = TargetTestRunner(
TargetSnowflake,
config=INVALID_TEST_CONFIG,
input_filepath="tests/target_test_streams/existing_table.singer"
)
with pytest.raises(Exception):
runner.sync_all()