Esempio n. 1
0
        public virtual void ApplyMigrationDown(IMigrationInfo migrationInfo, bool useTransaction)
        {
            if (migrationInfo == null)
            {
                throw new ArgumentNullException("migrationInfo");
            }

            var name = migrationInfo.GetName();

            _announcer.Heading(string.Format("{0} reverting", name));

            _stopWatch.Start();

            using (IMigrationScope scope = _migrationScopeHandler.CreateOrWrapMigrationScope(useTransaction))
            {
                ExecuteMigration(migrationInfo.Migration, (m, c) => m.GetDownExpressions(c));
                if (migrationInfo.IsAttributed())
                {
                    VersionLoader.DeleteVersion(migrationInfo.Version);
                }

                scope.Complete();

                _stopWatch.Stop();

                _announcer.Say(string.Format("{0} reverted", name));
                _announcer.ElapsedTime(_stopWatch.ElapsedTime());
            }
        }
Esempio n. 2
0
        public void Rollback(int steps, bool useAutomaticTransactionManagement)
        {
            var availableMigrations  = MigrationLoader.LoadMigrations();
            var migrationsToRollback = new List <IMigrationInfo>();

            foreach (long version in VersionLoader.VersionInfo.AppliedMigrations())
            {
                IMigrationInfo migrationInfo;
                if (availableMigrations.TryGetValue(version, out migrationInfo))
                {
                    migrationsToRollback.Add(migrationInfo);
                }
            }

            using (IMigrationScope scope = _migrationScopeHandler.CreateOrWrapMigrationScope(useAutomaticTransactionManagement && TransactionPerSession))
            {
                foreach (IMigrationInfo migrationInfo in migrationsToRollback.Take(steps))
                {
                    ApplyMigrationDown(migrationInfo, useAutomaticTransactionManagement && migrationInfo.TransactionBehavior == TransactionBehavior.Default);
                }

                scope.Complete();
            }

            VersionLoader.LoadVersionInfo();

            if (!VersionLoader.VersionInfo.AppliedMigrations().Any())
            {
                VersionLoader.RemoveVersionTable();
            }
        }
Esempio n. 3
0
        public void MigrateDown(long targetVersion, bool useAutomaticTransactionManagement)
        {
            var migrationInfos = GetDownMigrationsToApply(targetVersion);

            using (IMigrationScope scope = _migrationScopeHandler.CreateOrWrapMigrationScope(useAutomaticTransactionManagement && TransactionPerSession))
            {
                try
                {
                    foreach (var migrationInfo in migrationInfos)
                    {
                        ApplyMigrationDown(migrationInfo, useAutomaticTransactionManagement && migrationInfo.TransactionBehavior == TransactionBehavior.Default);
                    }

                    ApplyProfiles();

                    scope.Complete();
                }
                catch
                {
                    if (scope.IsActive)
                    {
                        scope.Cancel();  // SQLAnywhere needs explicit call to rollback transaction
                    }
                    throw;
                }
            }

            VersionLoader.LoadVersionInfo();
        }
Esempio n. 4
0
        public void MigrateUp(bool useAutomaticTransactionManagement)
        {
            var migrations = MigrationLoader.LoadMigrations();

            using (IMigrationScope scope = _migrationScopeHandler.CreateOrWrapMigrationScope(useAutomaticTransactionManagement && TransactionPerSession))
            {
                ApplyMaintenance(MigrationStage.BeforeAll, useAutomaticTransactionManagement);

                foreach (var pair in migrations)
                {
                    ApplyMaintenance(MigrationStage.BeforeEach, useAutomaticTransactionManagement);
                    ApplyMigrationUp(pair.Value, useAutomaticTransactionManagement && pair.Value.TransactionBehavior == TransactionBehavior.Default);
                    ApplyMaintenance(MigrationStage.AfterEach, useAutomaticTransactionManagement);
                }

                ApplyMaintenance(MigrationStage.BeforeProfiles, useAutomaticTransactionManagement);

                ApplyProfiles();

                ApplyMaintenance(MigrationStage.AfterAll, useAutomaticTransactionManagement);

                scope.Complete();
            }

            VersionLoader.LoadVersionInfo();
        }
