82 lines
2.4 KiB
Batchfile
82 lines
2.4 KiB
Batchfile
@echo off
|
|
chcp 65001 >nul
|
|
setlocal enabledelayedexpansion
|
|
|
|
REM ==========================================
|
|
REM Finance System - Database Init Script
|
|
REM ==========================================
|
|
|
|
set PGHOST=localhost
|
|
set PGPORT=5432
|
|
set PGUSER=postgres
|
|
set PGPASSWORD=postgres
|
|
set DB_NAME=finance_system
|
|
set SQL_FILE=%~dp0init_schema.sql
|
|
set PSQL_PATH=D:\develop\PostgreSQL\bin\psql.exe
|
|
|
|
echo ==========================================
|
|
echo Finance System - Database Initialization
|
|
echo Database: %DB_NAME%
|
|
echo Host: %PGHOST%:%PGPORT%
|
|
echo ==========================================
|
|
echo.
|
|
|
|
REM Check if psql exists
|
|
if not exist "%PSQL_PATH%" (
|
|
echo [ERROR] psql not found at: %PSQL_PATH%
|
|
echo Please check your PostgreSQL installation path.
|
|
pause
|
|
exit /b 1
|
|
)
|
|
|
|
if not exist "%SQL_FILE%" (
|
|
echo [ERROR] SQL file not found: %SQL_FILE%
|
|
pause
|
|
exit /b 1
|
|
)
|
|
|
|
echo [1/3] Checking PostgreSQL connection...
|
|
"%PSQL_PATH%" -h %PGHOST% -p %PGPORT% -U %PGUSER% -d postgres -c "SELECT 1;" >nul 2>nul
|
|
if %ERRORLEVEL% neq 0 (
|
|
echo [ERROR] Cannot connect to PostgreSQL at %PGHOST%:%PGPORT%
|
|
echo Please make sure PostgreSQL service is running.
|
|
pause
|
|
exit /b 1
|
|
)
|
|
echo OK
|
|
|
|
echo [2/3] Checking/Creating database %DB_NAME%...
|
|
"%PSQL_PATH%" -h %PGHOST% -p %PGPORT% -U %PGUSER% -d postgres -t -c "SELECT 1 FROM pg_database WHERE datname='%DB_NAME%';" 2>nul | findstr "1" >nul
|
|
if %ERRORLEVEL% equ 0 (
|
|
echo Database %DB_NAME% already exists, skipping creation.
|
|
) else (
|
|
echo Creating database %DB_NAME%...
|
|
"%PSQL_PATH%" -h %PGHOST% -p %PGPORT% -U %PGUSER% -d postgres -c "CREATE DATABASE %DB_NAME% WITH ENCODING 'UTF8';"
|
|
if !ERRORLEVEL! neq 0 (
|
|
echo [ERROR] Failed to create database!
|
|
pause
|
|
exit /b 1
|
|
)
|
|
echo Database created successfully.
|
|
)
|
|
|
|
echo [3/3] Executing SQL script: %SQL_FILE%
|
|
"%PSQL_PATH%" -h %PGHOST% -p %PGPORT% -U %PGUSER% -d %DB_NAME% -f "%SQL_FILE%"
|
|
if %ERRORLEVEL% equ 0 (
|
|
echo.
|
|
echo ==========================================
|
|
echo SUCCESS: Database initialization complete!
|
|
echo Database: %DB_NAME%
|
|
echo ==========================================
|
|
echo.
|
|
echo Backend config (application.yml):
|
|
echo spring.datasource.url: jdbc:postgresql://%PGHOST%:%PGPORT%/%DB_NAME%
|
|
echo spring.datasource.username: %PGUSER%
|
|
echo spring.datasource.password: %PGPASSWORD%
|
|
) else (
|
|
echo [ERROR] SQL script execution failed.
|
|
)
|
|
|
|
echo.
|
|
pause
|