-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapply-migrations.ps1
More file actions
66 lines (53 loc) · 2.33 KB
/
apply-migrations.ps1
File metadata and controls
66 lines (53 loc) · 2.33 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
# Apply Supabase Migrations
# This script applies all migrations to the Supabase database
# Set Supabase URL and Key from environment variables
$SUPABASE_URL = $env:VITE_SUPABASE_URL
$SUPABASE_KEY = $env:VITE_SUPABASE_ANON_KEY
if (-not $SUPABASE_URL -or -not $SUPABASE_KEY) {
Write-Error "Missing Supabase environment variables. Please set VITE_SUPABASE_URL and VITE_SUPABASE_ANON_KEY."
exit 1
}
Write-Host "Applying database migrations to Supabase..." -ForegroundColor Green
# Run migrations from the supabase/migrations directory
# Each migration is applied in order by filename
# Check if npx is available
try {
npx --version | Out-Null
} catch {
Write-Error "npx is not available. Please install Node.js and npm."
exit 1
}
# Use Supabase CLI to apply migrations if installed
try {
# Check if Supabase CLI is installed
supabase --version | Out-Null
Write-Host "Using Supabase CLI to apply migrations..." -ForegroundColor Green
supabase db push
} catch {
Write-Host "Supabase CLI not found. Trying alternative method..." -ForegroundColor Yellow
# Alternative method: Use direct HTTP requests to apply migrations
$migrations = Get-ChildItem -Path "supabase/migrations" -Filter "*.sql" | Sort-Object Name
foreach ($migration in $migrations) {
Write-Host "Applying migration: $($migration.Name)" -ForegroundColor Cyan
$sqlContent = Get-Content -Path $migration.FullName -Raw
# Use curl or Invoke-RestMethod to apply the migration
# This is a simplified example and may need adjustments based on your Supabase setup
$headers = @{
"apikey" = $SUPABASE_KEY
"Authorization" = "Bearer $SUPABASE_KEY"
"Content-Type" = "application/json"
}
$body = @{
"query" = $sqlContent
} | ConvertTo-Json
try {
Invoke-RestMethod -Uri "$SUPABASE_URL/rest/v1/rpc/exec_sql" -Method POST -Headers $headers -Body $body
Write-Host "Migration applied successfully: $($migration.Name)" -ForegroundColor Green
} catch {
Write-Host "Error applying migration: $($migration.Name)" -ForegroundColor Red
Write-Host $_.Exception.Message
exit 1
}
}
}
Write-Host "All migrations applied successfully!" -ForegroundColor Green