Skip to content

Commit

Permalink
🔨 fix various issues
Browse files Browse the repository at this point in the history
  • Loading branch information
danyx23 committed Mar 15, 2024
1 parent efdb77e commit 9253472
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 24 deletions.
9 changes: 5 additions & 4 deletions adminSiteServer/apiRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ import {
gdocFromJSON,
getAllGdocIndexItemsOrderedByUpdatedAt,
getAndLoadGdocById,
getDbEnrichedGdocFromOwidGdoc,
getGdocBaseObjectById,
loadGdocFromGdocBase,
setTagsForGdoc,
Expand Down Expand Up @@ -2463,7 +2464,7 @@ postRouteWithRWTransaction(
gdoc.published = false
gdoc.createdAt = new Date()
gdoc.publishedAt = post.published_at
await upsertGdoc(trx, gdoc.toDbRawPostGdoc())
await upsertGdoc(trx, gdoc)
await setTagsForGdoc(trx, gdocId, tags)
}
return { googleDocsId: gdocId }
Expand Down Expand Up @@ -2662,11 +2663,11 @@ putRouteWithRWTransaction(apiRouter, "/gdocs/:id", async (req, res, trx) => {
// enqueue), so I opted for the version that matches the closest the current
// baking model, which is "bake what is persisted in the DB". Ultimately, a
// full sucessful deploy would resolve the state discrepancy either way.
await upsertGdoc(trx, nextGdoc.toDbRawPostGdoc())
await upsertGdoc(trx, nextGdoc)

const hasChanges = checkHasChanges(prevGdoc, nextGdoc)
const prevJson = prevGdoc.toDbRawPostGdoc()
const nextJson = nextGdoc.toDbRawPostGdoc()
const prevJson = getDbEnrichedGdocFromOwidGdoc(prevGdoc)
const nextJson = getDbEnrichedGdocFromOwidGdoc(nextGdoc)
if (checkIsLightningUpdate(prevJson, nextJson, hasChanges)) {
await enqueueLightningChange(
res.locals.user,
Expand Down
5 changes: 3 additions & 2 deletions adminSiteServer/mockSiteRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -392,13 +392,14 @@ mockSiteRouter.get("/*", async (req, res) => {

await db.knexReadonlyTransaction(async (knex) => {
try {
res.send(await renderGdocsPageBySlug(knex, slug))
const page = await renderGdocsPageBySlug(knex, slug)
res.send(page)
} catch (e) {
console.error(e)
}

try {
const page = renderPageBySlug(slug, knex)
const page = await renderPageBySlug(slug, knex)
res.send(page)
} catch (e) {
console.error(e)
Expand Down
14 changes: 13 additions & 1 deletion db/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,19 @@ export const knexRaw = async <TRow = unknown>(
knex: Knex<any, any[]>,
str: string,
params?: any[] | Record<string, any>
): Promise<TRow[]> => (await knex.raw(str, params ?? []))[0]
): Promise<TRow[]> => {
try {
const rawReturnConstruct = await knex.raw(str, params ?? [])
return rawReturnConstruct[0]
} catch (e) {
console.error("Exception when executing SQL statement!", {
sql: str,
params,
error: e,
})
throw e
}
}

export const knexRawFirst = async <TRow = unknown>(
knex: Knex<any, any[]>,
Expand Down
13 changes: 1 addition & 12 deletions db/model/Gdoc/GdocBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -823,25 +823,14 @@ export class GdocBase implements OwidGdocBaseInterface {
await this._loadSubclassAttachments(knex)
await this.validate(knex)
}

toDbRawPostGdoc(): DbEnrichedPostGdoc {
const readyToSerialize = omit(this, [
"_enrichSubclassContent",
"_filenameProperties",
"_getSubclassEnrichedBlocks",
"_omittableFields",
"_validateSubclass",
...this._omittableFields,
]) as OwidGdocBaseInterface
return readyToSerialize
}
}

// This function would naturally live in GdocFactory but that would create a circular dependency
export async function getMinimalGdocBaseObjectsByIds(
knex: KnexReadonlyTransaction,
ids: string[]
): Promise<OwidGdocMinimalPostInterface[]> {
if (ids.length === 0) return []
const rows = await db.knexRaw<{
id: string
title: string
Expand Down
30 changes: 26 additions & 4 deletions db/model/Gdoc/GdocFactory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { get, groupBy } from "lodash"
import { get, groupBy, omit } from "lodash"

Check warning on line 1 in db/model/Gdoc/GdocFactory.ts

View workflow job for this annotation

GitHub Actions / eslint

'omit' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 1 in db/model/Gdoc/GdocFactory.ts

View workflow job for this annotation

GitHub Actions / eslint

'omit' is defined but never used. Allowed unused vars must match /^_/u
import { match, P } from "ts-pattern"
import {
DATA_INSIGHTS_INDEX_PAGE_SIZE,
Expand Down Expand Up @@ -455,15 +455,37 @@ export async function setTagsForGdoc(
.insert(tagIds.map(({ id: tagId }) => ({ gdocId, tagId })))
}

export function getDbEnrichedGdocFromOwidGdoc(
gdoc: OwidGdoc | GdocBase
): DbEnrichedPostGdoc {
const enrichedGdoc = {
breadcrumbs: gdoc.breadcrumbs,
content: gdoc.content,
createdAt: gdoc.createdAt,
id: gdoc.id,
markdown: gdoc.markdown,
publicationContext: gdoc.publicationContext,
published: gdoc.published,
publishedAt: gdoc.publishedAt,
revisionId: gdoc.revisionId,
slug: gdoc.slug,
updatedAt: gdoc.updatedAt,
} satisfies DbEnrichedPostGdoc
return enrichedGdoc
}
export async function upsertGdoc(
knex: KnexReadWriteTransaction,
gdoc: DbEnrichedPostGdoc
gdoc: OwidGdoc | GdocBase
): Promise<number[]> {
return knex
const enrichedGdoc = getDbEnrichedGdocFromOwidGdoc(gdoc)
const rawPost = serializePostsGdocsRow(enrichedGdoc)
const query = knex
.table(PostsGdocsTableName)
.insert(serializePostsGdocsRow(gdoc))
.insert(rawPost)
.onConflict("id")
.merge()
console.error(query.toSQL())
return query
}

// TODO:
Expand Down
12 changes: 11 additions & 1 deletion db/tests/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,24 @@ test("knex interface", async () => {
expect(afterUpdate.length).toBe(2)
expect(afterUpdate[1].isSuperuser).toBe(1)

// Use raw queries, using ?? to specify the table name using the shared const value
// The pick type is used to type the result row
const usersFromRawQuery: Pick<DbPlainUser, "email">[] = await knexRaw(
trx,
"select email from users",
[]
)
expect(usersFromRawQuery.length).toBe(2)

// Check if in queries work as expected
const usersFromRawQueryWithInClause: Pick<DbPlainUser, "email">[] =
await knexRaw(trx, "select * from users where email in (:emails)", {
emails: [
usersFromRawQuery[0].email,
usersFromRawQuery[1].email,
],
})
expect(usersFromRawQueryWithInClause.length).toBe(2)

await deleteUser(trx, 2)
}, knexInstance)
})
Expand Down

0 comments on commit 9253472

Please sign in to comment.