fix: make storage migrations idempotent
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 existswithoutcascadecausing issues when it has dependencies like RLS policies - Some roles are created without checking for existence
migrations/tenant/0038-iceberg-catalog-flag-on-buckets.sqlcreates an index onbucket_idwithout 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_idcolumn 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 replaceis already idempotent but it doesn't work if the return type of the function changes like it does withstorage.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 withoutcascadebut that means we would remove anything dependent on that function including RLS policies. In general I think we should avoidcascadewhere we can and rely oncreate or replaceif we make updates to a function such as changing return type we might want to add a suffix with_v2instead -
Added existence checks when creating roles (same approach as super_user)
-
Updated
0038-iceberg-catalog-flag-on-buckets.sqlto check forbucket_idbefore creating the index, since it’s renamed tobucket_namein migration0048 -
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.migrationstable as the times the migrations took place are always going to differ. -
Separate the
alter table add column catalog_idfor bothiceberg_namespacesandiceberg_tablesso 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
0040drop 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_dumpof the database after running all migrations for the first time - Runs new script
test-migration-idempotency.tsthat 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-tablesto theDBMigrationtype generation. This was needed because theresetMigrationrequires you to pass in the previous migration if you want to clear out the current migration. Well for the first migration there is actually thecreate-migration-tablesmigration first but since the that wasn't in thekeyof typeof DBMigrationTypeScript was complaining. - I also modified
resetMigrationto maketenantIdoptional likerunMigrationsOnTenantdoes. 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_dumpand compare the before and after to make sure the db ends up in the same state
- Include
- will take a
Additional context
Also to clarify resetMigration doesn't do a "down" migration, it simply clears migrations table.
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 | |
|---|---|
| Change from base Build 19835797812: | -0.003% |
| Covered Lines: | 25415 |
| Relevant Lines: | 33196 |
💛 - Coveralls
Putting this back in draft, there is more testing I need to do