public void Should_be_able_to_roundtrip_model_through_gzip()
        {
            var modelBuilder = new DbModelBuilder();

            modelBuilder.Entity<MigrationsCustomer>();

            var model = modelBuilder.Build(new DbProviderInfo(DbProviders.Sql, "2008"));

            var edmxString = new StringBuilder();
            using (var xmlWriter = XmlWriter.Create(edmxString, new XmlWriterSettings { Indent = true }))
            {
                EdmxWriter.WriteEdmx(model, xmlWriter);
            }

            var modelCompressor = new ModelCompressor();

            var bytes = modelCompressor.Compress(model.ToXDocument());

            Assert.True(bytes.Length > 2000);

            var edmxXDocument = modelCompressor.Decompress(bytes);

            using (var stringWriter = new StringWriter())
            {
                edmxXDocument.Save(stringWriter);

                Assert.Equal(edmxString.ToString(), stringWriter.ToString());
            }
        }
Beispiel #2
0
        public void Should_be_able_to_roundtrip_model_through_gzip()
        {
            var modelBuilder = new DbModelBuilder();

            modelBuilder.Entity <MigrationsCustomer>();

            var model = modelBuilder.Build(new DbProviderInfo(DbProviders.Sql, "2008"));

            var edmxString = new StringBuilder();

            using (var xmlWriter = XmlWriter.Create(
                       edmxString, new XmlWriterSettings
            {
                Indent = true
            }))
            {
                EdmxWriter.WriteEdmx(model, xmlWriter);
            }

            var modelCompressor = new ModelCompressor();

            var bytes = modelCompressor.Compress(model.GetModel());

            Assert.True(bytes.Length > 2000);

            var edmxXDocument = modelCompressor.Decompress(bytes);

            using (var stringWriter = new StringWriter())
            {
                edmxXDocument.Save(stringWriter);

                Assert.Equal(edmxString.ToString(), stringWriter.ToString());
            }
        }
Beispiel #3
0
        internal ScaffoldedMigration Scaffold(string migrationName, string @namespace, bool ignoreChanges)
        {
            string migrationId = null;
            var rescaffolding = false;

            var pendingMigrations = GetPendingMigrations().ToList();

            if (pendingMigrations.Any())
            {
                var lastMigration = pendingMigrations.Last();

                if (!lastMigration.EqualsIgnoreCase(migrationName)
                    && !lastMigration.MigrationName().EqualsIgnoreCase(migrationName))
                {
                    throw Error.MigrationsPendingException(pendingMigrations.Join());
                }

                rescaffolding = true;
                migrationId = lastMigration;
                migrationName = lastMigration.MigrationName();
            }

            XDocument sourceModel = null;
            CheckLegacyCompatibility(() => sourceModel = _currentModel);

            string sourceMigrationId = null, sourceModelVersion = null;

            sourceModel 
                = sourceModel
                    ?? (_historyRepository.GetLastModel(out sourceMigrationId, out sourceModelVersion) 
                        ?? _emptyModel.Value);
            
            var migrationOperations
                = ignoreChanges
                    ? Enumerable.Empty<MigrationOperation>()
                      : _modelDiffer.Diff(
                            sourceModel, 
                            _currentModel, 
                            _modificationCommandTreeGenerator, 
                            SqlGenerator,
                            sourceModelVersion: sourceModelVersion)
                        .ToList();

            if (!rescaffolding)
            {
                migrationName = _migrationAssembly.UniquifyName(migrationName);
                migrationId = MigrationAssembly.CreateMigrationId(migrationName);
            }

            var modelCompressor = new ModelCompressor();

            var scaffoldedMigration
                = _configuration.CodeGenerator.Generate(
                    migrationId,
                    migrationOperations,
                    (sourceModel == _emptyModel.Value)
                    || (sourceModel == _currentModel)
                    || !sourceMigrationId.IsAutomaticMigration()
                        ? null
                        : Convert.ToBase64String(modelCompressor.Compress(sourceModel)),
                    Convert.ToBase64String(modelCompressor.Compress(_currentModel)),
                    @namespace,
                    migrationName);

            scaffoldedMigration.MigrationId = migrationId;
            scaffoldedMigration.Directory = _configuration.MigrationsDirectory;
            scaffoldedMigration.IsRescaffold = rescaffolding;
            scaffoldedMigration.Resources.Add(DefaultSchemaResourceKey, _defaultSchema);

            return scaffoldedMigration;
        }
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;

            if (ReferenceEquals(lastModel, _emptyModel.Value))
            {
                includeSystemOps = true;

                if (!targetModel.HasSystemOperations())
                {
                    // upgrade scenario, inject the history model
                    _historyRepository
                        .AppendHistoryModel(
                            targetModel,
                            new DbProviderInfo(_usersContextInfo.ConnectionProviderName, _providerManifestToken));
                }
            }

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

            migration.Up();

            ExecuteOperations(migrationMetadata.Id, targetModel, migration.Operations.Concat(systemOperations), false);
        }
