Revert migrations

This commit is contained in:
Andrew 2022-01-27 21:53:07 -05:00
parent 42062004a2
commit 45ab07eb77
4 changed files with 51 additions and 31 deletions

View File

@ -24,7 +24,7 @@ ignore=
# Add files or directories matching the regex patterns to the ignore-list. The # Add files or directories matching the regex patterns to the ignore-list. The
# regex matches against paths and can be in Posix or Windows format. # regex matches against paths and can be in Posix or Windows format.
ignore-paths=app/migrations ignore-paths=app/migrations, app/shared/migration.py
# Files or directories matching the regex patterns are skipped. The regex # Files or directories matching the regex patterns are skipped. The regex
# matches against base names, not paths. # matches against base names, not paths.

View File

@ -5,7 +5,7 @@ import sys
import os import os
import re import re
from functools import wraps from functools import wraps
# pylint: disable=no-name-in-module
from functools import cached_property from functools import cached_property
from app.classes.shared.helpers import helper from app.classes.shared.helpers import helper
@ -22,8 +22,9 @@ try:
) )
except ModuleNotFoundError as e: except ModuleNotFoundError as e:
logger.critical(f"Import Error: Unable to load {e.name} module", exc_info=True) logger.critical("Import Error: Unable to load {} module".format(
console.critical(f"Import Error: Unable to load {e.name} module") e.name), exc_info=True)
console.critical("Import Error: Unable to load {} module".format(e.name))
sys.exit(1) sys.exit(1)
MIGRATE_TABLE = 'migratehistory' MIGRATE_TABLE = 'migratehistory'
@ -76,7 +77,7 @@ def get_model(method):
# noinspection PyProtectedMember # noinspection PyProtectedMember
class Migrator(): class Migrator(object):
def __init__(self, database: t.Union[peewee.Database, peewee.Proxy]): def __init__(self, database: t.Union[peewee.Database, peewee.Proxy]):
""" """
Initializes the migrator Initializes the migrator
@ -103,7 +104,7 @@ class Migrator():
""" """
Cleans the operations. Cleans the operations.
""" """
self.operations = [] self.operations = list()
def sql(self, sql: str, *params): def sql(self, sql: str, *params):
""" """
@ -277,7 +278,7 @@ class Migrator():
# noinspection PyProtectedMember # noinspection PyProtectedMember
class MigrationManager(): class MigrationManager(object):
filemask = re.compile(r"[\d]+_[^\.]+\.py$") filemask = re.compile(r"[\d]+_[^\.]+\.py$")
def __init__(self, database: t.Union[peewee.Database, peewee.Proxy]): def __init__(self, database: t.Union[peewee.Database, peewee.Proxy]):
@ -285,7 +286,7 @@ class MigrationManager():
Initializes the migration manager. Initializes the migration manager.
""" """
if not isinstance(database, (peewee.Database, peewee.Proxy)): if not isinstance(database, (peewee.Database, peewee.Proxy)):
raise RuntimeError(f'Invalid database: {database}') raise RuntimeError('Invalid database: {}'.format(database))
self.database = database self.database = database
@cached_property @cached_property
@ -293,7 +294,6 @@ class MigrationManager():
""" """
Initialize and cache the MigrationHistory model. Initialize and cache the MigrationHistory model.
""" """
#pylint: disable=no-member
MigrateHistory._meta.database = self.database MigrateHistory._meta.database = self.database
MigrateHistory._meta.table_name = 'migratehistory' MigrateHistory._meta.table_name = 'migratehistory'
MigrateHistory._meta.schema = None MigrateHistory._meta.schema = None
@ -305,7 +305,6 @@ class MigrationManager():
""" """
Scans migrations in the database. Scans migrations in the database.
""" """
# pylint: disable=no-member
return [mm.name for mm in self.model.select().order_by(self.model.id)] return [mm.name for mm in self.model.select().order_by(self.model.id)]
@property @property
@ -314,7 +313,8 @@ class MigrationManager():
Scans migrations in the file system. Scans migrations in the file system.
""" """
if not os.path.exists(helper.migration_dir): if not os.path.exists(helper.migration_dir):
logger.warning(f'Migration directory: {helper.migration_dir} does not exist.') logger.warning('Migration directory: {} does not exist.'.format(
helper.migration_dir))
os.makedirs(helper.migration_dir) os.makedirs(helper.migration_dir)
return sorted(f[:-3] for f in os.listdir(helper.migration_dir) if self.filemask.match(f)) return sorted(f[:-3] for f in os.listdir(helper.migration_dir) if self.filemask.match(f))
@ -343,7 +343,7 @@ class MigrationManager():
name = datetime.utcnow().strftime('%Y%m%d%H%M%S') + '_' + name name = datetime.utcnow().strftime('%Y%m%d%H%M%S') + '_' + name
filename = name + '.py' filename = name + '.py'
path = os.path.join(helper.migration_dir, filename) path = os.path.join(helper.migration_dir, filename)
with open(path, 'w', encoding='utf-8') as f: with open(path, 'w') as f:
f.write(MIGRATE_TEMPLATE.format( f.write(MIGRATE_TEMPLATE.format(
migrate=migrate, rollback=rollback, name=filename)) migrate=migrate, rollback=rollback, name=filename))
@ -357,14 +357,13 @@ class MigrationManager():
if auto: if auto:
raise NotImplementedError raise NotImplementedError
logger.info(f'Creating migration "{name}"') logger.info('Creating migration "{}"'.format(name))
name = self.compile(name, migrate, rollback) name = self.compile(name, migrate, rollback)
logger.info(f'Migration has been created as "{name}"') logger.info('Migration has been created as "{}"'.format(name))
return name return name
def clear(self): def clear(self):
"""Clear migrations.""" """Clear migrations."""
# pylint: disable=no-member
self.model.delete().execute() self.model.delete().execute()
def up(self, name: t.Optional[str] = None): def up(self, name: t.Optional[str] = None):
@ -381,6 +380,7 @@ class MigrationManager():
console.info('There is nothing to migrate') console.info('There is nothing to migrate')
return done return done
migrator = self.migrator
for mname in diff: for mname in diff:
done.append(self.up_one(mname, self.migrator)) done.append(self.up_one(mname, self.migrator))
if name and name == mname: if name and name == mname:
@ -392,15 +392,14 @@ class MigrationManager():
""" """
Reads a migration from a file. Reads a migration from a file.
""" """
call_params = {} call_params = dict()
if helper.is_os_windows() and sys.version_info >= (3, 0): if helper.is_os_windows() and sys.version_info >= (3, 0):
# if system is windows - force utf-8 encoding # if system is windows - force utf-8 encoding
call_params['encoding'] = 'utf-8' call_params['encoding'] = 'utf-8'
with open(os.path.join(helper.migration_dir, name + '.py'), **call_params, encoding='utf-8') as f: with open(os.path.join(helper.migration_dir, name + '.py'), **call_params) as f:
code = f.read() code = f.read()
scope = {} scope = {}
code = compile(code, '<string>', 'exec', dont_inherit=True) code = compile(code, '<string>', 'exec', dont_inherit=True)
# pylint: disable=exec-used
exec(code, scope, None) exec(code, scope, None)
return scope.get('migrate', lambda m, d: None), scope.get('rollback', lambda m, d: None) return scope.get('migrate', lambda m, d: None), scope.get('rollback', lambda m, d: None)
@ -417,26 +416,24 @@ class MigrationManager():
return name return name
with self.database.transaction(): with self.database.transaction():
if rollback: if rollback:
logger.info(f'Rolling back "{name}"') logger.info('Rolling back "{}"'.format(name))
rollback_fn(migrator, self.database) rollback_fn(migrator, self.database)
migrator.run() migrator.run()
# pylint: disable=no-member
self.model.delete().where(self.model.name == name).execute() self.model.delete().where(self.model.name == name).execute()
else: else:
logger.info(f'Migrate "{name}"') logger.info('Migrate "{}"'.format(name))
migrate_fn(migrator, self.database) migrate_fn(migrator, self.database)
migrator.run() migrator.run()
if name not in self.done: if name not in self.done:
# pylint: disable=no-member
self.model.create(name=name) self.model.create(name=name)
logger.info(f'Done "{name}"') logger.info('Done "{}"'.format(name))
return name return name
except Exception: except Exception:
self.database.rollback() self.database.rollback()
operation_name = 'Rollback' if rollback else 'Migration' operation_name = 'Rollback' if rollback else 'Migration'
logger.exception(f'{operation_name} failed: {name}') logger.exception('{} failed: {}'.format(operation_name, name))
raise raise
def down(self): def down(self):
@ -450,4 +447,4 @@ class MigrationManager():
migrator = self.migrator migrator = self.migrator
self.up_one(name, migrator, False, True) self.up_one(name, migrator, False, True)
logger.warning(f'Rolled back migration: {name}') logger.warning('Rolled back migration: {}'.format(name))

