views:

101

answers:

1

Hi,

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

+1  A: 

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.

Greg
And what do you do on those scripts? Could you provide a little example please? Thanks
emzero
The scripts contain sql statements which can do whatever you like. The Down script should undo whatever the up script does. I created mine using SqlPubWiz and a preexisting database, as in my answer here: http://stackoverflow.com/questions/2321052/get-script-of-sql-server-data/2501702#2501702 and then separated the resulting script into things-which-create-things and things-which-destroy-things, and used them as up and down scripts respectively.
Greg