-
Notifications
You must be signed in to change notification settings - Fork 0
/
2c97929c18d0_add_regulation_tables.py
141 lines (128 loc) · 4.1 KB
/
2c97929c18d0_add_regulation_tables.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
"""add_regulation_tables
Revision ID: 2c97929c18d0
Revises: b6d1707c5ba1
Create Date: 2022-07-12 15:13:46.326828
"""
import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql
from sqlalchemy.orm.session import Session
import json
from app.helpers.regulations_utils import insert_regulation_check
from app.models.regulation_check import RegulationCheckType
from app.services.get_regulation_checks import get_regulation_checks
# revision identifiers, used by Alembic.
revision = "2c97929c18d0"
down_revision = "b6d1707c5ba1"
branch_labels = None
depends_on = None
def fill_regulation_checks():
session = Session(bind=op.get_bind())
regulation_check_data = get_regulation_checks()
for r in regulation_check_data:
if r.type == RegulationCheckType.MAXIMUM_WORK_IN_CALENDAR_WEEK:
continue
if r.type == RegulationCheckType.NO_LIC:
continue
insert_regulation_check(session=session, regulation_check_data=r)
def upgrade():
op.create_table(
"regulation_check",
sa.Column("creation_time", sa.DateTime(), nullable=False),
sa.Column(
"type",
sa.Enum(
"minimumDailyRest",
"maximumWorkDayTime",
"minimumWorkDayBreak",
"maximumUninterruptedWorkTime",
"maximumWorkedDaysInWeek",
name="regulationchecktype",
native_enum=False,
),
nullable=False,
),
sa.Column(
"unit",
sa.Enum(
"day",
"week",
name="unittype",
native_enum=False,
),
nullable=False,
),
sa.Column("label", sa.String(length=255), nullable=False),
sa.Column("description", sa.TEXT(), nullable=True),
sa.Column("date_application_start", sa.Date(), nullable=False),
sa.Column("date_application_end", sa.Date(), nullable=True),
sa.Column(
"regulation_rule",
sa.Enum(
"dailyWork",
"dailyRest",
"weeklyWork",
"weeklyRest",
name="regulationrule",
native_enum=False,
),
nullable=False,
),
sa.Column(
"variables",
postgresql.JSONB(none_as_null=True, astext_type=sa.Text()),
nullable=True,
),
sa.Column("id", sa.Integer(), nullable=False),
sa.PrimaryKeyConstraint("id"),
)
fill_regulation_checks()
op.create_table(
"regulatory_alert",
sa.Column("creation_time", sa.DateTime(), nullable=False),
sa.Column("day", sa.Date(), nullable=False),
sa.Column(
"extra",
postgresql.JSONB(none_as_null=True, astext_type=sa.Text()),
nullable=True,
),
sa.Column(
"submitter_type",
sa.Enum(
"employee", "admin", name="submittertype", native_enum=False
),
nullable=False,
),
sa.Column("user_id", sa.Integer(), nullable=False),
sa.Column("regulation_check_id", sa.Integer(), nullable=False),
sa.Column("id", sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(
["regulation_check_id"],
["regulation_check.id"],
),
sa.ForeignKeyConstraint(
["user_id"],
["user.id"],
),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint(
"day",
"user_id",
"regulation_check_id",
"submitter_type",
name="only_one_entry_per_user_day_check_and_submitter_type",
),
)
op.create_index(
op.f("ix_regulatory_alert_regulation_check_id"),
"regulatory_alert",
["regulation_check_id"],
unique=False,
)
def downgrade():
op.drop_index(
op.f("ix_regulatory_alert_regulation_check_id"),
table_name="regulatory_alert",
)
op.drop_table("regulatory_alert")
op.drop_table("regulation_check")