My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
MSBuildTarget  
Updated Feb 4, 2010 by geoffl...@gmail.com

A Simple Example

This example just runs the migrations from the current DB version to the highest migration.

<PropertyGroup>
    <MigratorTasksPath>$(MSBuildProjectDirectory)\migrator</MigratorTasksPath>
</PropertyGroup>
    
<Import Project="$(MigratorTasksPath)\Migrator.Targets" />

<Target name="Migrate" DependsOnTargets="Build">
    <Migrate Provider="SqlServer"
        Connectionstring="Database=MyDB;Data Source=localhost;User Id=;Password=;"
        Migrations="bin/MyProject.dll"/>
</Target>

Run the migration with MSBuild to update the database schema to the latest version

e.g. MSBuild build.proj /t:Migrate

More Complex Example

If you need a bit more flexibility you can also setup your task to allow you to optionally pass a version number to it. If the version number is lower than the current DB version then the Down() targets will be run to undo database changes. If the version number is higher than the current DB version then the Up() targets will be run to migrate the database to the version specified.

In this case we've called the property SchemaVersion.

<Target name="Migrate" DependsOnTargets="Build">
    <CreateProperty Value="-1"  Condition="'$(SchemaVersion)'==''">
        <Output TaskParameter="Value" PropertyName="SchemaVersion"/>
    </CreateProperty>
    <Migrate Provider="SqlServer"
            Connectionstring="Database=MyDB;Data Source=localhost;User Id=;Password=;"
            Migrations="bin/MyProject.dll"
            To="$(SchemaVersion)"/>
</Target>

Run the migration with MSBuild to a specific version (either up or down)

MSBuild build.proj /t:Migrate /p:SchemaVersion=5

Run the migrations to bring the database to the latest version just don't pass a SchemaVersion property

MSBuild build.proj /t:Migrate

Compiling Migrations on the Fly

Rather than specifying a pre-compiled DLL with your migrations, you can just specify a directory that contains all the code. The migrations will then be compiled "on the fly" and executed.

The default language to use is CSharp. If you want to use a different language pass a Language parameter as well.

<Target name="Migrate" DependsOnTargets="Build">
    <CreateProperty Value="-1"  Condition="'$(SchemaVersion)'==''">
        <Output TaskParameter="Value" PropertyName="SchemaVersion"/>
    </CreateProperty>
    <Migrate Provider="SqlServer"
            Connectionstring="Database=MyDB;Data Source=localhost;User Id=;Password=;"
            Directory="migrations"
            To="$(SchemaVersion)"/>
</Target>

Outputing Generated SQL Statements

To save all of the generated SQL Statements that Migrator.Net runs against your database add a ScriptFile attribute.

<Target name="Migrate" DependsOnTargets="Build">
    <CreateProperty Value="-1"  Condition="'$(SchemaVersion)'==''">
        <Output TaskParameter="Value" PropertyName="SchemaVersion"/>
    </CreateProperty>
    <Migrate Provider="SqlServer"
            Connectionstring="Database=MyDB;Data Source=localhost;User Id=;Password=;"
            Directory="migrations"
            To="$(SchemaVersion)"
            Scriptfile="migrations.sql"/>
</Target>

See the example/example-msbuild.proj for a working example.

Full Example

Here's a full MSBuild example that compiles and runs the migrations.

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
        <ClassLibraryOutputDirectory>bin\$(Configuration)</ClassLibraryOutputDirectory>
        <MigratorTasksPath>$(MSBuildProjectDirectory)\migrator</MigratorTasksPath>
        <MigrationsProject>ProjectMigrations\ProjectMigrations.csproj</MigrationsProject>
    </PropertyGroup>
    
    <Import Project="$(MigratorTasksPath)\Migrator.Targets" />
    
    <Target Name="Build-Migrations">
        <MSBuild Projects="$(MigrationsProject)" Targets="Build">
            <Output TaskParameter="TargetOutputs" ItemName="MigrationAssemblies" />
        </MSBuild>
        
        <Message Text="Built: @(MigrationAssemblies)"/>
    </Target>
    
    <Target Name="Migrate" DependsOnTargets="Build-Migrations">
        <Message Text="Migrating: @(MigrationAssemblies)"/>
        
        <CreateProperty Value="-1"  Condition="'$(SchemaVersion)'==''">
            <Output TaskParameter="Value" PropertyName="SchemaVersion"/>
        </CreateProperty>
        <Migrate Provider="SqlServer"
            Connectionstring="Database=test2;Data Source=localhost;User Id=sa;Password=sql;"
            Migrations="@(MigrationAssemblies)"
            To="$(SchemaVersion)"/>
    </Target>
</Project>
Comment by a.j.da...@gmail.com, May 28, 2009

The Target "name" attribute needs to be "Name".

Comment by m...@longtailvideo.com, Jul 28, 2009

Also, "Scriptfile" needs to be "scriptFile".

Comment by dan...@gmail.com, Oct 10, 2009

Something that is not mentioned here that was not obvious to me as someone who is not very familiar with MSBuild is that you have to copy some files to %ProgramFiles?%\MSBuild\MigratorTasks?. See this article for the details.

Comment by dan...@gmail.com, Oct 10, 2009

Or, to follow on from my previous comment, you can as that article points out, use <UsingTask> instead.

Comment by stephenp...@gmail.com, Nov 18, 2010

I'm having issues when using the following:

<UsingTask?
AssemblyFile?="$(MSBuildProjectDirectory)\..\Library\migratordotnet\Migrator.MSBuild.dll" TaskName?="Migrator.MSBuild.Migrate"/>
<Target Name="Migrate" DependsOnTargets="Build">
<Migrate
Provider="MySql?" Connectionstring="Database=migrations;Data Source=localhost;User Id=root;Password=admin;Port=3306" Migrations="$(OutputPath?)$(AssemblyName?).dll" To="$(To)" />
</Target>

I have that in my project's file.

Getting this error: Microsoft (R) Build Engine Version 3.5.30729.4926 .NET Framework, Version 2.0.50727.4952? Copyright (C) Microsoft Corporation 2007. All rights reserved.

Build started 11/18/2010 3:38:17 PM. Project "C:\dev\MigratorDotNet?\MigratorDotNet?\MigratorDotNet?.csproj" on node 0 (Migrate target(s)). C:\Windows\Microsoft.NET\Framework\v3.5\Microsoft.Common.targets(539,9): error : The OutputPath? property is not set for this project. Please check to make sure that you have specified a valid Configuration/Platform combination. Configuration='Debug' Platform='HPD' Done Building Project "C:\dev\MigratorDotNet?\MigratorDotNet?\MigratorDotNet?.csproj" (Migrate target(s)) -- FAILED.

Build FAILED.

"C:\dev\MigratorDotNet?\MigratorDotNet?\MigratorDotNet?.csproj" (Migrate target) (1) -> (CheckForInvalidConfigurationAndPlatform? target) ->

C:\Windows\Microsoft.NET\Framework\v3.5\Microsoft.Common.targets(539,9): error : The OutputPath? property is not set for this project. Please check to make sure that you have specified a valid Configuration/Platform combination. Configuration='Debug' Platform='HPD'

0 Warning(s)
  1. Error(s)

Time Elapsed 00:00:00.00

I can't figure this out. Any help?


Sign in to add a comment
Powered by Google Project Hosting