Skip to content

fix: Handle missing 'database' key when migrating from Appwrite Cloud#172

Open
vickysawant19 wants to merge 1 commit intoutopia-php:mainfrom
vickysawant19:fix/cloud-to-self-hosted-migration
Open

fix: Handle missing 'database' key when migrating from Appwrite Cloud#172
vickysawant19 wants to merge 1 commit intoutopia-php:mainfrom
vickysawant19:fix/cloud-to-self-hosted-migration

Conversation

@vickysawant19
Copy link
Copy Markdown

@vickysawant19 vickysawant19 commented Apr 23, 2026

Summary

When migrating databases from Appwrite Cloud to a self-hosted Appwrite instance, the migration worker crashes with a PHP warning:
Warning: Undefined array key "database" in .../Migration/Sources/Appwrite.php on line 868

Root Cause

In exportDatabases(), the code builds a database object using the raw API response from Appwrite Cloud:

$newDatabase = self::getDatabase($databaseFamilyType, [
    ...
    'database' => $database['database'], // crashes if key absent
    ...
]);

Newer versions of the Appwrite Cloud API do not return the internal database key in the database listing response. This causes a fatal PHP warning that halts the migration.

Fix

Updated the logic to use null instead of an empty string '' for the database field when it's missing from the source. This is crucial because:

  • An empty string '' causes Appwrite 1.9.0 to attempt parsing an invalid DSN (e.g., mysql://), leading to HTTP 500 errors.
    • Using null allows Appwrite's internal getAttribute() logic to correctly trigger the default fallback to the internal connection pool (e.g., database_db_main).
'database' => $database['database'] ?? null,

Testing

Tested successfully by performing a full Cloud to self-hosted migration:
1 database migrated
15 tables migrated
141 columns migrated
11,791 rows migrated

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Apr 23, 2026

Greptile Summary

Adds a ?? null null-coalescing fallback for the database key in exportDatabases(), preventing a PHP warning when the Appwrite Cloud API omits that field. The chosen null fallback is consistent with Database::fromArray()'s own default for this field, addressing the prior review concern. Note: the PR description mentions ?? '' as the fix, but the actual committed code correctly uses ?? null.

Confidence Score: 5/5

Safe to merge — minimal, targeted fix with correct semantics.

The change is a single-line null-coalescing guard with the correct null default (consistent with how Database::fromArray() handles the same field). No P0/P1 issues remain.

No files require special attention.

Important Files Changed

Filename Overview
src/Migration/Sources/Appwrite.php Single-line null-coalescing fix for missing 'database' key in API response; uses null fallback consistent with Database::fromArray() defaults.

Reviews (2): Last reviewed commit: "Fix: Handle missing 'database' key in Ap..." | Re-trigger Greptile

Comment thread src/Migration/Sources/Appwrite.php Outdated
@vickysawant19 vickysawant19 changed the title fix: Handle missing 'database' key in Appwrite source to prevent Undefined array key crash fix: Handle missing 'database' key when migrating from Appwrite Cloud Apr 23, 2026
@vickysawant19 vickysawant19 force-pushed the fix/cloud-to-self-hosted-migration branch from 16e5090 to 5c55df0 Compare April 23, 2026 07:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant