Skip to content

Commit

Permalink
updated readme and alembic script
Browse files Browse the repository at this point in the history
  • Loading branch information
Furrior committed Feb 28, 2025
1 parent fe81905 commit e45be0b
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 10 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ API для объеденения множества серверов SS13 и SS
Конфиг берется по пути `./.config.toml`. Пример можно посмотреть в файле `./config_example.toml`.
Конфиг логов берется по пути `./log_config.yaml`.

## БД

Для обновления ревизии бд используется

```sh
alembic upgrade head
```

## Запуск

```sh
Expand Down
5 changes: 2 additions & 3 deletions alembic/env.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
from logging.config import fileConfig
import os

from sqlalchemy import engine_from_config
from sqlalchemy import pool

from alembic import context

# this is the Alembic Config object, which provides
Expand All @@ -28,6 +25,8 @@
# ... etc.
from app.core.db import engine
db_url = os.getenv("DB_URL") # Read from environment
if db_url is None:
raise ValueError("DB_URL env var is not set")
config.set_main_option("sqlalchemy.url", db_url)


Expand Down
111 changes: 111 additions & 0 deletions alembic/versions/5024705f5bb5_initial_migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
"""Initial migration
Revision ID: 5024705f5bb5
Revises:
Create Date: 2025-02-28 16:30:04.602809
"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
import sqlmodel

# revision identifiers, used by Alembic.
revision: str = '5024705f5bb5'
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('api_auth',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('token_hash', sqlmodel.sql.sqltypes.AutoString(length=64), nullable=False),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_api_auth_token_hash'), 'api_auth', ['token_hash'], unique=True)
op.create_table('ckey_link_token',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('ckey', sqlmodel.sql.sqltypes.AutoString(length=32), nullable=False),
sa.Column('token', sqlmodel.sql.sqltypes.AutoString(length=64), nullable=False),
sa.Column('expiration_time', sa.DateTime(), nullable=False),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_ckey_link_token_ckey'), 'ckey_link_token', ['ckey'], unique=True)
op.create_index(op.f('ix_ckey_link_token_token'), 'ckey_link_token', ['token'], unique=True)
op.create_table('player',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('discord_id', sqlmodel.sql.sqltypes.AutoString(length=32), nullable=False),
sa.Column('ckey', sqlmodel.sql.sqltypes.AutoString(length=32), nullable=True),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_player_ckey'), 'player', ['ckey'], unique=True)
op.create_index(op.f('ix_player_discord_id'), 'player', ['discord_id'], unique=True)
op.create_table('donation',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('player_id', sa.Integer(), nullable=False),
sa.Column('tier', sa.Integer(), nullable=False),
sa.Column('issue_time', sa.DateTime(), nullable=False),
sa.Column('expiration_time', sa.DateTime(), nullable=False),
sa.Column('valid', sa.Boolean(), nullable=False),
sa.ForeignKeyConstraint(['player_id'], ['player.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_donation_player_id'), 'donation', ['player_id'], unique=False)
op.create_table('whitelist',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('player_id', sa.Integer(), nullable=False),
sa.Column('server_type', sqlmodel.sql.sqltypes.AutoString(length=32), nullable=False),
sa.Column('admin_id', sa.Integer(), nullable=False),
sa.Column('issue_time', sa.DateTime(), nullable=False),
sa.Column('expiration_time', sa.DateTime(), nullable=False),
sa.Column('valid', sa.Boolean(), nullable=False),
sa.ForeignKeyConstraint(['admin_id'], ['player.id'], ),
sa.ForeignKeyConstraint(['player_id'], ['player.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_whitelist_player_id'), 'whitelist', ['player_id'], unique=False)
op.create_index(op.f('ix_whitelist_server_type'), 'whitelist', ['server_type'], unique=False)
op.create_index(op.f('ix_whitelist_valid'), 'whitelist', ['valid'], unique=False)
op.create_table('whitelist_ban',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('player_id', sa.Integer(), nullable=False),
sa.Column('server_type', sqlmodel.sql.sqltypes.AutoString(length=32), nullable=False),
sa.Column('admin_id', sa.Integer(), nullable=False),
sa.Column('issue_time', sa.DateTime(), nullable=False),
sa.Column('expiration_time', sa.DateTime(), nullable=False),
sa.Column('valid', sa.Boolean(), nullable=False),
sa.Column('reason', sqlmodel.sql.sqltypes.AutoString(length=1024), nullable=True),
sa.ForeignKeyConstraint(['admin_id'], ['player.id'], ),
sa.ForeignKeyConstraint(['player_id'], ['player.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_whitelist_ban_player_id'), 'whitelist_ban', ['player_id'], unique=False)
op.create_index(op.f('ix_whitelist_ban_server_type'), 'whitelist_ban', ['server_type'], unique=False)
op.create_index(op.f('ix_whitelist_ban_valid'), 'whitelist_ban', ['valid'], unique=False)
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index(op.f('ix_whitelist_ban_valid'), table_name='whitelist_ban')
op.drop_index(op.f('ix_whitelist_ban_server_type'), table_name='whitelist_ban')
op.drop_index(op.f('ix_whitelist_ban_player_id'), table_name='whitelist_ban')
op.drop_table('whitelist_ban')
op.drop_index(op.f('ix_whitelist_valid'), table_name='whitelist')
op.drop_index(op.f('ix_whitelist_server_type'), table_name='whitelist')
op.drop_index(op.f('ix_whitelist_player_id'), table_name='whitelist')
op.drop_table('whitelist')
op.drop_index(op.f('ix_donation_player_id'), table_name='donation')
op.drop_table('donation')
op.drop_index(op.f('ix_player_discord_id'), table_name='player')
op.drop_index(op.f('ix_player_ckey'), table_name='player')
op.drop_table('player')
op.drop_index(op.f('ix_ckey_link_token_token'), table_name='ckey_link_token')
op.drop_index(op.f('ix_ckey_link_token_ckey'), table_name='ckey_link_token')
op.drop_table('ckey_link_token')
op.drop_index(op.f('ix_api_auth_token_hash'), table_name='api_auth')
op.drop_table('api_auth')
# ### end Alembic commands ###
2 changes: 1 addition & 1 deletion app/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ class Database(CustomBaseModel):
pool_recycle: int = 3600
pool_pre_ping: bool = True

needs_rebuild: bool = True

class Redis(CustomBaseModel):
connection_string: str = "redis://127.0.0.1:6379/0"


class OAuth(CustomBaseModel):
# From discord app's settings
client_secret: str = "12345678"
Expand Down
5 changes: 1 addition & 4 deletions app/core/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,4 @@


def init_db() -> None:
if not CONFIG.database.needs_rebuild:
return

SQLModel.metadata.create_all(engine)
return
2 changes: 0 additions & 2 deletions config_example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ pool_size = 10
overflow = 5
pool_recycle = 3600
pool_pre_ping = true
# Creates tables if they don't exist
needs_rebuild = true

[redis]
connection_string = "redis://default:user@hostname:6379"
Expand Down

0 comments on commit e45be0b

Please sign in to comment.