forked from cleveragents/cleveragents-core
0c5b140d29
Move alembic configuration and migration files from repository root into the Python package structure to ensure they are included in the wheel distribution. This fix resolves the FileNotFoundError when running `agents init` in Docker containers or any environment using the built wheel distribution. Changes: - Move alembic/ directory from repo root to src/cleveragents/infrastructure/database/migrations/ - Move alembic.ini to the same new location and update script_location setting - Update MigrationRunner._find_alembic_ini() to search from the new canonical location within the package - Update create_template_db.py to point to the new alembic.ini location - Update documentation references to reflect new migration file locations - Create __init__.py for migrations package - The env.py file is imported when running tests that verify all modules can be imported without errors. However, context.config is only available when alembic is actually running migrations, not during normal module imports. This caused an AttributeError when the test tried to import the migrations.env module. - Fix by using getattr() with a default value to safely access context.config, and guard all code that uses config with None checks. This allows the module to be safely imported while still functioning correctly during migrations. Testing: - Verified MigrationRunner can locate alembic.ini in new location - Tested agents init succeeds in creating project with database - Template database creation works correctly - All migration tests should pass without changes Alembic files now follow standard Python packaging conventions, making them automatically included in wheel distributions without special configuration. ISSUES CLOSED: #4180
83 lines
2.5 KiB
Python
83 lines
2.5 KiB
Python
#!/usr/bin/env python3
|
|
"""Create a pre-migrated template SQLite database for fast test setup.
|
|
|
|
Instead of running 25 Alembic migrations per scenario (~0.5-3s each),
|
|
tests can copy this template file (~1ms) and get an identical schema.
|
|
|
|
Usage:
|
|
python scripts/create_template_db.py [output_path]
|
|
|
|
Default output: build/.template-migrated.db
|
|
"""
|
|
|
|
import os
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
# Ensure src/ is importable
|
|
src_dir = Path(__file__).resolve().parent.parent / "src"
|
|
sys.path.insert(0, str(src_dir))
|
|
|
|
|
|
def create_template(output_path: str = "build/.template-migrated.db") -> None:
|
|
"""Create a fully-migrated template SQLite database.
|
|
|
|
Uses Base.metadata.create_all() to create all tables in a single DDL
|
|
batch (~5ms), then stamps the alembic_version table with the head
|
|
revision so MigrationRunner sees no pending migrations.
|
|
"""
|
|
from alembic import command
|
|
from alembic.config import Config
|
|
from alembic.script import ScriptDirectory
|
|
from sqlalchemy import create_engine
|
|
|
|
from cleveragents.infrastructure.database.models import Base
|
|
|
|
out = Path(output_path)
|
|
out.parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
# Remove existing template so we always create fresh
|
|
if out.exists():
|
|
out.unlink()
|
|
|
|
db_url = f"sqlite:///{out.resolve()}"
|
|
engine = create_engine(db_url, connect_args={"check_same_thread": False})
|
|
|
|
# Create all 33 tables in one fast DDL batch
|
|
Base.metadata.create_all(engine)
|
|
|
|
# Stamp alembic_version with the head revision so MigrationRunner
|
|
# sees the database as fully migrated (no pending migrations).
|
|
alembic_ini = (
|
|
Path(__file__).resolve().parent.parent
|
|
/ "src"
|
|
/ "cleveragents"
|
|
/ "infrastructure"
|
|
/ "database"
|
|
/ "migrations"
|
|
/ "alembic.ini"
|
|
)
|
|
cfg = Config(str(alembic_ini))
|
|
sd = ScriptDirectory.from_config(cfg)
|
|
head = sd.get_current_head()
|
|
if head is None:
|
|
msg = "No Alembic revisions found — cannot stamp template database."
|
|
raise RuntimeError(msg)
|
|
|
|
with engine.connect() as conn:
|
|
cfg.attributes["connection"] = conn
|
|
command.stamp(cfg, head)
|
|
conn.commit()
|
|
|
|
engine.dispose()
|
|
|
|
# Ensure the template database is writable (0o664 = rw-rw-r--)
|
|
# This prevents sqlite3.OperationalError: attempt to write a readonly database
|
|
# when tests copy and modify the template during test setup.
|
|
os.chmod(str(out), 0o664)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
path = sys.argv[1] if len(sys.argv) > 1 else "build/.template-migrated.db"
|
|
create_template(path)
|