Skip to content

Commit

Permalink
fix: add missing code to sync
Browse files Browse the repository at this point in the history
  • Loading branch information
mlbrgl committed Feb 13, 2024
1 parent 68512b7 commit 5912d1d
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 1 deletion.
2 changes: 1 addition & 1 deletion db/migration/1707467006420-AddRedirectsTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class AddRedirectsTable1707467006420 implements MigrationInterface {
id INT AUTO_INCREMENT PRIMARY KEY,
source TEXT NOT NULL,
target TEXT NOT NULL,
code ENUM('301', '302') DEFAULT '301',
code INT DEFAULT 301,
createdAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
Expand Down
12 changes: 12 additions & 0 deletions db/model/Redirect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as db from "../db"
import { Redirect } from "@ourworldindata/types"

export const getRedirectsFromDb = async (): Promise<Redirect[]> => {
const redirectsFromDb = (
await db.knexInstance().raw(`
SELECT source, target, code FROM redirects
`)
)[0]

return redirectsFromDb
}
37 changes: 37 additions & 0 deletions db/syncRedirectsToGrapher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import * as db from "./db"
import * as wpdb from "./wpdb"
import { getRedirectsFromDb } from "./model/Redirect.js"

export const syncRedirectsToGrapher = async (): Promise<void> => {
const allWordpressRedirects = await wpdb.FOR_SYNC_ONLY_getRedirects()
const existingRedirectsFromDb = await getRedirectsFromDb()

await db.knexInstance().transaction(async (t) => {
for (const { source, target, code } of allWordpressRedirects) {
// We only want to insert redirects for which we don't have a redirected source yet
if (existingRedirectsFromDb.some((r) => r.source === source)) {
console.log(
`Skipping, a redirect already exists for "${source}"`
)
continue
}
console.log(`Adding redirect: ${source} -> ${target} (${code})`)
await t.raw(
`INSERT INTO redirects (source, target, code) VALUES (?, ?, ?)`,
[source, target, code]
)
}
})
}

const main = async (): Promise<void> => {
try {
await db.getConnection()
await syncRedirectsToGrapher()
} finally {
await wpdb.singleton.end()
await db.closeTypeOrmAndKnexConnections()
}
}

main()
8 changes: 8 additions & 0 deletions db/wpdb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { Knex, knex } from "knex"
import { Base64 } from "js-base64"
import { registerExitHandler } from "./cleanup.js"
import { WP_PostType, JsonError, PostRestApi } from "@ourworldindata/utils"
import { Redirect } from "@ourworldindata/types"

let _knexInstance: Knex

Expand Down Expand Up @@ -266,3 +267,10 @@ export const FOR_SYNC_ONLY_getTables = async (): Promise<

return tables
}

export const FOR_SYNC_ONLY_getRedirects = async (): Promise<Redirect[]> => {
return singleton.query(`
SELECT url AS source, action_data AS target, action_code AS code
FROM wp_redirection_items WHERE status = 'enabled'
`)
}
10 changes: 10 additions & 0 deletions packages/@ourworldindata/types/src/dbTypes/Redirects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export enum RedirectCode {
MOVED_PERMANENTLY = 301,
FOUND = 302,
}

export interface Redirect {
source: string
target: string
code: RedirectCode
}
2 changes: 2 additions & 0 deletions packages/@ourworldindata/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -598,3 +598,5 @@ export {
serializeVariableProcessingLog,
type License,
} from "./dbTypes/Variables.js"

export { RedirectCode, type Redirect } from "./dbTypes/Redirects.js"

0 comments on commit 5912d1d

Please sign in to comment.