Search code examples
migrationfluent-migrator

Getting output of Migrate.exe in Batch file


I have a batch file that runs a FluentMigrator migration based on user input. The last step is to run the migrator as below:

"%~dp0\FluentMigrator.1.1.2.1\tools\Migrate.exe" /conn "DATA SOURCE=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=%HostName%)(PORT=%ServerPort%))(CONNECT_DATA=(SERVICE_NAME=%ServiceName%)));PASSWORD=%installerUserPW%;PERSIST SECURITY INFO=True;USER ID=%installerUserName%;" /provider oracle /context %installerUserName% /assembly ./bin/Debug/DatabaseMigrations.dll  >> install_log.txt

ECHO.
echo ------------------------------------------------------------------------------->> install_log.txt
echo ------------------ Database Setup has executed successfully ------------------ >> install_log.txt
echo ------------------------------------------------------------------------------->> install_log.txt
TYPE install_log.txt
GOTO:EOF

This works just as required - The Migrate.exe app prints all the output to the file, then the success message, then the file is written to the console so there is a console and file copy. But if the migration fails, for example if the user does not have write permissions for the database, it does not work as expected. In this case, the end message expects the file to have executed correctly. Is there any way to determine if the migrator has exited with an error?

I would like to display:

  • The migration code
  • (If failed) The error message for failure
  • (else) A success message

EDIT: I originally asked about how to display both the error and general output - this was achieved by the >> install_log.txt 2>&1 code. Now I just need to figure out how to identify if an error has occurred on migration.


Solution

  • The solution I found is as follows:

    "%~dp0\FluentMigrator.1.1.2.1\tools\Migrate.exe" /conn "DATA SOURCE=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=%HostName%)(PORT=%ServerPort%))(CONNECT_DATA=(SERVICE_NAME=%ServiceName%)));PASSWORD=%installerUserPW%;PERSIST SECURITY INFO=True;USER ID=%installerUserName%;" /provider oracle /context %installerUserName% /assembly ./bin/Debug/DatabaseMigrations.dll  >> migrate_log.txt 2>> migrateerror_log.txt
    if exist migrate_log.txt (
            if exist migrateerror_log.txt (
            TYPE migrateerror_log.txt >> install_log.txt
            echo ------------------------------------------------------------------------------->> install_log.txt
            echo -------------------------- Database Setup has failed ------------------------- >> install_log.txt
            echo ------------------------------------------------------------------------------->> install_log.txt
        ) else (
            echo ------------------------------------------------------------------------------->> install_log.txt
            echo ----------------- Database Setup has completed successfully ------------------ >> install_log.txt
            echo ------------------------------------------------------------------------------->> install_log.txt
        )
    ) else (
        echo ------------------------------------------------------------------------------->> install_log.txt
        echo ------------- An unexpected error has occurred in FluentMigrator ------------- >> install_log.txt
        echo ------------------------------------------------------------------------------->> install_log.txt
    )