Skip to content

Commit

Permalink
Fix: migration
Browse files Browse the repository at this point in the history
  • Loading branch information
Rotheem committed Jan 7, 2025
1 parent de4d72a commit cf74758
Showing 1 changed file with 18 additions and 21 deletions.
39 changes: 18 additions & 21 deletions migrations/versions/27-schools.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import enum
from collections.abc import Sequence
from typing import TYPE_CHECKING
from uuid import UUID

if TYPE_CHECKING:
from pytest_alembic import MigrationContext
Expand All @@ -22,6 +23,11 @@
centrale_regex = r"^[\w\-.]*@(etu(-enise)?\.)?ec-lyon\.fr$"


class SchoolType(enum.Enum):
no_school = UUID("dce19aa2-8863-4c93-861e-fb7be8f610ed")
centrale_lyon = UUID("d9772da7-1142-4002-8b86-b694b431dfed")


class AccountType(enum.Enum):
student = "student"
former_student = "former_student"
Expand Down Expand Up @@ -80,21 +86,22 @@ def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table(
"core_school",
sa.Column("id", sa.Uuid(), nullable=False, index=True),
sa.Column("id", sa.Uuid(), nullable=False),
sa.Column("name", sa.String(), nullable=False, unique=True),
sa.Column("email_regex", sa.String(), nullable=False),
sa.PrimaryKeyConstraint("id"),
)

conn = op.get_bind()

conn.execute(
sa.insert(school_table).values(
id="dce19aa2-8863-4c93-861e-fb7be8f610ed",
name="no_school",
email_regex=".*",
),
)
for school in SchoolType:
conn.execute(
sa.insert(school_table).values(
id=school.value,
name=school.name,
email_regex=".*",
),
)

users = conn.execute(
sa.select(user_table.c.id, user_table.c.account_type),
Expand All @@ -110,7 +117,7 @@ def upgrade() -> None:
"school_id",
sa.Uuid(),
nullable=False,
server_default="dce19aa2-8863-4c93-861e-fb7be8f610ed",
server_default=str(SchoolType.no_school.value),
),
)
batch_op.create_foreign_key(
Expand Down Expand Up @@ -151,23 +158,15 @@ def upgrade() -> None:
),
)

conn.execute(
sa.insert(school_table).values(
id="d9772da7-1142-4002-8b86-b694b431dfed",
name="Centrale Lyon",
email_regex=centrale_regex,
),
)

for user in users:
conn.execute(
sa.update(user_table)
.where(user_table.c.id == user.id)
.values(
account_type=user.account_type,
school_id="d9772da7-1142-4002-8b86-b694b431dfed"
school_id=SchoolType.centrale_lyon.value
if user.account_type != AccountType.external
else "dce19aa2-8863-4c93-861e-fb7be8f610ed",
else SchoolType.no_school.value,
),
)

Expand Down Expand Up @@ -227,8 +226,6 @@ def downgrade() -> None:
),
)

op.drop_index(op.f("ix_core_school_name"), table_name="core_school")
op.drop_index(op.f("ix_core_school_id"), table_name="core_school")
op.drop_table("core_school")

for user in users:
Expand Down

0 comments on commit cf74758

Please sign in to comment.