File size: 10,656 Bytes
8eeb77a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# 🛡️ Chahua Database Management Console - Windows Setup
# Enterprise Database Management System for Windows VPS

Write-Host "🚀 Chahua Database Management Console Setup" -ForegroundColor Cyan
Write-Host "=" * 50 -ForegroundColor Gray

# Check if running as Administrator
$isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")

if (-not $isAdmin) {
    Write-Host "⚠️  WARNING: Not running as Administrator" -ForegroundColor Yellow
    Write-Host "Some operations may require elevated privileges" -ForegroundColor Yellow
}

# Function to check if a command exists
function Test-Command($cmdname) {
    return [bool](Get-Command -Name $cmdname -ErrorAction SilentlyContinue)
}

# Check prerequisites
Write-Host "`n🔍 Checking Prerequisites..." -ForegroundColor Yellow

if (Test-Command "node") {
    $nodeVersion = (node --version 2>$null)
    Write-Host "✅ Node.js: $nodeVersion" -ForegroundColor Green
} else {
    Write-Host "❌ Node.js not found. Please install Node.js first." -ForegroundColor Red
    exit 1
}

if (Test-Command "npm") {
    $npmVersion = (npm --version 2>$null)
    Write-Host "✅ NPM: v$npmVersion" -ForegroundColor Green
} else {
    Write-Host "❌ NPM not found. Please install NPM first." -ForegroundColor Red
    exit 1
}

if (Test-Command "pg_dump") {
    Write-Host "✅ PostgreSQL client tools available" -ForegroundColor Green
} else {
    Write-Host "⚠️  PostgreSQL client tools not found in PATH" -ForegroundColor Yellow
    Write-Host "   Database backup features may not work" -ForegroundColor Yellow
}

# Check if database console exists
$consolePath = "scripts\database-console"
if (Test-Path $consolePath) {
    Write-Host "✅ Database Console files found" -ForegroundColor Green
} else {
    Write-Host "❌ Database Console files not found at: $consolePath" -ForegroundColor Red
    Write-Host "   Please ensure you're running this from the project root" -ForegroundColor Red
    exit 1
}

# Check .env file
if (Test-Path ".env") {
    Write-Host "✅ Environment configuration found" -ForegroundColor Green
} else {
    Write-Host "⚠️  .env file not found. Creating from example..." -ForegroundColor Yellow
    if (Test-Path "config\env.example") {
        Copy-Item "config\env.example" ".env"
        Write-Host "✅ Created .env from example" -ForegroundColor Green
        Write-Host "   Please update .env with your database credentials" -ForegroundColor Cyan
    } else {
        Write-Host "❌ No .env.example found. Please create .env manually" -ForegroundColor Red
    }
}

# Display menu
function Show-Menu {
    Write-Host "`n🎯 Chahua Database Management Options:" -ForegroundColor Cyan
    Write-Host "=" * 40 -ForegroundColor Gray
    Write-Host "1. 🌐 Start Database Console (Web Interface)" -ForegroundColor White
    Write-Host "2. 🔍 Check Database Status" -ForegroundColor White  
    Write-Host "3. 🔧 Fix Database Issues" -ForegroundColor White
    Write-Host "4. 📊 Database Statistics" -ForegroundColor White
    Write-Host "5. 🗂️  Module Management" -ForegroundColor White
    Write-Host "6. 💾 Backup Database" -ForegroundColor White
    Write-Host "7. 🧪 Generate Sample Data" -ForegroundColor White
    Write-Host "8. 📋 List Tables" -ForegroundColor White
    Write-Host "9. ⚙️  Advanced Options" -ForegroundColor White
    Write-Host "0. 🚪 Exit" -ForegroundColor Red
    Write-Host "=" * 40 -ForegroundColor Gray
}

# Function to start database console
function Start-DatabaseConsole {
    Write-Host "`n🌐 Starting Chahua Database Console..." -ForegroundColor Cyan
    Write-Host "Server will be available at: http://localhost:3000" -ForegroundColor Green
    Write-Host "Console URL: http://localhost:3000/scripts/database-console" -ForegroundColor Green
    Write-Host "`nPress Ctrl+C to stop the server" -ForegroundColor Yellow
    
    # Start server in background and open browser
    Start-Process "http://localhost:3000/scripts/database-console"
    npm run start
}

# Function to check database status  
function Check-DatabaseStatus {
    Write-Host "`n🔍 Checking Database Status..." -ForegroundColor Cyan
    npm run db:status
}

# Function to fix database issues
function Fix-DatabaseIssues {
    Write-Host "`n🔧 Database Fix Options:" -ForegroundColor Cyan
    Write-Host "1. Fix Forum Issues (tags column)" -ForegroundColor White
    Write-Host "2. Fix Plugin System" -ForegroundColor White
    Write-Host "3. Fix Payment System" -ForegroundColor White
    Write-Host "4. Fix Core System" -ForegroundColor White
    Write-Host "5. Fix Dashboard" -ForegroundColor White
    Write-Host "6. Fix Bank Transfer" -ForegroundColor White
    Write-Host "7. Fix All Issues" -ForegroundColor White
    Write-Host "8. Return to Main Menu" -ForegroundColor Gray
    
    $choice = Read-Host "`nSelect option (1-8)"
    
    switch ($choice) {
        "1" { npm run db:fix-forum }
        "2" { npm run db:fix-plugins }
        "3" { npm run db:fix-payment }
        "4" { npm run db:fix-core }
        "5" { npm run db:fix-dashboard }
        "6" { npm run db:fix-bank }
        "7" { 
            Write-Host "🔄 Running all database fixes..." -ForegroundColor Cyan
            npm run db:fix-core
            npm run db:fix-payment  
            npm run db:fix-forum
            npm run db:fix-plugins
            npm run db:fix-dashboard
            npm run db:fix-bank
        }
        "8" { return }
        default { Write-Host "❌ Invalid option" -ForegroundColor Red }
    }
}

