Search code examples
.netmigratordotnet

MitratorDotNet (Migrator.Net) - Can I use with just bare SQL up/down migration files?


Could I use migrator.net bare migration framework and just have a set of SQL files to do the upgrade/downgrade? i.e. just use the framework to check database version and which scripts to run etc?

thanks


Solution

  • Yes. I have a mixture of sql and code migrations. My migration which uses sql files looks something like:

    using System.Data;
    using Migrator.Framework;
    using System.IO;
    using System.Reflection;
    
    namespace MyDBMigration
    {
        [Migration(2)]
        public class CreateStructures_002 : Migration
        {
    
            public override void Up()
            {
                Assembly asm = Assembly.GetAssembly(typeof(CreateStructures_002));
                Stream s = asm.GetManifestResourceStream("MyDBMigration.SqlScripts.createbaredb.sql");
                StreamReader sr = new StreamReader(s);
                string sql = sr.ReadToEnd();
                Database.ExecuteNonQuery(sql);
            }
    
            public override void Down()
            {
                Assembly asm = Assembly.GetAssembly(typeof(CreateStructures_002));
                Stream s = asm.GetManifestResourceStream("MyDBMigration.SqlScripts.dropbaredb.sql");
                StreamReader sr = new StreamReader(s);
                string sql = sr.ReadToEnd();
                Database.ExecuteNonQuery(sql);
            }
        }
    }
    

    Where I have two files 'createbaredb.sql' and 'dropbaredb.sql' in a directory (SqlScripts) and set as 'Embedded Resource' in the file property pane.