Files
windy-git/alembic/env.py
Grant Whitmer 659991b2bd G0: cell substrate — invariants made executable
Strand G0 complete and VERIFIED against real Postgres, not asserted.

  - FastAPI plane, fail-closed provider seams, repair-pointer error taxonomy
  - migration 001: all 10 tables incl. repo_type NOT NULL and model_cards (I-7)
  - 17 invariant tests, ruff clean, vocabulary audit clean

Two bugs found by RUNNING it that review would not have caught:

  1. SQLAlchemy Enum persists .name, not .value — so RepoState.deleted_soft
     and CreatedVia.imported would have written labels migration 001 never
     declared, failing at runtime rather than at review. Pinned via
     values_callable.
  2. op.create_table asks each Enum to emit its own CREATE TYPE with no
     checkfirst, so the second reference raised DuplicateObject and the
     migration died halfway. Types are now created once, referenced with
     create_type=False.

Proven live, with the hostile env var set:
  - I-12: COMMIT_SHA=deadbeef... in the environment, /version reports real HEAD.
    That env pin is the documented root cause of nine sibling services
    misreporting their commit; here it is structurally ignored.
  - I-8: three unconfigured providers -> status degraded, HTTP 503, each saying
    'refusing to report healthy'. No mock, no false green.
  - G0.4: upgrade -> downgrade -> upgrade round-trip clean (10 -> 0 -> 10).

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-11 14:19:28 -04:00

54 lines
1.9 KiB
Python

"""Alembic environment. Truth is this repo's migrations, not a hand-kept .sql file.
A sibling service names `postgres-schema.sql` as its source of truth in two
documents; that file omits the identity spine and ten tables, so a fresh deploy
from it produces a server that cannot register a user. Prod works only because
prod was built from migrations. We keep one source and it is `alembic/versions/`.
"""
from __future__ import annotations
import os
from logging.config import fileConfig
from alembic import context
from sqlalchemy import engine_from_config, pool
from api.app.models.core import SCHEMA, Base
config = context.config
if config.config_file_name:
fileConfig(config.config_file_name)
target_metadata = Base.metadata
def _url() -> str:
url = os.environ.get("DATABASE_URL", "postgresql://windygit:windygit@localhost:5432/windygit")
return url.replace("+asyncpg", "")
def run_migrations_offline() -> None:
context.configure(url=_url(), target_metadata=target_metadata, literal_binds=True,
include_schemas=True, version_table_schema=SCHEMA)
with context.begin_transaction():
context.run_migrations()
def run_migrations_online() -> None:
config.set_main_option("sqlalchemy.url", _url())
connectable = engine_from_config(config.get_section(config.config_ini_section, {}),
prefix="sqlalchemy.", poolclass=pool.NullPool)
with connectable.connect() as connection:
connection.exec_driver_sql(f"CREATE SCHEMA IF NOT EXISTS {SCHEMA}")
connection.commit()
context.configure(connection=connection, target_metadata=target_metadata,
include_schemas=True, version_table_schema=SCHEMA)
with context.begin_transaction():
context.run_migrations()
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()