Skip to content

Commit af594ab

Browse files
committed
fix(drizzle-kit): handle missing snapshot in migrateToFoldersV3 (fixes #5591)
1 parent 1a01082 commit af594ab

1 file changed

Lines changed: 15 additions & 12 deletions

File tree

drizzle-kit/src/cli/commands/utils.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,16 +1020,17 @@ export const migrateToFoldersV3 = (out: string) => {
10201020
const [snapshotPrefix, ...rest] = entry.tag.split('_');
10211021
const migrationName = rest.join('_');
10221022
const oldSnapshotPath = join(metaPath, `${snapshotPrefix}_snapshot.json`);
1023-
1024-
if (!existsSync(oldSnapshotPath)) {
1025-
// If for some reason this happens we need to throw an error
1026-
// This can't happen unless there were wrong drizzle-kit migrations usage
1027-
console.error('No snapshot was found');
1028-
process.exit(1);
1023+
const snapshotExists = existsSync(oldSnapshotPath);
1024+
1025+
if (!snapshotExists) {
1026+
// This can happen when the migration was created before drizzle-kit started
1027+
// tracking snapshots (e.g. the very first migration in older projects).
1028+
// We skip creating a snapshot.json for this entry and only migrate the SQL file.
1029+
console.warn(
1030+
`Warning: No snapshot found for migration "${entry.tag}". This migration will be migrated without a snapshot.`,
1031+
);
10291032
}
10301033

1031-
const oldSnapshot = readFileSync(oldSnapshotPath);
1032-
10331034
// Reading SQL files
10341035
let oldSqlPath = join(out, `${entry.tag}.sql`);
10351036
const sqlFileFromJournal = join(out, `${entry.tag}.sql`);
@@ -1046,10 +1047,12 @@ export const migrateToFoldersV3 = (out: string) => {
10461047
const oldSql = readFileSync(oldSqlPath);
10471048

10481049
mkdirSync(join(out, `${folderName}_${migrationName}`));
1049-
writeFileSync(
1050-
join(out, `${folderName}_${migrationName}/snapshot.json`),
1051-
oldSnapshot,
1052-
);
1050+
if (snapshotExists) {
1051+
writeFileSync(
1052+
join(out, `${folderName}_${migrationName}/snapshot.json`),
1053+
readFileSync(oldSnapshotPath),
1054+
);
1055+
}
10531056
writeFileSync(
10541057
join(out, `${folderName}_${migrationName}/migration.sql`),
10551058
oldSql,

0 commit comments

Comments
 (0)