refactor(database): use alembic to maintain the whole database
This commit is contained in:
@@ -30,11 +30,6 @@ async def get_db():
|
|||||||
yield session
|
yield session
|
||||||
|
|
||||||
|
|
||||||
async def create_tables():
|
|
||||||
async with engine.begin() as conn:
|
|
||||||
await conn.run_sync(SQLModel.metadata.create_all)
|
|
||||||
|
|
||||||
|
|
||||||
# Redis 依赖
|
# Redis 依赖
|
||||||
def get_redis():
|
def get_redis():
|
||||||
return redis_client
|
return redis_client
|
||||||
|
|||||||
3
main.py
3
main.py
@@ -4,7 +4,7 @@ from contextlib import asynccontextmanager
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
from app.config import settings
|
from app.config import settings
|
||||||
from app.dependencies.database import create_tables, engine, redis_client
|
from app.dependencies.database import engine, redis_client
|
||||||
from app.dependencies.fetcher import get_fetcher
|
from app.dependencies.fetcher import get_fetcher
|
||||||
from app.dependencies.scheduler import init_scheduler, stop_scheduler
|
from app.dependencies.scheduler import init_scheduler, stop_scheduler
|
||||||
from app.router import (
|
from app.router import (
|
||||||
@@ -21,7 +21,6 @@ from fastapi import FastAPI
|
|||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def lifespan(app: FastAPI):
|
async def lifespan(app: FastAPI):
|
||||||
# on startup
|
# on startup
|
||||||
await create_tables()
|
|
||||||
await get_fetcher() # 初始化 fetcher
|
await get_fetcher() # 初始化 fetcher
|
||||||
init_scheduler()
|
init_scheduler()
|
||||||
await daily_challenge_job()
|
await daily_challenge_job()
|
||||||
|
|||||||
@@ -1,40 +0,0 @@
|
|||||||
"""beatmapset: support favourite count
|
|
||||||
|
|
||||||
Revision ID: 1178d0758ebf
|
|
||||||
Revises:
|
|
||||||
Create Date: 2025-08-01 04:05:09.882800
|
|
||||||
|
|
||||||
"""
|
|
||||||
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
from collections.abc import Sequence
|
|
||||||
|
|
||||||
from alembic import op
|
|
||||||
import sqlalchemy as sa
|
|
||||||
from sqlalchemy.dialects import mysql
|
|
||||||
|
|
||||||
# revision identifiers, used by Alembic.
|
|
||||||
revision: str = "1178d0758ebf"
|
|
||||||
down_revision: str | Sequence[str] | None = None
|
|
||||||
branch_labels: str | Sequence[str] | None = None
|
|
||||||
depends_on: str | Sequence[str] | None = None
|
|
||||||
|
|
||||||
|
|
||||||
def upgrade() -> None:
|
|
||||||
"""Upgrade schema."""
|
|
||||||
# ### commands auto generated by Alembic - please adjust! ###
|
|
||||||
op.drop_column("beatmapsets", "favourite_count")
|
|
||||||
# ### end Alembic commands ###
|
|
||||||
|
|
||||||
|
|
||||||
def downgrade() -> None:
|
|
||||||
"""Downgrade schema."""
|
|
||||||
# ### commands auto generated by Alembic - please adjust! ###
|
|
||||||
op.add_column(
|
|
||||||
"beatmapsets",
|
|
||||||
sa.Column(
|
|
||||||
"favourite_count", mysql.INTEGER(), autoincrement=False, nullable=False
|
|
||||||
),
|
|
||||||
)
|
|
||||||
# ### end Alembic commands ###
|
|
||||||
@@ -1,54 +0,0 @@
|
|||||||
"""relationship: fix unique relationship
|
|
||||||
|
|
||||||
Revision ID: 58a11441d302
|
|
||||||
Revises: 1178d0758ebf
|
|
||||||
Create Date: 2025-08-01 04:23:02.498166
|
|
||||||
|
|
||||||
"""
|
|
||||||
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
from collections.abc import Sequence
|
|
||||||
|
|
||||||
from alembic import op
|
|
||||||
import sqlalchemy as sa
|
|
||||||
from sqlalchemy.dialects import mysql
|
|
||||||
|
|
||||||
# revision identifiers, used by Alembic.
|
|
||||||
revision: str = "58a11441d302"
|
|
||||||
down_revision: str | Sequence[str] | None = "1178d0758ebf"
|
|
||||||
branch_labels: str | Sequence[str] | None = None
|
|
||||||
depends_on: str | Sequence[str] | None = None
|
|
||||||
|
|
||||||
|
|
||||||
def upgrade() -> None:
|
|
||||||
"""Upgrade schema."""
|
|
||||||
# ### commands auto generated by Alembic - please adjust! ###
|
|
||||||
op.add_column(
|
|
||||||
"relationship",
|
|
||||||
sa.Column("id", sa.BigInteger(), autoincrement=True, nullable=False),
|
|
||||||
)
|
|
||||||
op.drop_constraint("PRIMARY", "relationship", type_="primary")
|
|
||||||
op.create_primary_key("pk_relationship", "relationship", ["id"])
|
|
||||||
op.alter_column(
|
|
||||||
"relationship", "user_id", existing_type=mysql.BIGINT(), nullable=True
|
|
||||||
)
|
|
||||||
op.alter_column(
|
|
||||||
"relationship", "target_id", existing_type=mysql.BIGINT(), nullable=True
|
|
||||||
)
|
|
||||||
# ### end Alembic commands ###
|
|
||||||
|
|
||||||
|
|
||||||
def downgrade() -> None:
|
|
||||||
"""Downgrade schema."""
|
|
||||||
# ### commands auto generated by Alembic - please adjust! ###
|
|
||||||
op.drop_constraint("pk_relationship", "relationship", type_="primary")
|
|
||||||
op.create_primary_key("PRIMARY", "relationship", ["user_id", "target_id"])
|
|
||||||
op.alter_column(
|
|
||||||
"relationship", "target_id", existing_type=mysql.BIGINT(), nullable=False
|
|
||||||
)
|
|
||||||
op.alter_column(
|
|
||||||
"relationship", "user_id", existing_type=mysql.BIGINT(), nullable=False
|
|
||||||
)
|
|
||||||
op.drop_column("relationship", "id")
|
|
||||||
# ### end Alembic commands ###
|
|
||||||
@@ -1,89 +0,0 @@
|
|||||||
"""playlist: index playlist id
|
|
||||||
|
|
||||||
Revision ID: d0c1b2cefe91
|
|
||||||
Revises: 58a11441d302
|
|
||||||
Create Date: 2025-08-06 06:02:10.512616
|
|
||||||
|
|
||||||
"""
|
|
||||||
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
from collections.abc import Sequence
|
|
||||||
|
|
||||||
from alembic import op
|
|
||||||
import sqlalchemy as sa
|
|
||||||
|
|
||||||
# revision identifiers, used by Alembic.
|
|
||||||
revision: str = "d0c1b2cefe91"
|
|
||||||
down_revision: str | Sequence[str] | None = "58a11441d302"
|
|
||||||
branch_labels: str | Sequence[str] | None = None
|
|
||||||
depends_on: str | Sequence[str] | None = None
|
|
||||||
|
|
||||||
|
|
||||||
def upgrade() -> None:
|
|
||||||
"""Upgrade schema."""
|
|
||||||
# ### commands auto generated by Alembic - please adjust! ###
|
|
||||||
op.create_index(
|
|
||||||
op.f("ix_room_playlists_id"), "room_playlists", ["id"], unique=False
|
|
||||||
)
|
|
||||||
op.create_table(
|
|
||||||
"playlist_best_scores",
|
|
||||||
sa.Column("user_id", sa.BigInteger(), nullable=True),
|
|
||||||
sa.Column("score_id", sa.BigInteger(), nullable=False),
|
|
||||||
sa.Column("room_id", sa.Integer(), nullable=False),
|
|
||||||
sa.Column("playlist_id", sa.Integer(), nullable=False),
|
|
||||||
sa.Column("total_score", sa.BigInteger(), nullable=True),
|
|
||||||
sa.ForeignKeyConstraint(
|
|
||||||
["playlist_id"],
|
|
||||||
["room_playlists.id"],
|
|
||||||
),
|
|
||||||
sa.ForeignKeyConstraint(
|
|
||||||
["room_id"],
|
|
||||||
["rooms.id"],
|
|
||||||
),
|
|
||||||
sa.ForeignKeyConstraint(
|
|
||||||
["score_id"],
|
|
||||||
["scores.id"],
|
|
||||||
),
|
|
||||||
sa.ForeignKeyConstraint(
|
|
||||||
["user_id"],
|
|
||||||
["lazer_users.id"],
|
|
||||||
),
|
|
||||||
sa.PrimaryKeyConstraint("score_id"),
|
|
||||||
)
|
|
||||||
op.create_index(
|
|
||||||
op.f("ix_playlist_best_scores_playlist_id"),
|
|
||||||
"playlist_best_scores",
|
|
||||||
["playlist_id"],
|
|
||||||
unique=False,
|
|
||||||
)
|
|
||||||
op.create_index(
|
|
||||||
op.f("ix_playlist_best_scores_room_id"),
|
|
||||||
"playlist_best_scores",
|
|
||||||
["room_id"],
|
|
||||||
unique=False,
|
|
||||||
)
|
|
||||||
op.create_index(
|
|
||||||
op.f("ix_playlist_best_scores_user_id"),
|
|
||||||
"playlist_best_scores",
|
|
||||||
["user_id"],
|
|
||||||
unique=False,
|
|
||||||
)
|
|
||||||
# ### end Alembic commands ###
|
|
||||||
|
|
||||||
|
|
||||||
def downgrade() -> None:
|
|
||||||
"""Downgrade schema."""
|
|
||||||
# ### commands auto generated by Alembic - please adjust! ###
|
|
||||||
op.drop_index(
|
|
||||||
op.f("ix_playlist_best_scores_user_id"), table_name="playlist_best_scores"
|
|
||||||
)
|
|
||||||
op.drop_index(
|
|
||||||
op.f("ix_playlist_best_scores_room_id"), table_name="playlist_best_scores"
|
|
||||||
)
|
|
||||||
op.drop_index(
|
|
||||||
op.f("ix_playlist_best_scores_playlist_id"), table_name="playlist_best_scores"
|
|
||||||
)
|
|
||||||
op.drop_table("playlist_best_scores")
|
|
||||||
op.drop_index(op.f("ix_room_playlists_id"), table_name="room_playlists")
|
|
||||||
# ### end Alembic commands ###
|
|
||||||
1069
migrations/versions/fdb3822a30ba_init.py
Normal file
1069
migrations/versions/fdb3822a30ba_init.py
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user