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());
            }
        }
示例#2
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());
                }
            }
        }
示例#3
0
        private string GetMigrationName(IMigrationInfo migration)
        {
            if (migration == null)
            {
                throw new ArgumentNullException("migration");
            }

            return(string.Format("{0}: {1}", migration.Version, migration.Migration.GetType().Name));
        }
        public static bool IsValid(this IMigrationInfo info)
        {
            var type         = info.Type != MigrationType.None;
            var defineType   = Enum.IsDefined(typeof(MigrationType), info.Type);
            var defineOrigin = Enum.IsDefined(typeof(MigrationOrigin), info.Origin);
            var year         = info.Type == MigrationType.ChartOfAccount ? info.Year == 0 : info.Year != 0;
            var vatNumber    = !String.IsNullOrEmpty(info.VatNumber?.Trim());
            var version      = info.Version == "2.0";

            return(type & defineType & defineOrigin & year & vatNumber & version);
        }
        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());
                }
            }
        }
示例#6
0
        public static IEnumerable <string> GetSqlExpressions(this IMigrationInfo migrationInfo, IMigrationContext migrationContext, IMigrationGenerator migrationGenerator)
        {
            List <string> sqlExpressions = new List <string>
            {
                "-- UP"
            };

            migrationInfo.Migration.GetUpExpressions(migrationContext);
            sqlExpressions.AddRange(migrationContext.Expressions.Select(exp => exp.GetSqlEquivalent(migrationGenerator)));
            migrationContext.Expressions.Clear();
            sqlExpressions.Add("-- DOWN");
            migrationInfo.Migration.GetDownExpressions(migrationContext);
            sqlExpressions.AddRange(migrationContext.Expressions.Select(exp => exp.GetSqlEquivalent(migrationGenerator)));
            migrationContext.Expressions.Clear();

            return(sqlExpressions);
        }
示例#7
0
        private void ApplyMigrationUp(IMigrationInfo migrationInfo, bool useTransaction)
        {
            if (!_alreadyOutputPreviewOnlyModeWarning && Processor.Options.PreviewOnly)
            {
                _announcer.Heading("PREVIEW-ONLY MODE");
                _alreadyOutputPreviewOnlyModeWarning = true;
            }

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

                try
                {
                    _stopWatch.Start();

                    if (useTransaction)
                    {
                        Processor.BeginTransaction();
                    }

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

                    if (useTransaction)
                    {
                        Processor.CommitTransaction();
                    }

                    _stopWatch.Stop();

                    _announcer.Say(string.Format("{0} migrated", name));
                    _announcer.ElapsedTime(_stopWatch.ElapsedTime());
                }
                catch (Exception)
                {
                    if (useTransaction)
                    {
                        Processor.RollbackTransaction();
                    }
                    throw;
                }
            }
        }
        public SortedList <long, IMigrationInfo> LoadMigrations()
        {
            var migrationInfos = new SortedList <long, IMigrationInfo>();

            var migrationTypes = FindMigrationTypes();

            foreach (var migrationType in migrationTypes)
            {
                IMigrationInfo migrationInfo = Conventions.GetMigrationInfo(migrationType);
                if (migrationInfos.ContainsKey(migrationInfo.Version))
                {
                    throw new DuplicateMigrationException(String.Format("Duplicate migration version {0}.",
                                                                        migrationInfo.Version));
                }
                migrationInfos.Add(migrationInfo.Version, migrationInfo);
            }

            return(migrationInfos);
        }
示例#9
0
    public SortedList <long, IMigrationInfo> LoadMigrations()
    {
        var sortedList = new SortedList <long, IMigrationInfo>();
        IEnumerable <IMigration> migrations = this.FindMigrations();

        if (migrations == null)
        {
            return(sortedList);
        }
        foreach (IMigration migration in migrations)
        {
            IMigrationInfo migrationInfo = this.Conventions.GetMigrationInfo(migration);
            if (sortedList.ContainsKey(migrationInfo.Version))
            {
                throw new DuplicateMigrationException(string.Format("Duplicate migration version {0}.", migrationInfo.Version));
            }
            sortedList.Add(migrationInfo.Version, migrationInfo);
        }
        return(sortedList);
    }
示例#10
0
        private void ApplyMigrationDown(IMigrationInfo migrationInfo, bool useTransaction)
        {
            var name = GetMigrationName(migrationInfo);

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

            try
            {
                _stopWatch.Start();

                if (useTransaction)
                {
                    Processor.BeginTransaction();
                }

                ExecuteMigration(migrationInfo.Migration, (m, c) => m.GetDownExpressions(c));
                VersionLoader.DeleteVersion(migrationInfo.Version);

                if (useTransaction)
                {
                    Processor.CommitTransaction();
                }

                _stopWatch.Stop();

                _announcer.Say(string.Format("{0} reverted", name));
                _announcer.ElapsedTime(_stopWatch.ElapsedTime());
            }
            catch (Exception)
            {
                if (useTransaction)
                {
                    Processor.RollbackTransaction();
                }
                throw;
            }
        }
 /// <summary>
 /// Returns <c>true</c> when the migration behind the <paramref name="migrationInfo"/> has a migration attribute
 /// </summary>
 /// <param name="migrationInfo">The migration information to test</param>
 /// <returns><c>true</c> when the migration behind the <paramref name="migrationInfo"/> has a migration attribute</returns>
 public static bool IsAttributed(this IMigrationInfo migrationInfo)
 {
     return(!(migrationInfo is NonAttributedMigrationToMigrationInfoAdapter));
 }
示例#12
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))
                {
                    ExecuteMigration(migrationInfo.Migration, (m, c) => m.GetUpExpressions(c));

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

                    scope.Complete();

                    _stopWatch.Stop();

                    _announcer.Say(string.Format("{0} migrated", name));
                    _announcer.ElapsedTime(_stopWatch.ElapsedTime());
                }
            }
        }
示例#13
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());
            }
        }
示例#14
0
        private static void WriteSqlFile(IMigrationContext migrationContext, IMigrationGenerator migrationGenerator, string sqlScriptsDirectory, IMigrationInfo migration)
        {
            IEnumerable <string> sqlExpressions = migration.GetSqlExpressions(migrationContext, migrationGenerator);
            string fileName = $"{migration.GetName().Replace(": ", "_")}.sql";
            string path     = Path.Combine(sqlScriptsDirectory, fileName);

            File.WriteAllLines(path, sqlExpressions);
        }