Fix: seed ne sme sesuti serverja ob duplicate slug (try/catch + guard)
This commit is contained in:
parent
9de740168a
commit
dac7e60a4d
@ -57,7 +57,11 @@ export async function registerRoutes(
|
|||||||
httpServer: Server,
|
httpServer: Server,
|
||||||
app: Express
|
app: Express
|
||||||
): Promise<Server> {
|
): Promise<Server> {
|
||||||
await seedDatabase();
|
try {
|
||||||
|
await seedDatabase();
|
||||||
|
} catch (err: any) {
|
||||||
|
console.error("[seed] seedDatabase failed (server continues):", err?.message);
|
||||||
|
}
|
||||||
|
|
||||||
startDailyScheduler();
|
startDailyScheduler();
|
||||||
|
|
||||||
|
|||||||
@ -288,24 +288,50 @@ export async function seedDatabase() {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
await storage.createArticle({
|
try {
|
||||||
title: article.title,
|
await storage.createArticle({
|
||||||
slug: article.slug,
|
title: article.title,
|
||||||
excerpt: article.excerpt,
|
slug: article.slug,
|
||||||
content: article.content,
|
excerpt: article.excerpt,
|
||||||
coverImage: article.coverImage,
|
content: article.content,
|
||||||
category: article.category,
|
coverImage: article.coverImage,
|
||||||
author: article.author,
|
category: article.category,
|
||||||
featured: article.featured,
|
author: article.author,
|
||||||
});
|
featured: article.featured,
|
||||||
|
});
|
||||||
|
|
||||||
if (article.publishedAt) {
|
if (article.publishedAt) {
|
||||||
await db.execute(
|
await db.execute(
|
||||||
sql`UPDATE articles SET published_at = ${article.publishedAt} WHERE slug = ${article.slug}`
|
sql`UPDATE articles SET published_at = ${article.publishedAt} WHERE slug = ${article.slug}`
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
newArticles.push(article);
|
|
||||||
added++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (added > 0) {
|
if (added > 0) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user