Spaces:
Running
Running
Commit
·
bd54343
1
Parent(s):
c27f115
Refactor migration script: streamline the database migration process by removing Alembic dependencies, updating logging for clarity, and initializing database tables directly. Enhance error handling for improved debugging during migration execution.
Browse files- scripts/migrate.py +23 -67
scripts/migrate.py
CHANGED
@@ -1,86 +1,42 @@
|
|
1 |
-
|
|
|
|
|
|
|
2 |
import os
|
3 |
import sys
|
4 |
import logging
|
5 |
-
from
|
6 |
-
from alembic import command
|
7 |
|
8 |
-
#
|
9 |
logging.basicConfig(
|
10 |
level=logging.INFO,
|
11 |
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
12 |
)
|
13 |
logger = logging.getLogger(__name__)
|
14 |
|
15 |
-
# --- Rely on PYTHONPATH set in Dockerfile ---
|
16 |
-
# project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
17 |
-
# if project_root not in sys.path:
|
18 |
-
# sys.path.insert(0, project_root)
|
19 |
-
|
20 |
-
# Debug: Print relevant info
|
21 |
-
logger.info(f"Current directory: {os.getcwd()}")
|
22 |
-
logger.info(f"Python Path (sys.path): {sys.path}")
|
23 |
-
|
24 |
-
models_db_path = "/app/app/models/database.py"
|
25 |
-
logger.info(f"Checking existence of {models_db_path}: {os.path.exists(models_db_path)}")
|
26 |
-
|
27 |
-
try:
|
28 |
-
# Direct import, assuming PYTHONPATH is correct
|
29 |
-
from app.models.database import Base
|
30 |
-
logger.info("Successfully imported Base from app.models.database")
|
31 |
-
except ImportError as e:
|
32 |
-
logger.error(f"Error importing Base: {e}")
|
33 |
-
# Log directory contents for deeper debugging
|
34 |
-
logger.info("Contents of /app:")
|
35 |
-
if os.path.exists("/app"):
|
36 |
-
try:
|
37 |
-
logger.info(os.listdir("/app"))
|
38 |
-
except Exception as list_err:
|
39 |
-
logger.error(f"Could not list /app: {list_err}")
|
40 |
-
logger.info("Contents of /app/app:")
|
41 |
-
if os.path.exists("/app/app"):
|
42 |
-
try:
|
43 |
-
logger.info(os.listdir("/app/app"))
|
44 |
-
except Exception as list_err:
|
45 |
-
logger.error(f"Could not list /app/app: {list_err}")
|
46 |
-
logger.info("Contents of /app/app/models:")
|
47 |
-
if os.path.exists("/app/app/models"):
|
48 |
-
try:
|
49 |
-
logger.info(os.listdir("/app/app/models"))
|
50 |
-
except Exception as list_err:
|
51 |
-
logger.error(f"Could not list /app/app/models: {list_err}")
|
52 |
-
raise
|
53 |
-
|
54 |
def run_migrations():
|
55 |
-
"""Run database migrations
|
56 |
try:
|
57 |
-
#
|
58 |
-
|
|
|
59 |
|
60 |
-
#
|
61 |
-
|
62 |
-
logger.info(f"Using Alembic config: {alembic_cfg_path}")
|
63 |
-
if not os.path.exists(alembic_cfg_path):
|
64 |
-
logger.error(f"Alembic config not found at {alembic_cfg_path}")
|
65 |
-
raise FileNotFoundError(f"Alembic config not found at {alembic_cfg_path}")
|
66 |
-
|
67 |
-
alembic_cfg = Config(alembic_cfg_path)
|
68 |
|
69 |
-
#
|
70 |
-
|
71 |
-
|
72 |
-
logger.info(f"Set Alembic script location to: {script_location}")
|
73 |
|
74 |
-
|
75 |
-
logger.info("Starting database migration...")
|
76 |
-
command.upgrade(alembic_cfg, "head")
|
77 |
-
logger.info("Database migration completed successfully.")
|
78 |
|
|
|
|
|
|
|
79 |
except Exception as e:
|
80 |
-
logger.error(f"Error
|
81 |
-
|
82 |
-
logger.error(traceback.format_exc())
|
83 |
-
raise
|
84 |
|
85 |
if __name__ == "__main__":
|
86 |
-
run_migrations()
|
|
|
|
1 |
+
#!/usr/bin/env python3
|
2 |
+
"""
|
3 |
+
Database migration script for the TTS API.
|
4 |
+
"""
|
5 |
import os
|
6 |
import sys
|
7 |
import logging
|
8 |
+
from pathlib import Path
|
|
|
9 |
|
10 |
+
# Set up logging
|
11 |
logging.basicConfig(
|
12 |
level=logging.INFO,
|
13 |
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
14 |
)
|
15 |
logger = logging.getLogger(__name__)
|
16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
def run_migrations():
|
18 |
+
"""Run database migrations."""
|
19 |
try:
|
20 |
+
# Log current environment
|
21 |
+
logger.info(f"Current directory: {os.getcwd()}")
|
22 |
+
logger.info(f"Python Path (sys.path): {sys.path}")
|
23 |
|
24 |
+
# Import the database configuration
|
25 |
+
from app.models import init_db
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
+
# Initialize database tables
|
28 |
+
init_db()
|
29 |
+
logger.info("Database tables created successfully")
|
|
|
30 |
|
31 |
+
return True
|
|
|
|
|
|
|
32 |
|
33 |
+
except ImportError as e:
|
34 |
+
logger.error(f"Error importing database configuration: {e}")
|
35 |
+
return False
|
36 |
except Exception as e:
|
37 |
+
logger.error(f"Error running migrations: {e}")
|
38 |
+
return False
|
|
|
|
|
39 |
|
40 |
if __name__ == "__main__":
|
41 |
+
success = run_migrations()
|
42 |
+
sys.exit(0 if success else 1)
|