From 6e604706b89bd9f94ceaa14f8326977a9c3b96c0 Mon Sep 17 00:00:00 2001 From: Chrystian Huot Date: Fri, 15 May 2020 16:26:31 -0400 Subject: [PATCH] Fix v4 migration when no existing .env --- .../20200428132918-new-v4-tables.js | 49 +++++++++++-------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/server/lib/rdio-scanner/migrations/20200428132918-new-v4-tables.js b/server/lib/rdio-scanner/migrations/20200428132918-new-v4-tables.js index e55b315..bca460f 100644 --- a/server/lib/rdio-scanner/migrations/20200428132918-new-v4-tables.js +++ b/server/lib/rdio-scanner/migrations/20200428132918-new-v4-tables.js @@ -37,7 +37,9 @@ module.exports = { const envFile = path.resolve(__dirname, '../../../.env'); - const env = fs.existsSync(envFile) ? dotenv.parse(fs.readFileSync(envFile)) : {}; + const envFileExists = fs.existsSync(envFile); + + const env = envFileExists ? dotenv.parse(fs.readFileSync(envFile)) : {}; const oldSystem = sequelize.define('rdioScannerSystem', { aliases: { @@ -76,26 +78,31 @@ module.exports = { let systems; - try { - systems = await oldSystem.findAll({ order: [['system', 'ASC']] }); + if (envFileExists) { + try { + systems = await oldSystem.findAll({ order: [['system', 'ASC']] }); - systems = systems.map((s) => ({ - id: s.system, - label: s.name, - talkgroups: s.talkgroups.map((t) => ({ - id: t.dec, - label: t.alphaTag, - name: t.description, - tag: t.tag, - group: t.group, - })), - units: Array.isArray(s.aliases) ? s.aliases.map((a) => ({ - id: a.uid, - label: a.name, - })) : [], - })); + systems = systems.map((s) => ({ + id: s.system, + label: s.name, + talkgroups: s.talkgroups.map((t) => ({ + id: t.dec, + label: t.alphaTag, + name: t.description, + tag: t.tag, + group: t.group, + })), + units: Array.isArray(s.aliases) ? s.aliases.map((a) => ({ + id: a.uid, + label: a.name, + })) : [], + })); - } catch (_) { + } catch (_) { + systems = getExampleSystems(); + } + + } else { systems = getExampleSystems(); } @@ -128,7 +135,9 @@ module.exports = { fs.writeFileSync(configFile, JSON.stringify(config, null, 4)); - fs.unlinkSync(envFile); + if (fs.existsSync(envFile)) { + fs.unlinkSync(envFile); + } } const transaction = await sequelize.transaction();