Esempio n. 1
0
        private string Apply(IMigration migration)
        {
            var report = migration.Apply(store);

            using (var session = store.OpenSession())
            {
                session.Load <Migrations>(Migrations.DocumentId).Add(migration.MigrationId, report);
                session.SaveChanges();
            }
            return(report);
        }
Esempio n. 2
0
        public SyntaxTree PerformMigration(IMigration migration, string sourceCode)
        {
            var compilation = CSharpCompilation.Create("Code")
                              .AddReferences(MetadataReference.CreateFromFile(typeof(object).Assembly.Location));

            var tree = CSharpSyntaxTree.ParseText(sourceCode);

            tree = tree.WithRootAndOptions(new AnnotationVisitor().Visit(tree.GetRoot()), tree.Options);

            compilation = compilation.AddSyntaxTrees(tree);

            var context = new MigrationContext();

            context.Populate(compilation, tree);

            return(migration.Apply(tree, context));
        }
Esempio n. 3
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;
            }
        }
Esempio n. 4
0
        protected override TResult Run(Execution <TInput, TResult> context)
        {
            TResult result = default(TResult);
            var     input  = context.Context;

            try
            {
                if (migration.ShouldApply(input))
                {
                    result = migration.Apply(input);
                }
            }
            catch (Exception ex)
            {
                logger.ErrorException(ex, () => "Error while applying migration");
            }

            return(result);
        }
Esempio n. 5
0
        protected override Task <TResult> RunAsync(Execution <TInput, TResult> execution)
        {
            TResult result = default(TResult);
            var     input  = execution.Context;

            try
            {
                if (migration.ShouldApply(input))
                {
                    result = migration.Apply(input);
                }
            }
            catch (Exception ex)
            {
                logger.ErrorException(ex, () => "Error while applying migration");
            }

            return(Task.FromResult(result));
        }
Esempio n. 6
0
        protected override IEnumerable <AggregateCommit> Run(Execution <AggregateCommit, IEnumerable <AggregateCommit> > context)
        {
            var commit     = context.Context;
            var newCommits = new List <AggregateCommit> {
                commit
            };

            try
            {
                if (migration.ShouldApply(commit))
                {
                    newCommits = migration.Apply(commit).ToList();
                }
            }
            catch (Exception ex)
            {
                log.ErrorException($"Error while applying migration", ex);
            }

            foreach (var newCommit in newCommits)
            {
                yield return(newCommit);
            }
        }