@@ -41,7 +41,7 @@ export const extractQuizzes = async (
4141 slug : string
4242) : Promise < QuestionDef [ ] > => {
4343 const compiledMdx = await compile (
44- // At some point I useed mdxSource.replace(/[^\x00-\x7F]/g, "") to fix some problem.
44+ // At some point I used mdxSource.replace(/[^\x00-\x7F]/g, "") to fix some problem.
4545 // Later it turned out it makes options non-unique (e.g. in `options={["Č", "Š", "Ž"]}`).
4646 // I removed it and it still works. I'm keeping the comment, just for the case.
4747 mdxSource ,
@@ -234,7 +234,8 @@ export const extractQuizzes = async (
234234const checkBooks = async (
235235 books : RawBookProps [ ] ,
236236 allBookSlugs : Set < string > ,
237- db : Database
237+ db : Database ,
238+ updatePath : string
238239) : Promise <
239240 Record < string , { frontmatter : ChapterFrontmatter ; questions : QuestionDef [ ] } >
240241> => {
@@ -250,7 +251,9 @@ const checkBooks = async (
250251 FROM books
251252 JOIN books_chapters ON books.id = books_chapters.bookId
252253 JOIN chapters ON books_chapters.chapterId = chapters.id
253- JOIN questions ON chapters.id = questions.chapterId`
254+ JOIN questions ON chapters.id = questions.chapterId
255+ WHERE books.path LIKE ?` ,
256+ [ `${ updatePath } /%` ]
254257 ) ;
255258 booksWithQuestions
256259 . filter ( ( { path } ) => ! allBookSlugs . has ( path ) )
@@ -412,7 +415,7 @@ const checkCollections = async (
412415 logError (
413416 collection . slug ,
414417 `The following books are missing in the collection:\n ${ missingBooks
415- . map ( ( b ) => ` ${ b } ` )
418+ . map ( ( b ) => ` ${ b . slug } ` )
416419 . join ( "\n" ) } `
417420 ) ;
418421 }
@@ -425,7 +428,7 @@ const checkCollections = async (
425428 logError (
426429 collection . slug ,
427430 `The following collections are missing in the collection:\n ${ missingCollections
428- . map ( ( c ) => ` ${ c } ` )
431+ . map ( ( c ) => ` ${ c . slug } ` )
429432 . join ( "\n" ) } `
430433 ) ;
431434 }
@@ -546,54 +549,54 @@ const insertCollections = async (
546549 for ( const {
547550 slug : collectionSlug ,
548551 frontmatter : { title, subTitle, date, description, public : isPublic , language, coverImg, recursiveContent } ,
549- books,
550- collections : subCollections ,
551552 } of collections ) {
552553 // Do not change this to "DELETE + INSERT" because it will delete rows that use this collections's id as foreign key.
553- const { id : collectionId } = await db . get (
554+ await db . get (
554555 `
555- INSERT INTO collections (
556- lastBuildId,
557- path, title, subtitle, description, date,
558- public, language, coverImg, recursiveContent)
559- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
560- ON CONFLICT(path) DO UPDATE SET title = excluded.title,
561- lastBuildId = excluded.lastBuildId,
562- subtitle = excluded.subtitle,
563- description = excluded.description,
564- date = excluded.date,
565- public = excluded.public,
566- language = excluded.language,
567- coverImg = excluded.coverImg,
568- recursiveContent = excluded.recursiveContent
569- RETURNING id
570- ` ,
556+ INSERT INTO collections (lastBuildId,
557+ path, title, subtitle, description, date,
558+ public, language, coverImg, recursiveContent)
559+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
560+ ON CONFLICT(path) DO UPDATE SET title = excluded.title,
561+ lastBuildId = excluded.lastBuildId,
562+ subtitle = excluded.subtitle,
563+ description = excluded.description,
564+ date = excluded.date,
565+ public = excluded.public,
566+ language = excluded.language,
567+ coverImg = excluded.coverImg,
568+ recursiveContent = excluded.recursiveContent
569+ RETURNING id
570+ ` ,
571571 [ buildId , collectionSlug , title , subTitle , description , date , isPublic , language , coverImg , recursiveContent ]
572572 ) ;
573+ }
573574
574- for ( const { slug : bookSlug } of books ) {
575+ for ( const { slug : collectionSlug , collections : subCollections , books } of collections ) {
576+ const collectionId = ( await db . get ( `SELECT id FROM collections WHERE path = ?` , [ collectionSlug ] ) ) . id ;
577+ for ( const { slug } of books ) {
575578 await db . run (
576579 `
577- INSERT INTO collections_books (collectionId, bookId, lastBuildId)
578- SELECT ?, id, ?
579- FROM books
580- WHERE path = ?
581- ON CONFLICT DO UPDATE SET lastBuildId = excluded.lastBuildId
582- ` ,
583- [ collectionId , buildId , bookSlug ]
580+ INSERT INTO collections_books (collectionId, bookId, lastBuildId)
581+ SELECT ?, id, ?
582+ FROM books
583+ WHERE path = ?
584+ ON CONFLICT DO UPDATE SET lastBuildId = excluded.lastBuildId
585+ ` ,
586+ [ collectionId , buildId , slug ]
584587 ) ;
585588 }
586589
587- for ( const { slug : subCollectionSlug } of subCollections ) {
590+ for ( const { slug } of subCollections ) {
588591 await db . run (
589592 `
590- INSERT INTO collections_collections (collectionId, subCollectionId, lastBuildId)
591- SELECT ?, id, ?
592- FROM collections
593- WHERE path = ?
594- ON CONFLICT DO UPDATE SET lastBuildId = excluded.lastBuildId
595- ` ,
596- [ collectionId , buildId , subCollectionSlug ]
593+ INSERT INTO collections_collections (collectionId, subCollectionId, lastBuildId)
594+ SELECT ?, id, ?
595+ FROM collections
596+ WHERE path = ?
597+ ON CONFLICT DO UPDATE SET lastBuildId = excluded.lastBuildId
598+ ` ,
599+ [ collectionId , buildId , slug ]
597600 ) ;
598601 }
599602 }
@@ -603,7 +606,8 @@ export const updatePaths = async (
603606 bookSlugs : string [ ] [ ] ,
604607 collectionSlugs : string [ ] [ ] ,
605608 db : Database ,
606- buildId : number
609+ buildId : number ,
610+ pathPrefix : string
607611) => {
608612 const rawBooks = await Promise . all ( bookSlugs . map ( getRawBook ) ) ;
609613 const serializedBooks = await Promise . all ( bookSlugs . map ( getBookProps ) ) ;
@@ -613,7 +617,7 @@ export const updatePaths = async (
613617 ) ;
614618 const allCollectionSlugs = new Set ( collections . map ( ( { slug } ) => slug ) ) ;
615619
616- const chapters = await checkBooks ( rawBooks , allBookSlugs , db ) ;
620+ const chapters = await checkBooks ( rawBooks , allBookSlugs , db , pathPrefix ) ;
617621 await checkQuestions ( chapters , db ) ;
618622 await checkCollections ( collections , allCollectionSlugs , allBookSlugs ) ;
619623 if ( hasError ( ) ) {
0 commit comments