I have finally solved this. Most of my problems were to do with a lack of understanding concerning FluentMigrator. I'll pick out my original questions one-by-one.
What is the right way to import an existing database schema?
I couldn't find a 'right way', but I could find a way that worked for me! I made the following core decisions:
- I scripted off the entire database as a baseline. I included all tables, procs, constraints, views, indexes, etc. I setup my first iteration as that baseline. I chose the CREATE option without DROP. This will be my migration up.
- I ran the same script dump but choose DROP only. This will be my migration down.
The baseline migration just has to use the EmbeddedScript
method to execute the attached script (I organise the scripts into iteration folders as well).
[Tags(Environments.DEV, Environments.TIERS, Environments.CI, Environments.TEST)]
[Migration(201403061552)]
public class Baseline : Migration
{
public override void Up()
{
this.Execute.EmbeddedScript("BaselineUp.sql");
}
public override void Down()
{
this.Execute.EmbeddedScript("BaselineDown.sql");
}
}
Baseline solved...
How to deal with .NetTiers
Ok, this was somewhat of a challenge. I created a specific .NetTiers database which I would use to run the .NetTiers code generation. In FluentMigrator you can 'tag' migrations. I decided to tag based on environments. Hence I have a 'tiers' tag as well as tags for 'dev', 'test', 'uat', 'prod', etc. How these get run will follow later.
When making schema changes I create the migration and use the tag 'tiers' to focus on the .NetTiers schema change. I then run migrate.exe out of Visual Studio external tools using that specific tag as a flag. The app.config database connection that matches my machine name will be the database connection used, so I point it at the tiers database. Now my migrate up has run my .NetTiers source database is ready. I can now run the .NetTiers Codesmith code generation tool to produce the new DLLs.
.NetTiers solved...
What is the right way to deploy migrations to a production environment?
I am using Octopus Deploy and to be perfectly honest, if you are deploying .NET applications, especially to multiple servers, this should be your absolute go-to-tool for doing so!
I won't go into the details of Octopus Deploy, but at a basic level you can hook TeamCity and Octopus deploy together. OD provide two items to get you going.
- A program called Octopack that wraps up your application as a NuGet package.
- A TeamCity plugin that makes TeamCity build the NuGet package and offer it as an artifact exposed on a NuGet feed.
Octopus Deploy then consumes that NuGet feed and can deploy those packages to the endpoint servers. Part of this deployment process is running a PreDeploy and PostDeploy Powershell script. In here is where I am going to run the migrate.exe application with my specific tags.
Deployment solved...