I read the same article by Troy Hunt 2 years ago. It was a revelation, and I quickly got us and worked with SQL Source Control + SQL Compare + TC. It was a great solution, but there were some things that didn't work the way I wanted. Chief among them was that SQL Source Control does not (or not so well) play well with industry / mergers. In addition, SQL Source Control was an out-of-band process that required developer training, acceptance and understanding of its features. He never took root. Our team eventually moved away from RedGate in favor of TeamCity-managed Entity Framework migration, and we did not look back.
To your immediate question: I think that what you are trying to do here is possible, although I do not think it is necessary for TeamCity to search for newly added SQL scripts. The Red Gate and Entity Framework identify the most recent script / migration deployed in the database itself and refer to it when they determine where it should start applying the changes. You can save the same value ("3.0") in your database or as a TC build parameter, and then use powershell to repeat newer folders and apply the latest scripts using sqlcmd.
You might consider the possibility of EF-based migration, especially if you adopted the first code model. If you're interested, I can update this answer to talk more about how we do it.
UPDATE:
Primary migrations of the EF code are performed by writing a discrete set of database schema changes in the migration class. This class has an Up
and Down
method that can apply / undo this particular batch of schema changes. This is more or less equivalent to how Ruby and others do it.
When changing the Entity model (adding a table, deleting a column, changing constraints, etc.), you create a new migration class by calling Add-Migration <DescriptionOfSchemaChange>
in the package manager console. This will create a new class called <timestamp>_<DescriptionOfSchemaChange>.cs
. You can then apply this migration locally by calling Update-Database
. The Entity Framework manages nuts and bolts for you, but you can add an arbitrary SQL step to the migration step if necessary. PluralSight offers an excellent course covering these concepts.
Migration is controlled using the migrate.exe
, which is deployed as part of the NuGet Entity Framework. In TeamCity, you can use migrate.exe
to start the migration with the specified database. First, you must copy migrate.exe
local to the assembly that contains your migration classes. Then you simply run migrate.exe
against this assembly with the DB connection string as a parameter. The working directory is the migration directory.
copy ..\..\packages\EntityFramework.5.0.0\tools\migrate.exe .
migrate.exe ASSEMBLY.dll /connectionString="CONNECTION_STRING" /connectionProviderName="System.Data.SqlClient"