-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_db_python.ps1
More file actions
81 lines (69 loc) · 2.68 KB
/
create_db_python.ps1
File metadata and controls
81 lines (69 loc) · 2.68 KB
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
# Alternative: Create database using Python (no psql needed)
Write-Host "Creating database using Python..." -ForegroundColor Cyan
Write-Host ""
$password = Read-Host "Enter PostgreSQL password for user 'postgres' (or press Enter for default 'postgres')"
if ([string]::IsNullOrWhiteSpace($password)) {
$password = "postgres"
}
Write-Host "Attempting to create database 'code_review_bot'..." -ForegroundColor Yellow
# Python script to create database
$pythonScript = @"
import asyncpg
import asyncio
import sys
async def create_database():
try:
# Connect to postgres database (default database)
conn = await asyncpg.connect(
host='localhost',
port=5432,
user='postgres',
password='$password',
database='postgres'
)
# Check if database exists
exists = await conn.fetchval(
"SELECT 1 FROM pg_database WHERE datname = 'code_review_bot'"
)
if exists:
print('[OK] Database code_review_bot already exists')
await conn.close()
return True
# Create database
await conn.execute('CREATE DATABASE code_review_bot')
print('[OK] Database code_review_bot created successfully!')
await conn.close()
return True
except Exception as e:
print(f'[ERROR] Failed to create database: {e}')
return False
if __name__ == '__main__':
success = asyncio.run(create_database())
sys.exit(0 if success else 1)
"@
# Save script temporarily
$tempScript = Join-Path $env:TEMP "create_db_temp.py"
$pythonScript | Out-File -FilePath $tempScript -Encoding UTF8
try {
& python $tempScript
if ($LASTEXITCODE -eq 0) {
Write-Host ""
Write-Host "[OK] Database setup complete!" -ForegroundColor Green
Write-Host ""
Write-Host "Next steps:" -ForegroundColor Cyan
Write-Host " 1. Update .env file with your password:" -ForegroundColor Yellow
Write-Host " DATABASE_URL=postgresql+asyncpg://postgres:$password@localhost:5432/code_review_bot" -ForegroundColor White
Write-Host " 2. Run migrations: alembic upgrade head" -ForegroundColor Yellow
Write-Host " 3. Start app: python run.py" -ForegroundColor Yellow
} else {
Write-Host ""
Write-Host "[ERROR] Failed to create database. Please check:" -ForegroundColor Red
Write-Host " 1. PostgreSQL is running" -ForegroundColor Yellow
Write-Host " 2. Password is correct" -ForegroundColor Yellow
}
} finally {
# Clean up temp file
Remove-Item $tempScript -ErrorAction SilentlyContinue
}
Write-Host ""
Read-Host "Press Enter to exit"