Exemplo n.º 1
0
        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();
        }
Exemplo n.º 2
0
        public UpgradeAsyncTests()
        {
            logger         = A.Fake <ILogger <MigrationRunner> >();
            container      = A.Fake <IRapidContainerAdapter>();
            environment    = A.Fake <IMigrationEnvironment>();
            appLocker      = A.Fake <IDistributedAppLockProvider>();
            contextFactory = A.Fake <IMigrationContextFactory>();
            finder         = A.Fake <IMigrationFinder>();
            storage        = A.Fake <IMigrationStorage>();
            context        = A.Fake <IMigrationContext>();
            appLock        = A.Fake <IDistributedAppLock>();
            migration1     = A.Fake <IMigration>();
            migration2     = A.Fake <IMigration>();

            A.CallTo(() => appLocker.AcquireAsync("RapidCoreMigrations", A <TimeSpan> ._, A <TimeSpan> ._))
            .Returns(Task.FromResult(appLock));
            A.CallTo(() => contextFactory.GetContext()).Returns(context);

            runner = new MigrationRunner(
                logger,
                container,
                environment,
                appLocker,
                contextFactory,
                finder,
                storage
                );
        }
Exemplo n.º 3
0
        /// <summary>
        /// Upgrade the environment
        /// </summary>
        public virtual async Task UpgradeAsync()
        {
            // 1. Ensure no one else runs migrations (i.e. lock)
            // 2. Find migrations
            // 3. Run each migration
            // 4. Mark successfully run migrations as completed

            using (await appLocker.AcquireAsync(GetLockName(), TimeSpan.FromSeconds(30), TimeSpan.MaxValue))
            {
                logger.LogInformation($"Lock {GetLockName()} acquired");
                var sw = new Stopwatch();

                var context = GetContext();

                foreach (var migration in await finder.FindMigrationsForUpgradeAsync(context))
                {
                    try
                    {
                        logger.LogInformation($"Attempt to run migration {migration.Name}");
                        sw.Restart();
                        await migration.UpgradeAsync(context);

                        sw.Stop();
                        logger.LogInformation($"Succeeded in running {migration.Name}. It took {sw.ElapsedMilliseconds} milliseconds.");

                        await storage.MarkAsCompleteAsync(context, migration, sw.ElapsedMilliseconds);
                    }
                    catch (Exception ex)
                    {
                        sw.Stop();
                        logger.LogCritical(666, ex, $"Failed to run migration {migration.Name} after {sw.ElapsedMilliseconds} milliseconds.");
                        throw new MigrationException($"Failed to run migration {migration.Name}. See inner exception.", ex);
                    }
                }
            }
        }