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
40
migrations/env.py
Normal file
40
migrations/env.py
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
"""Alembic environment configuration."""
|
||||
|
||||
import os
|
||||
from logging.config import fileConfig
|
||||
|
||||
from alembic import context
|
||||
from sqlalchemy import create_engine, pool
|
||||
|
||||
config = context.config
|
||||
|
||||
if config.config_file_name is not None:
|
||||
fileConfig(config.config_file_name)
|
||||
|
||||
# Override sqlalchemy.url from environment variable
|
||||
db_url = os.environ.get("DATABASE_URL", "")
|
||||
if db_url:
|
||||
config.set_main_option("sqlalchemy.url", db_url)
|
||||
|
||||
|
||||
def run_migrations_offline() -> None:
|
||||
"""Run migrations in 'offline' mode."""
|
||||
url = config.get_main_option("sqlalchemy.url")
|
||||
context.configure(url=url, target_metadata=None, literal_binds=True, dialect_opts={"paramstyle": "named"})
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
def run_migrations_online() -> None:
|
||||
"""Run migrations in 'online' mode."""
|
||||
connectable = create_engine(config.get_main_option("sqlalchemy.url"), poolclass=pool.NullPool)
|
||||
with connectable.connect() as connection:
|
||||
context.configure(connection=connection, target_metadata=None)
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
if context.is_offline_mode():
|
||||
run_migrations_offline()
|
||||
else:
|
||||
run_migrations_online()
|
||||
26
migrations/script.py.mako
Normal file
26
migrations/script.py.mako
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
"""${message}
|
||||
|
||||
Revision ID: ${up_revision}
|
||||
Revises: ${down_revision | comma,n}
|
||||
Create Date: ${create_date}
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
${imports if imports else ""}
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = ${repr(up_revision)}
|
||||
down_revision: Union[str, None] = ${repr(down_revision)}
|
||||
branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
|
||||
depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
${upgrades if upgrades else "pass"}
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
${downgrades if downgrades else "pass"}
|
||||
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