Skip to content

Commit cfa3a66

Browse files
committed
check for schema existence in list_relations_without_caching()
needed to move code from adapter methods into jinja macros in order for this to work
1 parent 0b412be commit cfa3a66

File tree

2 files changed

+40
-30
lines changed

2 files changed

+40
-30
lines changed

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) -%}

0 commit comments

Comments
 (0)