diff --git a/src/constants.js b/src/constants.js index 5e4364f..e64fff6 100644 --- a/src/constants.js +++ b/src/constants.js @@ -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 || ""; diff --git a/src/sources/fetchExportFromFTP.js b/src/sources/fetchExportFromFTP.js index 5dfa67d..c3042d5 100644 --- a/src/sources/fetchExportFromFTP.js +++ b/src/sources/fetchExportFromFTP.js @@ -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", ""); diff --git a/src/utils/createDbDump.js b/src/utils/createDbDump.js index 15fadac..0a99176 100644 --- a/src/utils/createDbDump.js +++ b/src/utils/createDbDump.js @@ -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; @@ -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) => { @@ -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'"], @@ -71,7 +71,7 @@ export const createDbDump = async () => { PGPASSWORD: pgConnection.password, PGHOST: pgConnection.host, PGPORT: pgConnection.port, - PGDATABASE: pgConnection.database, + PGDATABASE: database, }, }, ); diff --git a/src/utils/importDbDump.js b/src/utils/importDbDump.js index 001461f..4532554 100644 --- a/src/utils/importDbDump.js +++ b/src/utils/importDbDump.js @@ -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"; @@ -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( @@ -84,7 +86,7 @@ export const importDbDump = async () => { "--if-exists", "--no-owner", `-U ${pgConnection.user}`, - `-d ${pgConnection.database}`, + `-d ${database}`, "--single-transaction", `${filePath}`, ], @@ -96,7 +98,7 @@ export const importDbDump = async () => { PGPASSWORD: pgConnection.password, PGHOST: pgConnection.host, PGPORT: pgConnection.port, - PGDATABASE: pgConnection.database, + PGDATABASE: database, }, }, );