Skip to content

Commit

Permalink
Macro: Add cratedb__reset_csv_table for DELETE FROM not TRUNCATE
Browse files Browse the repository at this point in the history
  • Loading branch information
amotl committed Nov 28, 2024
1 parent c51c715 commit 4a5d3b2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 13 additions & 0 deletions dbt/include/cratedb/macros/materializations/seed.sql
Original file line number Diff line number Diff line change
@@ -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 %}
32 changes: 32 additions & 0 deletions tests/functional/adapter/test_cratedb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand Down Expand Up @@ -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,)]

0 comments on commit 4a5d3b2

Please sign in to comment.