@echo off

SET ASPNETCORE_ENVIRONMENT=%1

REM Set the current directory to where the .csproj file is located
cd C:\JenkinsHomeDir\workspace\Staging-UtilityDBDeployment\UtilityDBDeployment

for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j
set filename=%ldt:~8,2%%ldt:~10,2%%ldt:~12,2%

SET EF_PATH=C:\Users\Administrator\.nuget\packages\microsoft.entityframeworkcore.tools\3.1.4\tools\netcoreapp2.0\any\ef.dll
SET ASSEMBLY_PATH=C:\JenkinsHomeDir\workspace\Staging-UtilityDBDeployment\UtilityDBDeployment\bin\Debug\netcoreapp3.1\UtilityDBDeployment.dll

REM Add migration
echo Adding new migration...
dotnet exec --depsfile bin\Debug\netcoreapp3.1\UtilityDBDeployment.deps.json %EF_PATH% migrations add %filename%_addenumtomariadb --assembly %ASSEMBLY_PATH% 
IF ERRORLEVEL 1 (
    echo Error while adding migration.
    exit /b
)

REM Update database
echo Updating database...
dotnet exec --depsfile bin\Debug\netcoreapp3.1\UtilityDBDeployment.deps.json %EF_PATH% database update --assembly %ASSEMBLY_PATH%
IF ERRORLEVEL 1 (
    echo Error while updating database.
    dotnet exec --depsfile bin\Debug\netcoreapp3.1\UtilityDBDeployment.deps.json %EF_PATH% migrations remove --assembly %ASSEMBLY_PATH%
    exit /b
)

echo Done.