View File

@ -222,6 +222,9 @@
{% if server['stats']['running'] %} {% if server['stats']['running'] %}
<span class="text-success"><i class="fas fa-signal"></i> {{ translate('dashboard', 'online', <span class="text-success"><i class="fas fa-signal"></i> {{ translate('dashboard', 'online',
data['lang']) }}</span> data['lang']) }}</span>
{% elif server['stats']['crashed'] %}
<span class="text-danger"><i class="fas fa-exclamation-triangle"></i> {{ translate('dashboard', 'crashed',
data['lang']) }}</span>
{% else %} {% else %}
<span class="text-warning"><i class="fas fa-ban"></i> {{ translate('dashboard', 'offline', <span class="text-warning"><i class="fas fa-ban"></i> {{ translate('dashboard', 'offline',
data['lang']) }}</span> data['lang']) }}</span>
@ -411,7 +414,11 @@
online_status = `<span class="text-success"><i class="fas fa-signal"></i> {{ translate('dashboard', 'online', data['lang'])}}</span>`; online_status = `<span class="text-success"><i class="fas fa-signal"></i> {{ translate('dashboard', 'online', data['lang'])}}</span>`;
} }
else { else {
if (server.crashed){
online_status = `<span class="text-danger"><i class="fas fa-exclamation-triangle"></i> {{ translate('dashboard', 'crashed', data['lang'])}}</span>`
}else{
online_status = `<span class="text-warning"><i class="fas fa-ban"></i> {{ translate('dashboard', 'offline', data['lang'])}}</span>`; online_status = `<span class="text-warning"><i class="fas fa-ban"></i> {{ translate('dashboard', 'offline', data['lang'])}}</span>`;
}
} }
server_online_status.innerHTML = online_status; server_online_status.innerHTML = online_status;
} }

