feat: add Alembic for database migrations
Replace inline migration logic with proper Alembic migrations: - 001: Initial schema (creates memories table with FTS) - 002: Add multi-user and secrets columns (user_id, is_sensitive, vault_path, encrypted_content) Migrations run automatically on app startup. Existing databases are handled gracefully with IF NOT EXISTS / column existence checks.
This commit is contained in:
parent
63205dbd0c
commit
8a7239fb77
8 changed files with 244 additions and 58 deletions
50
migrations/versions/001_initial_schema.py
Normal file
50
migrations/versions/001_initial_schema.py
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
"""Initial schema with memories table.
|
||||
|
||||
Revision ID: 001
|
||||
Revises:
|
||||
Create Date: 2026-03-14
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
revision: str = "001"
|
||||
down_revision: Union[str, None] = None
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
conn = op.get_bind()
|
||||
# Check if table already exists (handles pre-Alembic installations)
|
||||
result = conn.execute(
|
||||
sa.text("SELECT EXISTS(SELECT 1 FROM information_schema.tables WHERE table_name = 'memories')")
|
||||
)
|
||||
if result.scalar():
|
||||
return
|
||||
|
||||
op.execute("""
|
||||
CREATE TABLE memories (
|
||||
id SERIAL PRIMARY KEY,
|
||||
content TEXT NOT NULL,
|
||||
category VARCHAR(50) DEFAULT 'facts',
|
||||
tags TEXT DEFAULT '',
|
||||
expanded_keywords TEXT DEFAULT '',
|
||||
importance REAL DEFAULT 0.5,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
search_vector tsvector GENERATED ALWAYS AS (
|
||||
setweight(to_tsvector('english', coalesce(content, '')), 'A') ||
|
||||
setweight(to_tsvector('english', coalesce(expanded_keywords, '')), 'B') ||
|
||||
setweight(to_tsvector('english', coalesce(tags, '')), 'C') ||
|
||||
setweight(to_tsvector('english', coalesce(category, '')), 'D')
|
||||
) STORED
|
||||
)
|
||||
""")
|
||||
op.execute("CREATE INDEX idx_memories_search ON memories USING GIN(search_vector)")
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_index("idx_memories_search")
|
||||
op.drop_table("memories")
|
||||
52
migrations/versions/002_add_multi_user_and_secrets.py
Normal file
52
migrations/versions/002_add_multi_user_and_secrets.py
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
"""Add multi-user support and secret management columns.
|
||||
|
||||
Revision ID: 002
|
||||
Revises: 001
|
||||
Create Date: 2026-03-14
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
revision: str = "002"
|
||||
down_revision: Union[str, None] = "001"
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def _column_exists(conn, column_name: str) -> bool:
|
||||
result = conn.execute(
|
||||
sa.text(
|
||||
"SELECT EXISTS(SELECT 1 FROM information_schema.columns "
|
||||
"WHERE table_name = 'memories' AND column_name = :col)"
|
||||
),
|
||||
{"col": column_name},
|
||||
)
|
||||
return result.scalar()
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
conn = op.get_bind()
|
||||
|
||||
if not _column_exists(conn, "user_id"):
|
||||
op.add_column("memories", sa.Column("user_id", sa.String(100), nullable=False, server_default="default"))
|
||||
|
||||
if not _column_exists(conn, "is_sensitive"):
|
||||
op.add_column("memories", sa.Column("is_sensitive", sa.Boolean(), server_default="false"))
|
||||
|
||||
if not _column_exists(conn, "vault_path"):
|
||||
op.add_column("memories", sa.Column("vault_path", sa.Text(), nullable=True))
|
||||
|
||||
if not _column_exists(conn, "encrypted_content"):
|
||||
op.add_column("memories", sa.Column("encrypted_content", sa.LargeBinary(), nullable=True))
|
||||
|
||||
op.execute("CREATE INDEX IF NOT EXISTS idx_memories_user ON memories(user_id)")
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_index("idx_memories_user")
|
||||
op.drop_column("memories", "encrypted_content")
|
||||
op.drop_column("memories", "vault_path")
|
||||
op.drop_column("memories", "is_sensitive")
|
||||
op.drop_column("memories", "user_id")
|
||||
Loading…
Add table
Add a link
Reference in a new issue