Skip to content

Commit

Permalink
🐝 simplify, comment and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
danyx23 committed Dec 6, 2024
1 parent 5341641 commit e04cb1e
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 174 deletions.
17 changes: 6 additions & 11 deletions adminSiteServer/apiRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ import {
DbPlainChartView,
ChartViewsTableName,
DbInsertChartView,
PostsGdocsComponentsTableName,
CHART_VIEW_PROPS_TO_PERSIST,
CHART_VIEW_PROPS_TO_OMIT,
} from "@ourworldindata/types"
Expand Down Expand Up @@ -177,7 +178,6 @@ import {
addImagesToContentGraph,
updateGdocContentOnly,
upsertGdoc,
updateDerivedGdocPostsComponents,
} from "../db/model/Gdoc/GdocFactory.js"
import { match } from "ts-pattern"
import { GdocDataInsight } from "../db/model/Gdoc/GdocDataInsight.js"
Expand Down Expand Up @@ -2781,11 +2781,6 @@ postRouteWithRWTransaction(
gdoc.createdAt = new Date()
gdoc.publishedAt = post.published_at
await upsertGdoc(trx, gdoc)
await updateDerivedGdocPostsComponents(
trx,
gdoc.id,
gdoc.content.body
)
await setTagsForGdoc(trx, gdocId, tags)
}
return { googleDocsId: gdocId }
Expand Down Expand Up @@ -2987,11 +2982,7 @@ putRouteWithRWTransaction(apiRouter, "/gdocs/:id", async (req, res, trx) => {
)

await upsertGdoc(trx, nextGdoc)
await updateDerivedGdocPostsComponents(
trx,
nextGdoc.id,
nextGdoc.content.body
)

await indexAndBakeGdocIfNeccesary(trx, res.locals.user, prevGdoc, nextGdoc)

return nextGdoc
Expand Down Expand Up @@ -3047,6 +3038,10 @@ deleteRouteWithRWTransaction(apiRouter, "/gdocs/:id", async (req, res, trx) => {
await trx.table(PostsGdocsLinksTableName).where({ sourceId: id }).delete()
await trx.table(PostsGdocsXImagesTableName).where({ gdocId: id }).delete()
await trx.table(PostsGdocsTableName).where({ id }).delete()
await trx
.table(PostsGdocsComponentsTableName)
.where({ gdocId: id })
.delete()
if (gdoc.published && checkIsGdocPostExcludingFragments(gdoc)) {
await removeIndividualGdocPostFromIndex(gdoc)
}
Expand Down
2 changes: 1 addition & 1 deletion db/migration/1732626230267-addPostsGdocsComponentsTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ CREATE TABLE posts_gdocs_components (
config JSON,
parent VARCHAR(1024),
path VARCHAR(1024),
FOREIGN KEY (gdocId) REFERENCES posts_gdocs(id),
FOREIGN KEY (gdocId) REFERENCES posts_gdocs(id) ON DELETE CASCADE ON UPDATE CASCADE,
INDEX idx_gdocId (gdocId)
) ENGINE=InnoDB;
`)
Expand Down
6 changes: 4 additions & 2 deletions db/model/Gdoc/GdocFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export async function createGdocAndInsertIntoDb(
// while fetching the live gdocs (GdocsContentSource.Gdocs) in
// loadGdocFromGdocBase().
await upsertGdoc(knex, gdoc)
await updateDerivedGdocPostsComponents(knex, gdoc.id, gdoc.content.body)

return gdoc
}

Expand Down Expand Up @@ -616,7 +616,9 @@ export async function upsertGdoc(
.onConflict("id")
.merge()
sql = query.toSQL()
return query
const indices = await query
await updateDerivedGdocPostsComponents(knex, gdoc.id, gdoc.content.body)
return indices
} catch (e) {
console.error(`Error occured in sql: ${sql}`, e)
throw e
Expand Down
Loading

0 comments on commit e04cb1e

Please sign in to comment.