Skip to content

Commit

Permalink
consolidate timestamp macros (#273)
Browse files Browse the repository at this point in the history
* consolidate timestamp macros

* add testing changes

* deleting extra timestamps.sql

* changie entry

* fix backcompat

* Update Breaking Changes-20220923-112314.yaml

* Update dev-requirements.txt

* remove current_timestamp_in_utc

* fix backcompat

* change test import

* fix BaseCurrentTimestamps import

* update dev-requirements

* Update change log body

* fix changie log
  • Loading branch information
colin-rogers-dbt authored Sep 30, 2022
1 parent c69e213 commit 2ef59aa
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 15 deletions.
7 changes: 7 additions & 0 deletions .changes/unreleased/Feature-20220923-112314.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
kind: Feature
body: Migrate dbt-utils current_timestamp macros into core + adapters
time: 2022-09-23T11:23:14.312738-07:00
custom:
Author: colin-rogers-dbt
Issue: "276"
PR: "273"
15 changes: 0 additions & 15 deletions dbt/include/snowflake/macros/adapters.sql
Original file line number Diff line number Diff line change
Expand Up @@ -161,21 +161,6 @@
{{ return(load_result('check_schema_exists').table) }}
{%- endmacro %}

{% macro snowflake__current_timestamp() -%}
convert_timezone('UTC', current_timestamp())
{%- endmacro %}


{% macro snowflake__snapshot_string_as_time(timestamp) -%}
{%- set result = "to_timestamp_ntz('" ~ timestamp ~ "')" -%}
{{ return(result) }}
{%- endmacro %}


{% macro snowflake__snapshot_get_time() -%}
to_timestamp_ntz({{ current_timestamp() }})
{%- endmacro %}


{% macro snowflake__rename_relation(from_relation, to_relation) -%}
{% call statement('rename_relation') -%}
Expand Down
20 changes: 20 additions & 0 deletions dbt/include/snowflake/macros/utils/timestamps.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{% macro snowflake__current_timestamp() -%}
convert_timezone('UTC', current_timestamp())
{%- endmacro %}

{% macro snowflake__snapshot_string_as_time(timestamp) -%}
{%- set result = "to_timestamp_ntz('" ~ timestamp ~ "')" -%}
{{ return(result) }}
{%- endmacro %}

{% macro snowflake__snapshot_get_time() -%}
to_timestamp_ntz({{ current_timestamp() }})
{%- endmacro %}

{% macro snowflake__current_timestamp_backcompat() %}
current_timestamp::{{ type_timestamp() }}
{% endmacro %}

{% macro snowflake__current_timestamp_in_utc_backcompat() %}
convert_timezone('UTC', {{ snowflake__current_timestamp_backcompat() }})::{{ type_timestamp() }}
{% endmacro %}
31 changes: 31 additions & 0 deletions tests/functional/adapter/test_timestamps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import imp
import pytest
from dbt.tests.adapter.utils.test_timestamps import BaseCurrentTimestamps

_MODEL_CURRENT_TIMESTAMP = """
SELECT {{current_timestamp()}} as current_timestamp,
{{current_timestamp_in_utc_backcompat()}} as current_timestamp_in_utc_backcompat,
{{current_timestamp_backcompat()}} as current_timestamp_backcompat
"""


class TestCurrentTimestampSnowflake(BaseCurrentTimestamps):
@pytest.fixture(scope="class")
def models(self):
return {"get_current_timestamp.sql": _MODEL_CURRENT_TIMESTAMP}

@pytest.fixture(scope="class")
def expected_schema(self):
return {
"CURRENT_TIMESTAMP": "TIMESTAMP_TZ",
"CURRENT_TIMESTAMP_IN_UTC_BACKCOMPAT": "TIMESTAMP_NTZ",
"CURRENT_TIMESTAMP_BACKCOMPAT": "TIMESTAMP_NTZ",
}

@pytest.fixture(scope="class")
def expected_sql(self):
return """
select convert_timezone('UTC', current_timestamp()) as current_timestamp,
convert_timezone('UTC', current_timestamp::TIMESTAMP)::TIMESTAMP as current_timestamp_in_utc_backcompat,
current_timestamp::TIMESTAMP as current_timestamp_backcompat
"""

0 comments on commit 2ef59aa

Please sign in to comment.