Skip to content

Commit

Permalink
adds warnings about missed migrations
Browse files Browse the repository at this point in the history
The postgres migrator would silently skip migrations that were merged in from older branches if a newer migration had already been executed. It also wasn't very clear about during what statement/migration issues happened
  • Loading branch information
Aeolun committed Jan 15, 2025
1 parent 10d2230 commit 9cdfa1d
Show file tree
Hide file tree
Showing 2 changed files with 290 additions and 144 deletions.
14 changes: 11 additions & 3 deletions drizzle-orm/src/migrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface MigrationConfig {
}

export interface MigrationMeta {
tag: string;
sql: string[];
folderMillis: number;
hash: string;
Expand All @@ -29,7 +30,9 @@ export function readMigrationFiles(config: MigrationConfig): MigrationMeta[] {
throw new Error(`Can't find meta/_journal.json file`);
}

const journalAsString = fs.readFileSync(`${migrationFolderTo}/meta/_journal.json`).toString();
const journalAsString = fs
.readFileSync(`${migrationFolderTo}/meta/_journal.json`)
.toString();

const journal = JSON.parse(journalAsString) as {
entries: { idx: number; when: number; tag: string; breakpoints: boolean }[];
Expand All @@ -39,20 +42,25 @@ export function readMigrationFiles(config: MigrationConfig): MigrationMeta[] {
const migrationPath = `${migrationFolderTo}/${journalEntry.tag}.sql`;

try {
const query = fs.readFileSync(`${migrationFolderTo}/${journalEntry.tag}.sql`).toString();
const query = fs
.readFileSync(`${migrationFolderTo}/${journalEntry.tag}.sql`)
.toString();

const result = query.split('--> statement-breakpoint').map((it) => {
return it;
});

migrationQueries.push({
tag: journalEntry.tag,
sql: result,
bps: journalEntry.breakpoints,
folderMillis: journalEntry.when,
hash: crypto.createHash('sha256').update(query).digest('hex'),
});
} catch {
throw new Error(`No file ${migrationPath} found in ${migrationFolderTo} folder`);
throw new Error(
`No file ${migrationPath} found in ${migrationFolderTo} folder`,
);
}
}

Expand Down
Loading

0 comments on commit 9cdfa1d

Please sign in to comment.