1+ import 'dotenv/config' ;
2+
13import { eq } from 'drizzle-orm' ;
24import { readFileSync } from 'fs' ;
35import { join } from 'path' ;
@@ -75,7 +77,7 @@ async function loadQuestions(partNumber: number): Promise<QuestionData[]> {
7577 return partData . questions ;
7678}
7779
78- async function ensureQuizExists ( ) : Promise < string > {
80+ async function ensureQuizExists ( forceDelete : boolean ) : Promise < string > {
7981 console . log ( 'Ensuring quiz exists...' ) ;
8082
8183 const [ category ] = await db
@@ -93,14 +95,24 @@ async function ensureQuizExists(): Promise<string> {
9395 const existing = await db . query . quizzes . findFirst ( {
9496 where : eq ( quizzes . slug , QUIZ_METADATA . slug ) ,
9597 } ) ;
98+
9699 if ( existing ) {
97- const existingAttempt = await db . query . quizAttempts . findFirst ( {
98- where : eq ( quizAttempts . quizId , existing . id ) ,
99- } ) ;
100- if ( existingAttempt ) {
101- throw new Error (
102- `Quiz ${ QUIZ_METADATA . slug } has existing attempts. Aborting to avoid data loss.`
103- ) ;
100+ const existingAttempts = await db
101+ . select ( )
102+ . from ( quizAttempts )
103+ . where ( eq ( quizAttempts . quizId , existing . id ) )
104+ . limit ( 1 ) ;
105+
106+ if ( existingAttempts . length > 0 ) {
107+ if ( ! forceDelete ) {
108+ throw new Error (
109+ `Quiz ${ QUIZ_METADATA . slug } has existing attempts. Use --force to delete them.`
110+ ) ;
111+ }
112+
113+ console . log ( 'Deleting existing attempts (--force flag used)...' ) ;
114+ await db . delete ( quizAttempts ) . where ( eq ( quizAttempts . quizId , existing . id ) ) ;
115+ console . log ( 'Attempts deleted.' ) ;
104116 }
105117
106118 await db . delete ( quizQuestions ) . where ( eq ( quizQuestions . quizId , existing . id ) ) ;
@@ -211,22 +223,24 @@ async function seedQuestions(
211223
212224async function seedQuizFromJson ( ) {
213225 const args = process . argv . slice ( 2 ) ;
214- const partArg = args [ 0 ] ;
226+ const forceDelete = args . includes ( '--force' ) ;
227+ const partArg = args . find ( arg => arg !== '--force' ) ;
215228
216229 if ( ! partArg ) {
217230 console . error ( 'Error: Please specify which part to upload' ) ;
218- console . log (
219- 'Usage: npx tsx db/seeds/seed-quiz-javascript.ts <part-number>'
220- ) ;
221- console . log ( 'Example: npx tsx db/seeds/seed-quiz-javascript.ts 1' ) ;
222- console . log ( 'Or upload all: npx tsx db/seeds/seed-quiz-javascript.ts all' ) ;
231+ console . log ( 'Usage: npx tsx db/seed-quiz-javascript.ts <part-number> [--force]' ) ;
232+ console . log ( 'Example: npx tsx db/seed-quiz-javascript.ts 1' ) ;
233+ console . log ( ' npx tsx db/seed-quiz-javascript.ts all --force' ) ;
223234 process . exit ( 1 ) ;
224235 }
225236
226237 console . log ( 'Starting JavaScript quiz seed...\n' ) ;
238+ if ( forceDelete ) {
239+ console . log ( 'WARNING: --force flag used. Existing attempts will be deleted.\n' ) ;
240+ }
227241
228242 try {
229- const quizId = await ensureQuizExists ( ) ;
243+ const quizId = await ensureQuizExists ( forceDelete ) ;
230244
231245 if ( partArg . toLowerCase ( ) === 'all' ) {
232246 console . log ( 'Uploading all parts...\n' ) ;
0 commit comments