# Function to show database statistics
function Show-DatabaseStats {
    Write-Host "`n📊 Database Statistics..." -ForegroundColor Cyan
    npm run db:list-tables
    Write-Host "`n📈 Additional Stats:" -ForegroundColor Cyan
    npm run db:status
}

# Function for module management
function Manage-Modules {
    Write-Host "`n🗂️  Database Module Management:" -ForegroundColor Cyan
    Write-Host "1. Core Module Status" -ForegroundColor White
    Write-Host "2. Payment Module Status" -ForegroundColor White
    Write-Host "3. Forum Module Status" -ForegroundColor White
    Write-Host "4. Plugin Module Status" -ForegroundColor White
    Write-Host "5. Dashboard Module Status" -ForegroundColor White
    Write-Host "6. Bank Transfer Module Status" -ForegroundColor White
    Write-Host "7. All Modules Status" -ForegroundColor White
    Write-Host "8. Return to Main Menu" -ForegroundColor Gray
    
    $choice = Read-Host "`nSelect option (1-8)"
    
    switch ($choice) {
        "1" { npm run db:fix-core }
        "2" { npm run db:fix-payment }
        "3" { npm run db:fix-forum }
        "4" { npm run db:fix-plugins }
        "5" { npm run db:fix-dashboard }
        "6" { npm run db:fix-bank }
        "7" { npm run db:status }
        "8" { return }
        default { Write-Host "❌ Invalid option" -ForegroundColor Red }
    }
}

# Function to backup database
function Backup-Database {
    Write-Host "`n💾 Database Backup..." -ForegroundColor Cyan
    
    if (-not (Test-Command "pg_dump")) {
        Write-Host "❌ pg_dump not found. Please install PostgreSQL client tools." -ForegroundColor Red
        return
    }
    
    $timestamp = Get-Date -Format "yyyyMMdd-HHmmss"
    $backupFile = "backup\chahua-db-$timestamp.sql"
    
    Write-Host "Creating backup: $backupFile" -ForegroundColor Green
    npm run db:backup
}

# Function for advanced options
function Show-AdvancedOptions {
    Write-Host "`n⚙️  Advanced Options:" -ForegroundColor Cyan
    Write-Host "1. 🔄 Reset Database (DANGEROUS)" -ForegroundColor Red
    Write-Host "2. 🧹 Clear Logs" -ForegroundColor White
    Write-Host "3. 🔐 Update Security Settings" -ForegroundColor White
    Write-Host "4. 📝 View Environment Config" -ForegroundColor White
    Write-Host "5. 🔧 Install Dependencies" -ForegroundColor White
    Write-Host "6. 📊 Performance Test" -ForegroundColor White
    Write-Host "7. Return to Main Menu" -ForegroundColor Gray
    
    $choice = Read-Host "`nSelect option (1-7)"
    
    switch ($choice) {
        "1" { 
            $confirm = Read-Host "⚠️  This will reset ALL data. Type 'RESET' to confirm"
            if ($confirm -eq "RESET") {
                npm run db:setup
            } else {
                Write-Host "❌ Reset cancelled" -ForegroundColor Green
            }
        }
        "2" { 
            if (Test-Path "logs") {
                Remove-Item "logs\*" -Recurse -Force
                Write-Host "✅ Logs cleared" -ForegroundColor Green
            }
        }
        "3" { 
            Write-Host "🔐 Security settings in .env file" -ForegroundColor Cyan
            if (Test-Path ".env") {
                Get-Content ".env" | Where-Object { $_ -match "SECURITY|TOKEN|SECRET" }
            }
        }
        "4" { 
            if (Test-Path ".env") {
                Write-Host "📝 Environment Configuration:" -ForegroundColor Cyan
                Get-Content ".env"
            }
        }
        "5" { 
            Write-Host "🔧 Installing dependencies..." -ForegroundColor Cyan
            npm install
        }
        "6" { 
            Write-Host "📊 Running performance test..." -ForegroundColor Cyan
            npm run test
        }
        "7" { return }
        default { Write-Host "❌ Invalid option" -ForegroundColor Red }
    }
}

# Main menu loop
do {
    Show-Menu
    $choice = Read-Host "`nSelect option (0-9)"
    
    switch ($choice) {
        "1" { Start-DatabaseConsole }
        "2" { Check-DatabaseStatus }
        "3" { Fix-DatabaseIssues }
        "4" { Show-DatabaseStats }
        "5" { Manage-Modules }
        "6" { Backup-Database }
        "7" { npm run db:sample-data }
        "8" { npm run db:list-tables }
        "9" { Show-AdvancedOptions }
        "0" { 
            Write-Host "`n👋 Goodbye! Thank you for using Chahua Database Console" -ForegroundColor Cyan
            break 
        }
        default { 
            Write-Host "❌ Invalid option. Please select 0-9" -ForegroundColor Red 
        }
    }
    
    if ($choice -ne "0") {
        Write-Host "`nPress any key to continue..." -ForegroundColor Gray
        $null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
    }
} while ($choice -ne "0")