storage icon indicating copy to clipboard operation
storage copied to clipboard

fix: make storage migrations idempotent

Open TylerHillery opened this issue 1 year ago • 2 comments

What kind of change does this PR introduce?

The storage.migrations table can sometimes fall out of sync, requiring migrations to be replayed on databases that already applied them. This PR makes all migrations fully idempotent, allowing you to clear storage.migrations and safely rerun the entire migration chain without errors, ensuring the final database state matches a clean first-run.

Current behavior

  • Several functions use drop function if exists without cascade causing issues when it has dependencies like RLS policies
  • Some roles are created without checking for existence
  • migrations/tenant/0038-iceberg-catalog-flag-on-buckets.sql creates an index on bucket_id without verifying that the column exists. This column gets renamed later on so it causes issues on the second run.
  • iceberg catalog id fks only get created if the catalog_id column doesn't exist, so if you run the migration again after clearing out the migrations table it's going to drop the fk constraint but it wont recreate it because the column is still there.

New behavior

  • My initial logic for creating the functions in an idempotent way is tricky because create or replace is already idempotent but it doesn't work if the return type of the function changes like it does with storage.get_size_by_bucket() so this needs to be dropped first. Dropping first introduces a problem though if there something else dependent on that function. If there is we can't drop it without cascade but that means we would remove anything dependent on that function including RLS policies. In general I think we should avoid cascade where we can and rely on create or replace if we make updates to a function such as changing return type we might want to add a suffix with _v2 instead

  • Added existence checks when creating roles (same approach as super_user)

  • Updated 0038-iceberg-catalog-flag-on-buckets.sql to check for bucket_id before creating the index, since it’s renamed to bucket_name in migration 0048

  • Added step in CI to drop the migrations table and rerun migrations to ensure we don't generate any new migrations that are not idempotent. I run pg_dump after the initial migration, then delete the migrations table and rerun the migrations taking another pg_dump comparing the two. I ignore the storage.migrations table as the times the migrations took place are always going to differ.

  • Separate the alter table add column catalog_id for both iceberg_namespaces and iceberg_tables so that the fk creation happens in a separate statement to ensure it gets created no matter how many times you run the migrations in a row by first checking if it doesn't exist.

  • migration 0040 drop triggers if they exists before creating them

  • New step in GitHub Action CI job to catch these ahead of time. It does the following:

    • will take a pg_dump of the database after running all migrations for the first time
    • Runs new script test-migration-idempotency.ts that runs all migrations one by one, resets them and reruns them. To do this I had to to make the following updates
      • Include create-migrations-tables to the DBMigration type generation. This was needed because the resetMigration requires you to pass in the previous migration if you want to clear out the current migration. Well for the first migration there is actually the create-migration-tables migration first but since the that wasn't in the keyof typeof DBMigration TypeScript was complaining.
      • I also modified resetMigration to make tenantId optional like runMigrationsOnTenant does. I then updated the function to only update the tenant state if the tenant id was being passed in. There is no "tenants" table when running in single tenant mode so updating that state doesn't make sense.
      • Takes another pg_dump and compare the before and after to make sure the db ends up in the same state

Additional context

Also to clarify resetMigration doesn't do a "down" migration, it simply clears migrations table.

TylerHillery avatar Nov 18 '25 18:11 TylerHillery

Pull Request Test Coverage Report for Build 19942944876

Details

  • 1 of 8 (12.5%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.003%) to 75.924%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/internal/database/migrations/migrate.ts 0 7 0.0%
Totals Coverage Status
Change from base Build 19835797812: -0.003%
Covered Lines: 25415
Relevant Lines: 33196

💛 - Coveralls

coveralls avatar Nov 18 '25 18:11 coveralls

Putting this back in draft, there is more testing I need to do

TylerHillery avatar Nov 20 '25 13:11 TylerHillery