Added better error checking to migration schema statics.
This commit is contained in:
parent
9fda308306
commit
ad0dd6bdbb
|
|
@ -24,6 +24,7 @@ const {mongoose} = require('mongoose');
|
|||
const config = require('../../../config.json');
|
||||
const {userModel} = require('../user/userSchema');
|
||||
const permissionModel = require('../permissionSchema');
|
||||
const loggerUtils = require('../../utils/loggerUtils');
|
||||
|
||||
|
||||
/**
|
||||
|
|
@ -66,11 +67,15 @@ const migrationSchema = new mongoose.Schema({
|
|||
},
|
||||
});
|
||||
|
||||
//TODO: before next commit, add error checking to the ingestLegacy statics down below
|
||||
//Also add a warning for the fail condition in ingestLegacyDump that bails out when missing files
|
||||
|
||||
//statics
|
||||
/**
|
||||
* Static method for ingesting data dump from legacy cytube/fore.st server
|
||||
*/
|
||||
migrationSchema.statics.ingestLegacyDump = async function(){
|
||||
try{
|
||||
//If migration is disabled
|
||||
if(!config.migrate){
|
||||
//BAIL!
|
||||
|
|
@ -87,6 +92,7 @@ migrationSchema.statics.ingestLegacyDump = async function(){
|
|||
await fs.stat(userDump);
|
||||
//If we caught an error (most likely it's missing)
|
||||
}catch(err){
|
||||
loggerUtils.consoleWarn("No migration files detected! Pleas provide legacy migration files or disable migration from config.json!");
|
||||
//BAIL!
|
||||
return;
|
||||
}
|
||||
|
|
@ -102,6 +108,9 @@ migrationSchema.statics.ingestLegacyDump = async function(){
|
|||
//Ingest the legacy user profile
|
||||
this.ingestLegacyUser(line);
|
||||
}
|
||||
}catch(err){
|
||||
return loggerUtils.localExceptionHandler(err);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -109,6 +118,7 @@ migrationSchema.statics.ingestLegacyDump = async function(){
|
|||
* @param {String} rawProfile - Line of text contianing raw profile dump
|
||||
*/
|
||||
migrationSchema.statics.ingestLegacyUser = async function(rawProfile){
|
||||
try{
|
||||
//If migration is disabled
|
||||
if(!config.migrate){
|
||||
//BAIL!
|
||||
|
|
@ -120,6 +130,8 @@ migrationSchema.statics.ingestLegacyUser = async function(rawProfile){
|
|||
|
||||
//If we have an invalid line
|
||||
if(profileMatches <= 0){
|
||||
loggerUtils.consoleWarn('Bad profile detected in legacy dump:');
|
||||
loggerUtils.consoleWarn(rawProfile);
|
||||
//BAIL!
|
||||
return;
|
||||
}
|
||||
|
|
@ -143,6 +155,8 @@ migrationSchema.statics.ingestLegacyUser = async function(rawProfile){
|
|||
|
||||
//If profile array is the wrong length
|
||||
if(profileArray.length != 10){
|
||||
loggerUtils.consoleWarn('Bad profile detected in legacy dump:');
|
||||
loggerUtils.consoleWarn(profileArray);
|
||||
//BAIL!
|
||||
return;
|
||||
}
|
||||
|
|
@ -154,7 +168,7 @@ migrationSchema.statics.ingestLegacyUser = async function(rawProfile){
|
|||
//If we found the user in the database
|
||||
if(foundMigration != null || foundUser != null){
|
||||
//Scream
|
||||
console.log(`Found legacy user ${profileArray[1]} in database, skipping migration!`);
|
||||
loggerUtils.consoleWarn(`Found legacy user ${profileArray[1]} in database, skipping migration!`);
|
||||
//BAIL!
|
||||
return;
|
||||
}
|
||||
|
|
@ -185,6 +199,9 @@ migrationSchema.statics.ingestLegacyUser = async function(rawProfile){
|
|||
|
||||
//Let the world know of our triumph!
|
||||
console.log(`Legacy user profile ${migrationProfile.user} migrated successfully!`);
|
||||
}catch(err){
|
||||
return loggerUtils.localExceptionHandler(err);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = mongoose.model("migration", migrationSchema);
|
||||
Loading…
Reference in a new issue