fix(init): register all models and create default platform in init_production

Add optional model imports so SQLAlchemy resolves string-based
relationships (e.g. Platform→SubscriptionTier). Add default OMS
platform creation step so seed scripts can reference platform_id.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-07 22:49:51 +01:00
parent d9ccf0018f
commit 691dca91e5

View File

@@ -38,9 +38,26 @@ from app.modules.tenancy.services.permission_discovery_service import (
permission_discovery_service,
)
from middleware.auth import AuthManager
from app.modules.tenancy.models import AdminSetting
from app.modules.tenancy.models import AdminSetting, Platform
from app.modules.tenancy.models import User
# Register all models with SQLAlchemy so string-based relationships resolve
for _mod in [
"app.modules.billing.models",
"app.modules.inventory.models",
"app.modules.cart.models",
"app.modules.messaging.models",
"app.modules.loyalty.models",
"app.modules.catalog.models",
"app.modules.customers.models",
"app.modules.orders.models",
"app.modules.marketplace.models",
]:
try:
__import__(_mod)
except ImportError:
pass
# =============================================================================
# HELPER FUNCTIONS
# =============================================================================
@@ -113,6 +130,33 @@ def create_admin_user(db: Session, auth_manager: AuthManager) -> User:
return admin
def create_default_platform(db: Session) -> Platform:
"""Create the default OMS platform."""
existing = db.execute(
select(Platform).where(Platform.code == "oms")
).scalar_one_or_none()
if existing:
print_warning(f"Platform already exists: {existing.name}")
return existing
platform = Platform(
code="oms",
name="Wizamart OMS",
description="Order Management System for multi-store e-commerce",
domain=settings.platform_domain,
is_active=True,
created_at=datetime.now(UTC),
updated_at=datetime.now(UTC),
)
db.add(platform)
db.flush()
print_success(f"Created platform: {platform.name} ({platform.code})")
return platform
def create_default_role_templates(db: Session) -> dict:
"""Create default role templates (not tied to any store).
@@ -353,12 +397,16 @@ def initialize_production(db: Session, auth_manager: AuthManager):
print_step(2, "Creating platform admin...")
admin = create_admin_user(db, auth_manager)
# Step 3: Set up default role templates
print_step(3, "Setting up role templates...")
# Step 3: Create default platform
print_step(3, "Creating default platform...")
platform = create_default_platform(db)
# Step 4: Set up default role templates
print_step(4, "Setting up role templates...")
role_templates = create_default_role_templates(db)
# Step 4: Create admin settings
print_step(4, "Creating admin settings...")
# Step 5: Create admin settings
print_step(5, "Creating admin settings...")
create_admin_settings(db)
# Commit all changes