From 5629cabbb38c7d91e66a419ba7cb3a93a9a988a1 Mon Sep 17 00:00:00 2001 From: batsumaru <> Date: Tue, 15 Sep 2026 14:25:49 +0900 Subject: Added long-term memory --- database/__init__.py | 0 database/database.py | 73 +++++++++++++++++++++++++++++ database/migrations/001_create_memories.sql | 6 +++ 3 files changed, 79 insertions(+) create mode 100644 database/__init__.py create mode 100644 database/database.py create mode 100644 database/migrations/001_create_memories.sql (limited to 'database') diff --git a/database/__init__.py b/database/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/database/database.py b/database/database.py new file mode 100644 index 0000000..7119c7e --- /dev/null +++ b/database/database.py @@ -0,0 +1,73 @@ +import sqlite3 +from datetime import UTC, datetime +from pathlib import Path + + +class Database: + def __init__(self, path: Path) -> None: + self.path = path + self.migrations_dir = Path(__file__).parent / "migrations" + + def connect(self) -> sqlite3.Connection: + return sqlite3.connect(self.path) + + def migrate(self) -> None: + with self.connect() as connection: + connection.execute( + """ + CREATE TABLE IF NOT EXISTS schema_migrations ( + version INTEGER PRIMARY KEY, + applied_at TEXT NOT NULL + ) + """ + ) + + applied_versions = { + row[0] + for row in connection.execute("SELECT version FROM schema_migrations") + } + + for migration_version, path in self._get_migrations(): + if migration_version in applied_versions: + continue + + sql = path.read_text(encoding="utf-8") + + connection.execute("BEGIN") + + try: + connection.execute(sql) + + applied_at = datetime.now(UTC).isoformat().replace("+00:00", "Z") + + connection.execute( + """ + INSERT INTO schema_migrations (version, applied_at) + VALUES (?, ?) + """, + (migration_version, applied_at), + ) + + connection.commit() + except Exception: + connection.rollback() + raise + + def _get_migrations(self) -> list[tuple[int, Path]]: + migrations = [] + + for path in self.migrations_dir.glob("*.sql"): + version_text, _, _ = path.stem.partition("_") + + if not version_text.isdigit(): + continue + + migrations.append((int(version_text), path)) + + migrations.sort() + + versions = [version for version, _ in migrations] + if len(versions) != len(set(versions)): + raise ValueError("Duplicate migration version") + + return migrations diff --git a/database/migrations/001_create_memories.sql b/database/migrations/001_create_memories.sql new file mode 100644 index 0000000..e79c907 --- /dev/null +++ b/database/migrations/001_create_memories.sql @@ -0,0 +1,6 @@ +CREATE TABLE IF NOT EXISTS memories ( + id TEXT PRIMARY KEY, + content TEXT NOT NULL, + category TEXT NOT NULL, + created_at TEXT NOT NULL +); -- cgit v1.3