Skip to content

Commit

Permalink
Better handling for dedicated schemas (#336)
Browse files Browse the repository at this point in the history
* Better handling for dedicated schemas

* Unit tests, PR feedback
  • Loading branch information
dogversioning authored Jan 21, 2025
1 parent 0d09ad4 commit 0dc79ae
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 4 deletions.
7 changes: 6 additions & 1 deletion cumulus_library/actions/cleaner.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,12 @@ def clean_study(
if dedicated := manifest.get_dedicated_schema():
view_table_list = [
(
f"`{dedicated}`.`{x[0]}`",
# Athena uses different quoting strategies for drop view statements
# versus drop table statements. -_-
# TODO: Consider moving this logic to a database object?
f"`{dedicated}`.`{x[0]}`"
if (x[1] == "TABLE" and config.db.db_type == "athena")
else f'"{dedicated}"."{x[0]}"',
x[1],
)
for x in view_table_list
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "cumulus-library"
requires-python = ">= 3.11"
dependencies = [
"cumulus-fhir-support >= 1.2",
"duckdb >= 1.1",
"duckdb >= 1.1.3",
"Jinja2 > 3",
"pandas <3, >=2.1.3",
"psmpy <1, >=0.3.13",
Expand Down
24 changes: 24 additions & 0 deletions tests/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,30 @@ def test_clean_study(mock_db_config, verbose, prefix, confirm, stats, target, ra
assert ("study_valid__456",) not in remaining_tables


def test_clean_dedicated_schema(mock_db_config):
with mock.patch.object(builtins, "input", lambda _: False):
mock_db_config.schema = "dedicated"
manifest = study_manifest.StudyManifest("./tests/test_data/study_dedicated_schema/")
mock_db_config.db.cursor().execute("CREATE SCHEMA dedicated")
builder.run_protected_table_builder(
config=mock_db_config,
manifest=manifest,
)
mock_db_config.db.cursor().execute("CREATE TABLE dedicated.table_1 (test int)")
mock_db_config.db.cursor().execute(
"CREATE VIEW dedicated.view_2 AS SELECT * FROM dedicated.table_1"
)
cleaner.clean_study(config=mock_db_config, manifest=manifest)
remaining_tables = (
mock_db_config.db.cursor()
.execute("select distinct(table_name) from information_schema.tables")
.fetchall()
)
assert (f"{enums.ProtectedTables.TRANSACTIONS.value}",) in remaining_tables
assert ("table_1",) not in remaining_tables
assert ("view_2",) not in remaining_tables


def test_clean_throws_error_on_missing_params(mock_db_config):
with pytest.raises(errors.CumulusLibraryError):
cleaner.clean_study(config=mock_db_config, manifest=None)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ def test_dedicated_schema(tmp_path):
)
for table in [
("dedicated", "table_1"),
("dedicated", "table_2"),
("dedicated", "view_2"),
("dedicated", "table_raw_sql"),
("main", "core__condition"),
]:
Expand Down
3 changes: 2 additions & 1 deletion tests/test_data/study_dedicated_schema/module2.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ class ModuleTwoRunner(cumulus_library.BaseTableBuilder):

def prepare_queries(self, *args, **kwargs):
self.queries.append(
"CREATE TABLE IF NOT EXISTS study_dedicated_schema__table_2 (test int);"
"""CREATE VIEW IF NOT EXISTS study_dedicated_schema__view_2 AS
SELECT * FROM dedicated.table_1;"""
)

0 comments on commit 0dc79ae

Please sign in to comment.