Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
76f45ca249 | ||
|
|
eba8ff4400 | ||
|
|
78fb6c2304 | ||
|
|
fef8483094 | ||
|
|
dcc72e9410 | ||
|
|
9c0988a5e5 | ||
|
|
47cba3fb20 | ||
|
|
fdd22387b6 |
No files matched your search
@@ -13,10 +13,22 @@ When making database schema changes, you must ensure that generated migrations a
|
||||
- **Add "IF EXISTS" clauses**: Prisma can sometimes produce brittle drop statements (`DROP INDEX "my_index"`). To prevent migrations from crashing if a state desynchronization occurs, manually edit the migration file to use `DROP INDEX IF EXISTS "my_index";` or `DROP TABLE IF EXISTS "table_name";`.
|
||||
- **Data Preservation**: When dropping columns or renaming, carefully review the migration to ensure data is correctly mapped. In PostgreSQL, ensure that cascaded drops don't accidentally remove required data.
|
||||
|
||||
## 2. Managing Failed Migrations
|
||||
## 2. P3005 Auto-Baseline (Handled in `docker-entrypoint.sh`)
|
||||
|
||||
`docker-entrypoint.sh` automatically recovers from Prisma **P3005** ("schema not empty, no migration history"). This happens when `pnpm dev` (which runs `prisma db push`) populates the database before the Docker stack is started.
|
||||
|
||||
The entrypoint only auto-baselines when **both** conditions hold:
|
||||
1. The error is exactly P3005.
|
||||
2. `prisma migrate diff` reports zero schema drift between the live DB and `prisma/schema.prisma`.
|
||||
|
||||
If drift is detected the container exits with a clear error message directing you to `pnpm db:reset`.
|
||||
|
||||
> **Never add manual `migrate resolve --applied` calls** unless you are outside the container and have independently verified the above two conditions. The entrypoint already handles the common case.
|
||||
|
||||
## 3. Managing Failed Migrations
|
||||
If a migration fails in production:
|
||||
- **Never blindly mark as resolved**: Do not run `prisma migrate resolve --applied <migration_name>` unless you have explicitly verified that the database schema precisely matches the expected state.
|
||||
- If you use `resolve --applied`, Prisma will permanently skip that migration in the future, leaving your database structurally out of sync with your Prisma Client.
|
||||
|
||||
## 3. Backups Before DDL
|
||||
## 4. Backups Before DDL
|
||||
Never perform direct DDL operations (ALTER, DROP, CREATE) on a production database without first confirming an automated backup exists, or manually taking a `pg_dump` backup of the database.
|
||||
+1
-1
@@ -29,7 +29,7 @@ services:
|
||||
db:
|
||||
condition: service_healthy
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "wget --no-verbose --tries=1 --spider http://localhost:3000/api/health || exit 1"]
|
||||
test: ["CMD-SHELL", "wget --no-verbose --tries=1 --spider http://127.0.0.1:3000/api/health || exit 1"]
|
||||
interval: 30s
|
||||
timeout: 10s
|
||||
retries: 3
|
||||
|
||||
+54
-2
@@ -29,8 +29,60 @@ if [ -z "$DATABASE_URL" ]; then
|
||||
fi
|
||||
|
||||
echo "[entrypoint] Running database migrations..."
|
||||
prisma migrate deploy
|
||||
echo "[entrypoint] Migrations complete."
|
||||
set +e
|
||||
MIGRATE_OUT=$(prisma migrate deploy 2>&1)
|
||||
MIGRATE_CODE=$?
|
||||
set -e
|
||||
|
||||
if [ $MIGRATE_CODE -eq 0 ]; then
|
||||
echo "[entrypoint] Migrations complete."
|
||||
elif echo "$MIGRATE_OUT" | grep -q "P3005"; then
|
||||
# P3005: schema exists but _prisma_migrations has no history.
|
||||
# Happens when 'prisma db push' (pnpm dev) ran before 'prisma migrate deploy' (docker).
|
||||
# Safe to auto-baseline only if there is zero schema drift.
|
||||
echo "[entrypoint] P3005: schema exists without migration history."
|
||||
echo "[entrypoint] Verifying schema matches migrations before auto-baselining..."
|
||||
set +e
|
||||
DIFF_OUT=$(prisma migrate diff \
|
||||
--from-url "$DATABASE_URL" \
|
||||
--to-schema-datamodel prisma/schema.prisma \
|
||||
--script 2>&1)
|
||||
DIFF_CODE=$?
|
||||
set -e
|
||||
if [ $DIFF_CODE -ne 0 ]; then
|
||||
echo "[entrypoint] Could not verify schema state — aborting." >&2
|
||||
echo "$DIFF_OUT" >&2
|
||||
exit 1
|
||||
fi
|
||||
# Positive-match for DDL keywords: any real drift emits at least one of these.
|
||||
# Checking for presence is safer than checking for absence of content.
|
||||
if echo "$DIFF_OUT" | grep -qiE "^(CREATE|ALTER|DROP|INSERT|UPDATE|DELETE|TRUNCATE)"; then
|
||||
echo "[entrypoint] Schema drift detected — cannot auto-baseline." >&2
|
||||
echo "[entrypoint] Run 'pnpm db:reset' to wipe and re-migrate, or resolve drift manually." >&2
|
||||
echo "$MIGRATE_OUT" >&2
|
||||
exit 1
|
||||
fi
|
||||
echo "[entrypoint] No drift detected. Baselining existing schema..."
|
||||
for migration_dir in prisma/migrations/*/; do
|
||||
if [ -f "${migration_dir}migration.sql" ]; then
|
||||
migration_name=$(basename "$migration_dir")
|
||||
echo "[entrypoint] Marking as applied: $migration_name"
|
||||
set +e
|
||||
prisma migrate resolve --applied "$migration_name"
|
||||
RESOLVE_CODE=$?
|
||||
set -e
|
||||
if [ $RESOLVE_CODE -ne 0 ]; then
|
||||
echo "[entrypoint] Failed to baseline $migration_name — aborting." >&2
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
done
|
||||
prisma migrate deploy
|
||||
echo "[entrypoint] Migrations complete."
|
||||
else
|
||||
echo "$MIGRATE_OUT" >&2
|
||||
exit $MIGRATE_CODE
|
||||
fi
|
||||
|
||||
# Generate self-signed SSL certificates for local HTTPS bridging if they don't exist
|
||||
if [ ! -f "./data/localhost.crt" ] || [ ! -f "./data/localhost.key" ]; then
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "worldwideview",
|
||||
"version": "2.17.7",
|
||||
"version": "2.17.8",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"packageManager": "[email protected]",
|
||||
|
||||
@@ -18,7 +18,7 @@ services:
|
||||
db:
|
||||
condition: service_healthy
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "wget --no-verbose --tries=1 --spider http://localhost:3000/api/health || exit 1"]
|
||||
test: ["CMD-SHELL", "wget --no-verbose --tries=1 --spider http://127.0.0.1:3000/api/health || exit 1"]
|
||||
interval: 30s
|
||||
timeout: 10s
|
||||
retries: 3
|
||||
|
||||
+11
-6
@@ -25,15 +25,20 @@ Invoke-WebRequest -Uri "https://raw.githubusercontent.com/silvertakana/worldwide
|
||||
|
||||
# Generate .env
|
||||
if (-Not (Test-Path .env)) {
|
||||
Write-Host "[*] Generating new .env file with AUTH_SECRET..."
|
||||
$bytes = New-Object Byte[] 32
|
||||
[System.Security.Cryptography.RandomNumberGenerator]::Create().GetBytes($bytes)
|
||||
$secret = -join ($bytes | ForEach-Object { $_.ToString("x2") })
|
||||
Write-Host "[*] Generating new .env file with secrets..."
|
||||
$rng = [System.Security.Cryptography.RandomNumberGenerator]::Create()
|
||||
$authBytes = New-Object Byte[] 32
|
||||
$rng.GetBytes($authBytes)
|
||||
$authSecret = -join ($authBytes | ForEach-Object { $_.ToString("x2") })
|
||||
$encBytes = New-Object Byte[] 16
|
||||
$rng.GetBytes($encBytes)
|
||||
$encKey = -join ($encBytes | ForEach-Object { $_.ToString("x2") })
|
||||
Write-Host "[*] Downloading .env template..."
|
||||
Invoke-WebRequest -Uri "https://raw.githubusercontent.com/silvertakana/worldwideview/main/.env.example" -OutFile .env
|
||||
|
||||
|
||||
$envContent = Get-Content .env -Raw
|
||||
$envContent = $envContent -replace "(?m)^AUTH_SECRET=.*", "AUTH_SECRET=$secret"
|
||||
$envContent = $envContent -replace "(?m)^AUTH_SECRET=.*", "AUTH_SECRET=$authSecret"
|
||||
$envContent = $envContent -replace "(?m)^ENCRYPTION_MASTER_KEY=.*", "ENCRYPTION_MASTER_KEY=$encKey"
|
||||
$envContent | Out-File -FilePath .env -Encoding utf8
|
||||
} else {
|
||||
Write-Host "[Success] .env already exists, skipping generation."
|
||||
|
||||
+8
-9
@@ -21,21 +21,20 @@ fi
|
||||
echo "📦 Downloading docker-compose.yml..."
|
||||
curl -fsSL -o docker-compose.yml https://raw.githubusercontent.com/silvertakana/worldwideview/main/self-host/docker-compose.yml
|
||||
|
||||
# 2. Generate .env with a persistent secret
|
||||
# 2. Generate .env with persistent secrets
|
||||
if [ ! -f .env ]; then
|
||||
echo "🔐 Generating new .env file with AUTH_SECRET..."
|
||||
# Use openssl if available, otherwise use urandom
|
||||
echo "🔐 Generating new .env file with secrets..."
|
||||
if command -v openssl &> /dev/null; then
|
||||
SECRET=$(openssl rand -hex 32)
|
||||
AUTH_SECRET=$(openssl rand -hex 32)
|
||||
ENCRYPTION_MASTER_KEY=$(openssl rand -hex 16)
|
||||
else
|
||||
SECRET=$(head -c 32 /dev/urandom | xxd -p)
|
||||
AUTH_SECRET=$(head -c 32 /dev/urandom | xxd -p)
|
||||
ENCRYPTION_MASTER_KEY=$(head -c 16 /dev/urandom | xxd -p)
|
||||
fi
|
||||
echo "📥 Downloading .env template..."
|
||||
curl -fsSL -o .env https://raw.githubusercontent.com/silvertakana/worldwideview/main/.env.example
|
||||
|
||||
# Replace AUTH_SECRET= with the generated secret
|
||||
# (macOS sed requires an empty string backup extension, Linux does not. Perl is safer across platforms)
|
||||
perl -pi -e "s/^AUTH_SECRET=.*$/AUTH_SECRET=$SECRET/" .env
|
||||
perl -pi -e "s/^AUTH_SECRET=.*$/AUTH_SECRET=$AUTH_SECRET/" .env
|
||||
perl -pi -e "s/^ENCRYPTION_MASTER_KEY=.*$/ENCRYPTION_MASTER_KEY=$ENCRYPTION_MASTER_KEY/" .env
|
||||
else
|
||||
echo "✅ .env already exists, skipping generation."
|
||||
fi
|
||||
|
||||
Reference in new issue
Block a user