Rollback to a specfic Migration in FluentMigrator

2019-03-06 14:55发布

问题:

Suppose I have crated a three Table using FluentMigrator and gave them version Number 1, 2, 3 respectively. Now Is there any way to rollback till version 2. I mean After rollback I should have Table 1 and 2 but not 3.

回答1:

Here is a batch file I use with the command line runner tool

@echo off
if "%1" == "rollback" goto rollback
if "%1" == "" goto migrate
if "%1" == "version" goto version
if "%1" == "down" goto down
goto error

:migrate
migrate -db SqlServer2014 -connection "Server=[YOUR CONNECTTION STRING]" -assembly "[YOUR MIGRATION ASSEMBLY]"
goto done

:rollback
migrate -db SqlServer2014 -connection "Server=[YOUR CONNECTTION STRING]" -assembly "[YOUR MIGRATION ASSEMBLY]" -task rollback:all
goto done

:version
migrate -db SqlServer2014 -version "%2" -connection "Server=[YOUR CONNECTTION STRING]" -assembly "[YOUR MIGRATION ASSEMBLY]"
goto done

:down
migrate -db SqlServer2014 -version %2 -connection "Server=[YOUR CONNECTTION STRING]" -assembly "[YOUR MIGRATION ASSEMBLY]" -task rollback:toversion

:error
echo "No valid command"

:done
echo "Completed"

Then you would use the forth option: down as follows

  1. Open a Cmd in the directory the batch file exists, the command line runner dlls and the migration assembly dll need to exist in that directory
  2. Run the following [Batch File Name].bat down [Version YOU WANT TO ROLLBACK TO: i.e., Migration you want as your last one]

So in your example that would be "down 2", which will rollback 3 and you would keep 1 and 2.

More about Command Line Runner