Skip to content

Commit 19b23bf

Browse files
[DOP-19907] - change migrations & move models, mixins (#115)
* [DOP-19907] - change migrations & move models, mixins * [DOP-19907] - recompile migrations * [DOP-19907] - overwrite rev_id
1 parent 83d8ac0 commit 19b23bf

31 files changed

+972
-773
lines changed

syncmaster/db/migrations/env.py

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
# SPDX-FileCopyrightText: 2023-2024 MTS PJSC
22
# SPDX-License-Identifier: Apache-2.0
33
import asyncio
4+
import os
45
from logging.config import fileConfig
56

67
from alembic import context
8+
from alembic.script import ScriptDirectory
79
from celery.backends.database.session import ResultModelBase
810
from sqlalchemy import pool
911
from sqlalchemy.engine import Connection
1012
from sqlalchemy.ext.asyncio import async_engine_from_config
1113

12-
from syncmaster.db.base import Base
14+
from syncmaster.db.models import Base
1315
from syncmaster.settings import Settings
1416

1517
config = context.config
@@ -29,6 +31,39 @@
2931
)
3032

3133

34+
def get_next_revision_id():
35+
script_directory = ScriptDirectory.from_config(context.config)
36+
versions_path = script_directory.versions
37+
38+
existing_filenames = os.listdir(versions_path)
39+
existing_ids = []
40+
41+
for filename in existing_filenames:
42+
# Assuming filename format: YYYY-MM-DD_XXXX_slug.py
43+
parts = filename.split("_")
44+
if len(parts) >= 2:
45+
id_part = parts[1]
46+
try:
47+
id_num = int(id_part)
48+
existing_ids.append(id_num)
49+
except ValueError:
50+
continue
51+
52+
if existing_ids:
53+
next_id = max(existing_ids) + 1
54+
else:
55+
next_id = 1
56+
57+
return next_id
58+
59+
60+
def process_revision_directives(context, revision, directives):
61+
if directives:
62+
script = directives[0]
63+
next_id = get_next_revision_id()
64+
script.rev_id = f"{next_id:04d}"
65+
66+
3267
def run_migrations_offline() -> None:
3368
"""Run migrations in 'offline' mode.
3469
@@ -46,6 +81,7 @@ def run_migrations_offline() -> None:
4681
url=url,
4782
target_metadata=target_metadata,
4883
literal_binds=True,
84+
process_revision_directives=process_revision_directives,
4985
dialect_opts={"paramstyle": "named"},
5086
)
5187

@@ -54,7 +90,11 @@ def run_migrations_offline() -> None:
5490

5591

5692
def do_run_migrations(connection: Connection) -> None:
57-
context.configure(connection=connection, target_metadata=target_metadata)
93+
context.configure(
94+
connection=connection,
95+
target_metadata=target_metadata,
96+
process_revision_directives=process_revision_directives,
97+
)
5898

5999
with context.begin_transaction():
60100
context.run_migrations()
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# SPDX-FileCopyrightText: 2023-2024 MTS PJSC
2+
# SPDX-License-Identifier: Apache-2.0
3+
"""Create user table
4+
5+
Revision ID: 0001
6+
Revises:
7+
Create Date: 2023-11-23 11:35:18.193060
8+
"""
9+
10+
import sqlalchemy as sa
11+
from alembic import op
12+
13+
# revision identifiers, used by Alembic.
14+
revision = "0001"
15+
down_revision = None
16+
branch_labels = None
17+
depends_on = None
18+
19+
20+
def upgrade():
21+
op.create_table(
22+
"user",
23+
sa.Column("id", sa.BigInteger(), nullable=False),
24+
sa.Column("username", sa.String(length=256), nullable=False),
25+
sa.Column("is_superuser", sa.Boolean(), nullable=False),
26+
sa.Column("is_active", sa.Boolean(), nullable=False),
27+
sa.Column("created_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False),
28+
sa.Column("updated_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False),
29+
sa.Column("is_deleted", sa.Boolean(), nullable=False),
30+
sa.PrimaryKeyConstraint("id", name=op.f("pk__user")),
31+
)
32+
op.create_index(op.f("ix__user__username"), "user", ["username"], unique=True)
33+
34+
35+
def downgrade():
36+
op.drop_index(op.f("ix__user__username"), table_name="user")
37+
op.drop_table("user")
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# SPDX-FileCopyrightText: 2023-2024 MTS PJSC
2+
# SPDX-License-Identifier: Apache-2.0
3+
"""Create group table
4+
5+
Revision ID: 0002
6+
Revises: 0001
7+
Create Date: 2023-11-23 11:36:00.000000
8+
"""
9+
10+
import sqlalchemy as sa
11+
from alembic import op
12+
from sqlalchemy.dialects import postgresql
13+
14+
# revision identifiers, used by Alembic.
15+
revision = "0002"
16+
down_revision = "0001"
17+
branch_labels = None
18+
depends_on = None
19+
20+
21+
def upgrade():
22+
op.create_table(
23+
"group",
24+
sa.Column("id", sa.BigInteger(), nullable=False),
25+
sa.Column("name", sa.String(length=256), nullable=False),
26+
sa.Column("description", sa.String(length=512), nullable=False),
27+
sa.Column("owner_id", sa.BigInteger(), nullable=False),
28+
sa.Column("created_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False),
29+
sa.Column("updated_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False),
30+
sa.Column("is_deleted", sa.Boolean(), nullable=False),
31+
sa.Column(
32+
"search_vector",
33+
postgresql.TSVECTOR(),
34+
sa.Computed("to_tsvector('english'::regconfig, name)", persisted=True),
35+
nullable=False,
36+
),
37+
sa.ForeignKeyConstraint(["owner_id"], ["user.id"], name=op.f("fk__group__owner_id__user"), ondelete="CASCADE"),
38+
sa.PrimaryKeyConstraint("id", name=op.f("pk__group")),
39+
sa.UniqueConstraint("name", name=op.f("uq__group__name")),
40+
)
41+
op.create_index(op.f("ix__group__owner_id"), "group", ["owner_id"], unique=False)
42+
43+
44+
def downgrade():
45+
op.drop_index(op.f("ix__group__owner_id"), table_name="group")
46+
op.drop_table("group")
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# SPDX-FileCopyrightText: 2023-2024 MTS PJSC
2+
# SPDX-License-Identifier: Apache-2.0
3+
"""Create queue table
4+
5+
Revision ID: 0003
6+
Revises: 0002
7+
Create Date: 2023-11-23 11:37:00.000000
8+
"""
9+
10+
import sqlalchemy as sa
11+
from alembic import op
12+
from sqlalchemy.dialects import postgresql
13+
14+
# revision identifiers, used by Alembic.
15+
revision = "0003"
16+
down_revision = "0002"
17+
branch_labels = None
18+
depends_on = None
19+
20+
21+
def upgrade():
22+
op.create_table(
23+
"queue",
24+
sa.Column("name", sa.String(length=128), nullable=False),
25+
sa.Column("id", sa.BigInteger(), nullable=False),
26+
sa.Column("group_id", sa.BigInteger(), nullable=False),
27+
sa.Column("description", sa.String(length=512), nullable=False),
28+
sa.Column("created_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False),
29+
sa.Column("updated_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False),
30+
sa.Column("is_deleted", sa.Boolean(), nullable=False),
31+
sa.Column(
32+
"search_vector",
33+
postgresql.TSVECTOR(),
34+
sa.Computed("to_tsvector('english'::regconfig, name)", persisted=True),
35+
nullable=False,
36+
),
37+
sa.ForeignKeyConstraint(
38+
["group_id"],
39+
["group.id"],
40+
name=op.f("fk__queue__group_id__group"),
41+
ondelete="CASCADE",
42+
),
43+
sa.PrimaryKeyConstraint("id", name=op.f("pk__queue")),
44+
sa.UniqueConstraint("name", name=op.f("uq__queue__name")),
45+
)
46+
op.create_index(op.f("ix__queue__group_id"), "queue", ["group_id"], unique=False)
47+
48+
49+
def downgrade():
50+
op.drop_index(op.f("ix__queue__group_id"), table_name="queue")
51+
op.drop_table("queue")
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# SPDX-FileCopyrightText: 2023-2024 MTS PJSC
2+
# SPDX-License-Identifier: Apache-2.0
3+
"""Create connection table
4+
5+
Revision ID: 0004
6+
Revises: 0003
7+
Create Date: 2023-11-23 11:38:00.000000
8+
"""
9+
10+
import sqlalchemy as sa
11+
from alembic import op
12+
from sqlalchemy.dialects import postgresql
13+
14+
# revision identifiers, used by Alembic.
15+
revision = "0004"
16+
down_revision = "0003"
17+
branch_labels = None
18+
depends_on = None
19+
20+
21+
def upgrade():
22+
sql_expression = (
23+
"to_tsvector('english'::regconfig, "
24+
"name || ' ' || "
25+
"COALESCE(json_extract_path_text(data, 'host'), '') || ' ' || "
26+
"COALESCE(translate(json_extract_path_text(data, 'host'), '.', ' '), '')"
27+
")"
28+
)
29+
op.create_table(
30+
"connection",
31+
sa.Column("id", sa.BigInteger(), nullable=False),
32+
sa.Column("group_id", sa.BigInteger(), nullable=False),
33+
sa.Column("name", sa.String(length=128), nullable=False),
34+
sa.Column("description", sa.String(length=512), nullable=False),
35+
sa.Column("data", sa.JSON(), nullable=False),
36+
sa.Column("is_deleted", sa.Boolean(), nullable=False),
37+
sa.Column("created_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False),
38+
sa.Column("updated_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False),
39+
sa.Column("search_vector", postgresql.TSVECTOR(), sa.Computed(sql_expression, persisted=True), nullable=False),
40+
sa.ForeignKeyConstraint(
41+
["group_id"],
42+
["group.id"],
43+
name=op.f("fk__connection__group_id__group"),
44+
ondelete="CASCADE",
45+
),
46+
sa.PrimaryKeyConstraint("id", name=op.f("pk__connection")),
47+
sa.UniqueConstraint("name", "group_id", name=op.f("uq__connection__name_group_id")),
48+
)
49+
op.create_index(op.f("ix__connection__group_id"), "connection", ["group_id"], unique=False)
50+
op.create_index(
51+
"idx_connection_search_vector",
52+
"connection",
53+
["search_vector"],
54+
unique=False,
55+
postgresql_using="gin",
56+
)
57+
58+
59+
def downgrade():
60+
op.drop_index(op.f("ix__connection__group_id"), table_name="connection")
61+
op.drop_index("idx_connection_search_vector", table_name="connection", postgresql_using="gin")
62+
op.drop_table("connection")
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# SPDX-FileCopyrightText: 2023-2024 MTS PJSC
2+
# SPDX-License-Identifier: Apache-2.0
3+
"""Create user_group table
4+
5+
Revision ID: 0005
6+
Revises: 0004
7+
Create Date: 2023-11-23 11:39:00.000000
8+
"""
9+
10+
import sqlalchemy as sa
11+
from alembic import op
12+
13+
# revision identifiers, used by Alembic.
14+
revision = "0005"
15+
down_revision = "0004"
16+
branch_labels = None
17+
depends_on = None
18+
19+
20+
def upgrade():
21+
op.create_table(
22+
"user_group",
23+
sa.Column("user_id", sa.BigInteger(), nullable=False),
24+
sa.Column("group_id", sa.BigInteger(), nullable=False),
25+
sa.Column("role", sa.String(255), nullable=False),
26+
sa.ForeignKeyConstraint(
27+
["group_id"],
28+
["group.id"],
29+
name=op.f("fk__user_group__group_id__group"),
30+
ondelete="CASCADE",
31+
),
32+
sa.ForeignKeyConstraint(
33+
["user_id"],
34+
["user.id"],
35+
name=op.f("fk__user_group__user_id__user"),
36+
ondelete="CASCADE",
37+
),
38+
sa.PrimaryKeyConstraint("user_id", "group_id", name=op.f("pk__user_group")),
39+
)
40+
op.create_index(op.f("ix__user_group__group_id"), "user_group", ["group_id"], unique=False)
41+
op.create_index(op.f("ix__user_group__user_id"), "user_group", ["user_id"], unique=False)
42+
43+
44+
def downgrade():
45+
op.drop_index(op.f("ix__user_group__user_id"), table_name="user_group")
46+
op.drop_index(op.f("ix__user_group__group_id"), table_name="user_group")
47+
op.drop_table("user_group")
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# SPDX-FileCopyrightText: 2023-2024 MTS PJSC
2+
# SPDX-License-Identifier: Apache-2.0
3+
"""Create auth_data table
4+
5+
Revision ID: 0006
6+
Revises: 0005
7+
Create Date: 2023-11-23 11:40:00.000000
8+
"""
9+
10+
import sqlalchemy as sa
11+
from alembic import op
12+
13+
# revision identifiers, used by Alembic.
14+
revision = "0006"
15+
down_revision = "0005"
16+
branch_labels = None
17+
depends_on = None
18+
19+
20+
def upgrade():
21+
op.create_table(
22+
"auth_data",
23+
sa.Column("connection_id", sa.BigInteger(), nullable=False),
24+
sa.Column("value", sa.String(), nullable=False),
25+
sa.Column("created_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False),
26+
sa.Column("updated_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False),
27+
sa.ForeignKeyConstraint(
28+
["connection_id"],
29+
["connection.id"],
30+
name=op.f("fk__auth_data__connection_id__connection"),
31+
ondelete="CASCADE",
32+
),
33+
sa.PrimaryKeyConstraint("connection_id", name=op.f("pk__auth_data")),
34+
)
35+
36+
37+
def downgrade():
38+
op.drop_table("auth_data")

0 commit comments

Comments
 (0)