Skip to content

Commit 85eeb3f

Browse files
committed
Added ensure_path option in sqlite driver
1 parent 47ebf40 commit 85eeb3f

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

docs/drivers.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ Additional parameters are available when connecting:
1919
- *pragma*: a dict of sqlite pragmas to set on connection
2020
- *ext*: a list of extension modules to load on connection
2121
- *fine_tune*: a boolean indicating to apply fine tuned pragmas for high concurrent workloads like web servers ([explanations](https://fractaledmind.github.io/2023/09/07/enhancing-rails-sqlite-fine-tuning/))
22+
- *foreign_keys*: boolean indicating if foreign keys should be enforced (sqlite default is False)
23+
- *ensure_path*: boolean indicating if the driver should ensure that the database file's directory exists
2224

2325
### Postgresql
2426

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "sqlorm-py"
3-
version = "0.4.0"
3+
version = "0.4.1"
44
description = "A new kind or ORM that do not abstract away your database or SQL queries."
55
authors = [
66
{"name" = "Maxime Bouroumeau-Fuseau", email = "[email protected]"}

src/sqlorm/drivers/sqlite.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,19 @@
1717
PRIMARY_KEY_SCHEMA_DEFINITION = "PRIMARY KEY AUTOINCREMENT"
1818

1919

20-
def connect(*args, **kwargs):
20+
def connect(filename, *args, **kwargs):
2121
pragmas = kwargs.pop("pragma", {})
2222
extensions = kwargs.pop("ext", None)
2323
fine_tune = kwargs.pop("fine_tune", False)
2424
foreign_keys = kwargs.pop("foreign_keys", False)
25+
ensure_path = kwargs.pop("ensure_path", False)
26+
27+
if ensure_path and filename != ":memory:":
28+
import os
29+
os.makedirs(os.path.dirname(filename), exist_ok=True)
2530

2631
kwargs.setdefault("check_same_thread", False) # better default to work with sqlorm pooling
27-
conn = sqlite3.connect(*args, **kwargs)
32+
conn = sqlite3.connect(filename, *args, **kwargs)
2833
conn.row_factory = sqlite3.Row
2934

3035
if foreign_keys:

0 commit comments

Comments
 (0)