Skip to content

Enforce migration version numbering rules

ses4j edited this page Aug 8, 2012 · 4 revisions

Frequently, it is useful for a project to employ a specific system for migration version numbering. A useful technique for simplying and encoding this system is with a custom attribute that inherits from MigrationAttribute.

An example is below. This uses a system where the migration version is of the format BBYYYYMMDDHHMM, where BB is a branch, YYYY is year, MM is date, and so on.


    /// 
    /// Mark all migrations with this INSTEAD of [Migration].
    /// 
    public class MyCustomMigrationAttribute : FluentMigrator.MigrationAttribute
    {
        public LcmpMigrationAttribute(int branchnumber, int year, int month, int day, int hour, int minute, string author)
            : base(CalculateValue(branchnumber, year, month, day, hour, minute))
        {
            this.Author = author;
        }

        public string Author { get; private set; }

        // Format: BBYYYYMMDDHHMM
        private static long CalculateValue(int branchnumber, int year, int month, int day, int hour, int minute)
        {
            return branchnumber* 1000000000000L + year * 100000000L + month * 100000L + day * 10000L + hour * 100L + minute;
        }
    }

A migration class that uses this attribute might look like this:


    [MyCustomMigration(author: "Scott Stafford", changeRequest: 12, year: 2012, month: 8, day: 7, hour: 14, minute: 01)]
    public class TestLcmpMigration : Migration
    {
        public override void Down() { /* ... */ }
        public override void Up() { /* ... */ }
    }
Clone this wiki locally