-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate.py
More file actions
156 lines (122 loc) · 4.78 KB
/
Copy pathmigrate.py
File metadata and controls
156 lines (122 loc) · 4.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env python3
"""
Standalone migration script.
This script can be run independently to apply database migrations
without starting the full application.
Usage:
python migrate.py # Run all pending migrations
python migrate.py --check # Check migration status
python migrate.py --rollback ID # Rollback specific migration
"""
import argparse
import sys
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
from app.database import SQLALCHEMY_DATABASE_URL
from app.migrations import run_migrations # noqa: E402
from app.migrations.runner import MigrationRunner
from app.migrations.migrations import get_all_migrations
# Configure unified logging for standalone script
from app.logging_config import setup_logging, logger
setup_logging()
def check_migration_status():
"""Check and display migration status."""
try:
runner = MigrationRunner(SQLALCHEMY_DATABASE_URL)
all_migrations = get_all_migrations()
applied_migrations = runner.get_applied_migrations()
logger.info("=== MIGRATION STATUS ===")
logger.info("Database: %s", SQLALCHEMY_DATABASE_URL)
logger.info("Total migrations: %s", len(all_migrations))
logger.info("Applied migrations: %s", len(applied_migrations))
logger.info("Migration Details:")
for migration in all_migrations:
status = "✅ APPLIED" if migration.migration_id in applied_migrations else "⏳ PENDING"
logger.info(" %s: %s", migration.migration_id, status)
logger.info(" Description: %s", migration.description)
pending_count = len(all_migrations) - len(applied_migrations)
if pending_count > 0:
logger.warning("%s migrations pending", pending_count)
return False
else:
logger.info("✅ Database is up to date")
return True
except Exception as e:
logger.error("Failed to check migration status: %s", e)
return False
def run_all_migrations():
"""Run all pending migrations."""
try:
logger.info("=== RUNNING MIGRATIONS ===")
migration_count = run_migrations(SQLALCHEMY_DATABASE_URL)
if migration_count > 0:
logger.info("✅ Successfully applied %s migrations", migration_count)
else:
logger.info("✅ No migrations needed - database is up to date")
return True
except Exception as e:
logger.error("Migration failed: %s", e)
return False
def rollback_migration(migration_id: str):
"""Rollback a specific migration."""
try:
runner = MigrationRunner(SQLALCHEMY_DATABASE_URL)
all_migrations = get_all_migrations()
# Find the migration to rollback
target_migration = None
for migration in all_migrations:
if migration.migration_id == migration_id:
target_migration = migration
break
if not target_migration:
logger.info("❌ Migration '%s' not found", migration_id)
return False
logger.info("\n=== ROLLING BACK MIGRATION ===")
logger.info("Migration: %s", migration_id)
logger.info("Description: %s", target_migration.description)
# Confirm rollback
confirm = input("\nAre you sure you want to rollback this migration? (yes/no): ")
if confirm.lower() != 'yes':
logger.info("Rollback cancelled")
return False
success = runner.rollback_migration(target_migration)
if success:
logger.info("✅ Migration rolled back successfully")
else:
logger.info("ℹ️ Migration was not applied, nothing to rollback")
return True
except Exception as e:
logger.error("Rollback failed: %s", e)
return False
def main():
"""Main entry point."""
parser = argparse.ArgumentParser(description="Database migration tool")
parser.add_argument(
"--check",
action="store_true",
help="Check migration status without applying"
)
parser.add_argument(
"--rollback",
type=str,
help="Rollback specific migration by ID"
)
args = parser.parse_args()
logger.info("🔧 Database Migration Tool")
logger.info("=" * 50)
if args.check:
success = check_migration_status()
elif args.rollback:
success = rollback_migration(args.rollback)
else:
# Default: run migrations
success = run_all_migrations()
if success:
logger.info("\n✅ Operation completed successfully")
sys.exit(0)
else:
logger.error("\n❌ Operation failed")
sys.exit(1)
if __name__ == "__main__":
main()