Skip to main content

create_patients_name_order_index

Add the patient-list ordering index to caches created before it existed.

metadata.create_all only builds a table's indexes at the moment it creates the table, so a cache predating ix_patients_name_order (declared alongside the patients ORM) never gains the index on open. This migration creates it in place, so an existing pod gets the same index-satisfied ORDER BY a fresh cache has from its ORM.

Unlike a per-type schema migration this is not tied to a version bump — the record shape is unchanged, only a secondary index is added — so it lives here with the other one-time cache migrations. The DDL is taken from the ORM Index object rather than re-spelled, so the two cannot drift.

Idempotent: a no-op once the index exists (which includes every fresh cache, where create_all already built it). Building the index sorts the whole patients table, so the first run on a large existing cache costs a moment (~0.1s at 200k patients); the migration marker then stops it re-running.

Module

Functions

apply

def apply(cache: CacheProtocol)> int:

Create the patient-list ordering index if it is missing.

Arguments

  • cache: The cache backend.

Returns 1 if the index was created, 0 if it already existed.

Global variables

  • NAME - Identifier recorded in the migration marker once this has run.