Beispiel #1
0
        internal override void ApplyMigration(DbMigration migration, DbMigration lastMigration)
        {
            IMigrationMetadata migrationMetadata = (IMigrationMetadata)migration;
            VersionedModel     sourceModel1      = this.GetLastModel(lastMigration, migrationMetadata.Id);
            VersionedModel     sourceModel2      = migration.GetSourceModel();
            VersionedModel     targetModel       = migration.GetTargetModel();

            if (sourceModel2 != null && this.IsModelOutOfDate(sourceModel2.Model, lastMigration))
            {
                base.AutoMigrate(migrationMetadata.Id.ToAutomaticMigrationId(), sourceModel1, sourceModel2, false);
                sourceModel1 = sourceModel2;
            }
            string    defaultSchema = DbMigrator.GetDefaultSchema(migration);
            XDocument historyModel  = this.GetHistoryModel(defaultSchema);
            IEnumerable <MigrationOperation> systemOperations = Enumerable.Empty <MigrationOperation>();

            if (object.ReferenceEquals((object)sourceModel1.Model, (object)this._emptyModel.Value) && !base.HistoryExists())
            {
                systemOperations = (IEnumerable <MigrationOperation>) this._modelDiffer.Diff(this._emptyModel.Value, historyModel, (Lazy <ModificationCommandTreeGenerator>)null, (MigrationSqlGenerator)null, (string)null, (string)null);
            }
            else
            {
                string lastDefaultSchema = this.GetLastDefaultSchema(migrationMetadata.Id);
                if (!string.Equals(lastDefaultSchema, defaultSchema, StringComparison.Ordinal))
                {
                    systemOperations = (IEnumerable <MigrationOperation>) this._modelDiffer.Diff(this.GetHistoryModel(lastDefaultSchema), historyModel, (Lazy <ModificationCommandTreeGenerator>)null, (MigrationSqlGenerator)null, (string)null, (string)null);
                }
            }
            migration.Up();
            this.ExecuteOperations(migrationMetadata.Id, targetModel, migration.Operations, systemOperations, false, false);
        }
Beispiel #2
0
        public static IList <MigrationOperation> GetOperations(this DbMigration migration)
        {
            var migrationOperations = (IList <MigrationOperation>)_operationsProperty.GetValue(migration, null);

            if (!migrationOperations.Any())
            {
                migration.Up();
            }

            return(migrationOperations);
        }
        internal override void ApplyMigration(DbMigration migration, DbMigration lastMigration)
        {
            DebugCheck.NotNull(migration);

            var migrationMetadata = (IMigrationMetadata)migration;
            var compressor        = new ModelCompressor();

            var lastModel   = GetLastModel(lastMigration, migrationMetadata.Id);
            var targetModel = compressor.Decompress(Convert.FromBase64String(migrationMetadata.Target));

            if (migrationMetadata.Source != null)
            {
                var sourceModel
                    = compressor.Decompress(Convert.FromBase64String(migrationMetadata.Source));

                if (IsModelOutOfDate(sourceModel, lastMigration))
                {
                    base.AutoMigrate(
                        migrationMetadata.Id.ToAutomaticMigrationId(),
                        lastModel,
                        sourceModel,
                        downgrading: false);

                    lastModel = sourceModel;
                }
            }

            var migrationSchema = GetDefaultSchema(migration);
            var historyModel    = GetHistoryModel(migrationSchema);

            var systemOperations = Enumerable.Empty <MigrationOperation>();

            if (ReferenceEquals(lastModel, _emptyModel.Value) &&
                !base.HistoryExists())
            {
                systemOperations = _modelDiffer.Diff(lastModel, historyModel);
            }
            else
            {
                var lastMigrationSchema = GetLastDefaultSchema(migrationMetadata.Id);

                if (!string.Equals(lastMigrationSchema, migrationSchema, StringComparison.Ordinal))
                {
                    var lastHistoryModel = GetHistoryModel(lastMigrationSchema);

                    systemOperations = _modelDiffer.Diff(lastHistoryModel, historyModel);
                }
            }

            migration.Up();

            ExecuteOperations(migrationMetadata.Id, targetModel, migration.Operations, systemOperations, false);
        }
Beispiel #4
0
        internal override void ApplyMigration(DbMigration migration, DbMigration lastMigration)
        {
            var migrationMetadata = (IMigrationMetadata)migration;
            var compressor        = new ModelCompressor();

            var lastModel   = GetLastModel(lastMigration, migrationMetadata.Id);
            var targetModel = compressor.Decompress(Convert.FromBase64String(migrationMetadata.Target));

            if (migrationMetadata.Source != null)
            {
                var sourceModel
                    = compressor.Decompress(Convert.FromBase64String(migrationMetadata.Source));

                if (IsModelOutOfDate(sourceModel, lastMigration))
                {
                    base.AutoMigrate(
                        migrationMetadata.Id.ToAutomaticMigrationId(),
                        lastModel,
                        sourceModel,
                        downgrading: false);

                    lastModel = sourceModel;
                }
            }

            bool?includeSystemOps = null;
            var  isFirstMigration = ReferenceEquals(lastModel, _emptyModel.Value);

            if (isFirstMigration && !_historyRepository.IsShared())
            {
                includeSystemOps = true;

                if (!targetModel.HasSystemOperations())
                {
                    // upgrade scenario, inject the history model
                    AttachHistoryModel(targetModel);
                }
            }

            var systemOperations
                = _modelDiffer.Diff(lastModel, targetModel, includeSystemOps)
                  .Where(o => o.IsSystem);

            migration.Up();

            ExecuteOperations(migrationMetadata.Id, targetModel, migration.Operations.Concat(systemOperations), false);
        }