Skip to content

Conversation

@jakebromberg
Copy link
Member

@jakebromberg jakebromberg commented Feb 2, 2026

Summary

  • Add database indexes for performance optimization on frequently-queried foreign key columns
  • Automate database migrations as part of the deployment pipeline
  • Fix orphaned migration file that was not tracked in Drizzle's journal
  • NEW: Add comprehensive migration testing and analysis tools

Changes

Migration Testing Tools (New)

Added infrastructure to catch migration issues before production deployment:

Tool Purpose
lint-migrations.js Static analysis for dangerous patterns
generate-rollback.mjs Auto-generate rollback SQL scripts
estimate-migration.mjs Estimate migration duration based on table sizes
create-schema-snapshot.mjs Export anonymized production schema
load-schema-snapshot.mjs Load snapshot into test database
test-migrations-snapshot.mjs Test migrations against production-like data

Lint Rules:

  • concurrent-index: Warns when CREATE INDEX misses CONCURRENTLY
  • not-null-no-default: Blocks ADD COLUMN NOT NULL without DEFAULT
  • missing-if-not-exists: Warns about missing idempotency guards
  • dangerous-ops: Blocks TRUNCATE, DROP TABLE without IF EXISTS

CI Integration:

  • Migration linting runs on PRs that modify shared/
  • Weekly snapshot testing workflow (Sundays 6 AM)

npm Scripts:

npm run lint:migrations          # Lint all migrations
npm run lint:migrations:changed  # Lint changed only (for CI)
npm run rollback:generate        # Generate rollback templates
npm run migration:estimate       # Estimate durations
npm run snapshot:test            # Test against snapshot

Database Indexes

Added B-tree indexes to improve query performance:

Table Index Columns
flowsheet flowsheet_show_id_idx show_id
flowsheet flowsheet_album_id_idx album_id
flowsheet flowsheet_rotation_id_idx rotation_id
show_djs show_djs_show_id_dj_id_idx (show_id, dj_id)
show_djs show_djs_dj_id_idx dj_id
bins bins_dj_id_idx dj_id
bins bins_album_id_idx album_id

Deployment Pipeline

Added migration jobs to the deployment workflow:

flowchart TB
    subgraph setup[Setup]
        A[Detect targets] --> B[Detect migration version]
    end
    
    subgraph build_phase[Build Phase]
        C[build-migrate] --> D[Build migrate:NNNN image]
        E[build] --> F[Build app images]
    end
    
    subgraph deploy_phase[Deploy Phase]
        G[migrate] --> H[Run migrations on EC2]
        H --> I[deploy]
        I --> J[Deploy app containers]
    end
    
    setup --> build_phase
    build_phase --> deploy_phase
Loading

Migration versioning: Images are tagged with the highest migration number (e.g., migrate:0028), providing traceability without manual version management.

Migration Fixes

  • Renamed orphaned 0024_anonymous_devices.sql0028_anonymous_devices.sql
  • Added missing journal entry and snapshot files
  • Fixed 0027_add-performance-indexes.sql to contain only index definitions

New Files

File Purpose
Dockerfile.migrate Lightweight image for running Drizzle migrations
.github/actions/run-migrations/action.yml Reusable action to run migrations on EC2
.github/workflows/migration-snapshot-test.yml Weekly snapshot testing workflow
scripts/lint-migrations.js Migration static analysis
scripts/generate-rollback.mjs Rollback script generator
scripts/estimate-migration.mjs Duration estimator
scripts/*-snapshot.mjs Snapshot testing scripts
shared/database/src/migrations/rollbacks/ Rollback scripts for all migrations

Test plan

  • CI passes (migrations run against fresh database in integration tests)
  • Manual: verify Dockerfile.migrate builds locally
  • Manual: run npm run lint:migrations to verify linter works
  • Manual: run npm run migration:estimate 0027_add-performance-indexes.sql
  • First deployment: verify migrations run before app deployment

Notes

Migrations are already tested in CI via init-db.mjs which runs npm run drizzle:migrate during test environment setup. Any PR that modifies shared/** (including migrations) triggers integration tests.

Jake Bromberg added 5 commits February 1, 2026 22:56
Add roster resource to access control statements. Add roster write
permission to stationManager role. Add new admin role with full
permissions including roster management.
Refactor email system to use discriminated union pattern with unified
sendEmail() function. Add accountSetup email type that sends a "Welcome!
Set up your password" message to new users created by admin.

The sendResetPassword callback now detects new users (empty realName)
and sends accountSetup email instead of passwordReset email.

- Add WXYCEmail discriminated union type
- Add sendEmail() function with getEmailContent() factory
- Add accountSetup email type with welcome messaging
- Update sendResetPassword to detect new vs existing users
- Add comprehensive unit tests for all email types
- Add capabilities text[] column to auth_user table
- Include capabilities in JWT payload via definePayload
- Register capabilities as Better Auth additionalField
- Add unit tests for capability storage and JWT structure
- Add database indexes for frequently-queried columns:
  - flowsheet: show_id, album_id, rotation_id
  - show_djs: composite (show_id, dj_id) and dj_id
  - bins: dj_id, album_id

- Add migration infrastructure to CI/CD:
  - Dockerfile.migrate for running drizzle-kit migrations
  - run-migrations GitHub Action
  - Migration jobs in deploy-base.yml workflow
  - Migrations tagged by count (e.g., migrate:0028)

- Fix orphaned migration file:
  - Rename 0024_anonymous_devices.sql to 0028
  - Add missing journal entry and snapshots
Add comprehensive migration testing infrastructure:

- Static analysis (lint-migrations.js): Detects dangerous patterns
  like missing CONCURRENTLY, NOT NULL without DEFAULT, missing
  IF NOT EXISTS guards, and destructive operations

- Rollback generator (generate-rollback.mjs): Auto-generates rollback
  SQL scripts from migrations with risk assessment and data loss warnings

- Runtime estimation (estimate-migration.mjs): Estimates migration
  duration based on table sizes and operation types

- Snapshot testing scripts: Create, load, and test migrations against
  production-like schema snapshots

- Weekly CI workflow (migration-snapshot-test.yml): Automated testing
  against production snapshots every Sunday

- CI integration: Added migration linting to PR checks

Includes rollback files for migrations 0000-0028.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant