Skip to content

Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.

fix(docker): fail fast when MySQL URL is missing

johavencadd3517d2be
diff --git a/backend/src/infrastructure/database/scripts/check-db.ts b/backend/src/infrastructure/database/scripts/check-db.ts
index b0c7a497..e1a19114 100644
--- a/backend/src/infrastructure/database/scripts/check-db.ts
+++ b/backend/src/infrastructure/database/scripts/check-db.ts
@@ -1,5 +1,5 @@
import { sql } from 'drizzle-orm'
-import { getDB } from './db'
+import { DatabaseConfigurationError, getDB } from './db'
async function checkConnection() {
const db = await getDB()
@@ -14,6 +14,7 @@ async function checkConnection() {
checkConnection().catch((error: unknown) => {
const message = error instanceof Error ? error.message : String(error)
- console.error(`Database check failed: ${message}`)
- process.exitCode = 1
+ const isConfigurationError = error instanceof DatabaseConfigurationError
+ console.error(`${isConfigurationError ? 'Database configuration error' : 'Database check failed'}: ${message}`)
+ process.exitCode = isConfigurationError ? 2 : 1
})
diff --git a/backend/src/infrastructure/database/scripts/db.ts b/backend/src/infrastructure/database/scripts/db.ts
index 47738ddf..354994e5 100644
--- a/backend/src/infrastructure/database/scripts/db.ts
+++ b/backend/src/infrastructure/database/scripts/db.ts
@@ -3,7 +3,16 @@ import { createConnection } from 'mysql2/promise'
import { configLoader } from '../../../configuration/config.loader'
import * as schema from '../schema'
+export class DatabaseConfigurationError extends Error {}
+
export async function getDB() {
- const client = await createConnection(configLoader().mysql.url)
+ const mysqlUrl = configLoader().mysql?.url
+ if (typeof mysqlUrl !== 'string' || !mysqlUrl.trim()) {
+ throw new DatabaseConfigurationError(
+ 'MySQL URL is not configured. Define "mysql.url" in environment.yaml or set the SYNCIN_MYSQL_URL environment variable.'
+ )
+ }
+
+ const client = await createConnection(mysqlUrl)
return drizzle({ client, schema: { ...schema }, mode: 'default' })
}
diff --git a/scripts/docker-sync-in-server.sh b/scripts/docker-sync-in-server.sh
index 0a88daa7..89c3afa8 100755
--- a/scripts/docker-sync-in-server.sh
+++ b/scripts/docker-sync-in-server.sh
@@ -7,12 +7,20 @@ if [ "${SKIP_INIT}" != "true" ]; then
COUNT=0
CONNECTED=false
+ # Retry transient connection failures while the database is starting.
while [ $COUNT -lt $MAX_RETRIES ]; do
COUNT=$((COUNT+1))
echo "Database connection attempt ${COUNT}/${MAX_RETRIES}..."
if OUTPUT=$(node server/infrastructure/database/scripts/check-db.js 2>&1); then
CONNECTED=true
break
+ else
+ CHECK_EXIT_CODE=$?
+ # Exit code 2 indicates an invalid database configuration, which cannot be fixed by retrying.
+ if [ "$CHECK_EXIT_CODE" -eq 2 ]; then
+ echo "$OUTPUT" >&2
+ exit "$CHECK_EXIT_CODE"
+ fi
fi
sleep 1
done