Skip to content

Commit 0216134

Browse files
authored
Merge pull request #8 from edgarrmondragon/object-schema-and-paths
Use YAML objects instead of colon-separated strings
2 parents 126647f + 1e59569 commit 0216134

File tree

4 files changed

+27
-21
lines changed

4 files changed

+27
-21
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ dbt_sqlite:
6767
schema: 'main'
6868

6969
# connect schemas to paths: at least one of these must be 'main'
70-
schemas_and_paths: 'main=/my_project/data/etl.db;dataset=/my_project/data/dataset_v1.db'
70+
schemas_and_paths:
71+
main: '/my_project/data/etl.db'
72+
dataset: '/my_project/data/dataset_v1.db'
7173

7274
# directory where all *.db files are attached as schema, using base filename
7375
# as schema name, and where new schema are created. this can overlap with the dirs of
@@ -76,7 +78,8 @@ dbt_sqlite:
7678

7779
# optional: semi-colon separated list of file paths for SQLite extensions to load.
7880
# crypto.so is needed to provide for snapshots to work; see README
79-
extensions: "/path/to/sqlean/crypto.so"
81+
extensions:
82+
- "/path/to/sqlean/crypto.so"
8083

8184
```
8285

dbt/adapters/sqlite/connections.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11

22
from contextlib import contextmanager
3-
from dataclasses import dataclass
3+
from dataclasses import dataclass, field
44
import glob
55
import os.path
66
import sqlite3
7-
from typing import Optional, Tuple, Any
7+
from typing import Optional, Tuple, Any, Dict, List
88

99

1010
from dbt.adapters.base import Credentials
@@ -23,9 +23,9 @@
2323
class SQLiteCredentials(Credentials):
2424
""" Required connections for a SQLite connection"""
2525

26-
schemas_and_paths: str
26+
schemas_and_paths: Dict[str, str]
2727
schema_directory: str
28-
extensions: Optional[str] = None
28+
extensions: List[str] = field(default_factory=list)
2929

3030
@property
3131
def type(self):
@@ -40,17 +40,16 @@ class SQLiteConnectionManager(SQLConnectionManager):
4040
TYPE = "sqlite"
4141

4242
@classmethod
43-
def open(cls, connection):
43+
def open(cls, connection: Connection):
4444
if connection.state == "open":
4545
logger.debug("Connection is already open, skipping open.")
4646
return connection
4747

48-
credentials = connection.credentials
48+
credentials: SQLiteCredentials = connection.credentials
4949

5050
schemas_and_paths = {}
51-
for path_entry in credentials.schemas_and_paths.split(";"):
52-
schema, path = path_entry.split("=", 1)
53-
# store abs path so we can tell if we've attached the file already
51+
for schema, path in credentials.schemas_and_paths.items():
52+
# Make .db file path absolute
5453
schemas_and_paths[schema] = os.path.abspath(path)
5554

5655
try:
@@ -59,12 +58,10 @@ def open(cls, connection):
5958
else:
6059
raise FailedToConnectException("at least one schema must be called 'main'")
6160

62-
extensions = [e for e in (connection.credentials.extensions or "").split(";") if e]
63-
64-
if len(extensions) > 0:
61+
if len(credentials.extensions) > 0:
6562
handle.enable_load_extension(True)
6663

67-
for ext_path in extensions:
64+
for ext_path in credentials.extensions:
6865
handle.load_extension(ext_path)
6966

7067
cursor = handle.cursor()

dbt/include/sqlite/sample_profiles.yml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,21 @@ default:
66
threads: 1
77
database: <database name>
88
schema: 'main'
9-
schemas_and_paths: 'main=/my_project/data/etl.db'
9+
schemas_and_paths:
10+
main: '/my_project/data/etl.db'
1011
schema_directory: '/my_project/data'
11-
extensions: '/path/to/sqlite-digest/digest.so'
12+
extensions:
13+
- '/path/to/sqlite-digest/digest.so'
1214

1315
prod:
1416
type: sqlite
1517
threads: 1
1618
database: <database name>
1719
schema: 'main'
18-
schemas_and_paths: 'main=/my_project/data/etl.db'
20+
schemas_and_paths:
21+
main: '/my_project/data/etl.db'
1922
schema_directory: '/my_project/data'
20-
extensions: '/path/to/sqlite-digest/digest.so'
23+
extensions:
24+
- '/path/to/sqlite-digest/digest.so'
2125

2226
target: dev

test/sqlite.dbtspec

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ target:
33
type: sqlite
44
database: adapter_test
55
schema: 'main'
6-
schemas_and_paths: "main=/tmp/dbt-sqlite-tests/adapter_test.db"
6+
schemas_and_paths:
7+
main: '/tmp/dbt-sqlite-tests/adapter_test.db'
78
schema_directory: '/tmp/dbt-sqlite-tests'
8-
extensions: "/home/jeff/git/sqlite-digest/digest.so"
9+
extensions:
10+
- "/home/spoton/edgar/sqlite-digest/digest.so"
911
threads: 1
1012
sequences:
1113
test_dbt_empty: empty

0 commit comments

Comments
 (0)