Skip to content

Commit af190d8

Browse files
committed
crone: 스크립트 개선
- 좀 더 효율적이고 실용적인 방법으로 수정
1 parent 4482d5c commit af190d8

File tree

5 files changed

+800
-27
lines changed

5 files changed

+800
-27
lines changed

scripts/migrate.ps1

Lines changed: 109 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,120 @@
1-
# 0. 기존 DB 드롭
2-
dotnet ef database drop --project "./ProjectVG.Infrastructure" --startup-project "./ProjectVG.Api" --force
1+
# EF Core 마이그레이션 스크립트 (일반 마이그레이션 - 기존 마이그레이션 유지)
2+
Write-Host "=== ProjectVG Database Migration ===" -ForegroundColor Green
33

4-
if ($LASTEXITCODE -ne 0) {
5-
Write-Error "DB 드롭 실패"
6-
exit $LASTEXITCODE
4+
# 시작 시간 기록
5+
$startTime = Get-Date
6+
7+
# 프로젝트 경로 확인
8+
$infrastructureProject = "./ProjectVG.Infrastructure"
9+
$startupProject = "./ProjectVG.Api"
10+
11+
if (!(Test-Path $infrastructureProject)) {
12+
Write-Host "Error: Infrastructure project not found at $infrastructureProject" -ForegroundColor Red
13+
exit 1
14+
}
15+
16+
if (!(Test-Path $startupProject)) {
17+
Write-Host "Error: API project not found at $startupProject" -ForegroundColor Red
18+
exit 1
719
}
820

9-
# 1. 마이그레이션 생성
10-
dotnet ef migrations add InitialCreate --project "./ProjectVG.Infrastructure" --startup-project "./ProjectVG.Api"
21+
Write-Host "1. Checking current migration status..." -ForegroundColor Yellow
22+
Write-Host " Infrastructure Project: $infrastructureProject" -ForegroundColor Gray
23+
Write-Host " Startup Project: $startupProject" -ForegroundColor Gray
1124

12-
if ($LASTEXITCODE -ne 0) {
13-
Write-Error "Migration 생성 실패"
14-
exit $LASTEXITCODE
25+
# 현재 마이그레이션 상태 확인
26+
try {
27+
$migrationsList = dotnet ef migrations list --project $infrastructureProject --startup-project $startupProject --no-build 2>$null
28+
if ($LASTEXITCODE -eq 0) {
29+
Write-Host " Existing migrations found" -ForegroundColor Green
30+
Write-Host $migrationsList -ForegroundColor Gray
31+
} else {
32+
Write-Host " No existing migrations or connection issue" -ForegroundColor Yellow
33+
}
34+
} catch {
35+
Write-Host " Could not check existing migrations" -ForegroundColor Yellow
1536
}
1637

17-
# 2. DB 업데이트
18-
dotnet ef database update --project "./ProjectVG.Infrastructure" --startup-project "./ProjectVG.Api"
38+
# 마이그레이션 이름 생성 (타임스탬프 기반)
39+
$timestamp = Get-Date -Format "yyyyMMddHHmmss"
40+
$migrationName = Read-Host "Enter migration name (or press Enter for auto-generated name)"
41+
42+
if ([string]::IsNullOrWhiteSpace($migrationName)) {
43+
$migrationName = "Migration_$timestamp"
44+
Write-Host " Using auto-generated name: $migrationName" -ForegroundColor Gray
45+
}
46+
47+
# 2. 마이그레이션 생성
48+
Write-Host "2. Creating new migration: $migrationName..." -ForegroundColor Yellow
49+
dotnet ef migrations add $migrationName --project $infrastructureProject --startup-project $startupProject
1950

2051
if ($LASTEXITCODE -ne 0) {
21-
Write-Error "DB 업데이트 실패"
52+
Write-Host "❌ Migration creation failed!" -ForegroundColor Red
53+
Write-Host "Common causes:" -ForegroundColor Yellow
54+
Write-Host " - Build errors in the project" -ForegroundColor Gray
55+
Write-Host " - Database connection issues" -ForegroundColor Gray
56+
Write-Host " - No model changes detected" -ForegroundColor Gray
2257
exit $LASTEXITCODE
2358
}
2459

25-
Write-Host "✅ DB 초기화 및 마이그레이션 완료"
60+
Write-Host " Migration created successfully!" -ForegroundColor Green
61+
62+
# 3. 사용자에게 DB 업데이트 여부 확인
63+
$updateChoice = Read-Host "Apply migration to database now? (y/n) [default: y]"
64+
if ([string]::IsNullOrWhiteSpace($updateChoice) -or $updateChoice.ToLower() -eq "y") {
65+
66+
Write-Host "3. Applying migration to database..." -ForegroundColor Yellow
67+
68+
# DB 연결 확인
69+
Write-Host " Checking database connection..." -ForegroundColor Gray
70+
$dbCheck = dotnet ef database drop --project $infrastructureProject --startup-project $startupProject --dry-run 2>$null
71+
if ($LASTEXITCODE -ne 0) {
72+
Write-Host " Warning: Could not verify database connection" -ForegroundColor Yellow
73+
} else {
74+
Write-Host " Database connection verified" -ForegroundColor Green
75+
}
76+
77+
# DB 업데이트 실행
78+
dotnet ef database update --project $infrastructureProject --startup-project $startupProject
79+
80+
if ($LASTEXITCODE -ne 0) {
81+
Write-Host "❌ Database update failed!" -ForegroundColor Red
82+
Write-Host "The migration was created but not applied." -ForegroundColor Yellow
83+
Write-Host "You can apply it later using:" -ForegroundColor Gray
84+
Write-Host " dotnet ef database update --project $infrastructureProject --startup-project $startupProject" -ForegroundColor Gray
85+
exit $LASTEXITCODE
86+
}
87+
88+
Write-Host " Database updated successfully!" -ForegroundColor Green
89+
90+
# 최종 상태 확인
91+
Write-Host "4. Verifying final migration status..." -ForegroundColor Yellow
92+
try {
93+
$finalMigrations = dotnet ef migrations list --project $infrastructureProject --startup-project $startupProject --no-build 2>$null
94+
if ($LASTEXITCODE -eq 0) {
95+
Write-Host " Current applied migrations:" -ForegroundColor Green
96+
Write-Host $finalMigrations -ForegroundColor Gray
97+
}
98+
} catch {
99+
Write-Host " Could not verify final status" -ForegroundColor Yellow
100+
}
101+
102+
} else {
103+
Write-Host "3. Skipping database update" -ForegroundColor Yellow
104+
Write-Host " Migration created but not applied to database" -ForegroundColor Gray
105+
Write-Host " To apply later, run:" -ForegroundColor Gray
106+
Write-Host " dotnet ef database update --project $infrastructureProject --startup-project $startupProject" -ForegroundColor Gray
107+
}
108+
109+
# 실행 시간 계산
110+
$endTime = Get-Date
111+
$duration = $endTime - $startTime
112+
Write-Host "`n=== Migration completed in $($duration.TotalSeconds.ToString('F1')) seconds ===" -ForegroundColor Green
113+
114+
Write-Host "`n=== Migration Commands Reference ===" -ForegroundColor Cyan
115+
Write-Host "List migrations: dotnet ef migrations list --project $infrastructureProject --startup-project $startupProject" -ForegroundColor Gray
116+
Write-Host "Remove last migration: dotnet ef migrations remove --project $infrastructureProject --startup-project $startupProject" -ForegroundColor Gray
117+
Write-Host "Update database: dotnet ef database update --project $infrastructureProject --startup-project $startupProject" -ForegroundColor Gray
118+
Write-Host "Generate SQL script: dotnet ef migrations script --project $infrastructureProject --startup-project $startupProject" -ForegroundColor Gray
119+
120+
Write-Host "`n✅ Migration process complete!" -ForegroundColor Green

scripts/reset-db-full.ps1

Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
# 완전 DB 초기화 스크립트 (Docker 볼륨 + 마이그레이션 포함)
2+
Write-Host "=== ProjectVG Complete Database Reset ===" -ForegroundColor Red
3+
Write-Host "⚠️ WARNING: This will completely destroy all database and Redis data!" -ForegroundColor Yellow
4+
5+
# 시작 시간 기록
6+
$startTime = Get-Date
7+
8+
# 프로젝트 경로 확인
9+
$infrastructureProject = "./ProjectVG.Infrastructure"
10+
$startupProject = "./ProjectVG.Api"
11+
$dbComposeFile = "docker-compose.db.yml"
12+
13+
# 파일 존재 확인
14+
if (!(Test-Path $infrastructureProject)) {
15+
Write-Host "Error: Infrastructure project not found at $infrastructureProject" -ForegroundColor Red
16+
exit 1
17+
}
18+
19+
if (!(Test-Path $startupProject)) {
20+
Write-Host "Error: API project not found at $startupProject" -ForegroundColor Red
21+
exit 1
22+
}
23+
24+
if (!(Test-Path $dbComposeFile)) {
25+
Write-Host "Error: Docker compose file not found: $dbComposeFile" -ForegroundColor Red
26+
exit 1
27+
}
28+
29+
# 사용자 확인 프롬프트 (강력한 안전장치)
30+
Write-Host "`n📋 This script will perform COMPLETE database reset:" -ForegroundColor Cyan
31+
Write-Host " 1. Stop all database containers" -ForegroundColor Gray
32+
Write-Host " 2. Remove Docker volumes (mssql_data, redis_data)" -ForegroundColor Gray
33+
Write-Host " 3. Remove Docker networks" -ForegroundColor Gray
34+
Write-Host " 4. Recreate containers with fresh volumes" -ForegroundColor Gray
35+
Write-Host " 5. Run database migrations" -ForegroundColor Gray
36+
Write-Host " 6. Verify system health" -ForegroundColor Gray
37+
38+
Write-Host "`n💥 THIS WILL DELETE ALL DATABASE AND REDIS DATA PERMANENTLY!" -ForegroundColor Red
39+
40+
$confirmation1 = Read-Host "`n❓ Type 'DELETE ALL DATA' to confirm complete reset"
41+
if ($confirmation1 -ne "DELETE ALL DATA") {
42+
Write-Host "Operation cancelled by user." -ForegroundColor Yellow
43+
exit 0
44+
}
45+
46+
$confirmation2 = Read-Host "❓ Are you absolutely sure? Type 'YES I UNDERSTAND' to proceed"
47+
if ($confirmation2 -ne "YES I UNDERSTAND") {
48+
Write-Host "Operation cancelled by user." -ForegroundColor Yellow
49+
exit 0
50+
}
51+
52+
Write-Host "`n🔥 Starting complete database infrastructure reset..." -ForegroundColor Red
53+
54+
# 1. 현재 상태 확인 및 로그
55+
Write-Host "1. Checking current system state..." -ForegroundColor Yellow
56+
57+
try {
58+
Write-Host " Current containers:" -ForegroundColor Gray
59+
docker-compose -f $dbComposeFile ps 2>$null
60+
61+
Write-Host " Current volumes:" -ForegroundColor Gray
62+
docker volume ls | Select-String "projectvg\|mssql\|redis" 2>$null
63+
64+
Write-Host " Current networks:" -ForegroundColor Gray
65+
docker network ls | Select-String "projectvg" 2>$null
66+
} catch {
67+
Write-Host " Could not check current state" -ForegroundColor Yellow
68+
}
69+
70+
# 2. 컨테이너 중지 및 제거 (볼륨 포함)
71+
Write-Host "2. Stopping and removing all database containers..." -ForegroundColor Yellow
72+
73+
docker-compose -f $dbComposeFile down -v --remove-orphans
74+
75+
if ($LASTEXITCODE -ne 0) {
76+
Write-Host " Warning: Some containers may not have been running" -ForegroundColor Yellow
77+
} else {
78+
Write-Host " Containers stopped and removed successfully" -ForegroundColor Green
79+
}
80+
81+
# 3. 관련 볼륨 강제 삭제 (혹시 남아있을 수 있는 볼륨들)
82+
Write-Host "3. Force removing all related Docker volumes..." -ForegroundColor Yellow
83+
84+
$volumesToRemove = @(
85+
"mainapiserver_mssql_data",
86+
"projectvg_mssql_data",
87+
"mssql_data",
88+
"mainapiserver_redis_data",
89+
"projectvg_redis_data",
90+
"redis_data",
91+
"projectvg-db-data",
92+
"projectvg-redis-data"
93+
)
94+
95+
$removedVolumes = 0
96+
foreach ($volume in $volumesToRemove) {
97+
$result = docker volume rm $volume 2>$null
98+
if ($LASTEXITCODE -eq 0) {
99+
Write-Host " Removed volume: $volume" -ForegroundColor Green
100+
$removedVolumes++
101+
}
102+
}
103+
104+
if ($removedVolumes -eq 0) {
105+
Write-Host " No volumes found to remove" -ForegroundColor Gray
106+
} else {
107+
Write-Host " Removed $removedVolumes volumes" -ForegroundColor Green
108+
}
109+
110+
# 4. 관련 네트워크 정리
111+
Write-Host "4. Cleaning up Docker networks..." -ForegroundColor Yellow
112+
113+
$networksToRemove = @(
114+
"projectvg-external-db",
115+
"mainapiserver_projectvg-external-db"
116+
)
117+
118+
$removedNetworks = 0
119+
foreach ($network in $networksToRemove) {
120+
$result = docker network rm $network 2>$null
121+
if ($LASTEXITCODE -eq 0) {
122+
Write-Host " Removed network: $network" -ForegroundColor Green
123+
$removedNetworks++
124+
}
125+
}
126+
127+
# 5. 필요한 네트워크 재생성
128+
Write-Host "5. Recreating external networks..." -ForegroundColor Yellow
129+
docker network create projectvg-external-db 2>$null
130+
if ($LASTEXITCODE -eq 0) {
131+
Write-Host " External network created successfully" -ForegroundColor Green
132+
} else {
133+
Write-Host " Network may already exist or creation failed" -ForegroundColor Yellow
134+
}
135+
136+
# 6. 새로운 DB 및 Redis 컨테이너 시작
137+
Write-Host "6. Starting fresh database and Redis containers..." -ForegroundColor Yellow
138+
139+
docker-compose -f $dbComposeFile up -d
140+
141+
if ($LASTEXITCODE -ne 0) {
142+
Write-Host "❌ Failed to start database containers!" -ForegroundColor Red
143+
exit $LASTEXITCODE
144+
}
145+
146+
Write-Host " Containers started successfully" -ForegroundColor Green
147+
148+
# 7. 컨테이너 상태 확인 (헬스체크 대기)
149+
Write-Host "7. Waiting for containers to be healthy..." -ForegroundColor Yellow
150+
151+
$maxWaitTime = 120 # 2분
152+
$waitTime = 0
153+
$healthyContainers = 0
154+
155+
do {
156+
Start-Sleep -Seconds 5
157+
$waitTime += 5
158+
159+
$dbHealth = docker inspect --format='{{.State.Health.Status}}' projectvg-db 2>$null
160+
$redisHealth = docker inspect --format='{{.State.Health.Status}}' projectvg-redis 2>$null
161+
162+
$healthyContainers = 0
163+
if ($dbHealth -eq "healthy") { $healthyContainers++ }
164+
if ($redisHealth -eq "healthy") { $healthyContainers++ }
165+
166+
Write-Host " Waiting... ($waitTime/$maxWaitTime seconds) - Healthy: $healthyContainers/2" -ForegroundColor Gray
167+
168+
if ($healthyContainers -eq 2) {
169+
Write-Host " All containers are healthy!" -ForegroundColor Green
170+
break
171+
}
172+
173+
} while ($waitTime -lt $maxWaitTime)
174+
175+
if ($healthyContainers -lt 2) {
176+
Write-Host " Warning: Not all containers are healthy, but proceeding..." -ForegroundColor Yellow
177+
docker-compose -f $dbComposeFile ps
178+
}
179+
180+
# 8. 데이터베이스 마이그레이션 실행
181+
Write-Host "8. Running database migrations..." -ForegroundColor Yellow
182+
183+
# 추가 대기 시간 (DB가 완전히 준비될 때까지)
184+
Write-Host " Waiting additional 10 seconds for database to be fully ready..." -ForegroundColor Gray
185+
Start-Sleep -Seconds 10
186+
187+
dotnet ef database update --project $infrastructureProject --startup-project $startupProject
188+
189+
if ($LASTEXITCODE -ne 0) {
190+
Write-Host "❌ Database migration failed!" -ForegroundColor Red
191+
Write-Host "Database containers are running, but migration could not be applied." -ForegroundColor Yellow
192+
Write-Host "You may need to run migrations manually:" -ForegroundColor Gray
193+
Write-Host " dotnet ef database update --project $infrastructureProject --startup-project $startupProject" -ForegroundColor Gray
194+
exit $LASTEXITCODE
195+
}
196+
197+
Write-Host " Database migrations applied successfully!" -ForegroundColor Green
198+
199+
# 9. 최종 시스템 상태 확인
200+
Write-Host "9. Final system verification..." -ForegroundColor Yellow
201+
202+
Write-Host " Container status:" -ForegroundColor Gray
203+
docker-compose -f $dbComposeFile ps
204+
205+
Write-Host " Volume status:" -ForegroundColor Gray
206+
docker volume ls | Select-String "mssql\|redis"
207+
208+
Write-Host " Migration status:" -ForegroundColor Gray
209+
try {
210+
$migrations = dotnet ef migrations list --project $infrastructureProject --startup-project $startupProject --no-build 2>$null
211+
if ($LASTEXITCODE -eq 0) {
212+
Write-Host $migrations -ForegroundColor Gray
213+
} else {
214+
Write-Host " Could not verify migrations" -ForegroundColor Yellow
215+
}
216+
} catch {
217+
Write-Host " Could not verify migrations" -ForegroundColor Yellow
218+
}
219+
220+
# 실행 시간 계산
221+
$endTime = Get-Date
222+
$duration = $endTime - $startTime
223+
Write-Host "`n=== Complete database reset finished in $($duration.TotalSeconds.ToString('F1')) seconds ===" -ForegroundColor Green
224+
225+
Write-Host "`n=== Reset Summary ===" -ForegroundColor Cyan
226+
Write-Host "✅ All containers stopped and removed" -ForegroundColor Green
227+
Write-Host "✅ All Docker volumes destroyed and recreated" -ForegroundColor Green
228+
Write-Host "✅ Networks cleaned and recreated" -ForegroundColor Green
229+
Write-Host "✅ Fresh database and Redis containers started" -ForegroundColor Green
230+
Write-Host "✅ Database migrations applied" -ForegroundColor Green
231+
232+
Write-Host "`n=== Connection Information ===" -ForegroundColor Cyan
233+
Write-Host "Database: localhost:1433" -ForegroundColor White
234+
Write-Host "Redis: localhost:6380" -ForegroundColor White
235+
Write-Host "Database Name: ProjectVG" -ForegroundColor White
236+
Write-Host "SA Password: ProjectVG123!" -ForegroundColor White
237+
238+
Write-Host "`n=== Useful Commands ===" -ForegroundColor Cyan
239+
Write-Host "Check logs: docker-compose -f $dbComposeFile logs" -ForegroundColor Gray
240+
Write-Host "Follow logs: docker-compose -f $dbComposeFile logs -f" -ForegroundColor Gray
241+
Write-Host "Container status: docker-compose -f $dbComposeFile ps" -ForegroundColor Gray
242+
243+
Write-Host "`n🎉 Complete database infrastructure reset successful!" -ForegroundColor Green
244+
Write-Host " Your database environment is now completely fresh and ready." -ForegroundColor White

0 commit comments

Comments
 (0)