public async void RunMigration() { var db = new FuncMigrationDatabase(); var storage = new FuncMigrationStorage(); var services = new ServiceCollection(); var contextFactory = A.Fake <IMigrationContextFactory>(); var context = new FuncMigrationContext { Database = db }; A.CallTo(() => contextFactory.GetContext()).Returns(context); var runner = new MigrationRunner( new LoggerFactory().CreateLogger <MigrationRunner>(), new ServiceProviderRapidContainerAdapter(services.BuildServiceProvider()), new MigrationEnvironment("staging"), A.Fake <IDistributedAppLockProvider>(), contextFactory, new ReflectionMigrationFinder(new List <Assembly> { typeof(MigrationStackTests).GetTypeInfo().Assembly }), storage ); // setup some state var five = new FuncMigrationKewlEntity { Id = "five", Reference = "five" }; db.UpsertKewl(five); var seven = new FuncMigrationKewlEntity { Id = "seven", Reference = "seven" }; db.UpsertKewl(seven); // let's say that migration01 has already been completed await storage.MarkAsCompleteAsync(context, new Migration01(), 123); await runner.UpgradeAsync(); // are all the migrations marked as completed? var allDocs = db.AllMigrationInfos(); Assert.Contains(allDocs, x => x.Name == nameof(Migration01) && x.MigrationCompleted); Assert.Contains(allDocs, x => x.Name == nameof(Migration02) && x.MigrationCompleted); Assert.Contains(allDocs, x => x.Name == nameof(Migration03) && x.MigrationCompleted); // check the state of the db var fiveUp = db.GetKewlById("five"); Assert.Equal("Mucho five yay", fiveUp.Reference); var sevenUp = db.GetKewlById("seven"); Assert.Equal("Mucho seven yay", sevenUp.Reference); }
public async Task MigrationRunner_PicksUpNonCompletedStepsFromPreviousMigrations() { await DropMigrationInfoTable(); await PrepareCounterTable(new List <Counter> { new Counter { Id = 999, CounterValue = 12 } }); var db = GetDb(); var services = new ServiceCollection(); var provider = new PostgreSqlConnectionProvider(); provider.Add("yolo", db, true); var contextFactory = new PostgreSqlMigrationContextFactory(provider); await PostgreSqlSchemaCreator.CreateSchemaIfNotExists(contextFactory.GetContext()); var runner = new MigrationRunner( new LoggerFactory().CreateLogger <MigrationRunner>(), new ServiceProviderRapidContainerAdapter(services.BuildServiceProvider()), new MigrationEnvironment("staging"), A.Fake <IDistributedAppLockProvider>(), contextFactory, new ReflectionMigrationFinder(new List <Assembly> { typeof(MigrationTests).GetAssembly() }), new PostgreSqlMigrationStorage() ); await InsertMigrationInfo(new MigrationInfo { Id = "1", Name = "Migration01", StepsCompleted = new List <string> { "Add at column" } }); await runner.UpgradeAsync(); // assert all migrations are marked as complete var migrationInfos = await GetAllMigrationInfo(); Assert.Contains(migrationInfos, x => x.Name == nameof(Migration01) && x.MigrationCompleted); Assert.Contains(migrationInfos, x => x.Name == nameof(Migration02) && x.MigrationCompleted); // check the state of the db var counter999 = await db.QuerySingleAsync <Counter>("select * from __Counter where Id = 999"); Assert.Equal("sample default value", counter999.Description); await DropCounterTable(); }
public async void Upgrade_Locks() { await runner.UpgradeAsync(); A.CallTo(() => appLocker.AcquireAsync("RapidCoreMigrations", A <TimeSpan> .That.Matches(x => x == TimeSpan.FromSeconds(30)), TimeSpan.MaxValue)).MustHaveHappened(); }
public async void RunMigration() { await EnsureEmptyDb(); var db = redisMuxer.GetDatabase(); var services = new ServiceCollection(); services.AddSingleton <IConnectionMultiplexer>(redisMuxer); var storage = new RedisMigrationStorage(); var context = new RedisMigrationContext { Container = new ServiceProviderRapidContainerAdapter(services.BuildServiceProvider()) }; var runner = new MigrationRunner( new LoggerFactory().CreateLogger <MigrationRunner>(), new ServiceProviderRapidContainerAdapter(services.BuildServiceProvider()), new MigrationEnvironment("staging"), A.Fake <IDistributedAppLockProvider>(), new RedisMigrationContextFactory(), new ReflectionMigrationFinder(new List <Assembly> { typeof(MigrationTests).GetTypeInfo().Assembly }), storage ); // setup some state await db.StringSetAsync("five", "5"); await db.StringSetAsync("seven", "7"); // let's say that migration01 has already been completed await storage.MarkAsCompleteAsync(context, new Migration01(), 123); await runner.UpgradeAsync(); // are all the migrations marked as completed? var migrationInfos = await GetAllMigrationInfos(); Assert.Equal(3, migrationInfos.Count); Assert.Contains(migrationInfos, x => x.Name == nameof(Migration01) && x.MigrationCompleted); Assert.Contains(migrationInfos, x => x.Name == nameof(Migration02) && x.MigrationCompleted); Assert.Contains(migrationInfos, x => x.Name == nameof(Migration03) && x.MigrationCompleted); // check the state of the db Assert.Equal("5 up", await db.StringGetAsync("five")); Assert.Equal("7 up", await db.StringGetAsync("seven")); Assert.Equal("OMG OMG OMG", await db.StringGetAsync("thirteen")); }
public async void RunMigration() { EnsureEmptyCollection <MigrationDocument>(); EnsureEmptyCollection <KewlEntity>(); var db = new MongoDbConnection(GetDb()); var services = new ServiceCollection(); var connectionProvider = new ConnectionProvider(); connectionProvider.Add("x", db, true); var storage = new MongoMigrationStorage(); var context = new MongoMigrationContext { ConnectionProvider = connectionProvider }; var runner = new MigrationRunner( new LoggerFactory().CreateLogger <MigrationRunner>(), new ServiceProviderRapidContainerAdapter(services.BuildServiceProvider()), new MigrationEnvironment("staging"), A.Fake <IDistributedAppLockProvider>(), new MongoMigrationContextFactory(connectionProvider), new ReflectionMigrationFinder(new List <Assembly> { typeof(MigrationTests).GetAssembly() }), storage ); // setup some state var five = new KewlEntity { Reference = 5 }; await db.InsertAsync <KewlEntity>(five); var seven = new KewlEntity { Reference = 7 }; await db.InsertAsync <KewlEntity>(seven); // let's say that migration01 has already been completed await storage.MarkAsCompleteAsync(context, new Migration01(), 123); await runner.UpgradeAsync(); // are all the migrations marked as completed? var allDocs = GetAll <MigrationDocument>(); Assert.Contains(allDocs, x => x.Name == nameof(Migration01) && x.MigrationCompleted); Assert.Contains(allDocs, x => x.Name == nameof(Migration02) && x.MigrationCompleted); Assert.Contains(allDocs, x => x.Name == nameof(Migration03) && x.MigrationCompleted); // check the state of the db var fiveUp = await db.FirstOrDefaultAsync <KewlEntityUpdated>(x => x.Id == five.Id); Assert.Equal("5", fiveUp.Reference); Assert.Equal("Ulla Henriksen", fiveUp.Mucho); var sevenUp = await db.FirstOrDefaultAsync <KewlEntityUpdated>(x => x.Id == seven.Id); Assert.Equal("7", sevenUp.Reference); Assert.Equal("Bubbly", sevenUp.Mucho); }