Skip to content

Commit 798b1f4

Browse files
authored
Merge pull request #10 from codeforkjeff/dbt-0-20-compat
Work with dbt 0.20.x
2 parents 0b412be + abf85bf commit 798b1f4

File tree

5 files changed

+58
-35
lines changed

5 files changed

+58
-35
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ and joins.
3333

3434
Use the right version:
3535

36+
- versions 0.2.x of this adapter work with dbt 0.20.x
3637
- versions 0.1.x of this adapter work with dbt 0.19.x
3738
- versions 0.0.x of this adapter work with dbt 0.18.x
3839

@@ -177,12 +178,12 @@ On Windows, you'll need to make adjustments to the commands below.
177178
```
178179
workon dbt-sqlite-test
179180
180-
pip install dbt==0.19.2
181+
pip install dbt==0.20.0
181182
182183
# install adapter test suite
183-
# version 0.5.0 doesn't work with dbt 0.19.x
184+
# NOTE: dbt 0.19.x doesn't work with >= 0.5.0; use 0.4.0
184185
# see https://github.com/dbt-labs/dbt-adapter-tests/issues/20
185-
pip install pytest-dbt-adapter==0.4.0
186+
pip install pytest-dbt-adapter==0.5.1
186187
187188
# install dbt-sqlite in development mode
188189
pip install -e .

dbt/adapters/sqlite/impl.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,6 @@ def rename_relation(self, from_relation, to_relation):
6565
f"I don't know how to rename this type of relation: {from_relation.type}," +
6666
f" from: {from_relation}, to: {to_relation}")
6767

68-
def list_schemas(self, database: str) -> List[str]:
69-
"""
70-
Schemas in SQLite are attached databases
71-
"""
72-
results = self.connections.execute("PRAGMA database_list", fetch=True)
73-
74-
schemas = [row[1] for row in results[1]]
75-
76-
return schemas
77-
78-
def check_schema_exists(self, database: str, schema: str) -> bool:
79-
return schema in self.list_schemas(database)
80-
8168
def get_columns_in_relation(self, relation):
8269
_, results = self.connections.execute(f"pragma {relation.schema}.table_info({relation.identifier})", fetch=True)
8370

dbt/include/sqlite/macros/adapters.sql

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
{% macro sqlite__list_schemas(database) %}
2-
{# no-op #}
3-
{# see SQLiteAdapter.list_schemas() #}
2+
{% call statement('list_schemas', fetch_result=True) %}
3+
pragma database_list
4+
{% endcall %}
5+
{% set results = load_result('list_schemas').table %}
6+
{{ return(results.select(['name']).rename(column_names = {'name': 'schema'})) }}
47
{% endmacro %}
58

69
{% macro sqlite__create_schema(relation, auto_begin=False) %}
@@ -34,25 +37,45 @@
3437
{%- endcall %}
3538
{% endmacro %}
3639

37-
{% macro sqlite__check_schema_exists(database, schema) -%}
38-
{# no-op #}
39-
{# see SQLiteAdapter.check_schema_exists() #}
40+
{% macro sqlite__check_schema_exists(information_schema, schema) -%}
41+
{% if schema in list_schemas(database).columns[0].values() %}
42+
{% call statement('check_schema_exists', fetch_result=True) %}
43+
SELECT 1 as schema_exist
44+
{% endcall %}
45+
{{ return(load_result('check_schema_exists').table) }}
46+
{% else %}
47+
{% call statement('check_schema_exists', fetch_result=True) %}
48+
SELECT 0 as schema_exist
49+
{% endcall %}
50+
{{ return(load_result('check_schema_exists').table) }}
51+
{% endif %}
4052
{% endmacro %}
4153

4254
{% macro sqlite__list_relations_without_caching(schema_relation) %}
43-
{% call statement('list_relations_without_caching', fetch_result=True) %}
44-
SELECT
45-
'{{ schema_relation.database }}' as database
46-
,name
47-
,'{{ schema_relation.schema }}' AS schema
48-
,type as data_type
49-
FROM
50-
{{ schema_relation.schema }}.sqlite_master
51-
WHERE
52-
name NOT LIKE 'sqlite_%'
53-
{% endcall %}
5455

55-
{{ return(load_result('list_relations_without_caching').table) }}
56+
{% set schemas = list_schemas(schema_relation.database).columns[0].values() %}
57+
58+
{% if schema_relation.schema in schemas %}
59+
{% call statement('list_relations_without_caching', fetch_result=True) %}
60+
SELECT
61+
'{{ schema_relation.database }}' as database
62+
,name
63+
,'{{ schema_relation.schema }}' AS schema
64+
,type as data_type
65+
FROM
66+
{{ schema_relation.schema }}.sqlite_master
67+
WHERE
68+
name NOT LIKE 'sqlite_%'
69+
{% endcall %}
70+
71+
{{ return(load_result('list_relations_without_caching').table) }}
72+
{% else %}
73+
{% call statement('empty_table', fetch_result=True) %}
74+
SELECT null as database, null as name, null as schema, null as data_type WHERE 1=0
75+
{% endcall %}
76+
77+
{{ return(load_result('empty_table').table) }}
78+
{% endif %}
5679
{% endmacro %}
5780

5881
{% macro sqlite__create_table_as(temporary, relation, sql) -%}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{% macro sqlite__get_test_sql(main_sql, fail_calc, warn_if, error_if, limit) -%}
2+
select
3+
{{ fail_calc }} as failures,
4+
case when {{ fail_calc }} {{ warn_if }}
5+
then 'true' else 'false' end as should_warn,
6+
case when {{ fail_calc }} {{ error_if }}
7+
then 'true' else 'false' end as should_error
8+
from (
9+
{{ main_sql }}
10+
{{ "limit " ~ limit if limit != none }}
11+
) dbt_internal_test
12+
{%- endmacro %}

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from setuptools import setup
44

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

@@ -29,7 +29,7 @@
2929
]
3030
},
3131
install_requires=[
32-
"dbt-core~=0.19.0",
32+
"dbt-core~=0.20.0",
3333
],
3434
classifiers=[
3535
'Development Status :: 3 - Alpha',

0 commit comments

Comments
 (0)