|
| 1 | +/* |
| 2 | + * VPDB - Virtual Pinball Database |
| 3 | + * Copyright (C) 2019 freezy <[email protected]> |
| 4 | + * |
| 5 | + * This program is free software; you can redistribute it and/or |
| 6 | + * modify it under the terms of the GNU General Public License |
| 7 | + * as published by the Free Software Foundation; either version 2 |
| 8 | + * of the License, or (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program; if not, write to the Free Software |
| 17 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | + */ |
| 19 | + |
| 20 | +import { logger } from '../../app/common/logger'; |
| 21 | +import { state } from '../../app/state'; |
| 22 | +import { apiCache } from '../../app/common/api.cache'; |
| 23 | + |
| 24 | +/** |
| 25 | + * Updates most ROM files with the type, so we know which is the main ROM, |
| 26 | + * which is the sound ROM etc. |
| 27 | + * |
| 28 | + * @see https://github.com/vpdb/vpx-js/issues/130 |
| 29 | + */ |
| 30 | +export async function up() { |
| 31 | + |
| 32 | + // read the parsed types |
| 33 | + const romTypes = require('../../../data/rom-types.json'); |
| 34 | + |
| 35 | + // fetch all roms |
| 36 | + const roms = await state.models.Rom.find({}).exec(); |
| 37 | + logger.info(null, '[migrate-23] Updating %s ROMs with file types...', roms.length); |
| 38 | + |
| 39 | + // loop through roms and rom files |
| 40 | + let count = 0; |
| 41 | + for (const rom of roms) { |
| 42 | + if (!romTypes[rom.id]) { |
| 43 | + continue; |
| 44 | + } |
| 45 | + for (const romFile of rom.rom_files) { |
| 46 | + const romType = romTypes[rom.id][romFile.filename.toLowerCase()]; |
| 47 | + if (!romType) { |
| 48 | + continue; |
| 49 | + } |
| 50 | + |
| 51 | + // update with new data |
| 52 | + romFile.type = romType.type; |
| 53 | + if (romType.romType) { |
| 54 | + romFile.system = romType.romType; |
| 55 | + } |
| 56 | + count++; |
| 57 | + } |
| 58 | + await rom.save(); |
| 59 | + } |
| 60 | + logger.info(null, '[migrate-23] Updated %s ROM files!', count); |
| 61 | + |
| 62 | + // clear all caches |
| 63 | + await apiCache.invalidateAll(); |
| 64 | + logger.info(null, '[migrate-23] Cache invalidated.'); |
| 65 | +} |
0 commit comments