48 lines
1.4 KiB
Python
48 lines
1.4 KiB
Python
from __future__ import annotations
|
|
|
|
import sqlite3
|
|
from pathlib import Path
|
|
|
|
SCHEMA = """
|
|
CREATE TABLE IF NOT EXISTS audio_tracks (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
file_path TEXT UNIQUE NOT NULL,
|
|
file_size INTEGER NOT NULL,
|
|
fast_hash TEXT NOT NULL,
|
|
sha256 TEXT,
|
|
duration REAL,
|
|
artist TEXT,
|
|
title TEXT,
|
|
album TEXT,
|
|
format TEXT,
|
|
bitrate INTEGER,
|
|
sample_rate INTEGER,
|
|
bit_depth INTEGER,
|
|
chromaprint TEXT,
|
|
score INTEGER,
|
|
status TEXT NOT NULL DEFAULT 'active',
|
|
inode INTEGER,
|
|
device INTEGER,
|
|
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
trashed_at TEXT,
|
|
review_status TEXT
|
|
);
|
|
CREATE INDEX IF NOT EXISTS idx_fast_hash ON audio_tracks(fast_hash);
|
|
CREATE INDEX IF NOT EXISTS idx_sha256 ON audio_tracks(sha256);
|
|
CREATE INDEX IF NOT EXISTS idx_duration ON audio_tracks(duration);
|
|
CREATE INDEX IF NOT EXISTS idx_status ON audio_tracks(status);
|
|
CREATE INDEX IF NOT EXISTS idx_duration_size ON audio_tracks(duration, file_size);
|
|
"""
|
|
|
|
|
|
def connect(path: Path) -> sqlite3.Connection:
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
conn = sqlite3.connect(path)
|
|
conn.row_factory = sqlite3.Row
|
|
conn.execute("PRAGMA journal_mode=WAL")
|
|
conn.execute("PRAGMA foreign_keys=ON")
|
|
conn.execute("PRAGMA busy_timeout=5000")
|
|
conn.executescript(SCHEMA)
|
|
return conn
|