/// <summary> /// Backs up each hangfire.mongo collection before executing migration /// </summary> /// <param name="storageOptions"></param> /// <param name="database"></param> /// <param name="fromSchema"></param> /// <param name="toSchema"></param> /// <exception cref="InvalidOperationException"></exception> public override void Backup(MongoStorageOptions storageOptions, IMongoDatabase database, MongoSchema fromSchema, MongoSchema toSchema) { var existingCollectionNames = MongoMigrationUtils.ExistingHangfireCollectionNames(database, fromSchema, storageOptions).ToList(); var backupCollectionNames = existingCollectionNames .ToDictionary(k => k, v => MongoMigrationUtils.GetBackupCollectionName(v, fromSchema, storageOptions)); // Let's double check that we have not backed up before. var existingBackupCollectionName = existingCollectionNames.FirstOrDefault(n => backupCollectionNames.ContainsValue(n)); if (existingBackupCollectionName != null) { throw new InvalidOperationException( $"{Environment.NewLine}{existingBackupCollectionName} already exists. Cannot perform backup." + $"{Environment.NewLine}Cannot overwrite existing backups. Please resolve this manually (e.g. by droping collection)." + $"{Environment.NewLine}Please see https://github.com/sergeyzwezdin/Hangfire.Mongo#migration for further information."); } // Now do the actual backup foreach (var collection in backupCollectionNames) { BackupCollection(database, collection.Key, collection.Value); } }
/// <summary> /// drop all hangfire collections /// </summary> /// <param name="database"></param> /// <param name="fromSchema"></param> /// <param name="toSchema"></param> protected override void ExecuteStrategy(IMongoDatabase database, MongoSchema fromSchema, MongoSchema toSchema) { foreach (var collectionName in MongoMigrationUtils.ExistingHangfireCollectionNames(database, fromSchema, StorageOptions)) { database.DropCollection(collectionName); } }