Skip to content

Commit

Permalink
fix: d1 migrations create to use the highest migration number plus one (
Browse files Browse the repository at this point in the history
#5184)

* fix: d1 migrations create to use the highest migration number plus one

* chore: lint

* chore: fix start migration number

* chore: add changeset
  • Loading branch information
Nora Söderlund authored Mar 11, 2024
1 parent 2680462 commit 046930e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/thick-yaks-cry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

fix: change d1 migrations create to use the highest migration number rather than the first non-existing migration number to allow for gaps in the migration files.
16 changes: 7 additions & 9 deletions packages/wrangler/src/d1/migrations/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,14 @@ function getMigrationNames(migrationsPath: string): Array<string> {
return migrations;
}

/**
* Returns the highest current migration number plus one, ignoring any missing numbers.
*/
export function getNextMigrationNumber(migrationsPath: string): number {
let highestMigrationNumber = -1;

for (const migration in getMigrationNames(migrationsPath)) {
const migrationNumber = parseInt(migration.split("_")[0]);

if (migrationNumber > highestMigrationNumber) {
highestMigrationNumber = migrationNumber;
}
}
const migrationNumbers = getMigrationNames(migrationsPath).map((migration) =>
parseInt(migration.split("_")[0])
);
const highestMigrationNumber = Math.max(...migrationNumbers, 0);

return highestMigrationNumber + 1;
}
Expand Down

0 comments on commit 046930e

Please sign in to comment.