Files
hr-portal/scripts/setup-database.ps1
Porsche Chen 360533393f feat: HR Portal - Complete Multi-Tenant System with Redis Session Storage
Major Features:
-  Multi-tenant architecture (tenant isolation)
-  Employee CRUD with lifecycle management (onboarding/offboarding)
-  Department tree structure with email domain management
-  Company info management (single-record editing)
-  System functions CRUD (permission management)
-  Email account management (multi-account per employee)
-  Keycloak SSO integration (auth.lab.taipei)
-  Redis session storage (10.1.0.254:6379)
  - Solves Cookie 4KB limitation
  - Cross-system session sharing
  - Sliding expiration (8 hours)
  - Automatic token refresh

Technical Stack:
Backend:
- FastAPI + SQLAlchemy
- PostgreSQL 16 (10.1.0.20:5433)
- Keycloak Admin API integration
- Docker Mailserver integration (SSH)
- Alembic migrations

Frontend:
- Next.js 14 (App Router)
- NextAuth 4 with Keycloak Provider
- Redis session storage (ioredis)
- Tailwind CSS

Infrastructure:
- Redis 7 (10.1.0.254:6379) - Session + Cache
- Keycloak 26.1.0 (auth.lab.taipei)
- Docker Mailserver (10.1.0.254)

Architecture Highlights:
- Session管理由 Keycloak + Redis 統一控制
- 支援多系統 (HR/WebMail/Calendar/Drive/Office) 共享 session
- Token 自動刷新,異質服務整合
- 未來可無縫遷移到雲端

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-02-23 20:12:43 +08:00

103 lines
3.0 KiB
PowerShell

# ====================================
# HR Portal - 資料庫設定腳本 (PowerShell)
# ====================================
Write-Host "=== HR Portal 資料庫設定 ===" -ForegroundColor Cyan
Write-Host ""
# 設定變數
$DB_HOST = "10.1.0.254"
$DB_NAME = "hr_portal"
$DB_USER = "hr_user"
$DB_PASSWORD = "hr_password_change_me" # 請修改為強密碼
Write-Host "連接到 PostgreSQL..." -ForegroundColor Yellow
Write-Host ""
# 檢查 psql 是否可用
$psqlAvailable = Get-Command psql -ErrorAction SilentlyContinue
if (-not $psqlAvailable) {
Write-Host "錯誤: 找不到 psql 命令" -ForegroundColor Red
Write-Host "請確認 PostgreSQL 客戶端已安裝" -ForegroundColor Red
Write-Host "或使用 Docker 方式執行" -ForegroundColor Yellow
exit 1
}
try {
# 1. 創建資料庫用戶
Write-Host "1. 創建資料庫用戶: $DB_USER" -ForegroundColor Green
$createUserSQL = @"
DO `$`$
BEGIN
IF NOT EXISTS (SELECT FROM pg_user WHERE usename = '$DB_USER') THEN
CREATE USER $DB_USER WITH PASSWORD '$DB_PASSWORD';
RAISE NOTICE 'User $DB_USER created';
ELSE
RAISE NOTICE 'User $DB_USER already exists';
END IF;
END
`$`$;
"@
$createUserSQL | psql -h $DB_HOST -U postgres -w
Write-Host ""
# 2. 創建資料庫
Write-Host "2. 創建資料庫: $DB_NAME" -ForegroundColor Green
$createDbSQL = @"
SELECT 'CREATE DATABASE $DB_NAME OWNER $DB_USER'
WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = '$DB_NAME')\gexec
"@
$createDbSQL | psql -h $DB_HOST -U postgres -w
Write-Host ""
# 3. 授予權限
Write-Host "3. 授予權限" -ForegroundColor Green
$grantSQL = @"
GRANT ALL PRIVILEGES ON DATABASE $DB_NAME TO $DB_USER;
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO $DB_USER;
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO $DB_USER;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO $DB_USER;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON SEQUENCES TO $DB_USER;
"@
$grantSQL | psql -h $DB_HOST -U postgres -d $DB_NAME -w
Write-Host ""
# 4. 執行 Schema 初始化
Write-Host "4. 執行 Schema 初始化" -ForegroundColor Green
$schemaFile = Join-Path $PSScriptRoot "init-db.sql"
if (Test-Path $schemaFile) {
Get-Content $schemaFile | psql -h $DB_HOST -U $DB_USER -d $DB_NAME
} else {
Write-Host "警告: 找不到 init-db.sql" -ForegroundColor Yellow
}
Write-Host ""
Write-Host "✅ 資料庫設定完成!" -ForegroundColor Green
Write-Host ""
Write-Host "資料庫連接資訊:" -ForegroundColor Cyan
Write-Host " Host: $DB_HOST"
Write-Host " Database: $DB_NAME"
Write-Host " User: $DB_USER"
Write-Host " Password: $DB_PASSWORD"
Write-Host ""
Write-Host "DATABASE_URL: postgresql://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:5432/${DB_NAME}" -ForegroundColor Yellow
Write-Host ""
Write-Host "請將上述 DATABASE_URL 複製到 backend/.env 檔案中" -ForegroundColor Cyan
} catch {
Write-Host "錯誤: $_" -ForegroundColor Red
exit 1
}