Skip to content

Commit 0daf8ea

Browse files
kathmbeckKatherine Beck
andauthored
fix: stagger starter migrations (#8216)
πŸŽ‰ Thanks for submitting a pull request! πŸŽ‰ #### Summary Fixes #<replace_with_issue_number> <!-- Explain the **motivation** for making this change. What existing problem does the pull request solve and how? --> --- For us to review and ship your PR efficiently, please perform the following steps: - [ ] Open a [bug/issue](https://github.com/netlify/cli/issues/new/choose) before writing your code πŸ§‘β€πŸ’». This ensures we can discuss the changes and get feedback from everyone that should be involved. If you\`re fixing a typo or something that\`s on fire πŸ”₯ (e.g. incident related), you can skip this step. - [ ] Read the [contribution guidelines](../CONTRIBUTING.md) πŸ“–. This ensures your code follows our style guide and passes our tests. - [ ] Update or add tests (if any source code was changed or added) πŸ§ͺ - [ ] Update or add documentation (if features were changed or added) πŸ“ - [ ] Make sure the status checks below are successful βœ… **A picture of a cute animal (not mandatory, but encouraged)** --------- Co-authored-by: Katherine Beck <katherine.beck@netlify.com>
1 parent a410f87 commit 0daf8ea

2 files changed

Lines changed: 13 additions & 8 deletions

File tree

β€Žsrc/commands/database/db-init.tsβ€Ž

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,13 @@ const scaffoldStarter = async (
175175
cwd: projectRoot,
176176
})
177177

178-
// The seed's timestamp is generated AFTER drizzle-kit runs, so it
179-
// lexicographically sorts after whatever drizzle-kit produced. If they
180-
// happen to land in the same second, `create_planets` < `seed_planets`
181-
// alphabetically still runs the CREATE TABLE first. We use the same
182-
// directory layout drizzle-kit uses so both migrations look consistent
183-
// in the migrations/ folder.
184-
const seedDirName = `${generateNextPrefix([], 'timestamp')}_${SEED_MIGRATION_NAME}`
178+
// The seed's timestamp is generated AFTER drizzle-kit runs. We read the
179+
// migrations directory so `generateNextPrefix` can detect a collision and
180+
// bump the prefix by one second if the two migrations landed in the same
181+
// second β€” the deploy-time validator rejects duplicate numeric prefixes.
182+
const existingMigrations = await readDirectoryEntries(migrationsDirectory)
183+
const seedPrefix = generateNextPrefix(existingMigrations, 'timestamp')
184+
const seedDirName = `${seedPrefix}_${SEED_MIGRATION_NAME}`
185185
const seedDir = join(migrationsDirectory, seedDirName)
186186
await mkdir(seedDir, { recursive: true })
187187
await writeFile(join(seedDir, 'migration.sql'), DRIZZLE_SEED_SQL, { flag: 'wx' })

β€Žsrc/commands/database/db-migration-new.tsβ€Ž

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@ export const detectNumberingScheme = (existingNames: string[]): NumberingScheme
4747

4848
export const generateNextPrefix = (existingNames: string[], scheme: NumberingScheme): string => {
4949
if (scheme === 'timestamp') {
50-
return utcTimestampPrefix()
50+
const now = utcTimestampPrefix()
51+
const existingPrefixes = new Set(existingNames.map((n) => /^(\d+)/.exec(n)?.[1]).filter(Boolean))
52+
if (existingPrefixes.has(now)) {
53+
return utcTimestampPrefix(new Date(Date.now() + 1000))
54+
}
55+
return now
5156
}
5257

5358
const prefixes = existingNames.map((name) => {

0 commit comments

Comments
Β (0)