Skip to content

Commit

Permalink
Merge pull request #152 from HSLdevcom/AB#31023-db-dump-fix
Browse files Browse the repository at this point in the history
AB#31023: db dump fix
  • Loading branch information
e-halinen authored Feb 27, 2024
2 parents 7150860 + f952d3a commit b42317c
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export const SERVER_PORT = secretsEnv.SERVER_PORT || 3000;
export const ADMIN_PASSWORD = secretsEnv.ADMIN_PASSWORD || "password";
export const PATH_PREFIX = secretsEnv.PATH_PREFIX || "/";
export const SCHEMA = "jore";
export const DEFAULT_DATABASE = "postgres";
export const INTERMEDIATE_SCHEMA = "jore_new";
export const AZURE_UPLOAD_CONTAINER = secretsEnv.AZURE_UPLOAD_CONTAINER || "joredumps";
export const AZURE_STORAGE_ACCOUNT = secretsEnv.AZURE_STORAGE_ACCOUNT || "";
Expand Down
2 changes: 1 addition & 1 deletion src/sources/fetchExportFromFTP.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export async function fetchExportFromFTP() {

await client.cd(FTP_PATH);
const files = await client.list();

const zips = files.filter(({ name }) => name.endsWith(".zip"));
const newestFile = orderBy(zips, "name", "desc")[0];
const newestExportName = get(newestFile, "name", "");
Expand Down
10 changes: 5 additions & 5 deletions src/utils/createDbDump.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import fs from "fs-extra";
import { format } from "date-fns";
import pgConnectionString from "pg-connection-string";

import { PG_CONNECTION_STRING } from "../constants.js";
import { PG_CONNECTION_STRING, DEFAULT_DATABASE } from "../constants.js";

const { parse } = pgConnectionString;

Expand All @@ -15,7 +15,7 @@ const dumpsDir = path.join(cwd, "dumps");

export const deleteFiles = ({ filesDir, minFileCount }) => {
fs.readdir(filesDir, (err, files) => {
if (files.length < minFileCount) return;
if (!files || files.length < minFileCount) return;
files.forEach((file) => {
const removableFile = path.join(filesDir, file);
fs.stat(removableFile, async (err, stat) => {
Expand Down Expand Up @@ -58,8 +58,8 @@ export const createDbDump = async () => {
resolve(filePath);
} else {
const pgConnection = parse(PG_CONNECTION_STRING);
console.log(`Dumping the ${pgConnection.database} database into ${filePath}`);

const database = pgConnection.database ? pgConnection.database : DEFAULT_DATABASE;
console.log(`Dumping the ${database} database into ${filePath}`);
const dumpProcess = childProcess.spawn(
"pg_dump",
[`-f ${filePath}`, "-Fc", "-N '*old'", "-N '*new'"],
Expand All @@ -71,7 +71,7 @@ export const createDbDump = async () => {
PGPASSWORD: pgConnection.password,
PGHOST: pgConnection.host,
PGPORT: pgConnection.port,
PGDATABASE: pgConnection.database,
PGDATABASE: database,
},
},
);
Expand Down
6 changes: 4 additions & 2 deletions src/utils/importDbDump.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
AZURE_STORAGE_KEY,
AZURE_STORAGE_ACCOUNT,
AZURE_UPLOAD_CONTAINER,
DEFAULT_DATABASE
} from "../constants.js";
import { clearDb } from "../setup/clearDb.js";
import { deleteFiles } from "./createDbDump.js";
Expand Down Expand Up @@ -75,6 +76,7 @@ export const importDbDump = async () => {

if (fileExists) {
const pgConnection = parse(PG_CONNECTION_STRING);
const database = pgConnection.database ? pgConnection.database : DEFAULT_DATABASE;
console.log(`Restoring db with ${filePath}`);

const dumpProcess = childProcess.spawn(
Expand All @@ -84,7 +86,7 @@ export const importDbDump = async () => {
"--if-exists",
"--no-owner",
`-U ${pgConnection.user}`,
`-d ${pgConnection.database}`,
`-d ${database}`,
"--single-transaction",
`${filePath}`,
],
Expand All @@ -96,7 +98,7 @@ export const importDbDump = async () => {
PGPASSWORD: pgConnection.password,
PGHOST: pgConnection.host,
PGPORT: pgConnection.port,
PGDATABASE: pgConnection.database,
PGDATABASE: database,
},
},
);
Expand Down

0 comments on commit b42317c

Please sign in to comment.