try to revert back to old migrate history if failed

This commit is contained in:
Silversthorn 2024-02-16 20:04:12 +01:00
parent 875c9058b0
commit 90e0777b9b
2 changed files with 50 additions and 30 deletions

View File

@ -24,7 +24,7 @@ logger = logging.getLogger(__name__)
class Servers(BaseModel):
server_id = CharField(primary_key=True, default=Helpers.create_uuid())
created = DateTimeField(default=datetime.datetime.now)
server_uuid = CharField(default="", index=True)
# server_uuid = CharField(default="", index=True)
server_name = CharField(default="Server", index=True)
path = CharField(default="")
backup_path = CharField(default="")

View File

@ -4,7 +4,7 @@ import peewee
import logging
from app.classes.shared.console import Console
from app.classes.shared.migration import Migrator
from app.classes.shared.migration import Migrator, MigrateHistory
from app.classes.models.management import (
AuditLog,
Webhooks,
@ -53,35 +53,47 @@ def migrate(migrator: Migrator, database, **kwargs):
table_name = "servers"
database = db
# Changes on Server Table
migrator.alter_column_type(
Servers,
"server_id",
peewee.CharField(primary_key=True, default=str(uuid.uuid4())),
)
try:
# Changes on Server Table
migrator.alter_column_type(
Servers,
"server_id",
peewee.CharField(primary_key=True, default=str(uuid.uuid4())),
)
# Changes on Audit Log Table
migrator.alter_column_type(
AuditLog,
"server_id",
peewee.ForeignKeyField(
Servers,
backref="audit_server",
null=True,
field=peewee.CharField(primary_key=True, default=str(uuid.uuid4())),
),
)
# Changes on Webhook Table
migrator.alter_column_type(
Webhooks,
"server_id",
peewee.ForeignKeyField(
Servers,
backref="webhook_server",
null=True,
field=peewee.CharField(primary_key=True, default=str(uuid.uuid4())),
),
)
# Changes on Audit Log Table
migrator.alter_column_type(
AuditLog,
"server_id",
peewee.ForeignKeyField(
Servers,
backref="audit_server",
null=True,
field=peewee.CharField(primary_key=True, default=str(uuid.uuid4())),
),
)
# Changes on Webhook Table
migrator.alter_column_type(
Webhooks,
"server_id",
peewee.ForeignKeyField(
Servers,
backref="webhook_server",
null=True,
field=peewee.CharField(primary_key=True, default=str(uuid.uuid4())),
),
)
except Exception as ex:
logger.error("Error while migrating Data from Int to UUID (Type Change)")
logger.error(ex)
Console.error("Error while migrating Data from Int to UUID (Type Change)")
Console.error(ex)
last_migration = (
MigrateHistory.select().order_by(MigrateHistory.id.desc()).get()
)
last_migration.delete()
return False
try:
# Changes on Audit Log Table
@ -152,6 +164,10 @@ def migrate(migrator: Migrator, database, **kwargs):
logger.error(ex)
Console.error("Error while migrating Data from Int to UUID (Foreign Keys)")
Console.error(ex)
last_migration = (
MigrateHistory.select().order_by(MigrateHistory.id.desc()).get()
)
last_migration.delete()
return False
try:
@ -166,6 +182,10 @@ def migrate(migrator: Migrator, database, **kwargs):
logger.error(ex)
Console.error("Error while migrating Data from Int to UUID (Primary Keys)")
Console.error(ex)
last_migration = (
MigrateHistory.select().order_by(MigrateHistory.id.desc()).get()
)
last_migration.delete()
return False
# Changes on Server Table