From 25944569f4e331a5e395cf1cbb90a6e1ac6df37f Mon Sep 17 00:00:00 2001 From: Daven Quinn Date: Sat, 21 Sep 2024 15:33:48 -0500 Subject: [PATCH 1/2] Mark individual env var approach as deprecated --- Makefile | 2 +- v2/credentials.ts | 26 ++++++++++++++++++-------- v2/larkin.ts | 22 ++++++++++++++++++---- 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/Makefile b/Makefile index 95d8cef1..5c3c8dd6 100644 --- a/Makefile +++ b/Makefile @@ -6,6 +6,6 @@ DOCKER_TAG = latest run: docker build -t macrostrat-api . - docker run --env-file .env --rm -it -p 5000:5000 macrostrat-api + docker run --env-file .env --rm -it -p 5000:5000 macrostrat-api diff --git a/v2/credentials.ts b/v2/credentials.ts index 7c4ef6b7..72f1eb9b 100644 --- a/v2/credentials.ts +++ b/v2/credentials.ts @@ -1,11 +1,21 @@ - -exports.pg = { - host: process.env.host, - port: process.env.port, - user: process.env.user, - password: process.env.password, - database: process.env.database -}; +if (process.env.MACROSTRAT_DATABASE != null) { + // Connect using a database URL + const macrostratDatabaseURL = process.env.MACROSTRAT_DATABASE + const elevationDatabaseURL = process.env.ELEVATION_DATABASE ?? macrostratDatabaseURL.replace("/macrostrat", "/elevation"); + exports.pg = { + macrostratDatabaseURL, + elevationDatabaseURL + }; +} else { + console.warn("Using deprecated database configuration, please migrate to the MACROSTRAT_DATABASE= format"); + exports.pg = { + host: process.env.host, + port: process.env.port, + user: process.env.user, + password: process.env.password, + database: process.env.database + }; +} exports.postgresDatabases = { diff --git a/v2/larkin.ts b/v2/larkin.ts index 85f58837..00e6b879 100644 --- a/v2/larkin.ts +++ b/v2/larkin.ts @@ -80,11 +80,25 @@ const { Client, Pool } = require("pg"); "In Macrostrat v2, 'geomacro' is merged with 'burwell' into the 'macrostrat' database.", ); } - let connectionDetails = {...credentials.pg}; - if (dbName == "elevation") { - /* Special case for elevation database (temporary) */ - connectionDetails.database = 'elevation' + let connectionDetails; + + const postgresCfg = credentials.pg; + + const inURLMode = postgresCfg.macrostratDatabaseURL != null; + if (inURLMode) { + let connectionString = postgresCfg.macrostratDatabaseURL + if (dbName == "elevation") { + connectionString = postgresCfg.elevationDatabaseURL + } + connectionDetails = { connectionString } + + } else { + connectionDetails = {...credentials.pg} + if (dbName == "elevation") { + /* Special case for elevation database (temporary) */ + connectionDetails.database = 'elevation' + } } const pool = new Pool(connectionDetails); From 5b8d31642b53663fd0597ec68a88649fcb61521a Mon Sep 17 00:00:00 2001 From: Daven Quinn Date: Tue, 24 Sep 2024 15:49:37 -0700 Subject: [PATCH 2/2] Add option to use MACROSTRAT_DB_URL instead of MACROSTRAT_DATABASE --- v2/credentials.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/v2/credentials.ts b/v2/credentials.ts index 72f1eb9b..c9ad6e8f 100644 --- a/v2/credentials.ts +++ b/v2/credentials.ts @@ -1,3 +1,8 @@ +if (process.env.MACROSTRAT_DB_URL != null && process.env.MACROSTRAT_DATABASE == null) { + console.warn("Using deprecated database configuration, please migrate to the MACROSTRAT_DATABASE= format"); + process.env.MACROSTRAT_DATABASE = process.env.MACROSTRAT_DB_URL; +} + if (process.env.MACROSTRAT_DATABASE != null) { // Connect using a database URL const macrostratDatabaseURL = process.env.MACROSTRAT_DATABASE