View File

@ -8,10 +8,15 @@
<b>{{ translate('serverStats', 'serverStatus', data['lang']) }}:</b> <span id="status" class="text-success">{{ translate('serverStats', 'online', data['lang']) }}</span><br /> <b>{{ translate('serverStats', 'serverStatus', data['lang']) }}:</b> <span id="status" class="text-success">{{ translate('serverStats', 'online', data['lang']) }}</span><br />
<b>{{ translate('serverStats', 'serverStarted', data['lang']) }}:</b> <span id="started">{{ data['server_stats']['started'] }} ({{ translate('serverStats', 'serverTime', data['lang']) }})</span><br /> <b>{{ translate('serverStats', 'serverStarted', data['lang']) }}:</b> <span id="started">{{ data['server_stats']['started'] }} ({{ translate('serverStats', 'serverTime', data['lang']) }})</span><br />
<b>{{ translate('serverStats', 'serverUptime', data['lang']) }}:</b> <span id="uptime">{{ translate('serverStats', 'errorCalculatingUptime', data['lang']) }}</span> <b>{{ translate('serverStats', 'serverUptime', data['lang']) }}:</b> <span id="uptime">{{ translate('serverStats', 'errorCalculatingUptime', data['lang']) }}</span>
{% elif data['server_stats']['crashed'] %}
<b>{{ translate('serverStats', 'serverStatus', data['lang']) }}:</b> <span id="status" class="text-danger"> <i class="fas fa-exclamation-triangle"></i> {{ translate('dashboard', 'crashed', data['lang']) }}</span><br />
<b>{{ translate('serverStats', 'serverStarted', data['lang']) }}:</b> <span id="started" class="text-danger"> <i class="fas fa-exclamation-triangle"></i> {{ translate('dashboard', 'crashed', data['lang']) }}</span><br />
<b>{{ translate('serverStats', 'serverUptime', data['lang']) }}:</b> <span id="uptime" class="text-danger"> <i class="fas fa-exclamation-triangle"></i> {{ translate('dashboard', 'crashed', data['lang']) }}</span>
{% else %} {% else %}
<b>{{ translate('serverStats', 'serverStatus', data['lang']) }}:</b> <span id="status" class="text-danger">{{ translate('serverStats', 'offline', data['lang']) }}</span><br /> <b>{{ translate('serverStats', 'serverStatus', data['lang']) }}:</b> <span id="status" class="text-warning">{{ translate('serverStats', 'offline', data['lang']) }}</span><br />
<b>{{ translate('serverStats', 'serverStarted', data['lang']) }}:</b> <span id="started" class="text-danger">{{ translate('serverStats', 'offline', data['lang']) }}</span><br /> <b>{{ translate('serverStats', 'serverStarted', data['lang']) }}:</b> <span id="started" class="text-warning">{{ translate('serverStats', 'offline', data['lang']) }}</span><br />
<b>{{ translate('serverStats', 'serverUptime', data['lang']) }}:</b> <span id="uptime" class="text-danger">{{ translate('serverStats', 'offline', data['lang']) }}</span> <b>{{ translate('serverStats', 'serverUptime', data['lang']) }}:</b> <span id="uptime" class="text-warning">{{ translate('serverStats', 'offline', data['lang']) }}</span>
<p>{{ data['server_stats']['crashed'] }}</p>
{% end %} {% end %}
<br> <br>
<b>{{ translate('serverStats', 'serverTimeZone', data['lang']) }}:</b> <span class="text-info">{{ data['serverTZ'] }}</span> <b>{{ translate('serverStats', 'serverTimeZone', data['lang']) }}:</b> <span class="text-info">{{ data['serverTZ'] }}</span>
@ -161,14 +166,25 @@
} }
else else
{ {
server_status.setAttribute("class", "text-danger"); if (server.crashed){
server_status.setAttribute("class", "text-danger");
server_status.innerHTML = `<i class="fas fa-exclamation-triangle"></i> {{ translate('dashboard', 'crashed', data['lang']) }}`;
server_started.setAttribute("class", "text-danger");
server_started.innerHTML = `<i class="fas fa-exclamation-triangle"></i> {{ translate('dashboard', 'crashed', data['lang']) }}`;
clearInterval(uptimeLoop);
uptimeLoop = null;
server_uptime.setAttribute("class", "text-danger");
server_uptime.innerHTML = `<i class="fas fa-exclamation-triangle"></i> {{ translate('dashboard', 'crashed', data['lang']) }}`;
}else{
server_status.setAttribute("class", "text-warning");
server_status.innerHTML = `{{ translate('serverStats', 'offline', data['lang']) }}`; server_status.innerHTML = `{{ translate('serverStats', 'offline', data['lang']) }}`;
server_started.setAttribute("class", "text-danger"); server_started.setAttribute("class", "text-warning");
server_started.innerHTML = `{{ translate('serverStats', 'offline', data['lang']) }}`; server_started.innerHTML = `{{ translate('serverStats', 'offline', data['lang']) }}`;
clearInterval(uptimeLoop); clearInterval(uptimeLoop);
uptimeLoop = null; uptimeLoop = null;
server_uptime.setAttribute("class", "text-danger"); server_uptime.setAttribute("class", "text-warning");
server_uptime.innerHTML = `{{ translate('serverStats', 'offline', data['lang']) }}`; server_uptime.innerHTML = `{{ translate('serverStats', 'offline', data['lang']) }}`;
}
} }
server_cpu.innerHTML = server.cpu + ` %`; server_cpu.innerHTML = server.cpu + ` %`;