45 lines
1.4 KiB
Python
45 lines
1.4 KiB
Python
"""Initialize database with seed data."""
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
|
|
from sqlmodel import SQLModel
|
|
|
|
from app.core.config import settings
|
|
from app.core.constants import Role
|
|
from app.core.security import hash_password
|
|
from app.db.mariadb import async_engine, AsyncSessionLocal
|
|
from app.models.mariadb.auth import OAuthAccount, RefreshToken # noqa: F401
|
|
from app.models.mariadb.device import Device, DeviceGroup # noqa: F401
|
|
from app.models.mariadb.monitoring import Alert, AlertRule # noqa: F401
|
|
from app.models.mariadb.system import AuditLog, SystemConfig # noqa: F401
|
|
from app.models.mariadb.user import User, UserProfile
|
|
|
|
|
|
async def init() -> None:
|
|
# Create tables
|
|
async with async_engine.begin() as conn:
|
|
await conn.run_sync(SQLModel.metadata.create_all)
|
|
|
|
# Seed default data
|
|
async with AsyncSessionLocal() as session:
|
|
# Create default device group
|
|
group = DeviceGroup(name="default", description="Default device group")
|
|
session.add(group)
|
|
|
|
# Create system configs
|
|
configs = [
|
|
SystemConfig(key="maintenance_mode", value="false", description="Enable maintenance mode"),
|
|
SystemConfig(key="max_devices_per_user", value="50", description="Max devices per user"),
|
|
]
|
|
for c in configs:
|
|
session.add(c)
|
|
|
|
await session.commit()
|
|
|
|
print("Database initialized successfully!")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(init())
|