fix: stabilize HartOMat runtime startup
This commit is contained in:
@@ -1,13 +1,13 @@
|
|||||||
"""Backfill persisted Schaeffler branding to HartOMat.
|
"""Backfill persisted Schaeffler branding to HartOMat.
|
||||||
|
|
||||||
Revision ID: 063
|
Revision ID: 063
|
||||||
Revises: 062
|
Revises: a68e8c6fb61b
|
||||||
"""
|
"""
|
||||||
from alembic import op
|
from alembic import op
|
||||||
import sqlalchemy as sa
|
import sqlalchemy as sa
|
||||||
|
|
||||||
revision = "063"
|
revision = "063"
|
||||||
down_revision = "062"
|
down_revision = "a68e8c6fb61b"
|
||||||
branch_labels = None
|
branch_labels = None
|
||||||
depends_on = None
|
depends_on = None
|
||||||
|
|
||||||
|
|||||||
@@ -134,6 +134,7 @@ TEMPLATES = [
|
|||||||
|
|
||||||
|
|
||||||
async def seed(db_url: str, admin_email: str = "admin@hartomat.com", admin_password: str = "Admin1234!"):
|
async def seed(db_url: str, admin_email: str = "admin@hartomat.com", admin_password: str = "Admin1234!"):
|
||||||
|
from app.domains.tenants.models import Tenant
|
||||||
from app.models.template import Template
|
from app.models.template import Template
|
||||||
from app.models.user import User, UserRole
|
from app.models.user import User, UserRole
|
||||||
from app.utils.auth import hash_password
|
from app.utils.auth import hash_password
|
||||||
@@ -142,6 +143,17 @@ async def seed(db_url: str, admin_email: str = "admin@hartomat.com", admin_passw
|
|||||||
session_factory = async_sessionmaker(engine, expire_on_commit=False)
|
session_factory = async_sessionmaker(engine, expire_on_commit=False)
|
||||||
|
|
||||||
async with session_factory() as session:
|
async with session_factory() as session:
|
||||||
|
# Ensure the default single-tenant setup exists before seeding tenant-scoped data.
|
||||||
|
tenant_result = await session.execute(select(Tenant).where(Tenant.slug == "hartomat"))
|
||||||
|
default_tenant = tenant_result.scalar_one_or_none()
|
||||||
|
if not default_tenant:
|
||||||
|
default_tenant = Tenant(name="HartOMat", slug="hartomat", is_active=True)
|
||||||
|
session.add(default_tenant)
|
||||||
|
await session.flush()
|
||||||
|
print(" + Default tenant: hartomat")
|
||||||
|
else:
|
||||||
|
print(" ~ Default tenant already exists: hartomat")
|
||||||
|
|
||||||
# Seed templates
|
# Seed templates
|
||||||
for tpl_data in TEMPLATES:
|
for tpl_data in TEMPLATES:
|
||||||
result = await session.execute(
|
result = await session.execute(
|
||||||
@@ -149,24 +161,32 @@ async def seed(db_url: str, admin_email: str = "admin@hartomat.com", admin_passw
|
|||||||
)
|
)
|
||||||
existing = result.scalar_one_or_none()
|
existing = result.scalar_one_or_none()
|
||||||
if not existing:
|
if not existing:
|
||||||
tpl = Template(**tpl_data)
|
tpl = Template(**tpl_data, tenant_id=default_tenant.id)
|
||||||
session.add(tpl)
|
session.add(tpl)
|
||||||
print(f" + Template: {tpl_data['category_key']}")
|
print(f" + Template: {tpl_data['category_key']}")
|
||||||
else:
|
else:
|
||||||
|
if existing.tenant_id is None:
|
||||||
|
existing.tenant_id = default_tenant.id
|
||||||
|
print(f" * Backfilled template tenant: {tpl_data['category_key']}")
|
||||||
print(f" ~ Template already exists: {tpl_data['category_key']}")
|
print(f" ~ Template already exists: {tpl_data['category_key']}")
|
||||||
|
|
||||||
# Seed admin user
|
# Seed admin user
|
||||||
result = await session.execute(select(User).where(User.email == admin_email))
|
result = await session.execute(select(User).where(User.email == admin_email))
|
||||||
if not result.scalar_one_or_none():
|
existing_admin = result.scalar_one_or_none()
|
||||||
|
if not existing_admin:
|
||||||
admin = User(
|
admin = User(
|
||||||
email=admin_email,
|
email=admin_email,
|
||||||
password_hash=hash_password(admin_password),
|
password_hash=hash_password(admin_password),
|
||||||
role=UserRole.global_admin,
|
role=UserRole.global_admin,
|
||||||
full_name="HartOMat Admin",
|
full_name="HartOMat Admin",
|
||||||
|
tenant_id=default_tenant.id,
|
||||||
)
|
)
|
||||||
session.add(admin)
|
session.add(admin)
|
||||||
print(f" + Admin user: {admin_email}")
|
print(f" + Admin user: {admin_email}")
|
||||||
else:
|
else:
|
||||||
|
if existing_admin.tenant_id is None:
|
||||||
|
existing_admin.tenant_id = default_tenant.id
|
||||||
|
print(f" * Backfilled admin tenant: {admin_email}")
|
||||||
print(f" ~ Admin user already exists: {admin_email}")
|
print(f" ~ Admin user already exists: {admin_email}")
|
||||||
|
|
||||||
await session.commit()
|
await session.commit()
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
name: hartomat
|
||||||
|
|
||||||
# External render-worker configuration.
|
# External render-worker configuration.
|
||||||
#
|
#
|
||||||
# Use this compose file on remote machines (GPU nodes, NAS, cloud VMs) that
|
# Use this compose file on remote machines (GPU nodes, NAS, cloud VMs) that
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
name: hartomat
|
||||||
|
|
||||||
services:
|
services:
|
||||||
postgres:
|
postgres:
|
||||||
image: postgres:16-alpine
|
image: postgres:16-alpine
|
||||||
|
|||||||
Reference in New Issue
Block a user