Skip to content

Commit

Permalink
Merge pull request #10 from codeforkjeff/dbt-0-20-compat
Browse files Browse the repository at this point in the history
Work with dbt 0.20.x
  • Loading branch information
codeforkjeff authored Aug 10, 2021
2 parents 0b412be + abf85bf commit 798b1f4
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 35 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ and joins.

Use the right version:

- versions 0.2.x of this adapter work with dbt 0.20.x
- versions 0.1.x of this adapter work with dbt 0.19.x
- versions 0.0.x of this adapter work with dbt 0.18.x

Expand Down Expand Up @@ -177,12 +178,12 @@ On Windows, you'll need to make adjustments to the commands below.
```
workon dbt-sqlite-test
pip install dbt==0.19.2
pip install dbt==0.20.0
# install adapter test suite
# version 0.5.0 doesn't work with dbt 0.19.x
# NOTE: dbt 0.19.x doesn't work with >= 0.5.0; use 0.4.0
# see https://github.com/dbt-labs/dbt-adapter-tests/issues/20
pip install pytest-dbt-adapter==0.4.0
pip install pytest-dbt-adapter==0.5.1
# install dbt-sqlite in development mode
pip install -e .
Expand Down
13 changes: 0 additions & 13 deletions dbt/adapters/sqlite/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,6 @@ def rename_relation(self, from_relation, to_relation):
f"I don't know how to rename this type of relation: {from_relation.type}," +
f" from: {from_relation}, to: {to_relation}")

def list_schemas(self, database: str) -> List[str]:
"""
Schemas in SQLite are attached databases
"""
results = self.connections.execute("PRAGMA database_list", fetch=True)

schemas = [row[1] for row in results[1]]

return schemas

def check_schema_exists(self, database: str, schema: str) -> bool:
return schema in self.list_schemas(database)

def get_columns_in_relation(self, relation):
_, results = self.connections.execute(f"pragma {relation.schema}.table_info({relation.identifier})", fetch=True)

Expand Down
57 changes: 40 additions & 17 deletions dbt/include/sqlite/macros/adapters.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{% macro sqlite__list_schemas(database) %}
{# no-op #}
{# see SQLiteAdapter.list_schemas() #}
{% call statement('list_schemas', fetch_result=True) %}
pragma database_list
{% endcall %}
{% set results = load_result('list_schemas').table %}
{{ return(results.select(['name']).rename(column_names = {'name': 'schema'})) }}
{% endmacro %}

{% macro sqlite__create_schema(relation, auto_begin=False) %}
Expand Down Expand Up @@ -34,25 +37,45 @@
{%- endcall %}
{% endmacro %}

{% macro sqlite__check_schema_exists(database, schema) -%}
{# no-op #}
{# see SQLiteAdapter.check_schema_exists() #}
{% macro sqlite__check_schema_exists(information_schema, schema) -%}
{% if schema in list_schemas(database).columns[0].values() %}
{% call statement('check_schema_exists', fetch_result=True) %}
SELECT 1 as schema_exist
{% endcall %}
{{ return(load_result('check_schema_exists').table) }}
{% else %}
{% call statement('check_schema_exists', fetch_result=True) %}
SELECT 0 as schema_exist
{% endcall %}
{{ return(load_result('check_schema_exists').table) }}
{% endif %}
{% endmacro %}

{% macro sqlite__list_relations_without_caching(schema_relation) %}
{% call statement('list_relations_without_caching', fetch_result=True) %}
SELECT
'{{ schema_relation.database }}' as database
,name
,'{{ schema_relation.schema }}' AS schema
,type as data_type
FROM
{{ schema_relation.schema }}.sqlite_master
WHERE
name NOT LIKE 'sqlite_%'
{% endcall %}

{{ return(load_result('list_relations_without_caching').table) }}
{% set schemas = list_schemas(schema_relation.database).columns[0].values() %}

{% if schema_relation.schema in schemas %}
{% call statement('list_relations_without_caching', fetch_result=True) %}
SELECT
'{{ schema_relation.database }}' as database
,name
,'{{ schema_relation.schema }}' AS schema
,type as data_type
FROM
{{ schema_relation.schema }}.sqlite_master
WHERE
name NOT LIKE 'sqlite_%'
{% endcall %}

{{ return(load_result('list_relations_without_caching').table) }}
{% else %}
{% call statement('empty_table', fetch_result=True) %}
SELECT null as database, null as name, null as schema, null as data_type WHERE 1=0
{% endcall %}

{{ return(load_result('empty_table').table) }}
{% endif %}
{% endmacro %}

{% macro sqlite__create_table_as(temporary, relation, sql) -%}
Expand Down
12 changes: 12 additions & 0 deletions dbt/include/sqlite/macros/materializations/test.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{% macro sqlite__get_test_sql(main_sql, fail_calc, warn_if, error_if, limit) -%}
select
{{ fail_calc }} as failures,
case when {{ fail_calc }} {{ warn_if }}
then 'true' else 'false' end as should_warn,
case when {{ fail_calc }} {{ error_if }}
then 'true' else 'false' end as should_error
from (
{{ main_sql }}
{{ "limit " ~ limit if limit != none }}
) dbt_internal_test
{%- endmacro %}
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from setuptools import setup

package_name = "dbt-sqlite"
package_version = "0.1.2"
package_version = "0.2.0"
description = """A SQLite adapter plugin for dbt (data build tool)"""
long_description = "Please see the github repository for detailed information"

Expand All @@ -29,7 +29,7 @@
]
},
install_requires=[
"dbt-core~=0.19.0",
"dbt-core~=0.20.0",
],
classifiers=[
'Development Status :: 3 - Alpha',
Expand Down

0 comments on commit 798b1f4

Please sign in to comment.