"""caddie M1 — the spine: projects, stages, the field dictionary, reference,
approvals, activity, templates, and the tool tables (schema now; the tool
router arrives with M2, so no migration is needed then).

The kit's four bw_* tables are created here too — managed mode puts them on the
app's Base.metadata, so there is one database and one migration history.

Revision ID: 0001_spine
"""

from alembic import op
import sqlalchemy as sa

revision = "0001_spine"
down_revision = None
branch_labels = None
depends_on = None


def upgrade() -> None:
    import sys
    sys.path.insert(0, "/app_root") if "/app_root" not in sys.path else None
    from app.accounts import init_accounts_kit  # noqa: F401  (registers bw_* tables)
    from app.models import Base
    from app import bw_store_sqlalchemy as bwstore
    from app.db import get_session_factory

    bwstore.managed(Base.metadata, get_session_factory())
    Base.metadata.create_all(op.get_bind())


def downgrade() -> None:
    from app.models import Base
    from app import bw_store_sqlalchemy as bwstore
    from app.db import get_session_factory

    bwstore.managed(Base.metadata, get_session_factory())
    Base.metadata.drop_all(op.get_bind())
