- Backend: add accuracy field (ENUM: EXACT, ESTIMATED, UNKNOWN) to EntrySeen and EpisodeSeen models - Backend: add migration in dataInitializer to add accuracy column, set existing to EXACT, set 2021-01-01 entry seens and 2020-01-01 episode seens to UNKNOWN
126 lines
3.9 KiB
JavaScript
126 lines
3.9 KiB
JavaScript
const { Sequelize } = require("sequelize");
|
|
const sequelize = require("../config/database");
|
|
const Collection = require("../models/Collection");
|
|
const Genre = require("../models/Genre");
|
|
const Media = require("../models/Media");
|
|
|
|
|
|
async function initializeData() {
|
|
initializeGenres();
|
|
migrateAccuracyField();
|
|
}
|
|
|
|
|
|
// const collections = await Collection.findAll({
|
|
// where: {
|
|
// posterPath: { [Sequelize.Op.notLike]: '/%' }
|
|
// }
|
|
// });
|
|
|
|
// collections.forEach(collection => {
|
|
// Media.create({
|
|
// mediaable: 'collection',
|
|
// mediaableId: collection.id,
|
|
// type: 'poster',
|
|
// path: collection.posterPath,
|
|
// isLocal: true,
|
|
// isSelected: true
|
|
// })
|
|
// });
|
|
|
|
// console.log(collections);
|
|
}
|
|
|
|
async function initializeGenres() {
|
|
try {
|
|
// * data to insert:
|
|
const initialGenres = [
|
|
{ id: 10759, name: 'Action & Adventure' },
|
|
{ id: 16, name: 'Animation' },
|
|
{ id: 35, name: 'Comedy' },
|
|
{ id: 80, name: 'Crime' },
|
|
{ id: 99, name: 'Documentary' },
|
|
{ id: 18, name: 'Drama' },
|
|
{ id: 10751, name: 'Family' },
|
|
{ id: 10762, name: 'Kids' },
|
|
{ id: 9648, name: 'Mystery' },
|
|
{ id: 10763, name: 'News' },
|
|
{ id: 10764, name: 'Reality' },
|
|
{ id: 10765, name: 'Sci-Fi & Fantasy' },
|
|
{ id: 10766, name: 'Soap' },
|
|
{ id: 10767, name: 'Talk' },
|
|
{ id: 10768, name: 'War & Politics' },
|
|
{ id: 37, name: 'Western' },
|
|
{ id: 28, name: 'Action' },
|
|
{ id: 12, name: 'Adventure' },
|
|
{ id: 14, name: 'Fantasy' },
|
|
{ id: 36, name: 'History' },
|
|
{ id: 27, name: 'Horror' },
|
|
{ id: 10402, name: 'Music' },
|
|
{ id: 10749, name: 'Romance' },
|
|
{ id: 878, name: 'Science Fiction' },
|
|
{ id: 10770, name: 'TV Movie' },
|
|
{ id: 53, name: 'Thriller' },
|
|
{ id: 10752, name: 'War' }
|
|
];
|
|
|
|
// * Check if the data already exists
|
|
const existingGenres = await Genre.findAll();
|
|
|
|
// ? If data already exists, return
|
|
if (existingGenres.length == initialGenres.length) {
|
|
console.log('Genres already initialized');
|
|
return;
|
|
}
|
|
|
|
// ? If data doesn't exist, insert initial data
|
|
await Genre.bulkCreate(initialGenres);
|
|
|
|
console.log(`${initialGenres.length} Genres initialized successfully`);
|
|
} catch (error) {
|
|
console.error('Error initializing data:', error);
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
initializeData,
|
|
|
|
initializeGenres,
|
|
}
|
|
|
|
async function migrateAccuracyField() {
|
|
try {
|
|
const qi = sequelize.getQueryInterface();
|
|
|
|
// Add accuracy column to entrySeens if it doesn't exist
|
|
const entrySeenColumns = await qi.describeTable('entrySeens');
|
|
if (!entrySeenColumns.accuracy) {
|
|
await qi.addColumn('entrySeens', 'accuracy', {
|
|
type: Sequelize.ENUM('EXACT', 'ESTIMATED', 'UNKNOWN'),
|
|
defaultValue: 'EXACT',
|
|
});
|
|
// Set all existing records to EXACT
|
|
await sequelize.query(`UPDATE entrySeens SET accuracy = 'EXACT' WHERE accuracy IS NULL`);
|
|
// Set records with date_seen = 2021-01-01 to UNKNOWN
|
|
await sequelize.query(`UPDATE entrySeens SET accuracy = 'UNKNOWN' WHERE DATE(date_seen) = '2021-01-01'`);
|
|
console.log('Migrated accuracy field on entrySeens');
|
|
}
|
|
|
|
// Add accuracy column to episodeSeens if it doesn't exist
|
|
const episodeSeenColumns = await qi.describeTable('episodeSeens');
|
|
if (!episodeSeenColumns.accuracy) {
|
|
await qi.addColumn('episodeSeens', 'accuracy', {
|
|
type: Sequelize.ENUM('EXACT', 'ESTIMATED', 'UNKNOWN'),
|
|
defaultValue: 'EXACT',
|
|
});
|
|
// Set all existing records to EXACT
|
|
await sequelize.query(`UPDATE episodeSeens SET accuracy = 'EXACT' WHERE accuracy IS NULL`);
|
|
// Set records with date_seen = 2020-01-01 to UNKNOWN
|
|
await sequelize.query(`UPDATE episodeSeens SET accuracy = 'UNKNOWN' WHERE DATE(date_seen) = '2020-01-01'`);
|
|
console.log('Migrated accuracy field on episodeSeens');
|
|
}
|
|
} catch (error) {
|
|
console.error('Error migrating accuracy field:', error);
|
|
}
|
|
}
|