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