From 4a5d3b213c114d9644377972c6c31f90824923b9 Mon Sep 17 00:00:00 2001 From: Andreas Motl Date: Thu, 28 Nov 2024 13:07:58 +0100 Subject: [PATCH] Macro: Add `cratedb__reset_csv_table` for `DELETE FROM` not `TRUNCATE` --- CHANGELOG.md | 2 ++ .../cratedb/macros/materializations/seed.sql | 13 ++++++++ tests/functional/adapter/test_cratedb.py | 32 +++++++++++++++++++ 3 files changed, 47 insertions(+) create mode 100644 dbt/include/cratedb/macros/materializations/seed.sql diff --git a/CHANGELOG.md b/CHANGELOG.md index f811ec7..13d6f50 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ and adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## Unreleased - Add integration test case with CrateDB using `rename_relation` +- Macro: Add `cratedb__reset_csv_table` for using `DELETE FROM` + instead of `TRUNCATE`. Thanks, @hlcianfagna. ## v0.0.2 - November 26, 2024 diff --git a/dbt/include/cratedb/macros/materializations/seed.sql b/dbt/include/cratedb/macros/materializations/seed.sql new file mode 100644 index 0000000..95c77b8 --- /dev/null +++ b/dbt/include/cratedb/macros/materializations/seed.sql @@ -0,0 +1,13 @@ +{# CrateDB: Use `DELETE FROM` instead of `TRUNCATE` #} +{% macro cratedb__reset_csv_table(model, full_refresh, old_relation, agate_table) %} + {% set sql = "" %} + {% if full_refresh %} + {{ adapter.drop_relation(old_relation) }} + {% set sql = create_csv_table(model, agate_table) %} + {% else %} + {{ adapter.truncate_relation(old_relation) }} + {% set sql = "delete from " ~ old_relation.render() %} + {% endif %} + + {{ return(sql) }} +{% endmacro %} diff --git a/tests/functional/adapter/test_cratedb.py b/tests/functional/adapter/test_cratedb.py index 146febc..d45de77 100644 --- a/tests/functional/adapter/test_cratedb.py +++ b/tests/functional/adapter/test_cratedb.py @@ -40,6 +40,26 @@ """ +reset_csv_table = """ +{# Create a random table. #} +{% set exists, random_table = get_or_create_relation(database=None, schema=target.schema, identifier="random_table", type="table") %} +{% set sql = get_create_table_as_sql(False, random_table, "SELECT 42 as value") %} +{% do run_query(sql) %} + +{# +This is a little excerpt from dbt/include/global_project/macros/materializations/seeds/seed.sql +#} +{% set full_refresh = (should_full_refresh()) %} +{% set agate_table = None %} +{% set sql_ddl = reset_csv_table(model=model, full_refresh=full_refresh, old_relation=random_table, agate_table=agate_table) %} +{% set sql_dml = load_csv_rows(model, agate_table) %} +{% set sql = get_csv_sql(sql_ddl, sql_dml) %} +{% do run_query(sql) %} + +SELECT TRUE +""" + + class TestCrateDB: """ A few test cases for specifically validating concerns of CrateDB. @@ -51,6 +71,7 @@ def models(self): "basic.sql": basic_sql, "rename_table.sql": rename_table_sql, "rename_view.sql": rename_view_sql, + "reset_csv_table.sql": reset_csv_table, } @pytest.fixture(autouse=True) @@ -102,3 +123,14 @@ def test_rename_relation_view(self, project): records = common.get_records(project, "view_target") assert records == [(42,)] + + def test_reset_csv(self, project): + """ + CrateDB needs an override for the `reset_csv_table` macro. Make sure it is in place. + """ + + result = run_dbt(["run", "--select", "reset_csv_table"]) + assert len(result) == 1 + + records = common.get_records(project, "reset_csv_table") + assert records == [(True,)]