Skip to content

Commit 995dff2

Browse files
committed
Migration: +2000 on promos below 2000
1 parent f496136 commit 995dff2

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
"""40-promo-above-2000
2+
3+
Create Date: 2025-09-07 09:54:34.421809
4+
"""
5+
6+
import uuid
7+
from typing import TYPE_CHECKING, Sequence, Union
8+
9+
from app.core.schools.schools_type import SchoolType
10+
11+
if TYPE_CHECKING:
12+
from pytest_alembic import MigrationContext
13+
14+
import sqlalchemy as sa
15+
from alembic import op
16+
17+
from app.types.sqlalchemy import TZDateTime
18+
19+
# revision identifiers, used by Alembic.
20+
revision: str = "91fadc90f892"
21+
down_revision: str | None = "2880c583d7f1"
22+
branch_labels: str | Sequence[str] | None = None
23+
depends_on: str | Sequence[str] | None = None
24+
25+
26+
def upgrade() -> None:
27+
op.execute("UPDATE core_user SET promo = 2000 + promo WHERE promo < 2000")
28+
29+
30+
def downgrade() -> None:
31+
# we afterwards cannot distinguish those who had 2023 from 23
32+
pass
33+
34+
35+
user_id_23 = str(uuid.uuid4())
36+
user_id_2023 = str(uuid.uuid4())
37+
user_id_null = str(uuid.uuid4())
38+
39+
40+
def pre_test_upgrade(
41+
alembic_runner: "MigrationContext",
42+
alembic_connection: sa.Connection,
43+
) -> None:
44+
alembic_runner.insert_into(
45+
"core_user",
46+
[
47+
{
48+
"id": user_id_23,
49+
"email": "email23",
50+
"school_id": str(SchoolType.no_school.value),
51+
"password_hash": "password_hash",
52+
"account_type": "student",
53+
"name": "name",
54+
"firstname": "firstname",
55+
"promo": 23,
56+
},
57+
{
58+
"id": user_id_2023,
59+
"email": "email2023",
60+
"school_id": str(SchoolType.no_school.value),
61+
"password_hash": "password_hash",
62+
"account_type": "student",
63+
"name": "name",
64+
"firstname": "firstname",
65+
"promo": 2023,
66+
},
67+
{
68+
"id": user_id_null,
69+
"email": "emailnull",
70+
"school_id": str(SchoolType.no_school.value),
71+
"password_hash": "password_hash",
72+
"account_type": "student",
73+
"name": "name",
74+
"firstname": "firstname",
75+
# promo is null
76+
},
77+
],
78+
)
79+
80+
81+
def test_upgrade(
82+
alembic_runner: "MigrationContext",
83+
alembic_connection: sa.Connection,
84+
) -> None:
85+
assert (
86+
alembic_connection.execute(
87+
sa.text(f"SELECT promo FROM core_user WHERE user_id = {user_id_23}"),
88+
).first()[9]
89+
== 2023
90+
)
91+
assert (
92+
alembic_connection.execute(
93+
sa.text(f"SELECT promo FROM core_user WHERE user_id = {user_id_2023}"),
94+
).first()[9]
95+
== 2023
96+
)
97+
assert (
98+
alembic_connection.execute(
99+
sa.text(f"SELECT promo FROM core_user WHERE user_id = {user_id_null}"),
100+
).first()[9]
101+
is None
102+
)

0 commit comments

Comments
 (0)