81 lines
2.5 KiB
Python
81 lines
2.5 KiB
Python
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass
|
|
|
|
from sqlalchemy import text
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
|
|
REQUIRED_TABLES = (
|
|
"auth.app_users",
|
|
"auth.roles",
|
|
"auth.permissions",
|
|
"auth.role_permissions",
|
|
"content.modules",
|
|
"content.content_items",
|
|
"content.media_assets",
|
|
"commerce.access_plans",
|
|
"commerce.access_plan_modules",
|
|
)
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
class ValidationResult:
|
|
ok: bool
|
|
missing_tables: tuple[str, ...]
|
|
details: dict[str, object]
|
|
|
|
|
|
class BootstrapValidator:
|
|
async def validate(self, session: AsyncSession) -> ValidationResult:
|
|
missing: list[str] = []
|
|
|
|
for qualified in REQUIRED_TABLES:
|
|
schema, table = qualified.split(".", 1)
|
|
exists = await session.scalar(
|
|
text(
|
|
"""
|
|
select exists (
|
|
select 1
|
|
from information_schema.tables
|
|
where table_schema = :schema
|
|
and table_name = :table
|
|
)
|
|
"""
|
|
),
|
|
{"schema": schema, "table": table},
|
|
)
|
|
if not exists:
|
|
missing.append(qualified)
|
|
|
|
details: dict[str, object] = {
|
|
"required_tables": len(REQUIRED_TABLES),
|
|
"missing_table_count": len(missing),
|
|
}
|
|
|
|
if not missing:
|
|
details["role_count"] = int(
|
|
await session.scalar(text("select count(*) from auth.roles")) or 0
|
|
)
|
|
details["permission_count"] = int(
|
|
await session.scalar(text("select count(*) from auth.permissions")) or 0
|
|
)
|
|
details["module_count"] = int(
|
|
await session.scalar(text("select count(*) from content.modules")) or 0
|
|
)
|
|
details["content_count"] = int(
|
|
await session.scalar(text("select count(*) from content.content_items")) or 0
|
|
)
|
|
details["media_asset_count"] = int(
|
|
await session.scalar(text("select count(*) from content.media_assets")) or 0
|
|
)
|
|
details["access_plan_count"] = int(
|
|
await session.scalar(text("select count(*) from commerce.access_plans")) or 0
|
|
)
|
|
|
|
return ValidationResult(
|
|
ok=not missing,
|
|
missing_tables=tuple(missing),
|
|
details=details,
|
|
)
|