-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e000fd2
commit 36729d3
Showing
19 changed files
with
275 additions
and
176 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,90 @@ | ||
from logging.config import fileConfig | ||
|
||
from alembic import context | ||
from sqlalchemy import engine_from_config | ||
from sqlalchemy import pool | ||
from sqlalchemy import pool, Connection | ||
from sqlalchemy.ext.asyncio import AsyncEngine | ||
import asyncio | ||
from src import config as app_config | ||
from alembic import context | ||
|
||
from src.db import Base # noqa: F401 | ||
|
||
# this is the Alembic Config object, which provides | ||
# access to the values within the .ini file in use. | ||
config = context.config | ||
|
||
# Interpret the config file for Python logging. | ||
# This line sets up loggers basically. | ||
if config.config_file_name is not None: | ||
fileConfig(config.config_file_name) | ||
fileConfig(config.config_file_name) # PGH003 | ||
|
||
# add your model's MetaData object here | ||
# for 'autogenerate' support | ||
# from myapp import mymodel | ||
# target_metadata = mymodel.Base.metadata | ||
target_metadata = None | ||
from src.db import Base # noqa: E402 | ||
|
||
target_metadata = Base.metadata | ||
|
||
# other values from the config, defined by the needs of env.py, | ||
# can be acquired: | ||
# my_important_option = config.get_main_option("my_important_option") | ||
# ... etc. | ||
|
||
|
||
def get_database_uri() -> str: | ||
return app_config.settings.SQLALCHEMY_DATABASE_URI | ||
|
||
|
||
def run_migrations_offline() -> None: | ||
"""Run migrations in 'offline' mode. | ||
This configures the context with just a URL | ||
and not an Engine, though an Engine is acceptable | ||
here as well. By skipping the Engine creation | ||
we don't even need a DBAPI to be available. | ||
Calls to context.execute() here emit the given string to the | ||
script output. | ||
""" | ||
url = config.get_main_option("sqlalchemy.url") | ||
url = get_database_uri() | ||
context.configure( | ||
url=url, | ||
target_metadata=target_metadata, | ||
literal_binds=True, | ||
dialect_opts={"paramstyle": "named"}, | ||
compare_type=True, | ||
compare_server_default=True, | ||
) | ||
|
||
with context.begin_transaction(): | ||
context.run_migrations() | ||
|
||
|
||
def run_migrations_online() -> None: | ||
"""Run migrations in 'online' mode. | ||
def do_run_migrations(connection: Connection) -> None: | ||
context.configure(connection=connection, target_metadata=target_metadata, compare_type=True) | ||
|
||
with context.begin_transaction(): | ||
context.run_migrations() | ||
|
||
|
||
async def run_migrations_online() -> None: | ||
"""Run migrations in 'online' mode. | ||
In this scenario we need to create an Engine | ||
and associate a connection with the context. | ||
""" | ||
connectable = engine_from_config( | ||
config.get_section(config.config_ini_section, {}), | ||
prefix="sqlalchemy.", | ||
poolclass=pool.NullPool, | ||
configuration = config.get_section(config.config_ini_section) | ||
assert configuration # noqa: S101 | ||
configuration["sqlalchemy.url"] = get_database_uri() | ||
connectable = AsyncEngine( | ||
engine_from_config( | ||
configuration, | ||
prefix="sqlalchemy.", | ||
poolclass=pool.NullPool, | ||
future=True, | ||
) | ||
) | ||
|
||
with connectable.connect() as connection: | ||
context.configure(connection=connection, target_metadata=target_metadata) | ||
|
||
with context.begin_transaction(): | ||
context.run_migrations() | ||
async with connectable.connect() as connection: | ||
await connection.run_sync(do_run_migrations) | ||
|
||
|
||
if context.is_offline_mode(): | ||
run_migrations_offline() | ||
else: | ||
run_migrations_online() | ||
asyncio.run(run_migrations_online()) |
145 changes: 145 additions & 0 deletions
145
alembic/versions/2024_01_20_1433-a72f51adf94a_init_db.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
"""init db | ||
Revision ID: a72f51adf94a | ||
Revises: | ||
Create Date: 2024-01-20 14:33:41.546660 | ||
""" | ||
from collections.abc import Sequence | ||
|
||
import sqlalchemy as sa | ||
from sqlalchemy.dialects import postgresql | ||
|
||
from alembic import op | ||
|
||
# revision identifiers, used by Alembic. | ||
revision: str = "a72f51adf94a" | ||
down_revision: str | None = None | ||
branch_labels: str | Sequence[str] | None = None | ||
depends_on: str | Sequence[str] | None = None | ||
|
||
|
||
def upgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_table( | ||
"menu", | ||
sa.Column("id", sa.Integer(), nullable=False), | ||
sa.Column("name", sa.String(), nullable=False, comment="the unique name of route"), | ||
sa.Column("hidden", sa.Boolean(), server_default=sa.text("false"), nullable=False), | ||
sa.Column("redirect", sa.String(), nullable=False, comment="redirect url for the route"), | ||
sa.Column( | ||
"hideChildrenInMenu", | ||
sa.Boolean(), | ||
server_default=sa.text("false"), | ||
nullable=False, | ||
comment="hide children in menu force or not", | ||
), | ||
sa.Column("order", sa.Integer(), nullable=False), | ||
sa.Column("title", sa.String(), nullable=False, comment="the title of the route, 面包屑"), | ||
sa.Column("icon", sa.String(), nullable=True), | ||
sa.Column( | ||
"keepAlive", | ||
sa.Boolean(), | ||
server_default=sa.text("false"), | ||
nullable=False, | ||
comment="cache route, 开启multi-tab时为true", | ||
), | ||
sa.Column( | ||
"hiddenHeaderContent", | ||
sa.Boolean(), | ||
server_default=sa.text("false"), | ||
nullable=False, | ||
comment="隐藏pageheader页面带的面包屑和标题栏", | ||
), | ||
sa.Column("permission", postgresql.ARRAY(sa.Integer(), dimensions=1), nullable=True), | ||
sa.Column("parent_id", sa.Integer(), nullable=True), | ||
sa.ForeignKeyConstraint(["parent_id"], ["menu.id"], ondelete="CASCADE"), | ||
sa.PrimaryKeyConstraint("id"), | ||
sa.UniqueConstraint("name"), | ||
) | ||
op.create_table( | ||
"permission", | ||
sa.Column("id", sa.UUID(), nullable=False), | ||
sa.Column("name", sa.String(), nullable=False), | ||
sa.Column("url", sa.String(), nullable=False), | ||
sa.Column("method", sa.String(), nullable=False), | ||
sa.Column("tag", sa.String(), nullable=False), | ||
sa.PrimaryKeyConstraint("id"), | ||
) | ||
op.create_table( | ||
"role", | ||
sa.Column("id", sa.Integer(), nullable=False), | ||
sa.Column("name", sa.String(), nullable=False), | ||
sa.Column("slug", sa.String(), nullable=False), | ||
sa.Column("description", sa.String(), nullable=True), | ||
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False), | ||
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False), | ||
sa.PrimaryKeyConstraint("id"), | ||
) | ||
op.create_index(op.f("ix_role_created_at"), "role", ["created_at"], unique=False) | ||
op.create_table( | ||
"group", | ||
sa.Column("id", sa.Integer(), nullable=False), | ||
sa.Column("name", sa.String(), nullable=False), | ||
sa.Column("description", sa.String(), nullable=True), | ||
sa.Column("role_id", sa.Integer(), nullable=False), | ||
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False), | ||
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False), | ||
sa.ForeignKeyConstraint(["role_id"], ["role.id"], ondelete="CASCADE"), | ||
sa.PrimaryKeyConstraint("id"), | ||
) | ||
op.create_index(op.f("ix_group_created_at"), "group", ["created_at"], unique=False) | ||
op.create_table( | ||
"role_menu", | ||
sa.Column("role_id", sa.Integer(), nullable=False), | ||
sa.Column("menu_id", sa.Integer(), nullable=False), | ||
sa.ForeignKeyConstraint(["menu_id"], ["menu.id"]), | ||
sa.ForeignKeyConstraint(["role_id"], ["role.id"]), | ||
sa.PrimaryKeyConstraint("role_id", "menu_id"), | ||
) | ||
op.create_table( | ||
"role_permission", | ||
sa.Column("role_id", sa.Integer(), nullable=False), | ||
sa.Column("permission_id", sa.UUID(), nullable=False), | ||
sa.ForeignKeyConstraint(["permission_id"], ["permission.id"]), | ||
sa.ForeignKeyConstraint(["role_id"], ["role.id"]), | ||
sa.PrimaryKeyConstraint("role_id", "permission_id"), | ||
) | ||
op.create_table( | ||
"user", | ||
sa.Column("id", sa.Integer(), nullable=False), | ||
sa.Column("name", sa.String(), nullable=False), | ||
sa.Column("email", sa.String(), nullable=True), | ||
sa.Column("phone", sa.String(), nullable=True), | ||
sa.Column("password", sa.String(), nullable=False), | ||
sa.Column("avatar", sa.String(), nullable=True), | ||
sa.Column("last_login", sa.DateTime(timezone=True), nullable=True), | ||
sa.Column("is_active", sa.Boolean(), server_default=sa.text("true"), nullable=False), | ||
sa.Column("group_id", sa.Integer(), nullable=False), | ||
sa.Column("role_id", sa.Integer(), nullable=False), | ||
sa.Column("auth_info", postgresql.JSON(astext_type=sa.Text()), nullable=True), | ||
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False), | ||
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False), | ||
sa.ForeignKeyConstraint(["group_id"], ["group.id"], ondelete="CASCADE"), | ||
sa.ForeignKeyConstraint(["role_id"], ["role.id"], ondelete="CASCADE"), | ||
sa.PrimaryKeyConstraint("id"), | ||
sa.UniqueConstraint("email"), | ||
sa.UniqueConstraint("phone"), | ||
) | ||
op.create_index(op.f("ix_user_created_at"), "user", ["created_at"], unique=False) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_index(op.f("ix_user_created_at"), table_name="user") | ||
op.drop_table("user") | ||
op.drop_table("role_permission") | ||
op.drop_table("role_menu") | ||
op.drop_index(op.f("ix_group_created_at"), table_name="group") | ||
op.drop_table("group") | ||
op.drop_index(op.f("ix_role_created_at"), table_name="role") | ||
op.drop_table("role") | ||
op.drop_table("permission") | ||
op.drop_table("menu") | ||
# ### end Alembic commands ### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.