readme+bugfixes
This commit is contained in:
parent
b3ac11a85e
commit
ce7f9ac236
4 changed files with 74 additions and 14 deletions
|
|
@ -1,8 +1,22 @@
|
|||
import sqlite3
|
||||
import threading
|
||||
from pathlib import Path
|
||||
|
||||
_init_lock = threading.Lock()
|
||||
_initialized_dbs: set[Path] = set()
|
||||
|
||||
|
||||
def init_db(db_path: Path) -> None:
|
||||
if db_path in _initialized_dbs:
|
||||
return
|
||||
with _init_lock:
|
||||
if db_path in _initialized_dbs:
|
||||
return
|
||||
_init_db_locked(db_path)
|
||||
_initialized_dbs.add(db_path)
|
||||
|
||||
|
||||
def _init_db_locked(db_path: Path) -> None:
|
||||
db_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
conn = sqlite3.connect(db_path)
|
||||
conn.row_factory = sqlite3.Row
|
||||
|
|
|
|||
|
|
@ -24,10 +24,12 @@ def _row_to_segment(row: sqlite3.Row) -> Segment:
|
|||
|
||||
def create_segment(db_path: Path, request: SegmentCreateRequest) -> Segment:
|
||||
conn = get_connection(db_path)
|
||||
conn.isolation_level = None # manual transaction control
|
||||
try:
|
||||
conn.execute("BEGIN IMMEDIATE")
|
||||
row = conn.execute(
|
||||
"SELECT COALESCE(MAX(sort_order) + 1, 0) AS next_order"
|
||||
" FROM segments WHERE page_id = ?",
|
||||
" FROM segments WHERE page_id IS ?",
|
||||
(request.page_id,),
|
||||
).fetchone()
|
||||
sort_order: int = row["next_order"] if row else 0
|
||||
|
|
@ -51,7 +53,7 @@ def create_segment(db_path: Path, request: SegmentCreateRequest) -> Segment:
|
|||
now,
|
||||
),
|
||||
)
|
||||
conn.commit()
|
||||
conn.execute("COMMIT")
|
||||
|
||||
return Segment(
|
||||
id=segment_id,
|
||||
|
|
@ -64,6 +66,9 @@ def create_segment(db_path: Path, request: SegmentCreateRequest) -> Segment:
|
|||
created_at=datetime.fromisoformat(now),
|
||||
updated_at=datetime.fromisoformat(now),
|
||||
)
|
||||
except Exception:
|
||||
conn.execute("ROLLBACK")
|
||||
raise
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue