110 lines
3.0 KiB
Python
110 lines
3.0 KiB
Python
import os
|
|
import sys
|
|
from logging.config import fileConfig
|
|
|
|
from sqlalchemy import engine_from_config, pool
|
|
|
|
from alembic import context
|
|
|
|
# Add your project directory to the Python path
|
|
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
|
|
|
|
from app.core.config import settings
|
|
from models.database.base import Base
|
|
|
|
# === IMPORTANT: Import all your DATABASE models here ===
|
|
# Only import SQLAlchemy models, not Pydantic API models
|
|
print("[ALEMBIC] Importing database models...")
|
|
|
|
try:
|
|
from models.database.user import User
|
|
print(" ✓ User model imported")
|
|
except ImportError as e:
|
|
print(f" ✗ User model failed: {e}")
|
|
|
|
try:
|
|
from models.database.product import Product
|
|
print(" ✓ Product model imported")
|
|
except ImportError as e:
|
|
print(f" ✗ Product model failed: {e}")
|
|
|
|
try:
|
|
from models.database.stock import Stock
|
|
print(" ✓ Stock model imported")
|
|
except ImportError as e:
|
|
print(f" ✗ Stock model failed: {e}")
|
|
|
|
try:
|
|
from models.database.shop import Shop, ShopProduct
|
|
print(" ✓ Shop models imported")
|
|
except ImportError as e:
|
|
print(f" ✗ Shop models failed: {e}")
|
|
|
|
try:
|
|
from models.database.marketplace import MarketplaceImportJob
|
|
print(" ✓ Marketplace model imported")
|
|
except ImportError as e:
|
|
print(f" ✗ Marketplace model failed: {e}")
|
|
|
|
# Check if there are any additional models in the database directory
|
|
try:
|
|
from models.database import auth as auth_models
|
|
print(" ✓ Auth models imported")
|
|
except ImportError:
|
|
print(" - Auth models not found (optional)")
|
|
|
|
try:
|
|
from models.database import admin as admin_models
|
|
print(" ✓ Admin models imported")
|
|
except ImportError:
|
|
print(" - Admin models not found (optional)")
|
|
|
|
print(f"[ALEMBIC] Model import completed. Tables found: {list(Base.metadata.tables.keys())}")
|
|
print(f"[ALEMBIC] Total tables to create: {len(Base.metadata.tables)}")
|
|
|
|
# Alembic Config object
|
|
config = context.config
|
|
|
|
# Override sqlalchemy.url with our settings
|
|
config.set_main_option("sqlalchemy.url", settings.database_url)
|
|
|
|
if config.config_file_name is not None:
|
|
fileConfig(config.config_file_name)
|
|
|
|
target_metadata = Base.metadata
|
|
|
|
|
|
def run_migrations_offline() -> None:
|
|
"""Run migrations in 'offline' mode."""
|
|
url = config.get_main_option("sqlalchemy.url")
|
|
context.configure(
|
|
url=url,
|
|
target_metadata=target_metadata,
|
|
literal_binds=True,
|
|
dialect_opts={"paramstyle": "named"},
|
|
)
|
|
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
|
|
|
|
def run_migrations_online() -> None:
|
|
"""Run migrations in 'online' mode."""
|
|
connectable = engine_from_config(
|
|
config.get_section(config.config_ini_section, {}),
|
|
prefix="sqlalchemy.",
|
|
poolclass=pool.NullPool,
|
|
)
|
|
|
|
with connectable.connect() as connection:
|
|
context.configure(connection=connection, target_metadata=target_metadata)
|
|
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
|
|
|
|
if context.is_offline_mode():
|
|
run_migrations_offline()
|
|
else:
|
|
run_migrations_online()
|