Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

fix(state): stamp existing sqlite initial schema

cyh978a1f957984
diff --git a/storage/migrations.py b/storage/migrations.py
index 15ba34ca0..6e9aa20a9 100644
--- a/storage/migrations.py
+++ b/storage/migrations.py
@@ -1,5 +1,6 @@
from __future__ import annotations
+import sqlite3
from pathlib import Path
from alembic import command
@@ -8,6 +9,24 @@ from alembic.config import Config
from config import paths
from storage.db import sqlite_url
+INITIAL_REVISION = "20260501_0001"
+INITIAL_TABLES = {
+ "schema_meta",
+ "scopes",
+ "channel_settings",
+ "guild_settings",
+ "guild_policies",
+ "user_settings",
+ "bind_codes",
+ "agent_session_bindings",
+ "active_threads",
+ "active_polls",
+ "processed_messages",
+ "chat_sessions",
+ "session_messages",
+ "discovered_chats",
+}
+
def alembic_dir() -> Path:
return Path(__file__).resolve().parent / "alembic"
@@ -21,4 +40,31 @@ def alembic_config(db_path: Path | None = None) -> Config:
def run_migrations(db_path: Path | None = None, *, revision: str = "head") -> None:
- command.upgrade(alembic_config(db_path), revision)
+ target_db = db_path or paths.get_sqlite_state_path()
+ cfg = alembic_config(target_db)
+ _stamp_existing_initial_schema(target_db, cfg)
+ command.upgrade(cfg, revision)
+
+
+def _stamp_existing_initial_schema(db_path: Path, cfg: Config) -> None:
+ path = db_path.expanduser().resolve()
+ if not path.exists():
+ return
+
+ with sqlite3.connect(path) as conn:
+ tables = {
+ row[0]
+ for row in conn.execute(
+ "select name from sqlite_master where type = 'table'",
+ )
+ }
+ if not tables:
+ return
+ if "alembic_version" in tables:
+ version = conn.execute("select version_num from alembic_version").fetchone()
+ if version is not None:
+ return
+ if not INITIAL_TABLES.issubset(tables):
+ return
+
+ command.stamp(cfg, INITIAL_REVISION)
diff --git a/tests/test_sqlite_state_migration.py b/tests/test_sqlite_state_migration.py
index 267da32a3..5a354dd9e 100644
--- a/tests/test_sqlite_state_migration.py
+++ b/tests/test_sqlite_state_migration.py
@@ -9,6 +9,7 @@ import pytest
from storage.db import SqliteInvalidationProbe, create_sqlite_engine
from storage.importer import ensure_sqlite_state
from storage.migrations import run_migrations
+from storage.models import metadata
def test_run_migrations_creates_initial_schema(tmp_path: Path) -> None:
@@ -41,6 +42,45 @@ def test_initial_migration_is_schema_snapshot() -> None:
assert "metadata.create_all" not in source
+def test_run_migrations_stamps_existing_initial_schema(tmp_path: Path) -> None:
+ db_path = tmp_path / "vibe.sqlite"
+ engine = create_sqlite_engine(db_path)
+ try:
+ metadata.create_all(engine)
+ finally:
+ engine.dispose()
+
+ with sqlite3.connect(db_path) as conn:
+ assert conn.execute("select name from sqlite_master where name = 'alembic_version'").fetchone() is None
+
+ run_migrations(db_path)
+ run_migrations(db_path)
+
+ with sqlite3.connect(db_path) as conn:
+ version = conn.execute("select version_num from alembic_version").fetchone()
+ assert version == ("20260501_0001",)
+
+
+def test_run_migrations_stamps_existing_initial_schema_with_empty_version_table(tmp_path: Path) -> None:
+ db_path = tmp_path / "vibe.sqlite"
+ engine = create_sqlite_engine(db_path)
+ try:
+ metadata.create_all(engine)
+ finally:
+ engine.dispose()
+
+ with sqlite3.connect(db_path) as conn:
+ conn.execute("create table alembic_version (version_num varchar(32) not null)")
+ conn.commit()
+
+ run_migrations(db_path)
+ run_migrations(db_path)
+
+ with sqlite3.connect(db_path) as conn:
+ version = conn.execute("select version_num from alembic_version").fetchone()
+ assert version == ("20260501_0001",)
+
+
def test_ensure_sqlite_state_imports_json_once(tmp_path: Path) -> None:
state_dir = tmp_path / "state"
state_dir.mkdir()