diff --git a/app/modules/db/add.py b/app/modules/db/add.py index 9c1686fb..de0096aa 100644 --- a/app/modules/db/add.py +++ b/app/modules/db/add.py @@ -10,6 +10,7 @@ SectionModel = { 'nginx': NginxSection, } + def update_saved_server(server, description, saved_id): try: SavedServer.update(server=server, description=description).where(SavedServer.id == saved_id).execute() diff --git a/app/modules/db/migrate.py b/app/modules/db/migrate.py index 6f6cea91..40c3f48d 100644 --- a/app/modules/db/migrate.py +++ b/app/modules/db/migrate.py @@ -8,6 +8,7 @@ sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../. from app.modules.db.migration_manager import create_migrations_table, migrate, rollback, create_migration, list_migrations + def main(): parser = argparse.ArgumentParser(description='Database migration tool') subparsers = parser.add_subparsers(dest='command', help='Command to run') @@ -17,14 +18,14 @@ def main(): create_parser.add_argument('name', help='Name of the migration') # Migrate command - migrate_parser = subparsers.add_parser('migrate', help='Apply pending migrations') + subparsers.add_parser('migrate', help='Apply pending migrations') # Rollback command rollback_parser = subparsers.add_parser('rollback', help='Rollback migrations') rollback_parser.add_argument('--steps', type=int, default=1, help='Number of migrations to roll back') # Initialize command - init_parser = subparsers.add_parser('init', help='Initialize the migrations table') + subparsers.add_parser('init', help='Initialize the migrations table') # list command subparsers.add_parser('list', help='List all migrations and their status') @@ -58,4 +59,4 @@ def main(): sys.exit(1) if __name__ == '__main__': - main() \ No newline at end of file + main() diff --git a/app/modules/db/migration_manager.py b/app/modules/db/migration_manager.py index 6289dd65..03500ad3 100644 --- a/app/modules/db/migration_manager.py +++ b/app/modules/db/migration_manager.py @@ -17,11 +17,13 @@ class Migration(BaseModel): class Meta: table_name = 'migrations' + def create_migrations_table(): """Create the migrations table if it doesn't exist.""" conn = connect() conn.create_tables([Migration], safe=True) + def get_migration_files(): """Get all migration files from the migrations directory.""" migrations_dir = os.path.join(os.path.dirname(__file__), 'migrations') @@ -36,10 +38,12 @@ def get_migration_files(): migration_files.sort() return migration_files + def get_applied_migrations(): """Get all migrations that have been applied.""" return [m.name for m in Migration.select(Migration.name)] + def apply_migration(migration_name): """Apply a single migration.""" try: @@ -55,9 +59,10 @@ def apply_migration(migration_name): print(f"Migration applied: {migration_name}") return True except Exception as e: - print("error: applying migration {migration_name}: {str(e)}") + print(f"error: applying migration {migration_name}: {str(e)}") return False + def rollback_migration(migration_name): """Rollback a single migration.""" try: @@ -73,9 +78,10 @@ def rollback_migration(migration_name): print(f"Migration rolled back: {migration_name}") return True except Exception as e: - print("error: rolling back migration {migration_name}: {str(e)}") + print(f"error: rolling back migration {migration_name}: {str(e)}") return False + def migrate(): """Apply all pending migrations.""" create_migrations_table() @@ -100,6 +106,7 @@ def migrate(): return success + def rollback(steps=1): """Rollback the specified number of migrations.""" create_migrations_table() @@ -123,6 +130,7 @@ def rollback(steps=1): return success + def create_migration(name): """Create a new migration file.""" timestamp = datetime.now().strftime('%Y%m%d%H%M%S') diff --git a/app/modules/db/migrations/20230101000000_initial_migration.py b/app/modules/db/migrations/20230101000000_initial_migration.py index c0cacb43..4e968c93 100644 --- a/app/modules/db/migrations/20230101000000_initial_migration.py +++ b/app/modules/db/migrations/20230101000000_initial_migration.py @@ -3,6 +3,7 @@ from app.modules.db.db_model import connect, Version migrator = connect(get_migrator=1) + def up(): """Apply the migration.""" # Insert version record with version '1.0' @@ -12,8 +13,9 @@ def up(): except Exception as e: print(f"Error inserting version record: {e}") + def down(): """Roll back the migration.""" # This is the initial migration, so rolling back would mean dropping all tables # This is dangerous and not recommended, so we'll just pass - pass \ No newline at end of file + pass diff --git a/app/modules/db/migrations/20230101000001_update_user_groups.py b/app/modules/db/migrations/20230101000001_update_user_groups.py index d5303776..b44d17f9 100644 --- a/app/modules/db/migrations/20230101000001_update_user_groups.py +++ b/app/modules/db/migrations/20230101000001_update_user_groups.py @@ -3,6 +3,7 @@ from app.modules.db.db_model import connect, User, UserGroups migrator = connect(get_migrator=1) + def up(): """Apply the migration.""" # This migration updates user groups @@ -17,8 +18,9 @@ def up(): else: raise e + def down(): """Roll back the migration.""" # This migration adds data, not schema changes, so rolling back would mean deleting data # This is potentially dangerous, so we'll just pass - pass \ No newline at end of file + pass diff --git a/app/modules/db/migrations/20230101000002_add_use_src_to_ha_cluster_vips.py b/app/modules/db/migrations/20230101000002_add_use_src_to_ha_cluster_vips.py index 7d9ea8e2..ad527edb 100644 --- a/app/modules/db/migrations/20230101000002_add_use_src_to_ha_cluster_vips.py +++ b/app/modules/db/migrations/20230101000002_add_use_src_to_ha_cluster_vips.py @@ -4,6 +4,7 @@ from peewee import IntegerField, SQL migrator = connect(get_migrator=1) + def up(): """Apply the migration.""" # This migration adds a use_src column to the ha_cluster_vips table @@ -22,6 +23,7 @@ def up(): else: raise e + def down(): """Roll back the migration.""" # This migration removes the use_src column from the ha_cluster_vips table @@ -31,4 +33,4 @@ def down(): ) except Exception as e: print(f"Error rolling back migration: {str(e)}") - raise e \ No newline at end of file + raise e diff --git a/app/modules/db/migrations/20230101000003_rename_backups_columns.py b/app/modules/db/migrations/20230101000003_rename_backups_columns.py index 1a41ee24..e91cc91b 100644 --- a/app/modules/db/migrations/20230101000003_rename_backups_columns.py +++ b/app/modules/db/migrations/20230101000003_rename_backups_columns.py @@ -3,6 +3,7 @@ from app.modules.db.db_model import connect migrator = connect(get_migrator=1) + def up(): """Apply the migration.""" # This migration renames columns in the backups table @@ -19,6 +20,7 @@ def up(): else: raise e + def down(): """Roll back the migration.""" # This migration renames columns back to their original names @@ -29,4 +31,4 @@ def down(): ) except Exception as e: print(f"Error rolling back migration: {str(e)}") - raise e \ No newline at end of file + raise e diff --git a/app/modules/db/migrations/20230101000004_rename_multiple_columns.py b/app/modules/db/migrations/20230101000004_rename_multiple_columns.py index 4546ec62..96a2fe62 100644 --- a/app/modules/db/migrations/20230101000004_rename_multiple_columns.py +++ b/app/modules/db/migrations/20230101000004_rename_multiple_columns.py @@ -3,6 +3,7 @@ from app.modules.db.db_model import connect migrator = connect(get_migrator=1) + def up(): """Apply the migration.""" # This migration renames multiple columns across different tables @@ -36,6 +37,7 @@ def up(): else: raise e + def down(): """Roll back the migration.""" # This migration renames columns back to their original names @@ -63,4 +65,4 @@ def down(): ) except Exception as e: print(f"Error rolling back migration: {str(e)}") - raise e \ No newline at end of file + raise e diff --git a/app/modules/db/migrations/20230101000005_add_shared_to_cred.py b/app/modules/db/migrations/20230101000005_add_shared_to_cred.py index 5b43b276..87108e0e 100644 --- a/app/modules/db/migrations/20230101000005_add_shared_to_cred.py +++ b/app/modules/db/migrations/20230101000005_add_shared_to_cred.py @@ -4,6 +4,7 @@ from peewee import IntegerField, SQL migrator = connect(get_migrator=1) + def up(): """Apply the migration.""" # This migration adds a shared column to the cred table @@ -22,6 +23,7 @@ def up(): else: raise e + def down(): """Roll back the migration.""" # This migration removes the shared column from the cred table @@ -31,4 +33,4 @@ def down(): ) except Exception as e: print(f"Error rolling back migration: {str(e)}") - raise e \ No newline at end of file + raise e diff --git a/app/modules/db/migrations/20230101000006_delete_prometheus_tools.py b/app/modules/db/migrations/20230101000006_delete_prometheus_tools.py index 9866e76b..9ffa1a99 100644 --- a/app/modules/db/migrations/20230101000006_delete_prometheus_tools.py +++ b/app/modules/db/migrations/20230101000006_delete_prometheus_tools.py @@ -3,6 +3,7 @@ from app.modules.db.db_model import connect, RoxyTool migrator = connect(get_migrator=1) + def up(): """Apply the migration.""" # This migration deletes rows from the RoxyTool table @@ -13,6 +14,7 @@ def up(): print(f"Error applying migration: {str(e)}") raise e + def down(): """Roll back the migration.""" # This migration adds back the deleted rows to the RoxyTool table @@ -34,4 +36,4 @@ def down(): ).on_conflict_ignore().execute() except Exception as e: print(f"Error rolling back migration: {str(e)}") - raise e \ No newline at end of file + raise e diff --git a/app/modules/db/migrations/20230101000007_rename_server_to_server_id.py b/app/modules/db/migrations/20230101000007_rename_server_to_server_id.py index 351ac2d1..b221c1c1 100644 --- a/app/modules/db/migrations/20230101000007_rename_server_to_server_id.py +++ b/app/modules/db/migrations/20230101000007_rename_server_to_server_id.py @@ -3,6 +3,7 @@ from app.modules.db.db_model import connect migrator = connect(get_migrator=1) + def up(): """Apply the migration.""" # This migration renames the server column to server_id in the backups table @@ -18,6 +19,7 @@ def up(): else: raise e + def down(): """Roll back the migration.""" # This migration renames the server_id column back to server in the backups table @@ -27,4 +29,4 @@ def down(): ) except Exception as e: print(f"Error rolling back migration: {str(e)}") - raise e \ No newline at end of file + raise e diff --git a/app/modules/db/migrations/20230101000008_rename_server_to_server_id_in_s3_backups.py b/app/modules/db/migrations/20230101000008_rename_server_to_server_id_in_s3_backups.py index af315ec1..4a7e412d 100644 --- a/app/modules/db/migrations/20230101000008_rename_server_to_server_id_in_s3_backups.py +++ b/app/modules/db/migrations/20230101000008_rename_server_to_server_id_in_s3_backups.py @@ -3,6 +3,7 @@ from app.modules.db.db_model import connect migrator = connect(get_migrator=1) + def up(): """Apply the migration.""" # This migration renames the server column to server_id in the s3_backups table @@ -18,6 +19,7 @@ def up(): else: raise e + def down(): """Roll back the migration.""" # This migration renames the server_id column back to server in the s3_backups table @@ -27,4 +29,4 @@ def down(): ) except Exception as e: print(f"Error rolling back migration: {str(e)}") - raise e \ No newline at end of file + raise e diff --git a/app/modules/db/migrations/20230101000009_rename_rhost_to_rserver.py b/app/modules/db/migrations/20230101000009_rename_rhost_to_rserver.py index b795c7cd..f3c29048 100644 --- a/app/modules/db/migrations/20230101000009_rename_rhost_to_rserver.py +++ b/app/modules/db/migrations/20230101000009_rename_rhost_to_rserver.py @@ -3,6 +3,7 @@ from app.modules.db.db_model import connect migrator = connect(get_migrator=1) + def up(): """Apply the migration.""" # This migration renames the rhost column to rserver in the backups table @@ -18,6 +19,7 @@ def up(): else: raise e + def down(): """Roll back the migration.""" # This migration renames the rserver column back to rhost in the backups table @@ -27,4 +29,4 @@ def down(): ) except Exception as e: print(f"Error rolling back migration: {str(e)}") - raise e \ No newline at end of file + raise e diff --git a/app/modules/db/migrations/20230101000010_rename_period_to_time.py b/app/modules/db/migrations/20230101000010_rename_period_to_time.py index 4dcafc50..8cc33a24 100644 --- a/app/modules/db/migrations/20230101000010_rename_period_to_time.py +++ b/app/modules/db/migrations/20230101000010_rename_period_to_time.py @@ -3,6 +3,7 @@ from app.modules.db.db_model import connect migrator = connect(get_migrator=1) + def up(): """Apply the migration.""" # This migration renames the period column to time in the git_setting table @@ -18,6 +19,7 @@ def up(): else: raise e + def down(): """Roll back the migration.""" # This migration renames the time column back to period in the git_setting table @@ -27,4 +29,4 @@ def down(): ) except Exception as e: print(f"Error rolling back migration: {str(e)}") - raise e \ No newline at end of file + raise e diff --git a/app/modules/db/migrations/20230101000011_rename_group_to_group_id.py b/app/modules/db/migrations/20230101000011_rename_group_to_group_id.py index 6f10171c..182c7dfa 100644 --- a/app/modules/db/migrations/20230101000011_rename_group_to_group_id.py +++ b/app/modules/db/migrations/20230101000011_rename_group_to_group_id.py @@ -3,6 +3,7 @@ from app.modules.db.db_model import connect migrator = connect(get_migrator=1) + def up(): """Apply the migration.""" # This migration renames the group column to group_id in the settings table @@ -18,6 +19,7 @@ def up(): else: raise e + def down(): """Roll back the migration.""" # This migration renames the group_id column back to group in the settings table @@ -27,4 +29,4 @@ def down(): ) except Exception as e: print(f"Error rolling back migration: {str(e)}") - raise e \ No newline at end of file + raise e diff --git a/app/modules/db/migrations/20230101000012_add_private_key_to_cred.py b/app/modules/db/migrations/20230101000012_add_private_key_to_cred.py index 3247fe6f..e6154ff8 100644 --- a/app/modules/db/migrations/20230101000012_add_private_key_to_cred.py +++ b/app/modules/db/migrations/20230101000012_add_private_key_to_cred.py @@ -3,6 +3,7 @@ from app.modules.db.db_model import connect, TextField migrator = connect(get_migrator=1) + def up(): """Apply the migration.""" # This migration adds a private_key column to the cred table @@ -17,6 +18,7 @@ def up(): else: raise e + def down(): """Roll back the migration.""" # This migration removes the private_key column from the cred table @@ -26,4 +28,4 @@ def down(): ) except Exception as e: print(f"Error rolling back migration: {str(e)}") - raise e \ No newline at end of file + raise e diff --git a/app/modules/db/migrations/20230101000013_add_is_checker_to_udp_balancers.py b/app/modules/db/migrations/20230101000013_add_is_checker_to_udp_balancers.py index 42d135f6..fc8ad9cf 100644 --- a/app/modules/db/migrations/20230101000013_add_is_checker_to_udp_balancers.py +++ b/app/modules/db/migrations/20230101000013_add_is_checker_to_udp_balancers.py @@ -4,6 +4,7 @@ from peewee import IntegerField, SQL migrator = connect(get_migrator=1) + def up(): """Apply the migration.""" # This migration adds an is_checker column to the udp_balancers table @@ -23,6 +24,7 @@ def up(): else: raise e + def down(): """Roll back the migration.""" # This migration removes the is_checker column from the udp_balancers table @@ -32,4 +34,4 @@ def down(): ) except Exception as e: print(f"Error rolling back migration: {str(e)}") - raise e \ No newline at end of file + raise e diff --git a/app/modules/db/migrations/20230101000014_update_version.py b/app/modules/db/migrations/20230101000014_update_version.py index 48bf60d5..bb2463c0 100644 --- a/app/modules/db/migrations/20230101000014_update_version.py +++ b/app/modules/db/migrations/20230101000014_update_version.py @@ -3,6 +3,7 @@ from app.modules.db.db_model import connect, Version migrator = connect(get_migrator=1) + def up(): """Apply the migration.""" # This migration updates the version in the database to 8.2.0 @@ -12,6 +13,7 @@ def up(): print(f"Error updating version: {str(e)}") raise e + def down(): """Roll back the migration.""" # This migration sets the version back to 8.1.6 @@ -19,4 +21,4 @@ def down(): Version.update(version='8.1.6').execute() except Exception as e: print(f"Error rolling back migration: {str(e)}") - raise e \ No newline at end of file + raise e