Schema migrations and domain model evolution
Kener uses ordered Knex migrations to create and change the database model that supports monitoring, public status data, operations, alerts, subscriptions, and access control. Each migration moves an existing database toward the current domain model while keeping old data usable across SQLite, PostgreSQL, and MySQL. This page is a child of Data persistence; see Monitoring data and history, Incidents and maintenance events, Alerting and subscriptions, and Identity, invitations, and access control for the user-facing flows built on this model.
Migration lifecycle
Migration history
The migration history is the ordered record of schema change. Knex reads timestamped files from migrations/ and records each applied file in knex_migrations, so a database receives only the changes that it does not already have.
The first migration establishes the core model. Later migrations add domains, add fields, change relationships, move data, or add indexes for important reads. The migration files include an up change and a down reversal, but a reversal can be less lossless than an upgrade when the newer model supports more relationships than the older one.
References: migrations/20250111153517_init.ts• L3-145 knexfile.ts• L58-76
Startup migration run
The production entry point runs migrations before it loads seed data and starts background work. It first repairs old .js migration names in knex_migrations because the files now use the .ts extension. This keeps the database history matched to the files on disk.
The migration step uses the configured application database. A migration error is logged by the startup wrapper, which then continues to the seed and startup steps. Operators must therefore treat a migration error as a blocked release even if the process continues to listen.
The following flow shows how a new process prepares its data store before normal work begins.
flowchart LR
A[Process starts] --> B[Open migration database]
B --> C[Repair old migration names]
C --> D[Apply pending migrations]
D --> E[Apply seed data]
E --> F[Start schedulers and queues]
D -. failure is logged .-> G[Process may still continue]
References: scripts/main.ts• L69-107 scripts/fix-migration-ext.ts• L13-39 src/…/startup.ts• L9-31
Database portability
The same domain model runs on SQLite, PostgreSQL, and MySQL. Knex selects the driver from DATABASE_URL, while migrations use portable schema calls where possible and handle known driver differences where needed.
SQLite cannot alter some constraints in place. The multi-monitor alert changes therefore rebuild the affected table on SQLite, copy the data, and restore the indexes. This makes the migration path part of the data contract: a schema change must preserve both the records and the constraints expected by each supported database.
Some migrations also tune the database without changing the domain shape. The monitoring-data maintenance migration applies only to PostgreSQL and leaves other supported drivers unchanged, so portability includes both schema behavior and safe handling of driver-specific maintenance features.
References: knexfile.ts• L4-8 knexfile.ts• L84-112 migrations/20260325120000_multi_monitor_alerts.ts• L96-169 migrations/20260328120000_fix_sqlite_monitor_alerts_config_nullable.ts• L14-81 migrations/20260831120000_monitoring_data_autovacuum.ts• L12-28
Domain schema
Monitoring records
The monitoring model separates a monitor definition from the observations produced for it. A monitor holds its stable identity and check settings. monitoring_data holds time-based status and latency records, with one record for a monitor and timestamp pair. The model also keeps raw status and error text so later status confirmation can distinguish an observed result from the committed result.
The schema evolved from basic status storage to support external links, hidden monitors, per-monitor settings, confirmation thresholds, and history queries. Index changes follow the main history reads rather than changing the meaning of a record. The older monitor_alerts table remains part of the original alert state model, while newer alert tables handle configured rules and their firings.
Monitoring history also has a high-churn storage concern. Nightly retention removes old monitoring_data rows, so PostgreSQL receives lower autovacuum and autoanalyze thresholds for this table and refreshes its planner statistics. MySQL and SQLite use their own row-reclaim behavior and do not receive this PostgreSQL-only setting.
References: migrations/20250111153517_init.ts• L4-26 migrations/20260202090915_add_new_columns_data_monitors.ts• L3-13 migrations/20260613120000_add_confirmation_threshold_and_raw_status.ts• L3-13 migrations/20260617120000_add_monitoring_data_covering_index.ts• L3-19 migrations/20260831120000_monitoring_data_autovacuum.ts• L3-21 src/…/db.ts• L5-89
Pages and operational events
Pages provide named public status surfaces, and page-monitor records connect a page to the monitors it shows. A page has a unique path and presentation settings. The junction records hold page-specific monitor settings and display position, so the same monitor can appear on more than one page with different presentation rules.
Incidents and maintenance are separate operational event models. Incidents have a time range, state, source, type, global scope, monitor links, and comments. Maintenance has a recurrence rule and duration, then creates dated maintenance events and optional monitor links. Foreign-key cascades remove page, maintenance, and alert association records when their owning record is removed.
These relationships let public views combine current monitor state with incident history and scheduled maintenance without putting event data into the monitor definition itself. See Public status pages and Incidents and maintenance events for the presentation and event lifecycles.
References: migrations/20260107120000_add_pages_tables.ts• L3-35 migrations/20260310120000_alter_page_subheader_to_text.ts• L3-12 migrations/20260313030304_add_position_page_monitors.ts• L3-19 migrations/20250111153517_init.ts• L108-144 migrations/20260109120000_add_maintenances_tables.ts• L3-46 migrations/20260219165446_add_global_colum_incidents.ts• L3-26 src/…/db.ts• L318-405 src/…/db.ts• L450-654
Alert and notification records
The alert model stores both the rule and the result of applying that rule. A monitor alert configuration defines what to watch, how many failures or successes are needed, whether to create an incident, and which trigger should deliver the alert. A trigger stores the delivery setup for a channel such as email or a webhook. monitor_alerts_v2 records each configured alert state and can link it to the incident it created.
The model changed from one alert configuration per monitor to configurations that can cover many monitors. A junction table stores the configuration-to-monitor relationship, while each firing keeps its own monitor tag. The migration copies old one-monitor links into the new relationship and backfills existing firings, so old alert history remains tied to the monitor that caused it.
Subscriptions use a separate three-part model: a subscriber identity, one or more delivery methods, and event subscriptions. The newer tables replace the older subscriber and subscription tables without changing the basic product goal. General email templates provide shared message content outside the per-alert trigger data. See Alert routing and notification delivery for the delivery flow.
References: migrations/20250111153517_init.ts• L15-25 migrations/20260124120000_add_monitor_alerts_config_tables.ts• L3-52 migrations/20260124120001_monitor_alert_config_id.ts• L3-51 migrations/20260325120000_multi_monitor_alerts.ts• L3-32 migrations/20250417144954_add_subscription_tables.ts• L3-50 migrations/20260128120000_redesign_subscription_system.ts• L3-137 migrations/20260128145145_add_general_email_templates.ts• L3-15 src/…/db.ts• L656-804 src/…/db.ts• L849-941
Users and role records
The user model stores account identity, password state, verification state, activity, and ownership. Local accounts use password authentication, while OpenID Connect (OIDC) accounts identify their authentication provider and provider subject in the user record. OIDC accounts keep the required password field populated with an empty value because they do not use a local password.
Role-based access control stores roles and permissions as shared records, with junction tables that allow a user to have many roles and a role to have many permissions. OIDC group-to-role mappings connect provider group names to those shared roles, so an external identity can receive application access through its provider groups.
The role migration preserves existing access by mapping legacy values to admin, editor, or member before removing the old role field. The resulting model makes role membership and permission status separate from the user record. Invitations extend this area with expiring, stateful records that connect an inviter to a pending account action.
References: migrations/20250111153517_init.ts• L82-105 migrations/20250315091330_add_table_invitations.ts• L3-29 migrations/20260226120000_add_is_owner_to_users.ts• L3-17 migrations/20260330120000_add_rbac_tables.ts• L3-50 migrations/20260331120000_remove_role_from_users.ts• L3-61 migrations/20260831130000_add_oidc_support.ts• L3-35 src/…/db.ts• L237-298
Images and shared content
Images are stored as data records with a stable identifier, media type, original name, dimensions, and byte size. This gives the application one place to store uploaded image data and its display metadata instead of placing large image content in each domain record.
Monitor and page records can carry image-related values for their public presentation. The image migration does not define a general foreign key from those records to images, so cleanup and reference rules remain a domain concern rather than a database cascade.
MySQL stores image data in longtext so image content can exceed the 65,535-byte limit of its regular text type. The migration changes only MySQL and blocks a rollback when existing image data would be truncated; PostgreSQL and SQLite keep their existing text behavior.
References: migrations/20260106120000_add_images_table.ts• L3-20 migrations/20261019120000_widen_images_data_on_mysql.ts• L3-21 migrations/20250111153517_init.ts• L46-66 migrations/20260107120000_add_pages_tables.ts• L5-16 src/…/db.ts• L65-89 src/…/db.ts• L427-493
Evolution patterns
Additive model changes
Most domain changes are additive. A migration adds a nullable field or a field with a safe default, then the repositories and typed records start using it. This pattern lets old rows remain valid while new code gains a new capability.
Examples include monitor visibility and settings, incident source and scope, page monitor position, maintenance impact, and monitoring error and raw status. Index migrations improve query paths without changing domain meaning. Seed files then provide the default roles, permissions, pages, monitors, or site values expected for a new installation.
References: migrations/20250201152526_add_new_column_to_incidents.ts• L3-15 migrations/20250222153624_add_incident_source_column.ts• L3-15 migrations/20260117120000_add_is_hidden_to_monitors.ts• L3-15 migrations/20260120120000_add_monitor_settings_json.ts• L3-15 migrations/20260123110239_maintenace_monitor_status.ts• L3-18 seeds/roles.ts• L1-200 seeds/permissions.ts• L1-200
Storage maintenance changes
Storage maintenance changes keep a busy persisted table usable without changing the meaning of its records. The monitoring-data autovacuum migration responds to nightly retention churn by making PostgreSQL clean dead rows and refresh query statistics sooner, while its rollback restores the database defaults.
This change is separate from the domain model. It supports the monitoring history lifecycle because stale dead rows and planner statistics can slow history reads after repeated retention deletes. Existing PostgreSQL installations with table bloat still need a one-time manual vacuum and analyze operation before the new thresholds can help.
References: migrations/20260831120000_monitoring_data_autovacuum.ts• L3-28
Relationship changes
When a domain moves from one owner to a shared relationship, the migration must create the new link model before it removes the old constraint. Existing data is copied into the new links, and records that need the former owner identity are backfilled before the old field becomes optional or disappears.
The alert redesign follows this pattern for many-monitor configurations. The access-control redesign follows it for one role per user becoming many roles per user. Both changes keep compatibility concerns visible in the migration history instead of hiding them in later reads.
References: migrations/20260325120000_multi_monitor_alerts.ts• L36-94 migrations/20260325120000_multi_monitor_alerts.ts• L171-214 migrations/20260330120000_add_rbac_tables.ts• L26-49 migrations/20260331120000_remove_role_from_users.ts• L12-61
Current domain contract
src/…/db.ts is the readable contract for the current persisted model. It names the record shape and the status values used by repositories and higher layers, including the latest fields for monitoring, pages, events, alerts, subscriptions, users, roles, and images.
The type file is not a replacement for migration history. Use it to understand the current shape, then read the migration that introduced a field or relationship when a change must preserve existing data. The repository layer exposes these records by domain, which keeps most application code independent of the migration sequence. See Data persistence for that access boundary.
References: src/…/db.ts• L5-24 src/…/db.ts• L237-298 src/…/db.ts• L318-405 src/…/db.ts• L450-654 src/…/db.ts• L656-941 src/…/dbimpl.ts• L5-49
Change checklist
When these files or sections are changed, remember to consider these:
- Add a migration for every change to a persisted field, constraint, relationship, or index. Keep the migration file order aligned with the data dependency order.
- When changing a relationship, preserve existing links before making the old field nullable or removing it. Check both the
uppath and the data loss behavior ofdown. - Keep src/…/db.ts aligned with the final schema, especially optional fields, status values, and old subscription types kept for compatibility.
- For alert changes, update the configuration-to-monitor links and the per-firing monitor identity together. A rule that covers many monitors must still identify the monitor that fired it.
- For user access changes, preserve role membership before removing legacy user fields. Reconcile the role and permission seeds with the new records.
- For OIDC changes, keep the provider and provider-subject fields aligned with account creation and login rules. Preserve the unique subject constraint and the group-to-role mapping link to seeded roles.
- Test SQLite table rebuild paths as well as PostgreSQL and MySQL paths when a change alters a constraint or column type.
- If migration filenames change extension, update existing
knex_migrationsnames or run the migration extension repair before applying new files. - When monitoring-data retention or write volume changes, reassess PostgreSQL cleanup and statistics settings and the need for a one-time maintenance pass on existing tables.
- When image data size changes, check the limits for each supported database and confirm that a rollback cannot truncate existing image content.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A migration fails during startup | The process logs the error and can continue toward seed and runtime startup with an old or partial schema. | Stop the release on migration errors and inspect the database before allowing normal traffic. |
| A relationship migration removes the old link before copying it | Existing monitors, alert firings, or role assignments lose their association. | Create the junction table first and backfill from the old field before changing its constraint. |
| SQLite runs a constraint change that is not rebuilt | The new column rule can differ from PostgreSQL and MySQL, or the migration can fail. | Use and verify the explicit SQLite rebuild path for affected tables. |
| An alert configuration loses its per-monitor firing identity | A multi-monitor rule cannot show which monitor caused the notification or incident. | Keep the configuration junction and the firing monitor_tag in sync. |
| A role migration finds an unmapped legacy role | The user can lose the intended access level or fail to receive a valid role assignment. | Use the defined legacy map and verify the seeded target roles exist before the backfill. |
| An OIDC subject is reused or a group maps to a missing role | A provider account can collide with another user, or group-based access cannot resolve to a valid role. | Keep oidc_sub unique and require each group mapping to reference an existing seeded role. |
| A rollback reduces many links to one link | Extra monitor relationships or role memberships can be deleted or collapsed. | Treat destructive rollback as a data operation and back up or verify data before using it. |
MySQL rolls images.data back while an image is larger than 65,535 bytes | The rollback fails instead of allowing image data to be truncated. | Check image row sizes before rollback and keep longtext when existing data exceeds the regular text limit. |
| PostgreSQL retains dead monitoring-data rows after nightly retention deletes | History reads can slow down and consume more connection capacity as table cleanup and planner statistics lag. | Apply the table maintenance migration and run the documented one-time vacuum and analyze operation for existing bloat. |
Key files
| File | Why to read it |
|---|---|
| knexfile.ts | Shows the supported database drivers and the migration and seed directories. |
| scripts/main.ts | Shows when migration repair, migration application, seeding, and runtime startup occur. |
| migrations/20250111153517_init.ts | Defines the original monitoring, user, incident, alert, and site-data foundation. |
| migrations/20260107120000_add_pages_tables.ts | Defines pages and the page-to-monitor relationship. |
| migrations/20260109120000_add_maintenances_tables.ts | Defines recurring maintenance, monitor impact, and dated maintenance events. |
| migrations/20260124120000_add_monitor_alerts_config_tables.ts | Defines alert rules and their trigger relationship. |
| migrations/20260325120000_multi_monitor_alerts.ts | Shows a data-preserving change from one-monitor alerts to many-monitor alerts. |
| migrations/20260128120000_redesign_subscription_system.ts | Defines subscriber identities, delivery methods, and event subscriptions. |
| migrations/20260330120000_add_rbac_tables.ts | Defines roles, permissions, and their many-to-many links. |
| migrations/20260331120000_remove_role_from_users.ts | Shows how legacy user roles move into the RBAC model. |
| migrations/20260831130000_add_oidc_support.ts | Adds provider identity fields for OIDC users and maps OIDC groups to application roles. |
| migrations/20260831120000_monitoring_data_autovacuum.ts | Shows how monitoring-history retention receives PostgreSQL-specific cleanup and statistics settings. |
| migrations/20261019120000_widen_images_data_on_mysql.ts | Keeps MySQL image storage large enough for image data and prevents unsafe rollback. |
| src/…/db.ts | Summarizes the current persisted record shapes and domain status values. |