-
-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
70 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters