Squash alembic migrations into single baseline

This commit is contained in:
Jarvis
2026-02-02 17:05:25 +00:00
parent 13b9bf95fb
commit da1a1cee59
3 changed files with 57 additions and 190 deletions

View File

@@ -1,28 +1,29 @@
"""baseline schema (no HR module)
"""Baseline schema (squashed)
Revision ID: bacd5e6a253d
Revision ID: 0a1b2c3d4e5f
Revises:
Create Date: 2026-02-02 16:37:34.122971
Create Date: 2026-02-02
This is a squashed baseline migration for Mission Control.
All prior incremental migrations were removed to keep the repo simple.
"""
from typing import Sequence, Union
from __future__ import annotations
from alembic import op
import sqlalchemy as sa
import sqlmodel
from alembic import op
# revision identifiers, used by Alembic.
revision: str = "bacd5e6a253d"
down_revision: Union[str, Sequence[str], None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
revision = "0a1b2c3d4e5f"
down_revision = None
branch_labels = None
depends_on = None
def upgrade() -> None:
"""Upgrade schema."""
# Departments (FK to employees added after employees table exists)
op.create_table(
"departments",
@@ -40,6 +41,7 @@ def upgrade() -> None:
sa.Column("name", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
sa.Column("employee_type", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
sa.Column("department_id", sa.Integer(), nullable=True),
sa.Column("team_id", sa.Integer(), nullable=True),
sa.Column("manager_id", sa.Integer(), nullable=True),
sa.Column("title", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
sa.Column("status", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
@@ -53,15 +55,50 @@ def upgrade() -> None:
# Break the departments<->employees cycle: add this FK after both tables exist
op.create_foreign_key(None, "departments", "employees", ["head_employee_id"], ["id"])
# Teams
op.create_table(
"teams",
sa.Column("id", sa.Integer(), primary_key=True, nullable=False),
sa.Column("name", sa.String(), nullable=False),
sa.Column("department_id", sa.Integer(), nullable=False),
sa.Column("lead_employee_id", sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(["department_id"], ["departments.id"], ondelete="CASCADE"),
sa.ForeignKeyConstraint(["lead_employee_id"], ["employees.id"], ondelete="SET NULL"),
sa.UniqueConstraint("department_id", "name", name="uq_teams_department_id_name"),
)
op.create_index("ix_teams_name", "teams", ["name"], unique=False)
op.create_index("ix_teams_department_id", "teams", ["department_id"], unique=False)
# Employees.team_id FK (added after teams exists)
op.create_index("ix_employees_team_id", "employees", ["team_id"], unique=False)
op.create_foreign_key(
"fk_employees_team_id_teams",
"employees",
"teams",
["team_id"],
["id"],
ondelete="SET NULL",
)
# Projects
op.create_table(
"projects",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("name", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
sa.Column("status", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
sa.Column("team_id", sa.Integer(), nullable=True),
sa.PrimaryKeyConstraint("id"),
)
op.create_index(op.f("ix_projects_name"), "projects", ["name"], unique=True)
op.create_index("ix_projects_team_id", "projects", ["team_id"], unique=False)
op.create_foreign_key(
"fk_projects_team_id_teams",
"projects",
"teams",
["team_id"],
["id"],
ondelete="SET NULL",
)
# Activities
op.create_table(
@@ -129,8 +166,6 @@ def upgrade() -> None:
def downgrade() -> None:
"""Downgrade schema."""
op.drop_index(op.f("ix_task_comments_task_id"), table_name="task_comments")
op.drop_table("task_comments")
@@ -142,9 +177,18 @@ def downgrade() -> None:
op.drop_table("activities")
op.drop_constraint("fk_projects_team_id_teams", "projects", type_="foreignkey")
op.drop_index("ix_projects_team_id", table_name="projects")
op.drop_index(op.f("ix_projects_name"), table_name="projects")
op.drop_table("projects")
op.drop_constraint("fk_employees_team_id_teams", "employees", type_="foreignkey")
op.drop_index("ix_employees_team_id", table_name="employees")
op.drop_index("ix_teams_department_id", table_name="teams")
op.drop_index("ix_teams_name", table_name="teams")
op.drop_table("teams")
op.drop_table("employees")
op.drop_index(op.f("ix_departments_name"), table_name="departments")

View File

@@ -1,73 +0,0 @@
"""Add teams and team ownership
Revision ID: 3f2c1b9c8e12
Revises: bacd5e6a253d
Create Date: 2026-02-02
"""
from __future__ import annotations
import sqlalchemy as sa
from alembic import op
# revision identifiers, used by Alembic.
revision = "3f2c1b9c8e12"
down_revision = "bacd5e6a253d"
branch_labels = None
depends_on = None
def upgrade() -> None:
# 1) Teams
op.create_table(
"teams",
sa.Column("id", sa.Integer(), primary_key=True, nullable=False),
sa.Column("name", sa.String(), nullable=False),
sa.Column("department_id", sa.Integer(), nullable=False),
sa.Column("lead_employee_id", sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(["department_id"], ["departments.id"], ondelete="CASCADE"),
sa.ForeignKeyConstraint(["lead_employee_id"], ["employees.id"], ondelete="SET NULL"),
sa.UniqueConstraint("department_id", "name", name="uq_teams_department_id_name"),
)
op.create_index("ix_teams_name", "teams", ["name"], unique=False)
op.create_index("ix_teams_department_id", "teams", ["department_id"], unique=False)
# 2) Employees belong to one (optional) team
op.add_column("employees", sa.Column("team_id", sa.Integer(), nullable=True))
op.create_index("ix_employees_team_id", "employees", ["team_id"], unique=False)
op.create_foreign_key(
"fk_employees_team_id_teams",
"employees",
"teams",
["team_id"],
["id"],
ondelete="SET NULL",
)
# 3) Projects are owned by teams (not departments)
op.add_column("projects", sa.Column("team_id", sa.Integer(), nullable=True))
op.create_index("ix_projects_team_id", "projects", ["team_id"], unique=False)
op.create_foreign_key(
"fk_projects_team_id_teams",
"projects",
"teams",
["team_id"],
["id"],
ondelete="SET NULL",
)
def downgrade() -> None:
op.drop_constraint("fk_projects_team_id_teams", "projects", type_="foreignkey")
op.drop_index("ix_projects_team_id", table_name="projects")
op.drop_column("projects", "team_id")
op.drop_constraint("fk_employees_team_id_teams", "employees", type_="foreignkey")
op.drop_index("ix_employees_team_id", table_name="employees")
op.drop_column("employees", "team_id")
op.drop_index("ix_teams_department_id", table_name="teams")
op.drop_index("ix_teams_name", table_name="teams")
op.drop_table("teams")

View File

@@ -1,104 +0,0 @@
"""Ensure human employee id is 1
Revision ID: 7f4e2c1a9a10
Revises: 3f2c1b9c8e12
Create Date: 2026-02-02
"""
from __future__ import annotations
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = "7f4e2c1a9a10"
down_revision = "3f2c1b9c8e12"
branch_labels = None
depends_on = None
def upgrade() -> None:
"""If the (single) human employee exists but is not id=1, move it to id=1.
This is a dev ergonomics migration: a bunch of code / examples assume the
primary human user is employee_id=1.
Safe behavior:
- Only runs when there is exactly one human employee.
- Only runs when employee id=1 is currently unused.
- Rewrites all known FKs that point at the old id.
"""
conn = op.get_bind()
human_ids = [
row[0]
for row in conn.execute(sa.text("SELECT id FROM employees WHERE employee_type='human' ORDER BY id"))
]
# Only attempt the rewrite in the "typical dev" scenario.
if len(human_ids) != 1:
return
old_id = int(human_ids[0])
if old_id == 1:
return
id1_exists = conn.execute(sa.text("SELECT 1 FROM employees WHERE id=1")).first() is not None
if id1_exists:
return
# Update foreign keys in known tables/columns.
conn.execute(
sa.text("UPDATE departments SET head_employee_id=1 WHERE head_employee_id=:old_id"),
{"old_id": old_id},
)
conn.execute(
sa.text("UPDATE teams SET lead_employee_id=1 WHERE lead_employee_id=:old_id"),
{"old_id": old_id},
)
conn.execute(
sa.text("UPDATE employees SET manager_id=1 WHERE manager_id=:old_id"),
{"old_id": old_id},
)
conn.execute(
sa.text("UPDATE activities SET actor_employee_id=1 WHERE actor_employee_id=:old_id"),
{"old_id": old_id},
)
conn.execute(
sa.text("UPDATE project_members SET employee_id=1 WHERE employee_id=:old_id"),
{"old_id": old_id},
)
conn.execute(
sa.text("UPDATE tasks SET assignee_employee_id=1 WHERE assignee_employee_id=:old_id"),
{"old_id": old_id},
)
conn.execute(
sa.text("UPDATE tasks SET reviewer_employee_id=1 WHERE reviewer_employee_id=:old_id"),
{"old_id": old_id},
)
conn.execute(
sa.text("UPDATE tasks SET created_by_employee_id=1 WHERE created_by_employee_id=:old_id"),
{"old_id": old_id},
)
conn.execute(
sa.text("UPDATE task_comments SET author_employee_id=1 WHERE author_employee_id=:old_id"),
{"old_id": old_id},
)
# Finally, rewrite the employee PK itself.
conn.execute(sa.text("UPDATE employees SET id=1 WHERE id=:old_id"), {"old_id": old_id})
# Keep the sequence in sync (Postgres).
conn.execute(
sa.text(
"SELECT setval(pg_get_serial_sequence('employees','id'), (SELECT COALESCE(MAX(id), 1) FROM employees), true)"
)
)
def downgrade() -> None:
# Non-reversible in a safe way; this is a dev convenience migration.
pass