**DB Models Changes**
- 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
This commit is contained in:
1 parent
9a8ee181e5
commit
ebdef6ba6c
3 files changed
+47
No files matched your search
@@ -1,4 +1,5 @@
|
||||
const { Sequelize } = require("sequelize");
|
||||
const sequelize = require("../config/database");
|
||||
const Collection = require("../models/Collection");
|
||||
const Genre = require("../models/Genre");
|
||||
const Media = require("../models/Media");
|
||||
@@ -6,6 +7,8 @@ const Media = require("../models/Media");
|
||||
|
||||
async function initializeData() {
|
||||
initializeGenres();
|
||||
migrateAccuracyField();
|
||||
}
|
||||
|
||||
|
||||
// const collections = await Collection.findAll({
|
||||
@@ -84,3 +87,39 @@ module.exports = {
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,10 @@ const EntrySeen = sequelize.define("entrySeen",
|
||||
|
||||
date_seen: { type: DataTypes.DATE },
|
||||
seen_with: { type: DataTypes.STRING(50) },
|
||||
accuracy: {
|
||||
type: DataTypes.ENUM('EXACT', 'ESTIMATED', 'UNKNOWN'),
|
||||
defaultValue: 'EXACT',
|
||||
},
|
||||
|
||||
}, {
|
||||
timestamps: false,
|
||||
|
||||
@@ -23,6 +23,10 @@ const EpisodeSeen = sequelize.define("episodeSeen",
|
||||
|
||||
date_seen: { type: DataTypes.DATE },
|
||||
seen_with: { type: DataTypes.STRING(50) },
|
||||
accuracy: {
|
||||
type: DataTypes.ENUM('EXACT', 'ESTIMATED', 'UNKNOWN'),
|
||||
defaultValue: 'EXACT',
|
||||
},
|
||||
|
||||
}
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user