Fix v4 migration when no existing .env

This commit is contained in:
Chrystian Huot 2020-05-15 16:26:31 -04:00
parent 020d559667
commit 6e604706b8

View file

@ -37,7 +37,9 @@ module.exports = {
const envFile = path.resolve(__dirname, '../../../.env'); 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', { const oldSystem = sequelize.define('rdioScannerSystem', {
aliases: { aliases: {
@ -76,26 +78,31 @@ module.exports = {
let systems; let systems;
try { if (envFileExists) {
systems = await oldSystem.findAll({ order: [['system', 'ASC']] }); try {
systems = await oldSystem.findAll({ order: [['system', 'ASC']] });
systems = systems.map((s) => ({ systems = systems.map((s) => ({
id: s.system, id: s.system,
label: s.name, label: s.name,
talkgroups: s.talkgroups.map((t) => ({ talkgroups: s.talkgroups.map((t) => ({
id: t.dec, id: t.dec,
label: t.alphaTag, label: t.alphaTag,
name: t.description, name: t.description,
tag: t.tag, tag: t.tag,
group: t.group, group: t.group,
})), })),
units: Array.isArray(s.aliases) ? s.aliases.map((a) => ({ units: Array.isArray(s.aliases) ? s.aliases.map((a) => ({
id: a.uid, id: a.uid,
label: a.name, label: a.name,
})) : [], })) : [],
})); }));
} catch (_) { } catch (_) {
systems = getExampleSystems();
}
} else {
systems = getExampleSystems(); systems = getExampleSystems();
} }
@ -128,7 +135,9 @@ module.exports = {
fs.writeFileSync(configFile, JSON.stringify(config, null, 4)); fs.writeFileSync(configFile, JSON.stringify(config, null, 4));
fs.unlinkSync(envFile); if (fs.existsSync(envFile)) {
fs.unlinkSync(envFile);
}
} }
const transaction = await sequelize.transaction(); const transaction = await sequelize.transaction();