Fix migrations errors on initial run

This commit is contained in:
Andrew 2024-06-15 14:32:18 -04:00
parent 4cc2d81044
commit 0b0b63d650
2 changed files with 202 additions and 24 deletions

View File

@ -5,13 +5,7 @@ import logging
from app.classes.shared.console import Console from app.classes.shared.console import Console
from app.classes.shared.migration import Migrator, MigrateHistory from app.classes.shared.migration import Migrator, MigrateHistory
from app.classes.models.management import ( from app.classes.models.roles import Roles
Webhooks,
Schedules,
Backups,
)
from app.classes.models.server_permissions import RoleServers
from app.classes.models.base_model import BaseModel
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -53,6 +47,78 @@ def migrate(migrator: Migrator, database, **kwargs):
table_name = "servers" table_name = "servers"
database = db database = db
# **********************************************************************************
# Role Servers Class
# **********************************************************************************
class RoleServers(peewee.Model):
role_id = peewee.ForeignKeyField(Roles, backref="role_server")
server_id = peewee.ForeignKeyField(Servers, backref="role_server")
permissions = peewee.CharField(default="00000000")
class Meta:
table_name = "role_servers"
primary_key = peewee.CompositeKey("role_id", "server_id")
database = db
# **********************************************************************************
# Webhooks Class
# **********************************************************************************
class Webhooks(peewee.Model):
id = peewee.AutoField()
server_id = peewee.ForeignKeyField(Servers, backref="webhook_server", null=True)
name = peewee.CharField(default="Custom Webhook", max_length=64)
url = peewee.CharField(default="")
webhook_type = peewee.CharField(default="Custom")
bot_name = peewee.CharField(default="Crafty Controller")
trigger = peewee.CharField(default="server_start,server_stop")
body = peewee.CharField(default="")
color = peewee.CharField(default="#005cd1")
enabled = peewee.BooleanField(default=True)
class Meta:
table_name = "webhooks"
database = db
# **********************************************************************************
# Schedules Class
# **********************************************************************************
class Schedules(peewee.Model):
schedule_id = peewee.IntegerField(unique=True, primary_key=True)
server_id = peewee.ForeignKeyField(Servers, backref="schedule_server")
enabled = peewee.BooleanField()
action = peewee.CharField()
interval = peewee.IntegerField()
interval_type = peewee.CharField()
start_time = peewee.CharField(null=True)
command = peewee.CharField(null=True)
name = peewee.CharField()
one_time = peewee.BooleanField(default=False)
cron_string = peewee.CharField(default="")
parent = peewee.IntegerField(null=True)
delay = peewee.IntegerField(default=0)
next_run = peewee.CharField(default="")
class Meta:
table_name = "schedules"
database = db
# **********************************************************************************
# Backups Class
# **********************************************************************************
class Backups(peewee.Model):
excluded_dirs = peewee.CharField(null=True)
max_backups = peewee.IntegerField()
max_backups = peewee.IntegerField()
server_id = peewee.ForeignKeyField(Servers, backref="backups_server")
compress = peewee.BooleanField(default=False)
shutdown = peewee.BooleanField(default=False)
before = peewee.CharField(default="")
after = peewee.CharField(default="")
class Meta:
table_name = "backups"
database = db
this_migration = MigrateHistory.get_or_none( this_migration = MigrateHistory.get_or_none(
MigrateHistory.name == "20240217_rework_servers_uuid_part2" MigrateHistory.name == "20240217_rework_servers_uuid_part2"
) )
@ -70,8 +136,8 @@ def migrate(migrator: Migrator, database, **kwargs):
return return
try: try:
logger.info("Migrating Data from Int to UUID (Foreign Keys)") logger.debug("Migrating Data from Int to UUID (Foreign Keys)")
Console.info("Migrating Data from Int to UUID (Foreign Keys)") Console.debug("Migrating Data from Int to UUID (Foreign Keys)")
# Changes on Webhooks Log Table # Changes on Webhooks Log Table
for webhook in Webhooks.select(): for webhook in Webhooks.select():
@ -122,8 +188,8 @@ def migrate(migrator: Migrator, database, **kwargs):
and RoleServers.server_id == old_server_id and RoleServers.server_id == old_server_id
).execute() ).execute()
logger.info("Migrating Data from Int to UUID (Foreign Keys) : SUCCESS") logger.debug("Migrating Data from Int to UUID (Foreign Keys) : SUCCESS")
Console.info("Migrating Data from Int to UUID (Foreign Keys) : SUCCESS") Console.debug("Migrating Data from Int to UUID (Foreign Keys) : SUCCESS")
except Exception as ex: except Exception as ex:
logger.error("Error while migrating Data from Int to UUID (Foreign Keys)") logger.error("Error while migrating Data from Int to UUID (Foreign Keys)")
@ -135,16 +201,16 @@ def migrate(migrator: Migrator, database, **kwargs):
return return
try: try:
logger.info("Migrating Data from Int to UUID (Primary Keys)") logger.debug("Migrating Data from Int to UUID (Primary Keys)")
Console.info("Migrating Data from Int to UUID (Primary Keys)") Console.debug("Migrating Data from Int to UUID (Primary Keys)")
# Migrating servers from the old id type to the new one # Migrating servers from the old id type to the new one
for server in Servers.select(): for server in Servers.select():
Servers.update(server_id=server.server_uuid).where( Servers.update(server_id=server.server_uuid).where(
Servers.server_id == server.server_id Servers.server_id == server.server_id
).execute() ).execute()
logger.info("Migrating Data from Int to UUID (Primary Keys) : SUCCESS") logger.debug("Migrating Data from Int to UUID (Primary Keys) : SUCCESS")
Console.info("Migrating Data from Int to UUID (Primary Keys) : SUCCESS") Console.debug("Migrating Data from Int to UUID (Primary Keys) : SUCCESS")
except Exception as ex: except Exception as ex:
logger.error("Error while migrating Data from Int to UUID (Primary Keys)") logger.error("Error while migrating Data from Int to UUID (Primary Keys)")
@ -203,9 +269,81 @@ def rollback(migrator: Migrator, database, **kwargs):
table_name = "servers" table_name = "servers"
database = db database = db
# **********************************************************************************
# Role Servers Class
# **********************************************************************************
class RoleServers(peewee.Model):
role_id = peewee.ForeignKeyField(Roles, backref="role_server")
server_id = peewee.ForeignKeyField(Servers, backref="role_server")
permissions = peewee.CharField(default="00000000")
class Meta:
table_name = "role_servers"
primary_key = peewee.CompositeKey("role_id", "server_id")
database = db
# **********************************************************************************
# Webhooks Class
# **********************************************************************************
class Webhooks(peewee.Model):
id = peewee.AutoField()
server_id = peewee.ForeignKeyField(Servers, backref="webhook_server", null=True)
name = peewee.CharField(default="Custom Webhook", max_length=64)
url = peewee.CharField(default="")
webhook_type = peewee.CharField(default="Custom")
bot_name = peewee.CharField(default="Crafty Controller")
trigger = peewee.CharField(default="server_start,server_stop")
body = peewee.CharField(default="")
color = peewee.CharField(default="#005cd1")
enabled = peewee.BooleanField(default=True)
class Meta:
table_name = "webhooks"
database = db
# **********************************************************************************
# Schedules Class
# **********************************************************************************
class Schedules(peewee.Model):
schedule_id = peewee.IntegerField(unique=True, primary_key=True)
server_id = peewee.ForeignKeyField(Servers, backref="schedule_server")
enabled = peewee.BooleanField()
action = peewee.CharField()
interval = peewee.IntegerField()
interval_type = peewee.CharField()
start_time = peewee.CharField(null=True)
command = peewee.CharField(null=True)
name = peewee.CharField()
one_time = peewee.BooleanField(default=False)
cron_string = peewee.CharField(default="")
parent = peewee.IntegerField(null=True)
delay = peewee.IntegerField(default=0)
next_run = peewee.CharField(default="")
class Meta:
table_name = "schedules"
database = db
# **********************************************************************************
# Backups Class
# **********************************************************************************
class Backups(peewee.Model):
excluded_dirs = peewee.CharField(null=True)
max_backups = peewee.IntegerField()
max_backups = peewee.IntegerField()
server_id = peewee.ForeignKeyField(Servers, backref="backups_server")
compress = peewee.BooleanField(default=False)
shutdown = peewee.BooleanField(default=False)
before = peewee.CharField(default="")
after = peewee.CharField(default="")
class Meta:
table_name = "backups"
database = db
try: try:
logger.info("Migrating Data from UUID to Int (Primary Keys)") logger.debug("Migrating Data from UUID to Int (Primary Keys)")
Console.info("Migrating Data from UUID to Int (Primary Keys)") Console.debug("Migrating Data from UUID to Int (Primary Keys)")
# Migrating servers from the old id type to the new one # Migrating servers from the old id type to the new one
new_id = 0 new_id = 0
for server in Servers.select(): for server in Servers.select():
@ -217,8 +355,8 @@ def rollback(migrator: Migrator, database, **kwargs):
Servers.server_id == server.server_id Servers.server_id == server.server_id
).execute() ).execute()
logger.info("Migrating Data from UUID to Int (Primary Keys) : SUCCESS") logger.debug("Migrating Data from UUID to Int (Primary Keys) : SUCCESS")
Console.info("Migrating Data from UUID to Int (Primary Keys) : SUCCESS") Console.debug("Migrating Data from UUID to Int (Primary Keys) : SUCCESS")
except Exception as ex: except Exception as ex:
logger.error("Error while migrating Data from UUID to Int (Primary Keys)") logger.error("Error while migrating Data from UUID to Int (Primary Keys)")
@ -230,8 +368,8 @@ def rollback(migrator: Migrator, database, **kwargs):
return return
try: try:
logger.info("Migrating Data from UUID to Int (Foreign Keys)") logger.debug("Migrating Data from UUID to Int (Foreign Keys)")
Console.info("Migrating Data from UUID to Int (Foreign Keys)") Console.debug("Migrating Data from UUID to Int (Foreign Keys)")
# Changes on Webhooks Log Table # Changes on Webhooks Log Table
for webhook in Webhooks.select(): for webhook in Webhooks.select():
old_server_id = webhook.server_id_id old_server_id = webhook.server_id_id
@ -281,8 +419,8 @@ def rollback(migrator: Migrator, database, **kwargs):
and RoleServers.server_id == old_server_id and RoleServers.server_id == old_server_id
).execute() ).execute()
logger.info("Migrating Data from UUID to Int (Foreign Keys) : SUCCESS") logger.debug("Migrating Data from UUID to Int (Foreign Keys) : SUCCESS")
Console.info("Migrating Data from UUID to Int (Foreign Keys) : SUCCESS") Console.debug("Migrating Data from UUID to Int (Foreign Keys) : SUCCESS")
except Exception as ex: except Exception as ex:
logger.error("Error while migrating Data from UUID to Int (Foreign Keys)") logger.error("Error while migrating Data from UUID to Int (Foreign Keys)")

