enh: adjust instance defaults migration

This commit is contained in:
Ben Allfree
2023-11-14 17:11:04 -08:00
parent b229cd5c35
commit 67f8763dcf
2 changed files with 44 additions and 1 deletions

View File

@@ -0,0 +1,38 @@
migrate(
(db) => {
try {
const APP_DEFAULTS = {
appName: 'Acme',
appUrl: 'http://localhost:8090',
senderName: 'Support',
senderAddress: 'support@example.com',
}
const { PH_APP_NAME, PH_INSTANCE_URL, PH_APEX_DOMAIN } = process.env
const dao = new Dao(db)
const settings = dao.findSettings()
if (!settings) {
throw new Error(`Expected settings here`)
}
const fix = (field, newValue) => {
if (!newValue || settings.meta[field] !== APP_DEFAULTS[field]) return
settings.meta[field] = newValue
}
const { hostname } = new URL(PH_INSTANCE_URL)
fix(`appName`, PH_APP_NAME)
fix(`appUrl`, PH_INSTANCE_URL)
fix(`senderName`, PH_APP_NAME)
fix(`senderAddress`, `${PH_APP_NAME}@${hostname}`)
dao.saveSettings(settings)
} catch (e) {
console.error(`***error applying defaults: ${e}`)
}
},
(db) => {
// add down queries...
},
)