public MigrationSpecification LoadMigrationIfPresent(int version)
 {
     if (!MigrationFiles.ContainsKey(version))
     {
         return(null);
     }
     return(FromFileContents(MigrationFiles[version]));
 }
Beispiel #2
0
        private static void ReportJson(MigrationFiles files)
        {
            var sb = new StringBuilder();

            sb.AppendLine("//BEGIN");
            sb.AppendLine("{");
            sb.AppendLine("  \"MigrationFile\": \"" + SerializePath(files.MigrationFile) + "\",");
            sb.AppendLine("  \"MetadataFile\": \"" + SerializePath(files.MetadataFile) + "\",");
            sb.AppendLine("  \"SnapshotFile\": \"" + SerializePath(files.SnapshotFile) + "\"");
            sb.AppendLine("}");
            sb.AppendLine("//END");
            ConsoleCommandLogger.Output(sb.ToString());
        }
 private static void ReportJson(MigrationFiles files)
 {
     Reporter.Output.WriteLine(JsonConvert.SerializeObject(files, Formatting.Indented));
 }
 private static void ReportJson(MigrationFiles files)
 => ConsoleCommandLogger.Json(files);
    /// <summary>
    ///     Removes the previous migration.
    /// </summary>
    /// <param name="projectDir">The project's root directory.</param>
    /// <param name="rootNamespace">The project's root namespace.</param>
    /// <param name="force">Don't check to see if the migration has been applied to the database.</param>
    /// <param name="language">The project's language.</param>
    /// <returns>The removed migration files.</returns>
    // TODO: DRY (file names)
    public virtual MigrationFiles RemoveMigration(
        string projectDir,
        string?rootNamespace,
        bool force,
        string?language)
    {
        var files = new MigrationFiles();

        var modelSnapshot = Dependencies.MigrationsAssembly.ModelSnapshot;

        if (modelSnapshot == null)
        {
            throw new OperationException(DesignStrings.NoSnapshot);
        }

        var codeGenerator = Dependencies.MigrationsCodeGeneratorSelector.Select(language);

        IModel?model      = null;
        var    migrations = Dependencies.MigrationsAssembly.Migrations
                            .Select(m => Dependencies.MigrationsAssembly.CreateMigration(m.Value, _activeProvider))
                            .ToList();

        if (migrations.Count != 0)
        {
            var migration = migrations[migrations.Count - 1];
            model = Dependencies.SnapshotModelProcessor.Process(migration.TargetModel);

            if (!Dependencies.MigrationsModelDiffer.HasDifferences(
                    model.GetRelationalModel(), Dependencies.SnapshotModelProcessor.Process(modelSnapshot.Model).GetRelationalModel()))
            {
                var applied = false;
                try
                {
                    applied = Dependencies.HistoryRepository.GetAppliedMigrations().Any(
                        e => e.MigrationId.Equals(migration.GetId(), StringComparison.OrdinalIgnoreCase));
                }
                catch (Exception ex) when(force)
                {
                    Dependencies.OperationReporter.WriteVerbose(ex.ToString());
                    Dependencies.OperationReporter.WriteWarning(
                        DesignStrings.ForceRemoveMigration(migration.GetId(), ex.Message));
                }

                if (applied)
                {
                    if (force)
                    {
                        Dependencies.Migrator.Migrate(
                            migrations.Count > 1
                                ? migrations[migrations.Count - 2].GetId()
                                : Migration.InitialDatabase);
                    }
                    else
                    {
                        throw new OperationException(DesignStrings.RevertMigration(migration.GetId()));
                    }
                }

                var migrationFileName = migration.GetId() + codeGenerator.FileExtension;
                var migrationFile     = TryGetProjectFile(projectDir, migrationFileName);
                if (migrationFile != null)
                {
                    Dependencies.OperationReporter.WriteInformation(DesignStrings.RemovingMigration(migration.GetId()));
                    File.Delete(migrationFile);
                    files.MigrationFile = migrationFile;
                }
                else
                {
                    Dependencies.OperationReporter.WriteWarning(
                        DesignStrings.NoMigrationFile(migrationFileName, migration.GetType().ShortDisplayName()));
                }

                var migrationMetadataFileName = migration.GetId() + ".Designer" + codeGenerator.FileExtension;
                var migrationMetadataFile     = TryGetProjectFile(projectDir, migrationMetadataFileName);
                if (migrationMetadataFile != null)
                {
                    File.Delete(migrationMetadataFile);
                    files.MetadataFile = migrationMetadataFile;
                }
                else
                {
                    Dependencies.OperationReporter.WriteVerbose(
                        DesignStrings.NoMigrationMetadataFile(migrationMetadataFile));
                }

                model = migrations.Count > 1
                    ? Dependencies.SnapshotModelProcessor.Process(migrations[migrations.Count - 2].TargetModel)
                    : null;
            }
            else
            {
                Dependencies.OperationReporter.WriteVerbose(DesignStrings.ManuallyDeleted);
            }
        }

        var modelSnapshotName     = modelSnapshot.GetType().Name;
        var modelSnapshotFileName = modelSnapshotName + codeGenerator.FileExtension;
        var modelSnapshotFile     = TryGetProjectFile(projectDir, modelSnapshotFileName);

        if (model == null)
        {
            if (modelSnapshotFile != null)
            {
                Dependencies.OperationReporter.WriteInformation(DesignStrings.RemovingSnapshot);
                File.Delete(modelSnapshotFile);
                files.SnapshotFile = modelSnapshotFile;
            }
            else
            {
                Dependencies.OperationReporter.WriteWarning(
                    DesignStrings.NoSnapshotFile(
                        modelSnapshotFileName,
                        modelSnapshot.GetType().ShortDisplayName()));
            }
        }
        else
        {
            var modelSnapshotNamespace = modelSnapshot.GetType().Namespace;
            Check.DebugAssert(!string.IsNullOrEmpty(modelSnapshotNamespace), "modelSnapshotNamespace is null or empty");
            var modelSnapshotCode = codeGenerator.GenerateSnapshot(
                modelSnapshotNamespace,
                _contextType,
                modelSnapshotName,
                model);

            if (modelSnapshotFile == null)
            {
                modelSnapshotFile = Path.Combine(
                    GetDirectory(projectDir, null, GetSubNamespace(rootNamespace, modelSnapshotNamespace)),
                    modelSnapshotFileName);
            }

            Dependencies.OperationReporter.WriteInformation(DesignStrings.RevertingSnapshot);
            File.WriteAllText(modelSnapshotFile, modelSnapshotCode, Encoding.UTF8);
        }

        return(files);
    }