migrate.bat 2.3 KB
@echo off
setlocal enabledelayedexpansion

set STARTUP=src\Rcs.Api\Rcs.Api.csproj
set PROJECT=src\Rcs.Infrastructure\Rcs.Infrastructure.csproj
set CONTEXT=AppDbContext
set CONFIGURATION=Release

set TIMESTAMP=%date:~0,4%%date:~5,2%%date:~8,2%%time:~0,2%%time:~3,2%%time:~6,2%
set TIMESTAMP=%TIMESTAMP: =0%
set MIGRATION_NAME=Migration_%TIMESTAMP%

echo ========================================
echo HaHRCS Database Migration Tool
echo ========================================

if "%1"=="-apply" goto apply
if "%1"=="-script" goto script

:create
echo [INFO] Creating migration: %MIGRATION_NAME%
call :build
dotnet ef migrations add %MIGRATION_NAME% --startup-project %STARTUP% --project %PROJECT% --context %CONTEXT% --configuration %CONFIGURATION% --no-build
if errorlevel 1 (
    echo [ERROR] Failed to create migration
    exit /b 1
)
echo [SUCCESS] Migration created
echo [INFO] Applying database migration...
call :build
dotnet ef database update --startup-project %STARTUP% --project %PROJECT% --context %CONTEXT% --configuration %CONFIGURATION% --no-build
if errorlevel 1 (
    echo [ERROR] Failed to update database
    exit /b 1
)
echo [SUCCESS] Database updated
goto end

:apply
echo [INFO] Applying database migration...
call :build
dotnet ef database update --startup-project %STARTUP% --project %PROJECT% --context %CONTEXT% --configuration %CONFIGURATION% --no-build
if errorlevel 1 (
    echo [ERROR] Migration failed
    exit /b 1
)
echo [SUCCESS] Migration applied
goto end

:script
echo [INFO] Generating SQL migration script...
call :build
if not exist ".specstory\history\sql" mkdir ".specstory\history\sql"
dotnet ef migrations script --startup-project %STARTUP% --project %PROJECT% --context %CONTEXT% --output ".specstory\history\sql\%TIMESTAMP%_migration.sql" --idempotent --configuration %CONFIGURATION% --no-build
if errorlevel 1 (
    echo [ERROR] Failed to generate SQL script
    exit /b 1
)
echo [SUCCESS] SQL script generated
goto end

:build
echo [INFO] Building project (%CONFIGURATION%)...
dotnet build %STARTUP% --configuration %CONFIGURATION% -m:1
if errorlevel 1 (
    echo [ERROR] Build failed. Please stop running Rcs.Api or other dotnet processes that may lock dll files, then retry.
    exit /b 1
)
goto :eof

:end
echo ========================================
echo Done
echo ========================================