internal MongoMigrationManager(MongoStorageOptions storageOptions, IMongoDatabase database)
 {
     _storageOptions  = storageOptions;
     _database        = database;
     _schemas         = _database.GetCollection <SchemaDto>(storageOptions.Prefix + ".schema");
     _migrationRunner = new MongoMigrationRunner(database, storageOptions, _schemas);
 }
Example #2
0
        public MongoMigrationManager(MongoStorageOptions storageOptions, HangfireDbContext dbContext)
        {
            _storageOptions  = storageOptions;
            _dbContext       = dbContext;
            _migrationRunner = new MongoMigrationRunner(dbContext, storageOptions);
            var migrateLockCollection = _dbContext.Database.GetCollection <DistributedLockDto>(_migrateLockCollectionName);

            _lock = new MongoDistributedLock(nameof(Migrate), TimeSpan.FromSeconds(30), migrateLockCollection,
                                             _storageOptions);
        }
Example #3
0
        public void Migrate(HangfireDbContext dbContext)
        {
            using (new MongoDistributedLock(nameof(Migrate), TimeSpan.FromSeconds(30), dbContext, _storageOptions))
            {
                var currentSchema = dbContext.Schema.Find(_ => true).FirstOrDefault();
                if (currentSchema == null)
                {
                    // We do not have a schema version yet
                    // - assume an empty database and run full migrations
                    var migrationRunner = new MongoMigrationRunner(dbContext, _storageOptions);
                    migrationRunner.Execute(MongoSchema.None, RequiredSchemaVersion);
                    return;
                }

                if (RequiredSchemaVersion < currentSchema.Version)
                {
                    var assemblyName = GetType().GetTypeInfo().Assembly.GetName();
                    throw new InvalidOperationException(
                              $"{Environment.NewLine}{assemblyName.Name} version: {assemblyName.Version}, uses a schema prior to the current database." +
                              $"{Environment.NewLine}Backwards migration is not supported. Please resolve this manually (e.g. by droping the database)." +
                              $"{Environment.NewLine}Please see https://github.com/sergeyzwezdin/Hangfire.Mongo#migration for further information.");
                }

                if (RequiredSchemaVersion == currentSchema.Version)
                {
                    // Nothing to migrate - so let's get outa here.
                    return;
                }

                IMongoMigrationStrategy migration;
                switch (_storageOptions.MigrationOptions.Strategy)
                {
                case MongoMigrationStrategy.None:
                    migration = new MongoMigrationStrategyNone();
                    break;

                case MongoMigrationStrategy.Drop:
                    migration = new MongoMigrationStrategyDrop(dbContext, _storageOptions);
                    break;

                case MongoMigrationStrategy.Migrate:
                    migration = new MongoMigrationStrategyMigrate(dbContext, _storageOptions);
                    break;

                default:
                    throw new ArgumentOutOfRangeException($@"Unknown migration strategy: {_storageOptions.MigrationOptions.Strategy}", $@"{nameof(MongoMigrationOptions)}.{nameof(MongoMigrationOptions.Strategy)}");
                }
                migration.Execute(currentSchema.Version, RequiredSchemaVersion);
            }
        }
        public void Migrate(HangfireDbContext dbContext)
        {
            // Read the current schema without grabing the migrate lock.
            // Chances are that we are on par with the schema.
            var currentSchemaVersion = GetCurrentSchema(dbContext);

            if (RequiredSchemaVersion == currentSchemaVersion)
            {
                // Nothing to migrate - so let's get outahere.
                return;
            }

            // We must migrate - so grap the migrate lock and go...
            using (new MongoDistributedLock("migrate", TimeSpan.FromSeconds(30), dbContext, _storageOptions))
            {
                // Read the current schema one more time under lock.
                // It might have changed in the time from aquiring
                // the lock until we actually got it.
                currentSchemaVersion = GetCurrentSchema(dbContext);

                if (currentSchemaVersion == RequiredSchemaVersion)
                {
                    // Another migration has brought us up to par.
                    // We can safely return here.
                    return;
                }

                if (RequiredSchemaVersion < currentSchemaVersion)
                {
                    var assemblyName = GetType().GetTypeInfo().Assembly.GetName();
                    throw new InvalidOperationException(
                              $"{Environment.NewLine}{assemblyName.Name} version: {assemblyName.Version}, uses a schema prior to the current connection." +
                              $"{Environment.NewLine}Backwards migration is not supported. Please resolve this manually (e.g. by droping the connection)." +
                              $"{Environment.NewLine}Please see https://github.com/sergeyzwezdin/Hangfire.Mongo#migration for further information.");
                }

                if (currentSchemaVersion == MongoSchema.None)
                {
                    // We do not have a schema version yet
                    // - assume an empty database and run a full migration
                    var migrationRunner = new MongoMigrationRunner(dbContext, _storageOptions);
                    migrationRunner.Execute(MongoSchema.None, RequiredSchemaVersion);
                    return;
                }

                IMongoMigrationStrategy migration;
                switch (_storageOptions.MigrationOptions.Strategy)
                {
                case MongoMigrationStrategy.None:
                    migration = new MongoMigrationStrategyNone();
                    break;

                case MongoMigrationStrategy.Drop:
                    migration = new MongoMigrationStrategyDrop(dbContext, _storageOptions);
                    break;

                case MongoMigrationStrategy.Migrate:
                    migration = new MongoMigrationStrategyMigrate(dbContext, _storageOptions);
                    break;

                default:
                    throw new ArgumentOutOfRangeException($@"Unknown migration strategy: {_storageOptions.MigrationOptions.Strategy}", $@"{nameof(MongoMigrationOptions)}.{nameof(MongoMigrationOptions.Strategy)}");
                }

                migration.Execute(currentSchemaVersion, RequiredSchemaVersion);
            }
        }