Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

migratordotnet - Run migrations from within application (w/o nant or build)

is there a way to run migrations from within the application itself?

Thanks!

like image 380
BvuRVKyUVlViVIc7 Avatar asked May 15 '09 11:05

BvuRVKyUVlViVIc7


2 Answers

I instantiate an instance of the Migrator class, and then you can call member methods like MigrateToLastVersion() or MigrateTo(long versionnr)

Migrator.Migrator m = new Migrator.Migrator ("SqlServer", connectionString, migrationsAssembly)

m.MigrateToLastVersion();
like image 94
Frederik Gheysels Avatar answered Nov 18 '22 08:11

Frederik Gheysels


I don't see why not.

Have a look at the nant task http://code.google.com/p/migratordotnet/source/browse/trunk/src/Migrator.NAnt/MigrateTask.cs

Relevant bits are here:

    private void Execute(Assembly asm)
    {
        Migrator mig = new Migrator(Provider, ConnectionString, asm, Trace, new TaskLogger(this));
        mig.DryRun = DryRun;
        if (ScriptChanges)
        {
            using (StreamWriter writer = new StreamWriter(ScriptFile))
            {
                mig.Logger = new SqlScriptFileLogger(mig.Logger, writer);
                RunMigration(mig);
            }
        }
        else
        {
            RunMigration(mig);
        }
    }

    private void RunMigration(Migrator mig)
    {
        if (mig.DryRun)
            mig.Logger.Log("********** Dry run! Not actually applying changes. **********");

        if (_to == -1)
            mig.MigrateToLastVersion();
        else
            mig.MigrateTo(_to);
    }
like image 35
Derek Ekins Avatar answered Nov 18 '22 06:11

Derek Ekins