Persist servers in separate database

This commit is contained in:
ShadowNinja
2021-06-13 16:06:20 -04:00
parent 12ed8aff60
commit 2441905511
26 changed files with 1386 additions and 640 deletions

40
migrations/alembic.ini Normal file
View File

@@ -0,0 +1,40 @@
[alembic]
[loggers]
keys = root,sqlalchemy,alembic,flask_migrate
[handlers]
keys = console
[formatters]
keys = generic
[logger_root]
level = WARN
handlers = console
qualname =
[logger_sqlalchemy]
level = WARN
handlers =
qualname = sqlalchemy.engine
[logger_alembic]
level = INFO
handlers =
qualname = alembic
[logger_flask_migrate]
level = INFO
handlers =
qualname = flask_migrate
[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic
[formatter_generic]
format = %(levelname)-5.5s [%(name)s] %(message)s
datefmt = %H:%M:%S

76
migrations/env.py Normal file
View File

@@ -0,0 +1,76 @@
from __future__ import with_statement
import logging
from logging.config import fileConfig
from flask import current_app
from alembic import context
config = context.config
fileConfig(config.config_file_name)
logger = logging.getLogger('alembic.env')
config.set_main_option(
'sqlalchemy.url',
str(current_app.extensions['migrate'].db.get_engine().url).replace('%', '%%'))
target_metadata = current_app.extensions['migrate'].db.metadata
def run_migrations_offline():
"""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")
context.configure(
url=url, target_metadata=target_metadata, literal_binds=True
)
with context.begin_transaction():
context.run_migrations()
def run_migrations_online():
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine
and associate a connection with the context.
"""
# this callback is used to prevent an auto-migration from being generated
# when there are no changes to the schema
# reference: http://alembic.zzzcomputing.com/en/latest/cookbook.html
def process_revision_directives(context, revision, directives):
if getattr(config.cmd_opts, 'autogenerate', False):
script = directives[0]
if script.upgrade_ops.is_empty():
directives[:] = []
logger.info('No changes in schema detected.')
connectable = current_app.extensions['migrate'].db.get_engine()
with connectable.connect() as connection:
context.configure(
connection=connection,
target_metadata=target_metadata,
process_revision_directives=process_revision_directives,
**current_app.extensions['migrate'].configure_args
)
with context.begin_transaction():
context.run_migrations()
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()

22
migrations/script.py.mako Normal file
View File

@@ -0,0 +1,22 @@
"""${message}
Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date.date()}
"""
from alembic import op
import sqlalchemy as sa
${imports if imports else ""}
revision = ${repr(up_revision)}
down_revision = ${repr(down_revision)}
branch_labels = ${repr(branch_labels)}
depends_on = ${repr(depends_on)}
def upgrade():
${upgrades if upgrades else "pass"}
def downgrade():
${downgrades if downgrades else "pass"}

View File

@@ -0,0 +1,73 @@
"""Initial migration
Revision ID: 00ac5d537063
Create Date: 2021-06-12
"""
from alembic import op
import sqlalchemy as sa
revision = '00ac5d537063'
down_revision = None
branch_labels = None
depends_on = None
def upgrade():
op.create_table('server',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('world_uuid', sa.String(length=36), nullable=True),
sa.Column('online', sa.Boolean(), nullable=False),
sa.Column('address', sa.String(), nullable=False),
sa.Column('port', sa.Integer(), nullable=False),
sa.Column('announce_ip', sa.String(), nullable=False),
sa.Column('server_id', sa.String(), nullable=True),
sa.Column('clients', sa.String(), nullable=True),
sa.Column('clients_top', sa.Integer(), nullable=False),
sa.Column('clients_max', sa.Integer(), nullable=False),
sa.Column('first_seen', sa.DateTime(), nullable=False),
sa.Column('start_time', sa.DateTime(), nullable=False),
sa.Column('last_update', sa.DateTime(), nullable=False),
sa.Column('total_uptime', sa.Float(), nullable=False),
sa.Column('down_time', sa.DateTime(), nullable=True),
sa.Column('game_time', sa.Integer(), nullable=False),
sa.Column('lag', sa.Float(), nullable=True),
sa.Column('ping', sa.Float(), nullable=False),
sa.Column('mods', sa.String(), nullable=True),
sa.Column('version', sa.String(), nullable=False),
sa.Column('proto_min', sa.Integer(), nullable=False),
sa.Column('proto_max', sa.Integer(), nullable=False),
sa.Column('game_id', sa.String(), nullable=False),
sa.Column('mapgen', sa.String(), nullable=True),
sa.Column('url', sa.String(), nullable=True),
sa.Column('default_privs', sa.String(), nullable=True),
sa.Column('name', sa.String(), nullable=False),
sa.Column('description', sa.String(), nullable=False),
sa.Column('popularity', sa.Float(), nullable=False),
sa.Column('geo_continent', sa.String(length=2), nullable=True),
sa.Column('creative', sa.Boolean(), nullable=False),
sa.Column('is_dedicated', sa.Boolean(), nullable=False),
sa.Column('damage_enabled', sa.Boolean(), nullable=False),
sa.Column('pvp_enabled', sa.Boolean(), nullable=False),
sa.Column('password_required', sa.Boolean(), nullable=False),
sa.Column('rollback_enabled', sa.Boolean(), nullable=False),
sa.Column('can_see_far_names', sa.Boolean(), nullable=False),
sa.PrimaryKeyConstraint('id')
)
op.create_index('ix_server_address_port', 'server', ['address', 'port'], unique=True)
op.create_index(op.f('ix_server_online'), 'server', ['online'], unique=False)
op.create_index(op.f('ix_server_world_uuid'), 'server', ['world_uuid'], unique=True)
op.create_table('stats',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('max_servers', sa.Integer(), nullable=False),
sa.Column('max_clients', sa.Integer(), nullable=False),
sa.PrimaryKeyConstraint('id')
)
def downgrade():
op.drop_table('stats')
op.drop_index(op.f('ix_server_world_uuid'), table_name='server')
op.drop_index(op.f('ix_server_online'), table_name='server')
op.drop_index('ix_server_address_port', table_name='server')
op.drop_table('server')