Jul 27 - Update code for scheduled tasks 2

This commit is contained in:
Nguyen Ngo
2026-07-27 15:26:06 -04:00
parent 44b577b59f
commit 455534ccda
9 changed files with 306 additions and 5 deletions
@@ -0,0 +1,56 @@
"""phase44 — scheduled inspection end date
Adds `end_date` to `scheduled_inspections`:
end_date DATE NULL -- last date this schedule may produce an occurrence
Separates the two ideas that `next_due_date` was carrying at once. `next_due_date`
is *mutable state* — ScheduledInspection.fulfill() rewrites it after every
completed inspection — whereas `end_date` is a *fixed boundary* set by the
manager and never touched by the app. NULL means "repeat indefinitely", which is
the behaviour every existing row has today, so no backfill and no schedule
changes cadence on deploy.
Only meaningful for recurring schedules; the create/edit routes force it to NULL
when frequency == 'once' (a one-time schedule already ends by deactivating on
completion).
Uses an INFORMATION_SCHEMA column-existence check — safe to re-run.
Additive only: no existing column is renamed, retyped or dropped. In particular
`next_due_date` keeps its name and its index — it is the API payload key the
iPad decodes and is used by the reminder cron.
"""
revision = 'phase44_sched_end_date'
down_revision = 'phase43_sched_recurrence'
branch_labels = None
depends_on = None
from alembic import op
import sqlalchemy as sa
def _column_exists(conn, table, column):
result = conn.execute(sa.text(
"SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS "
"WHERE TABLE_SCHEMA = DATABASE() "
"AND TABLE_NAME = :t AND COLUMN_NAME = :c"
), {"t": table, "c": column})
return result.scalar() > 0
def upgrade():
bind = op.get_bind()
if not _column_exists(bind, 'scheduled_inspections', 'end_date'):
op.execute(sa.text(
"ALTER TABLE scheduled_inspections "
"ADD COLUMN end_date DATE NULL AFTER next_due_date"
))
def downgrade():
bind = op.get_bind()
if _column_exists(bind, 'scheduled_inspections', 'end_date'):
op.execute(sa.text(
"ALTER TABLE scheduled_inspections DROP COLUMN end_date"
))