コード例 #1
0
        public void ForNullProviderShouldThrowException()
        {
            Assembly assembly = Assembly.Load("ECM7.Migrator.TestAssembly");

            var loader = new MigrationAssembly(assembly);

            var mi = loader.GetMigrationInfo(1);

            Assert.Throws <Exception>(() => loader.InstantiateMigration(mi, null));
        }
コード例 #2
0
        public void ForNullProviderShouldThrowException()
        {
            Assembly assembly = GetType().GetTypeInfo().Assembly;

            var loader = new MigrationAssembly(assembly);

            var mi = loader.GetMigrationInfo(1);

            Assert.Throws <ArgumentNullException>(() => loader.InstantiateMigration(mi, null));
        }
コード例 #3
0
        public void CanCreateMigrationObject()
        {
            Assembly          assembly          = Assembly.Load("ECM7.Migrator.TestAssembly");
            MigrationAssembly migrationAssembly = new MigrationAssembly(assembly);

            Mock <ITransformationProvider> provider = new Mock <ITransformationProvider>();

            var        mi        = migrationAssembly.GetMigrationInfo(2);
            IMigration migration = migrationAssembly.InstantiateMigration(mi, provider.Object);

            Assert.IsNotNull(migration);
            Assert.That(migration is SecondTestMigration);
            Assert.AreSame(provider.Object, migration.Database);
        }
コード例 #4
0
        public void CanCreateMigrationObject()
        {
            Assembly          assembly          = GetType().GetTypeInfo().Assembly;
            MigrationAssembly migrationAssembly = new MigrationAssembly(assembly);

            Mock <ITransformationProvider> provider = new Mock <ITransformationProvider>();

            var       mi        = migrationAssembly.GetMigrationInfo(2);
            Migration migration = migrationAssembly.InstantiateMigration(mi, provider.Object);

            Assert.NotNull(migration);
            Assert.True(migration is TestMigration02);
            Assert.Same(provider.Object, migration.Database);
        }
コード例 #5
0
        /// <summary>
        /// ���������� ��������
        /// </summary>
        /// <param name="targetVersion">������ ����������� ��������</param>
        /// <param name="currentDatabaseVersion">������� ������ ��</param>
        public void ExecuteMigration(long targetVersion, long currentDatabaseVersion)
        {
            var migrationInfo = migrationAssembly.GetMigrationInfo(targetVersion);

            Migration migration = migrationAssembly.InstantiateMigration(migrationInfo, Provider);

            try
            {
                if (!migrationInfo.WithoutTransaction)
                {
                    Provider.BeginTransaction();
                }

                if (targetVersion <= currentDatabaseVersion)
                {
                    Logger.MigrateDown(targetVersion, migration.Name);
                    migration.Revert();
                    Provider.MigrationUnApplied(targetVersion, Key);
                }
                else
                {
                    Logger.MigrateUp(targetVersion, migration.Name);
                    migration.Apply();
                    Provider.MigrationApplied(targetVersion, Key);
                }

                if (!migrationInfo.WithoutTransaction)
                {
                    Provider.Commit();
                }
            }
            catch (Exception ex)
            {
                Logger.Exception(targetVersion, migration.Name, ex);

                if (!migrationInfo.WithoutTransaction)
                {
                    // если изменения выполняются в транзакции, откатываем
                    // изменения текущей миграции, которые успели выполниться
                    Provider.Rollback();
                    Logger.RollingBack(currentDatabaseVersion);
                }

                throw;
            }
        }
コード例 #6
0
        /// <summary>
        /// Выполнение миграции
        /// </summary>
        /// <param name="targetVersion">Версия выполняемой миграции</param>
        /// <param name="currentDatabaseVersion">Текущая версия БД</param>
        public void ExecuteMigration(long targetVersion, long currentDatabaseVersion)
        {
            var migrationInfo = migrationAssembly.GetMigrationInfo(targetVersion);

            IMigration migration = migrationAssembly.InstantiateMigration(migrationInfo, provider);

            try
            {
                if (!migrationInfo.WithoutTransaction)
                {
                    provider.BeginTransaction();
                }

                if (targetVersion <= currentDatabaseVersion)
                {
                    MigratorLogManager.Log.MigrateDown(targetVersion, migration.Name);
                    migration.Revert();
                    provider.MigrationUnApplied(targetVersion, Key);
                }
                else
                {
                    MigratorLogManager.Log.MigrateUp(targetVersion, migration.Name);
                    migration.Apply();
                    provider.MigrationApplied(targetVersion, Key);
                }

                if (!migrationInfo.WithoutTransaction)
                {
                    provider.Commit();
                }
            }
            catch (Exception ex)
            {
                MigratorLogManager.Log.Exception(targetVersion, migration.Name, ex);

                if (!migrationInfo.WithoutTransaction)
                {
                    // при ошибке откатываем изменения
                    provider.Rollback();
                    MigratorLogManager.Log.RollingBack(currentDatabaseVersion);
                }

                throw;
            }
        }