Skip to content

Commit 49ee134

Browse files
committed
fix: replace partial unique index on sources.url with full index
1 parent 2df6910 commit 49ee134

3 files changed

Lines changed: 38 additions & 8 deletions

File tree

apps/server/src/index.ts

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -181,15 +181,36 @@ app.post("/api/chat", async (req, res) => {
181181
}
182182
});
183183

184-
console.log("Running database migrations...");
185-
await migrate(db, {
186-
migrationsFolder: join(
187-
fileURLToPath(
188-
new URL("../../../packages/db/src/migrations", import.meta.url),
189-
),
184+
const migrationsFolder = join(
185+
fileURLToPath(
186+
new URL("../../../packages/db/src/migrations", import.meta.url),
190187
),
191-
});
192-
console.log("Migrations applied.");
188+
);
189+
console.log(`[migrations] Running from: ${migrationsFolder}`);
190+
191+
const { rows: before } = await db.execute<{ hash: string }>(
192+
sql`SELECT hash FROM drizzle.__drizzle_migrations ORDER BY created_at`,
193+
);
194+
const appliedBefore = new Set(before.map((r) => r.hash));
195+
196+
await migrate(db, { migrationsFolder });
197+
198+
const { rows: after } = await db.execute<{ hash: string }>(
199+
sql`SELECT hash FROM drizzle.__drizzle_migrations ORDER BY created_at`,
200+
);
201+
202+
const newlyApplied = after
203+
.map((r) => r.hash)
204+
.filter((h) => !appliedBefore.has(h));
205+
206+
if (newlyApplied.length === 0) {
207+
console.log("[migrations] No new migrations to apply.");
208+
} else {
209+
for (const hash of newlyApplied) {
210+
console.log(`[migrations] Applied: ${hash}`);
211+
}
212+
}
213+
console.log(`[migrations] Done. Total applied: ${after.length}`);
193214

194215
app.listen(3000, () => {
195216
console.log("Server is running on http://localhost:3000");
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
DROP INDEX IF EXISTS sources_url_unique;
2+
CREATE UNIQUE INDEX sources_url_unique ON sources (url);

packages/db/src/migrations/meta/_journal.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@
3636
"when": 1772800001000,
3737
"tag": "0004_sources_url_unique",
3838
"breakpoints": true
39+
},
40+
{
41+
"idx": 5,
42+
"version": "7",
43+
"when": 1772800002000,
44+
"tag": "0005_fix_sources_url_unique_index",
45+
"breakpoints": true
3946
}
4047
]
4148
}

0 commit comments

Comments
 (0)