Skip to content

Commit

Permalink
🔨 switch Variable and User models to support knex
Browse files Browse the repository at this point in the history
Also make sure we pass down knex in the related places
and use transaction scopes where appropriate.
  • Loading branch information
danyx23 committed Jan 29, 2024
1 parent 844e029 commit 8a389c2
Show file tree
Hide file tree
Showing 19 changed files with 372 additions and 208 deletions.
24 changes: 14 additions & 10 deletions adminSiteServer/adminRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ adminRouter.get("/datasets/:datasetId.csv", async (req, res) => {
callback(null)
},
})
await Dataset.writeCSV(datasetId, writeStream)
await Dataset.writeCSV(datasetId, writeStream, db.knexInstance())
res.end()
})

Expand All @@ -167,13 +167,13 @@ adminRouter.get("/datasets/:datasetId/downloadZip", async (req, res) => {
adminRouter.get("/posts/preview/:postId", async (req, res) => {
const postId = expectInt(req.params.postId)

res.send(await renderPreview(postId))
res.send(await renderPreview(postId, db.knexInstance()))
})

adminRouter.get("/posts/compare/:postId", async (req, res) => {
const postId = expectInt(req.params.postId)

const wpPage = await renderPreview(postId)
const wpPage = await renderPreview(postId, db.knexInstance())
const archieMlText = await Post.select(
"archieml",
"archieml_update_statistics"
Expand Down Expand Up @@ -279,13 +279,16 @@ adminRouter.get("/datapage-preview/:id", async (req, res) => {
await explorerAdminServer.getAllPublishedExplorersBySlugCached()

res.send(
await renderDataPageV2({
variableId,
variableMetadata,
isPreviewing: true,
useIndicatorGrapherConfigs: true,
publishedExplorersBySlug,
})
await renderDataPageV2(
{
variableId,
variableMetadata,
isPreviewing: true,
useIndicatorGrapherConfigs: true,
publishedExplorersBySlug,
},
db.knexInstance()
)
)
})

Expand All @@ -300,6 +303,7 @@ adminRouter.get("/grapher/:slug", async (req, res) => {
res.send(
await renderPreviewDataPageOrGrapherPage(
entity.config,
db.knexInstance(),
publishedExplorersBySlug
)
)
Expand Down
55 changes: 26 additions & 29 deletions adminSiteServer/apiRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ import {
import {
GrapherInterface,
OwidGdocLinkType,
UsersRow,
DbPlainUser,
UsersTableName,
grapherKeysToSerialize,
} from "@ourworldindata/types"
Expand Down Expand Up @@ -1415,20 +1415,20 @@ apiRouter.post(
)

apiRouter.get("/users.json", async (req: Request, res: Response) => ({
users: db
users: await db
.knexInstance()
.select(
"id" satisfies keyof UsersRow,
"email" satisfies keyof UsersRow,
"fullName" satisfies keyof UsersRow,
"isActive" satisfies keyof UsersRow,
"isSuperuser" satisfies keyof UsersRow,
"createdAt" satisfies keyof UsersRow,
"updatedAt" satisfies keyof UsersRow,
"lastLogin" satisfies keyof UsersRow,
"lastSeen" satisfies keyof UsersRow
"id" satisfies keyof DbPlainUser,
"email" satisfies keyof DbPlainUser,
"fullName" satisfies keyof DbPlainUser,
"isActive" satisfies keyof DbPlainUser,
"isSuperuser" satisfies keyof DbPlainUser,
"createdAt" satisfies keyof DbPlainUser,
"updatedAt" satisfies keyof DbPlainUser,
"lastLogin" satisfies keyof DbPlainUser,
"lastSeen" satisfies keyof DbPlainUser
)
.from<UsersRow>(UsersTableName)
.from<DbPlainUser>(UsersTableName)
.orderBy("lastSeen", "desc"),
}))

Expand All @@ -1455,23 +1455,18 @@ apiRouter.put("/users/:userId", async (req: Request, res: Response) => {
if (!res.locals.user.isSuperuser)
throw new JsonError("Permission denied", 403)

const userId = parseIntOrUndefined(req.params.userId)
const user =
userId !== undefined
? await getUserById(db.knexInstance(), userId)
: null
if (!user) throw new JsonError("No such user", 404)
return db.knexInstance().transaction(async (t) => {
const userId = parseIntOrUndefined(req.params.userId)
const user = userId !== undefined ? await getUserById(t, userId) : null
if (!user) throw new JsonError("No such user", 404)

user.fullName = req.body.fullName
user.isActive = req.body.isActive
user.fullName = req.body.fullName
user.isActive = req.body.isActive

await updateUser(
db.knexInstance(),
userId!,
pick(user, ["fullName", "isActive"])
)
await updateUser(t, userId!, pick(user, ["fullName", "isActive"]))

return { success: true }
return { success: true }
})
})

apiRouter.post("/users/add", async (req: Request, res: Response) => {
Expand All @@ -1491,7 +1486,7 @@ apiRouter.post("/users/add", async (req: Request, res: Response) => {
apiRouter.get("/variables.json", async (req) => {
const limit = parseIntOrUndefined(req.query.limit as string) ?? 50
const query = req.query.search as string
return await searchVariables(query, limit)
return await searchVariables(query, limit, db.knexInstance())
})

apiRouter.get(
Expand Down Expand Up @@ -1709,8 +1704,10 @@ apiRouter.get(

await Chart.assignTagsForCharts(charts)

const grapherConfig =
await getMergedGrapherConfigForVariable(variableId)
const grapherConfig = await getMergedGrapherConfigForVariable(
variableId,
db.knexInstance()
)
if (
grapherConfig &&
(!grapherConfig.dimensions || grapherConfig.dimensions.length === 0)
Expand Down
2 changes: 1 addition & 1 deletion adminSiteServer/gitDataExport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export async function syncDatasetToGitRepo(
await Promise.all([
fs.writeFile(
path.join(tmpDatasetDir, `${dataset.filename}.csv`),
await dataset.toCSV()
await dataset.toCSV(db.knexInstance())
),
fs.writeFile(
path.join(tmpDatasetDir, `datapackage.json`),
Expand Down
29 changes: 20 additions & 9 deletions adminSiteServer/mockSiteRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import {
} from "../baker/GrapherBaker.js"
import { GdocPost } from "../db/model/Gdoc/GdocPost.js"
import { GdocDataInsight } from "../db/model/Gdoc/GdocDataInsight.js"
import * as db from "../db/db.js"

require("express-async-errors")

Expand Down Expand Up @@ -160,6 +161,7 @@ mockSiteRouter.get("/grapher/:slug", async (req, res) => {
res.send(
await renderPreviewDataPageOrGrapherPage(
entity.config,
db.knexInstance(),
publishedExplorersBySlug
)
)
Expand Down Expand Up @@ -227,19 +229,28 @@ mockSiteRouter.get("/datapage-preview/:id", async (req, res) => {
await explorerAdminServer.getAllPublishedExplorersBySlugCached()

res.send(
await renderDataPageV2({
variableId,
variableMetadata,
isPreviewing: true,
useIndicatorGrapherConfigs: true,
publishedExplorersBySlug,
})
await renderDataPageV2(
{
variableId,
variableMetadata,
isPreviewing: true,
useIndicatorGrapherConfigs: true,
publishedExplorersBySlug,
},
db.knexInstance()
)
)
})

countryProfileSpecs.forEach((spec) =>
mockSiteRouter.get(`/${spec.rootPath}/:countrySlug`, async (req, res) =>
res.send(await countryProfileCountryPage(spec, req.params.countrySlug))
res.send(
await countryProfileCountryPage(
spec,
req.params.countrySlug,
db.knexInstance()
)
)
)
)

Expand Down Expand Up @@ -345,7 +356,7 @@ mockSiteRouter.get("/*", async (req, res) => {
}

try {
res.send(await renderPageBySlug(slug))
res.send(await renderPageBySlug(slug, db.knexInstance()))
} catch (e) {
console.error(e)
res.status(404).send(await renderNotFoundPage())
Expand Down
7 changes: 5 additions & 2 deletions baker/DeployUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { SiteBaker } from "../baker/SiteBaker.js"
import { WebClient } from "@slack/web-api"
import { DeployChange, DeployMetadata } from "@ourworldindata/utils"
import { Knex } from "knex"

const deployQueueServer = new DeployQueueServer()

Expand All @@ -34,6 +35,7 @@ export const defaultCommitMessage = async (): Promise<string> => {
*/
const triggerBakeAndDeploy = async (
deployMetadata: DeployMetadata,
knex: Knex<any, any[]>,
lightningQueue?: DeployChange[]
) => {
// deploy to Buildkite if we're on master and BUILDKITE_API_ACCESS_TOKEN is set
Expand All @@ -60,7 +62,7 @@ const triggerBakeAndDeploy = async (

await baker.bakeGDocPosts(lightningQueue.map((c) => c.slug!))
} else {
await baker.bakeAll()
await baker.bakeAll(knex)
}
}
}
Expand Down Expand Up @@ -151,7 +153,7 @@ let deploying = false
* the end of the current one, as long as there are changes in the queue.
* If there are no changes in the queue, a deploy won't be initiated.
*/
export const deployIfQueueIsNotEmpty = async () => {
export const deployIfQueueIsNotEmpty = async (knex: Knex<any, any[]>) => {
if (deploying) return
deploying = true
let failures = 0
Expand Down Expand Up @@ -184,6 +186,7 @@ export const deployIfQueueIsNotEmpty = async () => {
await getChangesSlackMentions(parsedQueue)
await triggerBakeAndDeploy(
{ title: changesAuthorNames[0], changesSlackMentions },
knex,
// If every DeployChange is a lightning change, then we can do a
// lightning deploy. In the future, we might want to separate
// lightning updates from regular deploys so we could prioritize
Expand Down
Loading

0 comments on commit 8a389c2

Please sign in to comment.