Search code examples
database-migrationfluent-migrator

New migration without erasing old data


We're using FluentMigrator in one project. Let's say I got code like this one below.

So every time when we run new migration all the previous data deleting. Is there are way to avoid it and keep safe the data in places which are not changing?

  public class Migration1 : Migration
    {
        public override void Up() {
            Create.Table("Project")
                .WithColumn("id").AsInt64().PrimaryKey().Identity()
                .WithColumn("name").AsString(30).Nullable()
                .WithColumn("author").AsString(30).Nullable()
                .WithColumn("date").AsDate().Nullable()
                .WithColumn("description").AsString(1000).Nullable();

            Create.Table("Data")
                .WithColumn("id").AsInt64().PrimaryKey().Identity()
                .WithColumn("project_id").AsInt64().ForeignKey("Project", "id")
                .WithColumn("a").AsInt32().Nullable()
                .WithColumn("b").AsInt32().Nullable()
                .WithColumn("c").AsInt32().Nullable()
                .WithColumn("d").AsInt32().Nullable();
        }

        public override void Down() {
            Delete.Table("data");
            Delete.Table("project");
        }
    }

Solution

  • As part of you Down method you could create some backup tables which are identical to the table you are deleting but are post fixed with a timestamp. Eg:

    Project_201407091059
    Data_201407091059
    

    You could then copy all the data from the tables being deleted to these tables.