private bool Migrate()
        {
            var currentSchema = _schemas.Find(_ => true).FirstOrDefault();

            if (currentSchema == null)
            {
                // We do not have a schema version yet
                // - assume an empty database and run full migrations
                currentSchema = new SchemaDto
                {
                    Version = MongoSchema.None
                };
            }

            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 dropping 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 outta here.
                return(false);
            }

            IMongoMigrationStrategy migration;

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

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

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

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

            migration.Execute(currentSchema.Version, RequiredSchemaVersion);
            return(true);
        }
예제 #2
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);
            }
        }