* Restores database data from a backup.
(options?: {
env?: string;
schema?: string;
fromEnv?: string;
args?: string[];
})
| 381 | * Restores database data from a backup. |
| 382 | */ |
| 383 | async function restore(options?: { |
| 384 | env?: string; |
| 385 | schema?: string; |
| 386 | fromEnv?: string; |
| 387 | args?: string[]; |
| 388 | }) { |
| 389 | // Find the latest backup file for the selected environment |
| 390 | const files = await readdir(backupsDir); |
| 391 | let file = files |
| 392 | .sort() |
| 393 | .reverse() |
| 394 | .filter((x) => x.endsWith(".sql")) |
| 395 | .find((file) => { |
| 396 | return options?.fromEnv |
| 397 | ? file.endsWith(`_${options.fromEnv}.sql`) |
| 398 | : !file.includes("_"); |
| 399 | }); |
| 400 | |
| 401 | if (!file) { |
| 402 | throw new Error("Backup file not found."); |
| 403 | } |
| 404 | |
| 405 | file = relative(process.cwd(), resolve(backupsDir, file)); |
| 406 | |
| 407 | const db = getDatabase(options); |
| 408 | const { current_database } = await db |
| 409 | .select(db.raw("current_database()")) |
| 410 | .first() |
| 411 | .finally(() => db.destroy()); |
| 412 | |
| 413 | const fileName = chalk.greenBright(file); |
| 414 | const dbName = chalk.greenBright(current_database); |
| 415 | console.log(`Restoring ${fileName} to ${dbName}...`); |
| 416 | |
| 417 | await execa( |
| 418 | "psql", |
| 419 | [ |
| 420 | "--file", |
| 421 | file, |
| 422 | "--echo-errors", |
| 423 | "--no-readline", |
| 424 | ...(options?.args ?? []), |
| 425 | ], |
| 426 | { stdio: "inherit" }, |
| 427 | ); |
| 428 | } |
| 429 | |
| 430 | /** |
| 431 | * Generates TypeScript types from a live PostgreSQL database. |
no test coverage detected