Skip to content

Commit e92a44d

Browse files
committed
fix: Added necessary migrations and corrected typos
1 parent 951d990 commit e92a44d

File tree

4 files changed

+279
-3
lines changed

4 files changed

+279
-3
lines changed

migrations/env.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
from alembic import context
1313

14-
from apolo import config as config_app
15-
from craftship.federation import federation
14+
from src import config as config_app
15+
from src.federation import init
1616

1717
# this is the Alembic Config object, which provides
1818
# access to the values within the .ini file in use.
@@ -27,7 +27,7 @@
2727
# from myapp import mymodel
2828
# target_metadata = mymodel.Base.metadata
2929
orm.clear_mappers()
30-
target_metadata, _ = federation()
30+
target_metadata, _ = init()
3131

3232
# other values from the config, defined by the needs of env.py,
3333
# can be acquired:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
"""user permissions
2+
3+
Revision ID: 4198d2577d59
4+
Revises: 6c26d63cff3f
5+
Create Date: 2020-12-22 19:45:43.709177
6+
7+
"""
8+
import sqlalchemy as sa
9+
from alembic import op, context
10+
from src import config
11+
12+
# revision identifiers, used by Alembic.
13+
revision = "4198d2577d59"
14+
down_revision = "6c26d63cff3f"
15+
branch_labels = None
16+
depends_on = None
17+
18+
19+
def upgrade():
20+
schema_upgrade()
21+
if context.get_x_argument(as_dictionary=True).get("data"):
22+
data_upgrade()
23+
24+
25+
def downgrade():
26+
if context.get_x_argument(as_dictionary=True).get("data"):
27+
data_downgrade()
28+
schema_downgrade()
29+
30+
31+
def schema_upgrade():
32+
# ### commands auto generated by Alembic - please adjust! ###
33+
op.create_table(
34+
"user_permissions",
35+
sa.Column("id_user", sa.String(length=64), nullable=False),
36+
sa.Column("id_permission", sa.String(length=64), nullable=False),
37+
sa.ForeignKeyConstraint(
38+
["id_permission"], ["permissions.id"], ondelete="CASCADE"
39+
),
40+
sa.ForeignKeyConstraint(["id_user"], ["users.id"], ondelete="CASCADE"),
41+
sa.PrimaryKeyConstraint("id_user", "id_permission"),
42+
)
43+
op.add_column(
44+
"users", sa.Column("id_role", sa.String(length=64), nullable=True)
45+
)
46+
op.create_foreign_key(None, "users", "roles", ["id_role"], ["id"])
47+
# ### end Alembic commands ###
48+
49+
50+
def schema_downgrade():
51+
# ### commands auto generated by Alembic - please adjust! ###
52+
op.drop_column("users", "id_role")
53+
op.drop_table("user_permissions")
54+
# ### end Alembic commands ###
55+
56+
57+
def data_upgrade():
58+
access_key, _ = config.default_user()
59+
op.execute(
60+
f"""
61+
INSERT INTO user_permissions
62+
SELECT u.id AS id_user, p.id AS id_permission
63+
FROM users u CROSS JOIN permissions p
64+
WHERE u.access_key = '{access_key}'
65+
"""
66+
)
67+
68+
69+
def data_downgrade():
70+
pass
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
"""user table
2+
3+
Revision ID: 52283d2bf15b
4+
Revises:
5+
Create Date: 2020-12-15 15:53:32.591880
6+
7+
"""
8+
import sqlalchemy as sa
9+
from uuid import uuid4
10+
from datetime import datetime
11+
from alembic import op, context
12+
13+
from src import config
14+
from src.auth.domain import model
15+
16+
# revision identifiers, used by Alembic.
17+
revision = "52283d2bf15b"
18+
down_revision = None
19+
branch_labels = None
20+
depends_on = None
21+
22+
23+
def upgrade():
24+
users = schema_upgrade()
25+
if context.get_x_argument(as_dictionary=True).get("data"):
26+
data_upgrade(users)
27+
28+
29+
def downgrade():
30+
if context.get_x_argument(as_dictionary=True).get("data"):
31+
data_downgrade()
32+
schema_downgrade()
33+
34+
35+
def schema_upgrade():
36+
# ### commands auto generated by Alembic - please adjust! ###
37+
users = op.create_table(
38+
"users",
39+
sa.Column(
40+
"id",
41+
sa.String(length=64),
42+
nullable=False,
43+
default=lambda: str(uuid4()),
44+
),
45+
sa.Column("access_key", sa.String(length=255), nullable=False),
46+
sa.Column("name", sa.String(length=255), nullable=True),
47+
sa.Column("email", sa.String(length=255), nullable=False),
48+
sa.Column("password", sa.Text(), nullable=False),
49+
sa.Column(
50+
"created_at",
51+
sa.TIMESTAMP(),
52+
nullable=False,
53+
default=lambda: datetime.today(),
54+
),
55+
sa.PrimaryKeyConstraint("id"),
56+
sa.UniqueConstraint("access_key"),
57+
sa.UniqueConstraint("id"),
58+
)
59+
# ### end Alembic commands ###
60+
return users
61+
62+
63+
def schema_downgrade():
64+
# ### commands auto generated by Alembic - please adjust! ###
65+
op.drop_table("users")
66+
# ### end Alembic commands ###
67+
68+
69+
def data_upgrade(users):
70+
access_key, pwd = config.default_user()
71+
op.bulk_insert(
72+
users,
73+
[
74+
{
75+
"access_key": access_key,
76+
"name": "Admin",
77+
"email": "[email protected]",
78+
"password": model.User.hash_password(pwd),
79+
}
80+
],
81+
)
82+
83+
84+
def data_downgrade():
85+
pass
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
"""role_aggregate
2+
3+
Revision ID: 6c26d63cff3f
4+
Revises: 52283d2bf15b
5+
Create Date: 2020-12-17 18:36:39.813342
6+
7+
"""
8+
import sqlalchemy as sa
9+
from alembic import op, context
10+
from uuid import uuid4
11+
from datetime import datetime
12+
13+
14+
# revision identifiers, used by Alembic.
15+
revision = "6c26d63cff3f"
16+
down_revision = "52283d2bf15b"
17+
branch_labels = None
18+
depends_on = None
19+
20+
21+
def upgrade():
22+
permissions = schema_upgrade()
23+
if context.get_x_argument(as_dictionary=True).get("data"):
24+
data_upgrade(permissions)
25+
26+
27+
def downgrade():
28+
if context.get_x_argument(as_dictionary=True).get("data"):
29+
data_downgrade()
30+
schema_downgrade()
31+
32+
33+
def schema_upgrade():
34+
# ### commands auto generated by Alembic - please adjust! ###
35+
permissions = op.create_table(
36+
"permissions",
37+
sa.Column(
38+
"id",
39+
sa.String(length=64),
40+
nullable=False,
41+
default=lambda: str(uuid4()),
42+
),
43+
sa.Column("resource", sa.String(length=128), nullable=False),
44+
sa.Column(
45+
"action",
46+
sa.Enum(
47+
"LIST", "GET", "CREATE", "UPDATE", name="permissionactionenum"
48+
),
49+
nullable=False,
50+
),
51+
sa.Column("is_conditional", sa.Boolean(), nullable=False),
52+
sa.PrimaryKeyConstraint("id"),
53+
sa.UniqueConstraint("id"),
54+
sa.UniqueConstraint("resource", "action", "is_conditional"),
55+
)
56+
op.create_table(
57+
"roles",
58+
sa.Column(
59+
"id",
60+
sa.String(length=64),
61+
nullable=False,
62+
default=lambda: str(uuid4()),
63+
),
64+
sa.Column("code", sa.String(length=128), nullable=False),
65+
sa.Column("name", sa.String(length=256), nullable=False),
66+
sa.Column(
67+
"created_at",
68+
sa.TIMESTAMP(),
69+
nullable=False,
70+
default=lambda: datetime.today(),
71+
),
72+
sa.PrimaryKeyConstraint("id", "code"),
73+
sa.UniqueConstraint("id"),
74+
sa.UniqueConstraint("code"),
75+
)
76+
op.create_table(
77+
"role_permissions",
78+
sa.Column("id_role", sa.String(length=64), nullable=False),
79+
sa.Column("id_permission", sa.String(length=64), nullable=False),
80+
sa.ForeignKeyConstraint(
81+
["id_permission"], ["permissions.id"], ondelete="CASCADE"
82+
),
83+
sa.ForeignKeyConstraint(["id_role"], ["roles.id"], ondelete="CASCADE"),
84+
sa.PrimaryKeyConstraint("id_role", "id_permission"),
85+
)
86+
# ### end Alembic commands ###
87+
return permissions
88+
89+
90+
def schema_downgrade():
91+
# ### commands auto generated by Alembic - please adjust! ###
92+
op.drop_table("role_permissions")
93+
op.drop_table("roles")
94+
op.drop_table("permissions")
95+
op.execute("DROP TYPE IF EXISTS permissionactionenum")
96+
# ### end Alembic commands ###
97+
98+
99+
def data_upgrade(permissions):
100+
op.bulk_insert(
101+
permissions,
102+
[
103+
{"resource": "user", "action": "CREATE", "is_conditional": False},
104+
{"resource": "user", "action": "UPDATE", "is_conditional": False},
105+
{"resource": "user", "action": "GET", "is_conditional": False},
106+
{"resource": "user", "action": "LIST", "is_conditional": False},
107+
{"resource": "role", "action": "CREATE", "is_conditional": False},
108+
{"resource": "role", "action": "UPDATE", "is_conditional": False},
109+
{"resource": "role", "action": "GET", "is_conditional": False},
110+
{"resource": "role", "action": "LIST", "is_conditional": False},
111+
{
112+
"resource": "permission",
113+
"action": "LIST",
114+
"is_conditional": False,
115+
},
116+
],
117+
)
118+
119+
120+
def data_downgrade():
121+
pass

0 commit comments

Comments
 (0)