Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions apps/tk/src/hooks/backup-filtered-db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { exec } from "child_process";
import cron from "node-cron";

const COMMAND = "pnpm --filter @forge/db with-env tsx scripts/seed_devdb.ts";

export function execute() {
cron.schedule("0 8 * * *", () => {
console.log(
"[CRON] Prod DB backup for local dev job fired:",
new Date().toISOString(),
);

exec(COMMAND, (error, stdout) => {
if (error) {
console.error("[CRON] Command failed:", error);
return;
}
if (stdout) {
console.log("[CRON] Command stdout:", stdout);
}
});
});
}
2 changes: 2 additions & 0 deletions apps/tk/src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// import { execute as beep } from "./beep";
import { execute as alumniSync } from "./alumni-assign";
import { execute as animals } from "./animals";
import { execute as prodBackup } from "./backup-filtered-db";
import { execute as daily } from "./daily";
import { execute as emailQueue } from "./email-queue";
import { execute as reminder } from "./reminder";
Expand All @@ -15,4 +16,5 @@ export const hooks = {
emailQueue,
alumniSync,
roleSync,
prodBackup,
};
63 changes: 63 additions & 0 deletions packages/db/scripts/get_prod_db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* eslint-disable no-console */
// Usage:
// pnpm --filter @forge/db with-env tsx scripts/get_prod_db.tsx

// Script to get prod db data into local db. Simply run the command above to get the rows and insert them automatically, this won't lose any data that you already have such as the superadmin from the bootstrap script. The bootstrap script can also be ran after running this command and it'll work fine.

import { exec } from "child_process";
import fs from "fs";
import { unlink } from "fs/promises";
import { pipeline } from "stream/promises";
import { promisify } from "util";

import { minioClient } from "../../api/src/minio/minio-client";
import { env } from "../src/env";

const execAsync = promisify(exec);

function parsePg() {
const u = new URL(env.DATABASE_URL);
return {
originalDb: u.pathname.slice(1),
user: u.username,
password: u.password,
host: u.hostname,
port: u.port,
};
}

async function main() {
const BUCKET_NAME = "dev-db-backups";
const objectName = "backup.sql";

const fileUrl = await minioClient.presignedGetObject(
BUCKET_NAME,
objectName,
60 * 60 * 24,
);

console.log("Pulling backup.sql from minio");

const res = await fetch(fileUrl);
if (!res.ok || !res.body) {
throw new Error(`Download failed: ${res.status}`);
}

await pipeline(res.body, fs.createWriteStream(objectName));

const { originalDb: _, user, password, host: _host, port } = parsePg();
/* eslint-disable no-restricted-properties */
const envN = { ...process.env, PGPASSWORD: password };

console.log("Inserting prod rows into local DB");
try {
await execAsync(
`psql -h localhost -p ${port} -U ${user} local < ${objectName}`,
{ env: envN },
);
} finally {
await unlink(objectName);
}
}

await main();
Loading
Loading