crafty-4/app/migrations/20240308_multi-backup.py

199 lines
7.7 KiB
Python
Raw Normal View History

import os
import datetime
import uuid
import peewee
import logging
2024-05-26 17:45:13 +00:00
from app.classes.models.management import Backups, Schedules
from app.classes.shared.helpers import Helpers
from app.classes.shared.console import Console
from app.classes.shared.migration import Migrator
from app.classes.shared.file_helpers import FileHelpers
logger = logging.getLogger(__name__)
def migrate(migrator: Migrator, database, **kwargs):
"""
Write your migrations here.
"""
db = database
2024-05-26 17:45:13 +00:00
Console.info("Starting Backups migrations")
Console.info(
"Migrations: Adding columns [backup_id, "
"backup_name, backup_location, enabled, default, action_id, backup_status]"
)
2024-05-28 23:42:02 +00:00
migrator.add_columns(
"backups",
2024-05-28 23:48:11 +00:00
backup_id=peewee.CharField(default=Helpers.create_uuid),
2024-05-28 23:42:02 +00:00
)
migrator.add_columns("backups", backup_name=peewee.CharField(default="Default"))
migrator.add_columns("backups", backup_location=peewee.CharField(default=""))
2024-05-25 17:51:40 +00:00
migrator.add_columns("backups", enabled=peewee.BooleanField(default=True))
2024-05-26 17:45:13 +00:00
migrator.add_columns("backups", default=peewee.BooleanField(default=False))
migrator.add_columns(
"backups",
status=peewee.CharField(default='{"status": "Standby", "message": ""}'),
)
2024-05-25 17:51:40 +00:00
migrator.add_columns(
"schedules", action_id=peewee.CharField(null=True, default=None)
)
class Servers(peewee.Model):
server_id = peewee.CharField(primary_key=True, default=str(uuid.uuid4()))
created = peewee.DateTimeField(default=datetime.datetime.now)
server_name = peewee.CharField(default="Server", index=True)
path = peewee.CharField(default="")
backup_path = peewee.CharField(default="")
executable = peewee.CharField(default="")
log_path = peewee.CharField(default="")
execution_command = peewee.CharField(default="")
auto_start = peewee.BooleanField(default=0)
auto_start_delay = peewee.IntegerField(default=10)
crash_detection = peewee.BooleanField(default=0)
stop_command = peewee.CharField(default="stop")
executable_update_url = peewee.CharField(default="")
server_ip = peewee.CharField(default="127.0.0.1")
server_port = peewee.IntegerField(default=25565)
logs_delete_after = peewee.IntegerField(default=0)
type = peewee.CharField(default="minecraft-java")
show_status = peewee.BooleanField(default=1)
created_by = peewee.IntegerField(default=-100)
shutdown_timeout = peewee.IntegerField(default=60)
ignored_exits = peewee.CharField(default="0")
class Meta:
table_name = "servers"
database = db
class NewBackups(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="")
2024-05-26 17:45:13 +00:00
default = peewee.BooleanField(default=False)
status = peewee.CharField(default='{"status": "Standby", "message": ""}')
2024-05-25 17:51:40 +00:00
enabled = peewee.BooleanField(default=True)
class Meta:
table_name = "new_backups"
database = db
2024-05-25 17:51:40 +00:00
class NewSchedules(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 = "new_schedules"
migrator.create_table(NewBackups)
2024-05-25 17:51:40 +00:00
migrator.create_table(NewSchedules)
migrator.run()
# Copy data from the existing backups table to the new one
for backup in Backups.select():
# Fetch the related server entry from the Servers table
server = Servers.get(Servers.server_id == backup.server_id)
2024-05-26 17:45:13 +00:00
Console.info(f"Migrations: Migrating backup for server {server.server_name}")
2024-05-25 19:11:46 +00:00
# Create a new backup entry with data from the
# old backup entry and related server
new_backup = NewBackups.create(
2024-05-26 17:45:13 +00:00
backup_name=f"{server.server_name} Backup",
2024-05-25 19:11:46 +00:00
# Set backup_location equal to backup_path
backup_location=server.backup_path,
excluded_dirs=backup.excluded_dirs,
max_backups=backup.max_backups,
server_id=server.server_id,
compress=backup.compress,
shutdown=backup.shutdown,
before=backup.before,
after=backup.after,
2024-05-26 17:45:13 +00:00
default=True,
2024-05-25 17:51:40 +00:00
enabled=True,
)
Helpers.ensure_dir_exists(
os.path.join(server.backup_path, new_backup.backup_id)
)
for file in os.listdir(server.backup_path):
if not os.path.isdir(os.path.join(os.path.join(server.backup_path, file))):
FileHelpers.move_file(
os.path.join(server.backup_path, file),
os.path.join(server.backup_path, new_backup.backup_id, file),
)
2024-05-26 17:45:13 +00:00
Console.debug("Migrations: Dropping old backup table")
# Drop the existing backups table
migrator.drop_table("backups")
2024-05-26 17:45:13 +00:00
Console.debug("Migrations: Renaming new_backups to backups")
# Rename the new table to backups
migrator.rename_table("new_backups", "backups")
2024-05-26 17:45:13 +00:00
Console.debug("Migrations: Dropping backup_path from servers table")
migrator.drop_columns("servers", ["backup_path"])
2024-05-25 17:51:40 +00:00
for schedule in Schedules.select():
action_id = None
if schedule.command == "backup_server":
2024-05-26 17:45:13 +00:00
Console.info(
f"Migrations: Adding backup ID to task with name {schedule.name}"
)
2024-05-25 17:51:40 +00:00
backup = NewBackups.get(NewBackups.server_id == schedule.server_id)
action_id = backup.backup_id
NewSchedules.create(
schedule_id=schedule.schedule_id,
server_id=schedule.server_id,
enabled=schedule.enabled,
action=schedule.action,
interval=schedule.interval,
interval_type=schedule.interval_type,
start_time=schedule.start_time,
command=schedule.command,
action_id=action_id,
name=schedule.name,
one_time=schedule.one_time,
cron_string=schedule.cron_string,
parent=schedule.parent,
delay=schedule.delay,
next_run=schedule.next_run,
)
2024-05-26 17:45:13 +00:00
Console.debug("Migrations: dropping old schedules table")
2024-05-25 17:51:40 +00:00
# Drop the existing backups table
migrator.drop_table("schedules")
2024-05-26 17:45:13 +00:00
Console.debug("Migrations: renaming new_schedules to schedules")
2024-05-25 17:51:40 +00:00
# Rename the new table to backups
migrator.rename_table("new_schedules", "schedules")
def rollback(migrator: Migrator, database, **kwargs):
"""
Write your rollback migrations here.
"""
db = database
migrator.drop_columns("backups", ["name", "backup_id", "backup_location"])
migrator.add_columns("servers", backup_path=peewee.CharField(default=""))