Skip to content

Commit 6464c5d

Browse files
committed
weird pkey issue with seeding
1 parent 98191bc commit 6464c5d

11 files changed

Lines changed: 97 additions & 17 deletions

File tree

04-database/src/db/seed.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ async function main() {
2525

2626
if (users.length === 0) {
2727
console.error(
28-
"❌ No users found in the database. Seed cannot assign authorId without existing users.",
28+
"❌ No users found in the database. Seed cannot assign authorId without existing users."
2929
);
3030
process.exit(1);
3131
}
@@ -79,7 +79,17 @@ async function main() {
7979

8080
console.log(`✅ Inserted ${SEED_COUNT} article(s) into the database\n`);
8181

82-
// Sequence is reset by TRUNCATE ... RESTART IDENTITY above, so no setval needed.
82+
// Ensure the articles sequence is synced to the current MAX(id). This is a
83+
// safety measure in case the DB was imported or mutated in a way that left
84+
// the sequence behind the table's max value.
85+
try {
86+
await sql.query(
87+
`SELECT setval(pg_get_serial_sequence('articles','id'), COALESCE((SELECT MAX(id) FROM articles), 1), true);`
88+
);
89+
console.log("✅ Sequence synced after seeding");
90+
} catch (err) {
91+
console.warn("⚠️ Failed to sync articles sequence after seeding:", err);
92+
}
8393
} catch (err) {
8494
console.error("💥 Seed failed:", err);
8595
process.exit(1);

05-object-storage/src/db/seed.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ async function main() {
2525

2626
if (users.length === 0) {
2727
console.error(
28-
"❌ No users found in the database. Seed cannot assign authorId without existing users.",
28+
"❌ No users found in the database. Seed cannot assign authorId without existing users."
2929
);
3030
process.exit(1);
3131
}
@@ -79,7 +79,17 @@ async function main() {
7979

8080
console.log(`✅ Inserted ${SEED_COUNT} article(s) into the database\n`);
8181

82-
// Sequence is reset by TRUNCATE ... RESTART IDENTITY above, so no setval needed.
82+
// Ensure the articles sequence is synced to the current MAX(id). This is a
83+
// safety measure in case the DB was imported or mutated in a way that left
84+
// the sequence behind the table's max value.
85+
try {
86+
await sql.query(
87+
`SELECT setval(pg_get_serial_sequence('articles','id'), COALESCE((SELECT MAX(id) FROM articles), 1), true);`
88+
);
89+
console.log("✅ Sequence synced after seeding");
90+
} catch (err) {
91+
console.warn("⚠️ Failed to sync articles sequence after seeding:", err);
92+
}
8393
} catch (err) {
8494
console.error("💥 Seed failed:", err);
8595
process.exit(1);

06-caching/src/db/seed.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,17 @@ async function main() {
7979

8080
console.log(`✅ Inserted ${SEED_COUNT} article(s) into the database\n`);
8181

82-
// Sequence is reset by TRUNCATE ... RESTART IDENTITY above, so no setval needed.
82+
// Ensure the articles sequence is synced to the current MAX(id). This is a
83+
// safety measure in case the DB was imported or mutated in a way that left
84+
// the sequence behind the table's max value.
85+
try {
86+
await sql.query(
87+
`SELECT setval(pg_get_serial_sequence('articles','id'), COALESCE((SELECT MAX(id) FROM articles), 1), true);`,
88+
);
89+
console.log("✅ Sequence synced after seeding");
90+
} catch (err) {
91+
console.warn("⚠️ Failed to sync articles sequence after seeding:", err);
92+
}
8393
} catch (err) {
8494
console.error("💥 Seed failed:", err);
8595
process.exit(1);

06-caching/src/lib/data/articles.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export async function getArticles(): Promise<ArticleList[]> {
3131
.from(articles)
3232
.leftJoin(usersSync, eq(articles.authorId, usersSync.id));
3333

34-
console.log("🙅‍♂️ Get Articles Cache Miss!");
34+
console.log("🏹 Get Articles Cache Miss!");
3535
try {
3636
await redis.set("articles:all", JSON.stringify(response), {
3737
ex: 60,

07-email/src/db/seed.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ async function main() {
2525

2626
if (users.length === 0) {
2727
console.error(
28-
"❌ No users found in the database. Seed cannot assign authorId without existing users.",
28+
"❌ No users found in the database. Seed cannot assign authorId without existing users."
2929
);
3030
process.exit(1);
3131
}
@@ -79,7 +79,17 @@ async function main() {
7979

8080
console.log(`✅ Inserted ${SEED_COUNT} article(s) into the database\n`);
8181

82-
// Sequence is reset by TRUNCATE ... RESTART IDENTITY above, so no setval needed.
82+
// Ensure the articles sequence is synced to the current MAX(id). This is a
83+
// safety measure in case the DB was imported or mutated in a way that left
84+
// the sequence behind the table's max value.
85+
try {
86+
await sql.query(
87+
`SELECT setval(pg_get_serial_sequence('articles','id'), COALESCE((SELECT MAX(id) FROM articles), 1), true);`
88+
);
89+
console.log("✅ Sequence synced after seeding");
90+
} catch (err) {
91+
console.warn("⚠️ Failed to sync articles sequence after seeding:", err);
92+
}
8393
} catch (err) {
8494
console.error("💥 Seed failed:", err);
8595
process.exit(1);

07-email/src/lib/data/articles.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export async function getArticles(): Promise<ArticleList[]> {
3131
.from(articles)
3232
.leftJoin(usersSync, eq(articles.authorId, usersSync.id));
3333

34-
console.log("🙅‍♂️ Get Articles Cache Miss!");
34+
console.log("🏹 Get Articles Cache Miss!");
3535
try {
3636
await redis.set("articles:all", JSON.stringify(response), {
3737
ex: 60,

08-ai/src/db/seed.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ async function main() {
2525

2626
if (users.length === 0) {
2727
console.error(
28-
"❌ No users found in the database. Seed cannot assign authorId without existing users.",
28+
"❌ No users found in the database. Seed cannot assign authorId without existing users."
2929
);
3030
process.exit(1);
3131
}
@@ -79,7 +79,17 @@ async function main() {
7979

8080
console.log(`✅ Inserted ${SEED_COUNT} article(s) into the database\n`);
8181

82-
// Sequence is reset by TRUNCATE ... RESTART IDENTITY above, so no setval needed.
82+
// Ensure the articles sequence is synced to the current MAX(id). This is a
83+
// safety measure in case the DB was imported or mutated in a way that left
84+
// the sequence behind the table's max value.
85+
try {
86+
await sql.query(
87+
`SELECT setval(pg_get_serial_sequence('articles','id'), COALESCE((SELECT MAX(id) FROM articles), 1), true);`
88+
);
89+
console.log("✅ Sequence synced after seeding");
90+
} catch (err) {
91+
console.warn("⚠️ Failed to sync articles sequence after seeding:", err);
92+
}
8393
} catch (err) {
8494
console.error("💥 Seed failed:", err);
8595
process.exit(1);

08-ai/src/lib/data/articles.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export async function getArticles(): Promise<ArticleList[]> {
3535
.from(articles)
3636
.leftJoin(usersSync, eq(articles.authorId, usersSync.id));
3737

38-
console.log("🙅‍♂️ Get Articles Cache Miss!");
38+
console.log("🏹 Get Articles Cache Miss!");
3939
// Store cache as JSON so we can retrieve a typed array later
4040
try {
4141
await redis.set("articles:all", JSON.stringify(response), {

09-with-tests/src/db/seed.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ async function main() {
2525

2626
if (users.length === 0) {
2727
console.error(
28-
"❌ No users found in the database. Seed cannot assign authorId without existing users.",
28+
"❌ No users found in the database. Seed cannot assign authorId without existing users."
2929
);
3030
process.exit(1);
3131
}
@@ -70,7 +70,17 @@ async function main() {
7070

7171
console.log(`✅ Inserted ${SEED_COUNT} article(s) into the database\n`);
7272

73-
// Sequence is reset by TRUNCATE ... RESTART IDENTITY above, so no setval needed.
73+
// Ensure the articles sequence is synced to the current MAX(id). This is a
74+
// safety measure in case the DB was imported or mutated in a way that left
75+
// the sequence behind the table's max value.
76+
try {
77+
await sql.query(
78+
`SELECT setval(pg_get_serial_sequence('articles','id'), COALESCE((SELECT MAX(id) FROM articles), 1), true);`
79+
);
80+
console.log("✅ Sequence synced after seeding");
81+
} catch (err) {
82+
console.warn("⚠️ Failed to sync articles sequence after seeding:", err);
83+
}
7484
} catch (err) {
7585
console.error("💥 Seed failed:", err);
7686
process.exit(1);

09-with-tests/src/lib/data/articles.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export async function getArticles(): Promise<ArticleList[]> {
3535
.from(articles)
3636
.leftJoin(usersSync, eq(articles.authorId, usersSync.id));
3737

38-
console.log("🙅‍♂️ Get Articles Cache Miss!");
38+
console.log("🏹 Get Articles Cache Miss!");
3939
// Store cache as JSON so we can retrieve a typed array later
4040
try {
4141
await redis.set("articles:all", JSON.stringify(response), {
@@ -57,7 +57,7 @@ export type ArticleWithAuthor = {
5757
};
5858

5959
export async function getArticleById(
60-
id: number,
60+
id: number
6161
): Promise<ArticleWithAuthor | null> {
6262
const response = await db
6363
.select({

0 commit comments

Comments
 (0)