-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dbt): supports snapshot, seed, test and docs (#8)
* refactor file structure * support snapshot * update docs * optimize the code * add impl for seed * impl for dbt docs * support for dbt test
- Loading branch information
Showing
17 changed files
with
565 additions
and
57 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,54 @@ | ||
{% macro oceanbase_mysql__get_catalog(information_schema, schemas) -%} | ||
{%- call statement('catalog', fetch_result=True) -%} | ||
select | ||
columns.table_database, | ||
columns.table_schema, | ||
columns.table_name, | ||
tables.table_type, | ||
columns.table_comment, | ||
tables.table_owner, | ||
columns.column_name, | ||
columns.column_index, | ||
columns.column_type, | ||
columns.column_comment | ||
from | ||
( | ||
select | ||
table_schema as "table_database", | ||
table_schema as "table_schema", | ||
table_name as "table_name", | ||
case when table_type = 'BASE TABLE' then 'table' | ||
when table_type = 'VIEW' then 'view' | ||
else table_type | ||
end as "table_type", | ||
null as "table_owner" | ||
|
||
from information_schema.tables | ||
) | ||
as tables | ||
join | ||
( | ||
select | ||
table_schema as "table_database", | ||
table_schema as "table_schema", | ||
table_name as "table_name", | ||
null as "table_comment", | ||
|
||
column_name as "column_name", | ||
ordinal_position as "column_index", | ||
data_type as "column_type", | ||
null as "column_comment" | ||
|
||
from information_schema.columns | ||
) | ||
as columns using (table_schema, table_name) | ||
where table_schema not in ('INFORMATION_SCHEMA', 'METRICS_SCHEMA', 'PERFORMANCE_SCHEMA', 'mysql') | ||
and ( | ||
{%- for schema in schemas -%} | ||
upper(table_schema) = upper('{{ schema }}'){%- if not loop.last %} or {% endif -%} | ||
{%- endfor -%} | ||
) | ||
order by column_index | ||
{%- endcall -%} | ||
{{ return(load_result('catalog').table) }} | ||
{%- 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{% macro oceanbase_mysql__post_snapshot(staging_relation) %} | ||
{# OceanBase MySQL does not support temp table so we should drop the table manually #} | ||
{{ drop_relation_if_exists(staging_relation) }} | ||
{% endmacro %} |
21 changes: 21 additions & 0 deletions
21
dbt/include/oceanbase_mysql/macros/snapshot/snapshot_merge.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,21 @@ | ||
{% macro oceanbase_mysql__snapshot_merge_sql(target, source, insert_cols) -%} | ||
{%- set insert_cols_csv = insert_cols | join(', ') -%} | ||
|
||
update | ||
{{ target }} as dbt_internal_target, | ||
{{ source }} as dbt_internal_source | ||
set | ||
dbt_internal_target.dbt_valid_to = dbt_internal_source.dbt_valid_to | ||
where | ||
dbt_internal_source.dbt_scd_id = dbt_internal_target.dbt_scd_id | ||
and dbt_internal_target.dbt_valid_to is null | ||
and dbt_internal_source.dbt_change_type in ('update', 'delete'); | ||
|
||
insert into {{ target }} ({{ insert_cols_csv }}) | ||
select {% for column in insert_cols -%} | ||
dbt_internal_source.{{ column }} {%- if not loop.last %}, {%- endif %} | ||
{%- endfor %} | ||
from {{ source }} as dbt_internal_source | ||
where dbt_internal_source.dbt_change_type = 'insert'; | ||
|
||
{% 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{% macro oceanbase_mysql__snapshot_hash_arguments(args) -%} | ||
md5({%- for arg in args -%} | ||
coalesce(cast({{ arg }} as char ), '') | ||
{% if not loop.last %} || '|' || {% endif %} | ||
{%- endfor -%}) | ||
{%- 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{% macro oceanbase_mysql__get_test_sql(main_sql, fail_calc, warn_if, error_if, limit) -%} | ||
select | ||
{{ fail_calc }} as failures, | ||
case | ||
when {{ fail_calc }} {{ warn_if | replace("!=","<>") }} then 'true' | ||
else 'false' | ||
end as should_warn, | ||
case | ||
when {{ fail_calc }} {{ error_if | replace("!=","<>") }} then 'true' | ||
else 'false' | ||
end as should_error | ||
from ( | ||
{{ main_sql }} | ||
{{ "limit " ~ limit if limit != none }} | ||
) dbt_internal_test | ||
{%- 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{% macro oceanbase_mysql__current_timestamp() -%} | ||
now() | ||
{%- 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
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 @@ | ||
# Copyright (c) 2023 OceanBase. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
import pytest | ||
|
||
from dbt.adapters.base import BaseAdapter | ||
from dbt.tests.adapter.column_types.test_column_types import run_dbt | ||
from tests.functional.utils import BaseOBMySQLTestCase | ||
|
||
_COUNTRIES_CSV = """iso3,name,iso2,iso_numeric,cow_alpha,cow_numeric,fao_code,un_code,wb_code,imf_code,fips,geonames_name,geonames_id,r_name,aiddata_name,aiddata_code,oecd_name,oecd_code,historical_name,historical_iso3,historical_iso2,historical_iso_numeric | ||
ABW,Aruba,AW,533,,,,533,ABW,314,AA,Aruba,3577279,ARUBA,Aruba,12,Aruba,373,,,, | ||
AFG,Afghanistan,AF,4,AFG,700,2,4,AFG,512,AF,Afghanistan,1149361,AFGHANISTAN,Afghanistan,1,Afghanistan,625,,,, | ||
AGO,Angola,AO,24,ANG,540,7,24,AGO,614,AO,Angola,3351879,ANGOLA,Angola,7,Angola,225,,,, | ||
AIA,Anguilla,AI,660,,,,660,AIA,312,AV,Anguilla,3573511,ANGUILLA,Anguilla,8,Anguilla,376,,,, | ||
ALA,Aland Islands,AX,248,,,,248,ALA,,,Aland Islands,661882,ALAND ISLANDS,,,,,,,, | ||
ALB,Albania,AL,8,ALB,339,3,8,ALB,914,AL,Albania,783754,ALBANIA,Albania,3,Albania,71,,,, | ||
AND,Andorra,AD,20,AND,232,6,20,ADO,,AN,Andorra,3041565,ANDORRA,,,,,,,, | ||
ANT,Netherlands Antilles,AN,530,,,,,ANT,353,NT,Netherlands Antilles,,NETHERLANDS ANTILLES,Netherlands Antilles,211,Netherlands Antilles,361,Netherlands Antilles,ANT,AN,530 | ||
ARE,United Arab Emirates,AE,784,UAE,696,225,784,ARE,466,AE,United Arab Emirates,290557,UNITED ARAB EMIRATES,United Arab Emirates,140,United Arab Emirates,576,,,, | ||
""" | ||
|
||
|
||
class TestSeed(BaseOBMySQLTestCase): | ||
|
||
@pytest.fixture(scope="class") | ||
def seeds(self): | ||
return {"countries.csv": _COUNTRIES_CSV} | ||
|
||
def test_seed_succeed(self, project): | ||
run_dbt(args=["seed"]) | ||
adapter: BaseAdapter = project.adapter | ||
with adapter.connection_named("test"): | ||
_, table = adapter.execute("select * from countries", fetch=True) | ||
assert len(table) == 9 |
Oops, something went wrong.