-
Notifications
You must be signed in to change notification settings - Fork 0
/
915e0942f14f_ajout_champ_last_submitter_id_dans_.py
63 lines (53 loc) · 1.59 KB
/
915e0942f14f_ajout_champ_last_submitter_id_dans_.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""ajout champ last_submitter_id dans activity
Revision ID: 915e0942f14f
Revises: 6bfdb39b721f
Create Date: 2022-02-15 11:02:33.733812
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
from sqlalchemy.orm.session import Session
# revision identifiers, used by Alembic.
revision = "915e0942f14f"
down_revision = "6bfdb39b721f"
branch_labels = None
depends_on = None
def _migrate_activities():
session = Session(bind=op.get_bind())
session.execute(
"""
UPDATE activity a
SET last_submitter_id = (
SELECT av.submitter_id
FROM activity_version av
WHERE (av.version_number = (
SELECT MAX(av2.version_number)
FROM activity_version av2
WHERE av2.activity_id = a.id
)
)
AND (av.activity_id = a.id)
)
"""
)
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column(
"activity", sa.Column("last_submitter_id", sa.Integer(), nullable=True)
)
op.create_foreign_key(
"activity_last_submitter_id_fkey",
"activity",
"user",
["last_submitter_id"],
["id"],
)
# ### end Alembic commands ###
_migrate_activities()
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint(
"activity_last_submitter_id_fkey", "activity", type_="foreignkey"
)
op.drop_column("activity", "last_submitter_id")
# ### end Alembic commands ###