Esempio n. 1
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);
        }
Esempio n. 2
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;

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

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

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

            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);
        }