Esempio n. 5
0
        public virtual void ApplyMigrationUp(IMigrationInfo migrationInfo, bool useTransaction)
        {
            if (migrationInfo == null)
            {
                throw new ArgumentNullException("migrationInfo");
            }

            if (!_alreadyOutputPreviewOnlyModeWarning && Processor.Options.PreviewOnly)
            {
                _announcer.Heading("PREVIEW-ONLY MODE");
                _alreadyOutputPreviewOnlyModeWarning = true;
            }

            if (!migrationInfo.IsAttributed() || !VersionLoader.VersionInfo.HasAppliedMigration(migrationInfo.Version))
            {
                var name = migrationInfo.GetName();
                _announcer.Heading(string.Format("{0} migrating", name));

                _stopWatch.Start();

                using (IMigrationScope scope = _migrationScopeHandler.CreateOrWrapMigrationScope(useTransaction))
                {
                    try
                    {
                        if (migrationInfo.IsAttributed() && migrationInfo.IsBreakingChange &&
                            !RunnerContext.PreviewOnly && !RunnerContext.AllowBreakingChange)
                        {
                            throw new InvalidOperationException(
                                      string.Format(
                                          "The migration {0} is identified as a breaking change, and will not be executed unless the necessary flag (allow-breaking-changes|abc) is passed to the runner.",
                                          migrationInfo.GetName()));
                        }

                        ExecuteMigration(migrationInfo.Migration, (m, c) => m.GetUpExpressions(c));

                        if (migrationInfo.IsAttributed())
                        {
                            VersionLoader.UpdateVersionInfo(migrationInfo.Version, migrationInfo.Description ?? migrationInfo.Migration.GetType().Name);
                        }

                        scope.Complete();
                    }
                    catch
                    {
                        if (useTransaction && scope.IsActive)
                        {
                            scope.Cancel();  // SQLAnywhere needs explicit call to rollback transaction
                        }
                        throw;
                    }

                    _stopWatch.Stop();

                    _announcer.Say(string.Format("{0} migrated", name));
                    _announcer.ElapsedTime(_stopWatch.ElapsedTime());
                }
            }
        }
        public virtual void ApplyMigrationUp(IMigrationInfo migrationInfo, bool useTransaction)
        {
            if (migrationInfo == null)
            {
                throw new ArgumentNullException("migrationInfo");
            }

            if (!_alreadyOutputPreviewOnlyModeWarning && Processor.Options.PreviewOnly)
            {
                _announcer.Heading("PREVIEW-ONLY MODE");
                _alreadyOutputPreviewOnlyModeWarning = true;
            }

            if (!migrationInfo.IsAttributed() || !VersionLoader.VersionInfo.HasAppliedMigration(migrationInfo.Version))
            {
                var name = migrationInfo.GetName();
                _announcer.Heading(string.Format("{0} migrating", name));

                _stopWatch.Start();

                using (IMigrationScope scope = _migrationScopeHandler.CreateOrWrapMigrationScope(useTransaction))
                {
                    try
                    {
                        ExecuteMigration(migrationInfo.Migration, (m, c) => m.GetUpExpressions(c));

                        if (migrationInfo.IsAttributed())
                        {
                            VersionLoader.UpdateVersionInfo(migrationInfo.Version, migrationInfo.Description ?? migrationInfo.Migration.GetType().Name);
                        }

                        scope.Complete();
                    }
                    catch
                    {
                        if (useTransaction && scope.IsActive)
                        {
                            scope.Cancel();  // SQLAnywhere needs explicit call to rollback transaction
                        }
                        throw;
                    }

                    _stopWatch.Stop();

                    _announcer.Say(string.Format("{0} migrated", name));
                    _announcer.ElapsedTime(_stopWatch.ElapsedTime());
                }
            }
        }
Esempio n. 7
0
        public void MigrateDown(long targetVersion, bool useAutomaticTransactionManagement)
        {
            var migrationInfos = GetDownMigrationsToApply(targetVersion);

            using (IMigrationScope scope = _migrationScopeHandler.CreateOrWrapMigrationScope(useAutomaticTransactionManagement && TransactionPerSession))
            {
                foreach (var migrationInfo in migrationInfos)
                {
                    ApplyMigrationDown(migrationInfo, useAutomaticTransactionManagement && migrationInfo.TransactionBehavior == TransactionBehavior.Default);
                }

                scope.Complete();
            }

            VersionLoader.LoadVersionInfo();
        }
Esempio n. 8
0
        public void RollbackToVersion(long version, bool useAutomaticTransactionManagement)
        {
            var availableMigrations  = MigrationLoader.LoadMigrations();
            var migrationsToRollback = new List <IMigrationInfo>();

            foreach (long appliedVersion in VersionLoader.VersionInfo.AppliedMigrations())
            {
                IMigrationInfo migrationInfo;
                if (availableMigrations.TryGetValue(appliedVersion, out migrationInfo))
                {
                    migrationsToRollback.Add(migrationInfo);
                }
            }

            using (IMigrationScope scope = _migrationScopeHandler.CreateOrWrapMigrationScope(useAutomaticTransactionManagement && TransactionPerSession))
            {
                try
                {
                    foreach (IMigrationInfo migrationInfo in migrationsToRollback)
                    {
                        if (version >= migrationInfo.Version)
                        {
                            continue;
                        }

                        ApplyMigrationDown(migrationInfo, useAutomaticTransactionManagement && migrationInfo.TransactionBehavior == TransactionBehavior.Default);
                    }

                    scope.Complete();
                }
                catch
                {
                    if (scope.IsActive)
                    {
                        scope.Cancel();  // SQLAnywhere needs explicit call to rollback transaction
                    }
                    throw;
                }
            }

            VersionLoader.LoadVersionInfo();

            if (version == 0 && !VersionLoader.VersionInfo.AppliedMigrations().Any())
            {
                VersionLoader.RemoveVersionTable();
            }
        }
        public void MigrateUp(bool useAutomaticTransactionManagement)
        {
            var migrations = MigrationLoader.LoadMigrations();

            using (IMigrationScope scope = _migrationScopeHandler.CreateOrWrapMigrationScope(useAutomaticTransactionManagement && TransactionPerSession))
            {
                try
                {
                    ApplyMaintenance(MigrationStage.BeforeAll, useAutomaticTransactionManagement);

                    foreach (var pair in migrations)
                    {
                        ApplyMaintenance(MigrationStage.BeforeEach, useAutomaticTransactionManagement);
                        ApplyMigrationUp(pair.Value, useAutomaticTransactionManagement && pair.Value.TransactionBehavior == TransactionBehavior.Default);
                        ApplyMaintenance(MigrationStage.AfterEach, useAutomaticTransactionManagement);
                    }

                    ApplyMaintenance(MigrationStage.BeforeProfiles, useAutomaticTransactionManagement);

                    ApplyProfiles();

                    ApplyMaintenance(MigrationStage.AfterAll, useAutomaticTransactionManagement);

                    scope.Complete();
                }
                catch
                {
                    if (scope.IsActive)
                    {
                        scope.Cancel();  // SQLAnywhere needs explicit call to rollback transaction
                    }
                    throw;
                }
            }

            VersionLoader.LoadVersionInfo();
        }