Beispiel #5
0
        internal ScaffoldedMigration Scaffold(string migrationName, string @namespace, bool ignoreChanges)
        {
            XDocument sourceModel = null;
            CheckLegacyCompatibility(() => sourceModel = _currentModel);

            string sourceMigrationId = null;
            sourceModel = sourceModel ?? (_historyRepository.GetLastModel(out sourceMigrationId) ?? _emptyModel.Value);
            var modelCompressor = new ModelCompressor();

            var migrationOperations
                = ignoreChanges
                      ? Enumerable.Empty<MigrationOperation>()
                      : _modelDiffer.Diff(sourceModel, _currentModel, false)
                            .ToList();

            string migrationId;

            if (migrationName.IsValidMigrationId())
            {
                migrationId = migrationName;
                migrationName = migrationName.MigrationName();
            }
            else
            {
                migrationName = _migrationAssembly.UniquifyName(migrationName);
                migrationId = MigrationAssembly.CreateMigrationId(migrationName);
            }

            var generatedMigration
                = _configuration.CodeGenerator.Generate(
                    migrationId,
                    migrationOperations,
                    (sourceModel == _emptyModel.Value)
                    || (sourceModel == _currentModel)
                    || !sourceMigrationId.IsAutomaticMigration()
                        ? null
                        : Convert.ToBase64String(modelCompressor.Compress(sourceModel)),
                    Convert.ToBase64String(modelCompressor.Compress(_currentModel)),
                    @namespace,
                    migrationName);

            generatedMigration.MigrationId = migrationId;
            generatedMigration.Directory = _configuration.MigrationsDirectory;

            return generatedMigration;
        }
        internal override void ApplyMigration(DbMigration migration, DbMigration lastMigration)
        {
            var migrationMetadata = (IMigrationMetadata)migration;
            var compressor = new ModelCompressor();

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

                if (IsModelOutOfDate(sourceModel, lastMigration))
                {
                    base.AutoMigrate(
                        migrationMetadata.Id + "_" + Strings.AutomaticMigration,
                        GetLastModel(lastMigration, migrationMetadata.Id),
                        sourceModel,
                        downgrading: false);
                }
            }

            migration.Up();

            var targetModel = compressor.Decompress(Convert.FromBase64String(migrationMetadata.Target));

            ExecuteOperations(migrationMetadata.Id, targetModel, migration.Operations.ToList(), false);
        }
        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 #8
0
        private IEnumerable<string> GetHistorySchemas()
        {
            var modelCompressor = new ModelCompressor();

            return
                from migrationId in _migrationAssembly.MigrationIds
                let migrationMetadata = (IMigrationMetadata)_migrationAssembly.GetMigration(migrationId)
                from entitySet in
                    modelCompressor
                    .Decompress(Convert.FromBase64String(migrationMetadata.Target))
                    .Descendants(EdmXNames.Ssdl.EntitySetNames)
                where entitySet.IsSystem()
                select entitySet.SchemaAttribute();
        }