-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Consolidate date macros into dates.sql * rename to timestamps.sql * fix whitespace + add changie * cleanup macros and add testing * fix whitespace * remove now macro * fix functional test * remove local config * make snowflake backwards compat return utc * move timestamps to adaptor base tests * move backcompat macros to respective adapters * change timestamp param to source_timestamp * move timestamps.py to utils * update changie.yaml * make expected schema a fixture * formatting * add debug message to assert * fix changie.yaml * Update tests/adapter/dbt/tests/adapter/utils/test_timestamps.py Co-authored-by: Doug Beatty <[email protected]> * Update plugins/postgres/dbt/include/postgres/macros/timestamps.sql Co-authored-by: Doug Beatty <[email protected]> * Update .changie.yaml * add backcompat utc * remove current_timestamp_in_utc * remove convert_timezone * add _in_utc_backcompat * fix macro_calls typo * add expected sql validation to test_timestamps * make expected_sql optional * improve sql check string comparison test * remove extraneous test file * add timestamp casting back * Update plugins/postgres/dbt/include/postgres/macros/adapters.sql Co-authored-by: Doug Beatty <[email protected]> * add check_relation_has_expected_schema to comments * fix whitespace * remove default impl of current_timestamp Co-authored-by: Doug Beatty <[email protected]> (cherry picked from commit a79960f) Co-authored-by: colin-rogers-dbt <[email protected]> Co-authored-by: leahwicz <[email protected]>
- Loading branch information
1 parent
2584465
commit 6042469
Showing
11 changed files
with
144 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
kind: Features | ||
body: Add standard timestamps.sql | ||
time: 2022-09-14T09:56:25.97818-07:00 | ||
custom: | ||
Author: colin-rogers-dbt | ||
Issue: "5521" | ||
PR: "5838" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 0 additions & 10 deletions
10
core/dbt/include/global_project/macros/adapters/freshness.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
core/dbt/include/global_project/macros/adapters/timestamps.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
{%- macro current_timestamp() -%} | ||
{{ adapter.dispatch('current_timestamp', 'dbt')() }} | ||
{%- endmacro -%} | ||
|
||
{% macro default__current_timestamp() -%} | ||
{{ exceptions.raise_not_implemented( | ||
'current_timestamp macro not implemented for adapter ' + adapter.type()) }} | ||
{%- endmacro %} | ||
|
||
{%- macro snapshot_get_time() -%} | ||
{{ adapter.dispatch('snapshot_get_time', 'dbt')() }} | ||
{%- endmacro -%} | ||
|
||
{% macro default__snapshot_get_time() %} | ||
{{ current_timestamp() }} | ||
{% endmacro %} | ||
|
||
--------------------------------------------- | ||
|
||
/* {# | ||
DEPRECATED: DO NOT USE IN NEW PROJECTS | ||
This is ONLY to handle the fact that Snowflake + Postgres had functionally | ||
different implementations of {{ dbt.current_timestamp }} + {{ dbt_utils.current_timestamp }} | ||
If you had a project or package that called {{ dbt_utils.current_timestamp() }}, you should | ||
continue to use this macro to guarantee identical behavior on those two databases. | ||
#} */ | ||
|
||
{% macro current_timestamp_backcompat() %} | ||
{{ return(adapter.dispatch('current_timestamp_backcompat', 'dbt')()) }} | ||
{% endmacro %} | ||
|
||
{% macro default__current_timestamp_backcompat() %} | ||
current_timestamp::timestamp | ||
{% endmacro %} | ||
|
||
{% macro current_timestamp_in_utc_backcompat() %} | ||
{{ return(adapter.dispatch('current_timestamp_in_utc_backcompat', 'dbt')()) }} | ||
{% endmacro %} | ||
|
||
{% macro default__current_timestamp_in_utc_backcompat() %} | ||
{{ return(adapter.dispatch('current_timestamp_backcompat', 'dbt')()) }} | ||
{% endmacro %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
plugins/postgres/dbt/include/postgres/macros/timestamps.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{% macro postgres__current_timestamp() -%} | ||
now() | ||
{%- endmacro %} | ||
|
||
{% macro postgres__snapshot_string_as_time(timestamp) -%} | ||
{%- set result = "'" ~ timestamp ~ "'::timestamp without time zone" -%} | ||
{{ return(result) }} | ||
{%- endmacro %} | ||
|
||
{% macro postgres__snapshot_get_time() -%} | ||
{{ current_timestamp() }}::timestamp without time zone | ||
{%- endmacro %} | ||
|
||
{% macro postgres__current_timestamp_backcompat() %} | ||
current_timestamp::{{ type_timestamp() }} | ||
{% endmacro %} | ||
|
||
{% macro postgres__current_timestamp_in_utc_backcompat() %} | ||
(current_timestamp at time zone 'utc')::{{ type_timestamp() }} | ||
{% endmacro %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import pytest | ||
import re | ||
from dbt.tests.util import check_relation_has_expected_schema, run_dbt | ||
|
||
_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 | ||
""" | ||
|
||
_MODEL_EXPECTED_SQL = """ | ||
select now() as current_timestamp, | ||
(current_timestamp at time zone 'utc')::TIMESTAMP as current_timestamp_in_utc_backcompat, | ||
current_timestamp::TIMESTAMP as current_timestamp_backcompat | ||
""" | ||
|
||
|
||
class BaseCurrentTimestamps: | ||
@pytest.fixture(scope="class") | ||
def models(self): | ||
return {"get_current_timestamp.sql": _MODEL_CURRENT_TIMESTAMP} | ||
|
||
# any adapters that don't want to check can set expected schema to None | ||
@pytest.fixture(scope="class") | ||
def expected_sql(self): | ||
return _MODEL_EXPECTED_SQL | ||
|
||
@pytest.fixture(scope="class") | ||
def expected_schema(self): | ||
return { | ||
"current_timestamp": "timestamp with time zone", | ||
"current_timestamp_in_utc_backcompat": "timestamp without time zone", | ||
"current_timestamp_backcompat": "timestamp without time zone", | ||
} | ||
|
||
def test_current_timestamps(self, project, models, expected_schema, expected_sql): | ||
results = run_dbt(["run"]) | ||
assert len(results) == 1 | ||
check_relation_has_expected_schema( | ||
project.adapter, | ||
relation_name="get_current_timestamp", | ||
expected_schema=expected_schema, | ||
) | ||
|
||
if expected_sql: | ||
generated_sql = results.results[0].node.compiled_code | ||
generated_sql_check = re.sub(r"\s+", "", generated_sql).lower() | ||
expected_sql_check = re.sub(r"\s+", "", expected_sql).lower() | ||
assert ( | ||
expected_sql_check == generated_sql_check | ||
), f"generated sql did not match expected: {generated_sql}" | ||
|
||
|
||
class TestCurrentTimestamps(BaseCurrentTimestamps): | ||
pass |