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()
|
||||
Loading…
Add table
Add a link
Reference in a new issue