Skip to content

Commit dbfe4cc

Browse files
committed
fix(server): update seeding script to drop table before insertion
1 parent 6dbc5a1 commit dbfe4cc

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

flake.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
gum style --margin "0 2" \
6666
"$(gum style --bold --foreground "#89b4fa" 'bun dev') - Start development servers" \
6767
"$(gum style --bold --foreground "#89b4fa" 'bun dashboard') - Open Convex dashboard" \
68-
"$(gum style --bold --foreground "#89b4fa" 'bun check') - Check code with Biome" \
68+
"$(gum style --bold --foreground "#89b4fa" 'bun check') - Check code with Biome"
6969
echo ""
7070
'';
7171
};

packages/server/convex/seed.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,42 @@ import type { Id } from "./_generated/dataModel";
1010
import { internalMutation } from "./_generated/server";
1111
import { schoolName } from "./schemas/schools";
1212

13+
/**
14+
* Clear all data from all tables
15+
* WARNING: This will delete all data in the database
16+
*/
17+
export const clearAll = internalMutation({
18+
handler: async (ctx) => {
19+
console.log("🗑️ Clearing all database tables...");
20+
21+
// Delete in reverse dependency order to maintain referential integrity
22+
const tables = [
23+
"userCourseOfferings",
24+
"userCourses",
25+
"students",
26+
"courseOfferings",
27+
"requirements",
28+
"prerequisites",
29+
"courses",
30+
"programs",
31+
"schools",
32+
"appConfigs",
33+
] as const;
34+
35+
for (const tableName of tables) {
36+
console.log(` Clearing ${tableName}...`);
37+
const documents = await ctx.db.query(tableName).collect();
38+
for (const doc of documents) {
39+
await ctx.db.delete(doc._id);
40+
}
41+
console.log(` ✓ Cleared ${documents.length} records from ${tableName}`);
42+
}
43+
44+
console.log("✅ All tables cleared successfully!");
45+
return { success: true, message: "All tables cleared" };
46+
},
47+
});
48+
1349
/**
1450
* Seed all data from JSON files
1551
* This is the main seeding function that handles all tables with proper relationships

packages/server/seed/seed.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,25 @@ async function seedDatabase() {
7272
console.log(` - ${userCourseOfferings.length} user course offerings`);
7373
console.log(`\n🔑 Using TEST_USER_ID: ${TEST_USER_ID}\n`);
7474

75+
// Step 1: Clear all existing data
76+
console.log("🗑️ Clearing existing database...\n");
77+
const clearCommand = "npx convex run seed:clearAll --no-push";
78+
79+
const { stdout: clearStdout, stderr: clearStderr } = await execAsync(
80+
clearCommand,
81+
{
82+
cwd: new URL("..", import.meta.url).pathname,
83+
maxBuffer: 10 * 1024 * 1024,
84+
},
85+
);
86+
87+
if (clearStderr) {
88+
console.error("stderr:", clearStderr);
89+
}
90+
91+
console.log(clearStdout);
92+
93+
// Step 2: Seed new data
7594
const data = JSON.stringify({
7695
appConfigs,
7796
schools,
@@ -85,7 +104,7 @@ async function seedDatabase() {
85104
userCourseOfferings,
86105
});
87106

88-
console.log("🚀 Calling Convex internal mutation via CLI...\n");
107+
console.log("\n🚀 Seeding new data via Convex CLI...\n");
89108

90109
const command = `npx convex run seed:seedAll --no-push '${data.replace(/'/g, "'\\''")}'`;
91110

0 commit comments

Comments
 (0)