Skip to content

Commit 101a86c

Browse files
committed
fix: make migrations resilient for 1.1.1
1 parent c30ba10 commit 101a86c

3 files changed

Lines changed: 58 additions & 3 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "Loopndroll",
3-
"version": "1.1.0",
3+
"version": "1.1.1",
44
"private": true,
55
"type": "module",
66
"packageManager": "pnpm@10.33.0",

src/bun/db/migrations.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,29 @@ function nowIsoString() {
705705
return new Date().toISOString();
706706
}
707707

708+
function shouldIgnoreMigrationStatementError(
709+
sqlite: Database,
710+
statement: string,
711+
error: unknown,
712+
) {
713+
const message = error instanceof Error ? error.message : String(error);
714+
if (!message.toLowerCase().includes("duplicate column name:")) {
715+
return false;
716+
}
717+
718+
const match = /^\s*alter\s+table\s+(\w+)\s+add\s+column\s+(\w+)/i.exec(statement);
719+
if (!match) {
720+
return false;
721+
}
722+
723+
const [, tableName, columnName] = match;
724+
const rows = sqlite.query(`pragma table_info(${tableName})`).all() as Array<{
725+
name?: string;
726+
}>;
727+
728+
return rows.some((row) => row.name === columnName);
729+
}
730+
708731
export function applyAppMigrations(
709732
sqlite: Database,
710733
migrations: readonly AppMigration[] = appMigrations,
@@ -727,7 +750,15 @@ export function applyAppMigrations(
727750

728751
const applyMigration = sqlite.transaction((migration: AppMigration) => {
729752
for (const statement of migration.statements) {
730-
sqlite.exec(statement);
753+
try {
754+
sqlite.exec(statement);
755+
} catch (error) {
756+
if (shouldIgnoreMigrationStatementError(sqlite, statement, error)) {
757+
continue;
758+
}
759+
760+
throw error;
761+
}
731762
}
732763

733764
insertAppliedMigration.run(migration.id, migration.name, nowIsoString());

src/bun/loopndroll.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2476,6 +2476,22 @@ function configureDatabase(db) {
24762476
}
24772477
}
24782478
2479+
function shouldIgnoreMigrationStatementError(db, statement, error) {
2480+
const message = error instanceof Error ? error.message : String(error);
2481+
if (!message.toLowerCase().includes("duplicate column name:")) {
2482+
return false;
2483+
}
2484+
2485+
const match = /^\\s*alter\\s+table\\s+(\\w+)\\s+add\\s+column\\s+(\\w+)/i.exec(statement);
2486+
if (!match) {
2487+
return false;
2488+
}
2489+
2490+
const [, tableName, columnName] = match;
2491+
const rows = db.query(\`pragma table_info(\${tableName})\`).all();
2492+
return rows.some((row) => row.name === columnName);
2493+
}
2494+
24792495
function applyMigrations(db) {
24802496
db.exec(\`create table if not exists schema_migrations (
24812497
id integer primary key,
@@ -2490,7 +2506,15 @@ function applyMigrations(db) {
24902506
);
24912507
const applyMigration = db.transaction((migration) => {
24922508
for (const statement of migration.statements) {
2493-
db.exec(statement);
2509+
try {
2510+
db.exec(statement);
2511+
} catch (error) {
2512+
if (shouldIgnoreMigrationStatementError(db, statement, error)) {
2513+
continue;
2514+
}
2515+
2516+
throw error;
2517+
}
24942518
}
24952519
24962520
insertMigration.run(migration.id, migration.name, nowIsoString());

0 commit comments

Comments
 (0)