diff --git a/README.md b/README.md index a8f4bc0..dd4e6c5 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 . diff --git a/dbt/adapters/sqlite/impl.py b/dbt/adapters/sqlite/impl.py index 1489de6..214b242 100644 --- a/dbt/adapters/sqlite/impl.py +++ b/dbt/adapters/sqlite/impl.py @@ -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) diff --git a/dbt/include/sqlite/macros/adapters.sql b/dbt/include/sqlite/macros/adapters.sql index 324735b..5f7aced 100644 --- a/dbt/include/sqlite/macros/adapters.sql +++ b/dbt/include/sqlite/macros/adapters.sql @@ -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) %} @@ -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) -%} diff --git a/dbt/include/sqlite/macros/materializations/test.sql b/dbt/include/sqlite/macros/materializations/test.sql new file mode 100644 index 0000000..5e9645b --- /dev/null +++ b/dbt/include/sqlite/macros/materializations/test.sql @@ -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 %} diff --git a/setup.py b/setup.py index 23537e1..e12ed75 100644 --- a/setup.py +++ b/setup.py @@ -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" @@ -29,7 +29,7 @@ ] }, install_requires=[ - "dbt-core~=0.19.0", + "dbt-core~=0.20.0", ], classifiers=[ 'Development Status :: 3 - Alpha',