-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08f4eaf160d3_soft_delete_users.py
109 lines (99 loc) · 2.63 KB
/
08f4eaf160d3_soft_delete_users.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
"""soft delete users
Revision ID: 08f4eaf160d3
Revises: 1729015196af
Create Date: 2021-11-01 01:38:35.818282
"""
import sqlalchemy as sa
from alembic import op
# revision identifiers, used by Alembic.
revision = "08f4eaf160d3"
down_revision = "1729015196af"
branch_labels = None
depends_on = None
def upgrade():
### Update users
op.add_column(
"users",
sa.Column("is_deleted", sa.Boolean(), default=False, nullable=False),
)
op.rename_table("users", "users_all")
active_users_view = """
CREATE VIEW users
AS
SELECT * from users_all
WHERE is_deleted=0;
"""
op.execute(active_users_view)
### Update foreign key for story_translations
op.drop_constraint(
u"story_translations_all_ibfk_1", "story_translations_all", type_="foreignkey"
)
op.drop_constraint(
u"story_translations_all_ibfk_2", "story_translations_all", type_="foreignkey"
)
op.create_foreign_key(
u"story_translations_all_ibfk_1",
"story_translations_all",
"users_all",
["reviewer_id"],
["id"],
)
op.create_foreign_key(
u"story_translations_all_ibfk_2",
"story_translations_all",
"users_all",
["translator_id"],
["id"],
)
### Update foreign key for users
op.drop_constraint(
u"comments_all_ibfk_2",
"comments_all",
type_="foreignkey",
)
op.create_foreign_key(
u"comments_all_ibfk_2",
"comments_all",
"users_all",
["user_id"],
["id"],
)
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
### foreign keys
op.drop_constraint(
u"comments_all_ibfk_2",
"comments_all",
type_="foreignkey",
)
op.create_foreign_key(
u"comments_all_ibfk_2",
"comments_all",
"users",
["user_id"],
["id"],
)
op.drop_constraint(
u"story_translations_all_ibfk_1", "story_translations_all", type_="foreignkey"
)
op.drop_constraint(
u"story_translations_all_ibfk_2", "story_translations_all", type_="foreignkey"
)
op.create_foreign_key(
u"story_translations_all_ibfk_1",
"story_translations_all",
"users",
["reviewer_id"],
["id"],
)
op.create_foreign_key(
u"story_translations_all_ibfk_2",
"story_translations_all",
"users",
["translator_id"],
["id"],
)
op.execute("DROP VIEW users")
op.rename_table("users_all", "users")
op.drop_column("users", "is_deleted")
# ### end Alembic commands ###