Fix: seed ne sme sesuti serverja ob duplicate slug (try/catch + guard)

This commit is contained in:
Folx Ops 2026-06-13 10:35:41 +00:00
parent 9de740168a
commit dac7e60a4d
2 changed files with 47 additions and 17 deletions

View File

@ -57,7 +57,11 @@ export async function registerRoutes(
httpServer: Server,
app: Express
): Promise<Server> {
try {
await seedDatabase();
} catch (err: any) {
console.error("[seed] seedDatabase failed (server continues):", err?.message);
}
startDailyScheduler();

View File

@ -288,6 +288,7 @@ export async function seedDatabase() {
continue;
}
try {
await storage.createArticle({
title: article.title,
slug: article.slug,
@ -306,6 +307,31 @@ export async function seedDatabase() {
}
newArticles.push(article);
added++;
} catch (err: any) {
// Slug ze obstaja (npr. paralelni deploy/generator je clanek vstavil
// med branjem in pisanjem) -> ne sesuj serverja, samo posodobi.
if (String(err?.message || "").includes("articles_slug_unique")) {
await db.execute(
sql`UPDATE articles SET
title = ${article.title},
excerpt = ${article.excerpt},
content = ${article.content},
cover_image = ${article.coverImage},
category = ${article.category},
author = ${article.author},
featured = ${article.featured}
WHERE slug = ${article.slug}`
);
if (article.publishedAt) {
await db.execute(
sql`UPDATE articles SET published_at = ${article.publishedAt} WHERE slug = ${article.slug}`
);
}
updated++;
} else {
console.error("[seed] createArticle failed for " + article.slug + ": " + err?.message);
}
}
}
if (added > 0) {