69 lines
2.5 KiB
Python
69 lines
2.5 KiB
Python
# #region Alembic.MaintenanceDateFormat [C:2] [TYPE Module] [SEMANTICS alembic,maintenance,date-format,settings]
|
|
# @ingroup Alembic
|
|
# @BRIEF Add maintenance_settings.date_format (configurable banner date/time format).
|
|
# @LAYER Database
|
|
# @RELATION DEPENDS_ON -> [Models.Maintenance.MaintenanceModels]
|
|
# @INVARIANT date_format is non-nullable with server default 'YYYY.MM.DD hh:mm:ss'.
|
|
# @RATIONALE The maintenance banner date rendering was hardcoded to strftime('%Y-%m-%d %H:%M');
|
|
# this column makes it user-configurable (friendly YYYY/MM/DD tokens).
|
|
# @REJECTED Making the column nullable was rejected — the renderer always needs a format.
|
|
|
|
"""add date_format to maintenance_settings
|
|
|
|
Revision ID: r2s3t4u5v6w7
|
|
Revises: 9a5a3b802c49
|
|
Create Date: 2026-08-10 10:00:00.000000
|
|
"""
|
|
|
|
from collections.abc import Sequence
|
|
|
|
import sqlalchemy as sa
|
|
from sqlalchemy import inspect
|
|
|
|
from alembic import op
|
|
|
|
# revision identifiers, used by Alembic.
|
|
revision: str = "r2s3t4u5v6w7"
|
|
down_revision: str | Sequence[str] | None = "9a5a3b802c49"
|
|
branch_labels: str | Sequence[str] | None = None
|
|
depends_on: str | Sequence[str] | None = None
|
|
|
|
|
|
def _table_exists(table: str) -> bool:
|
|
"""Check if a table exists in the current database connection."""
|
|
return inspect(op.get_bind()).has_table(table)
|
|
|
|
|
|
def _column_exists(table: str, column: str) -> bool:
|
|
"""Check if a column exists in the given table."""
|
|
inspector = inspect(op.get_bind())
|
|
return any(c["name"] == column for c in inspector.get_columns(table))
|
|
|
|
|
|
def upgrade() -> None:
|
|
"""Add date_format column to maintenance_settings if missing."""
|
|
# maintenance_settings is a create_all()-only table on a fresh install (no base
|
|
# migration creates it); guard so a clean-DB upgrade is a no-op and the ORM schema
|
|
# initialization creates it with the final shape.
|
|
if _table_exists("maintenance_settings") and not _column_exists(
|
|
"maintenance_settings", "date_format"
|
|
):
|
|
op.add_column(
|
|
"maintenance_settings",
|
|
sa.Column(
|
|
"date_format",
|
|
sa.String(),
|
|
nullable=False,
|
|
server_default="YYYY.MM.DD hh:mm:ss",
|
|
),
|
|
)
|
|
|
|
|
|
def downgrade() -> None:
|
|
"""Drop the date_format column (values are discarded)."""
|
|
if _table_exists("maintenance_settings") and _column_exists(
|
|
"maintenance_settings", "date_format"
|
|
):
|
|
op.drop_column("maintenance_settings", "date_format")
|
|
# #endregion Alembic.MaintenanceDateFormat
|