I need to move some data from a database to a web service. So I need to call the web service during a migration and upload existing data before the containing table is dropped. If data upload fails, the table must not be dropped. Also I would like to remove the data from the service in case when an exception occurs during other steps of this migration.
If I place the web service call within the Up
method, the web service is called before the migration and the migration will not run if the service call throws. However, I do not see where can I put the rollback code. The statements in the Up
method only enqueue operations to be executed and do not perform any database checks, so adding a catch
there won't help.
The only possible solution I see now is to create my own migration runner rather than use migrate.exe
and run migrations one by one:
var migrator = new DbMigrator(new MyMigrationConfiguration());
var pendingMigrations = migrator.GetPendingMigrations();
foreach (var migration in pendingMigrations)
{
try
{
migrator.Update(migration);
}
catch (SqlException)
{
if (migration == "MoveDataToWebService")
{
// Delete data from service
}
throw;
}
}
Are there any built-in ways to handle migration failures, so I can run migrations from Visual Studio or migrate.exe
? Or maybe my approach to the original task is wrong and I should use something else than EF migrations to move the data?