View File

@ -5,7 +5,6 @@ import peewee
import logging import logging
from app.classes.models.management import Backups, Schedules
from app.classes.shared.helpers import Helpers from app.classes.shared.helpers import Helpers
from app.classes.shared.console import Console from app.classes.shared.console import Console
from app.classes.shared.migration import Migrator from app.classes.shared.migration import Migrator
@ -67,6 +66,25 @@ def migrate(migrator: Migrator, database, **kwargs):
table_name = "servers" table_name = "servers"
database = db database = db
class Backups(peewee.Model):
backup_id = peewee.CharField(primary_key=True, default=Helpers.create_uuid)
backup_name = peewee.CharField(default="New Backup")
backup_location = peewee.CharField(default="")
excluded_dirs = peewee.CharField(null=True)
max_backups = peewee.IntegerField()
server_id = peewee.ForeignKeyField(Servers, backref="backups_server")
compress = peewee.BooleanField(default=False)
shutdown = peewee.BooleanField(default=False)
before = peewee.CharField(default="")
after = peewee.CharField(default="")
default = peewee.BooleanField(default=False)
status = peewee.CharField(default='{"status": "Standby", "message": ""}')
enabled = peewee.BooleanField(default=True)
class Meta:
table_name = "backups"
database = db
class NewBackups(peewee.Model): class NewBackups(peewee.Model):
backup_id = peewee.CharField(primary_key=True, default=Helpers.create_uuid) backup_id = peewee.CharField(primary_key=True, default=Helpers.create_uuid)
backup_name = peewee.CharField(default="New Backup") backup_name = peewee.CharField(default="New Backup")
@ -86,6 +104,27 @@ def migrate(migrator: Migrator, database, **kwargs):
table_name = "new_backups" table_name = "new_backups"
database = db database = db
class Schedules(peewee.Model):
schedule_id = peewee.IntegerField(unique=True, primary_key=True)
server_id = peewee.ForeignKeyField(Servers, backref="schedule_server")
enabled = peewee.BooleanField()
action = peewee.CharField()
interval = peewee.IntegerField()
interval_type = peewee.CharField()
start_time = peewee.CharField(null=True)
command = peewee.CharField(null=True)
action_id = peewee.CharField(null=True)
name = peewee.CharField()
one_time = peewee.BooleanField(default=False)
cron_string = peewee.CharField(default="")
parent = peewee.IntegerField(null=True)
delay = peewee.IntegerField(default=0)
next_run = peewee.CharField(default="")
class Meta:
table_name = "schedules"
database = db
class NewSchedules(peewee.Model): class NewSchedules(peewee.Model):
schedule_id = peewee.IntegerField(unique=True, primary_key=True) schedule_id = peewee.IntegerField(unique=True, primary_key=True)
server_id = peewee.ForeignKeyField(Servers, backref="schedule_server") server_id = peewee.ForeignKeyField(Servers, backref="schedule_server")
@ -105,6 +144,7 @@ def migrate(migrator: Migrator, database, **kwargs):
class Meta: class Meta:
table_name = "new_schedules" table_name = "new_schedules"
database = db
migrator.create_table(NewBackups) migrator.create_table(NewBackups)
migrator.create_table(NewSchedules) migrator.create_table(NewSchedules)