diff --git a/composer.json b/composer.json index 5585f2a53f..6bd6404197 100644 --- a/composer.json +++ b/composer.json @@ -22,12 +22,13 @@ "check": "./vendor/bin/phpstan analyse --level 3 src tests --memory-limit 2G" }, "require": { - "php": ">=8.1", + "php": ">=8.2", "ext-curl": "*", "ext-openssl": "*", - "appwrite/appwrite": "19.*", + + "appwrite/appwrite": "23.*", "utopia-php/database": "5.*", - "utopia-php/storage": "1.0.*", + "utopia-php/storage": "2.*", "utopia-php/dsn": "0.2.*", "halaxa/json-machine": "^1.2" }, @@ -38,9 +39,6 @@ "laravel/pint": "1.*", "phpstan/phpstan": "1.*" }, - "platform": { - "php": "8.1" - }, "config": { "allow-plugins": { "php-http/discovery": true, diff --git a/composer.lock b/composer.lock index 17d1422175..ab0a68c73c 100644 --- a/composer.lock +++ b/composer.lock @@ -4,29 +4,29 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "37980b9001fbbd4f213f3102c1332727", + "content-hash": "44746ecb1183e23d963fc90b1481541a", "packages": [ { "name": "appwrite/appwrite", - "version": "19.1.0", + "version": "23.1.0", "source": { "type": "git", "url": "https://github.com/appwrite/sdk-for-php.git", - "reference": "8738e812062f899c85b2598eef43d6a247f08a56" + "reference": "2f275921f10ceb7cff99f2d463f7328b296234fa" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/appwrite/sdk-for-php/zipball/8738e812062f899c85b2598eef43d6a247f08a56", - "reference": "8738e812062f899c85b2598eef43d6a247f08a56", + "url": "https://api.github.com/repos/appwrite/sdk-for-php/zipball/2f275921f10ceb7cff99f2d463f7328b296234fa", + "reference": "2f275921f10ceb7cff99f2d463f7328b296234fa", "shasum": "" }, "require": { "ext-curl": "*", "ext-json": "*", - "php": ">=7.1.0" + "php": ">=8.2.0" }, "require-dev": { - "mockery/mockery": "^1.6.12", + "mockery/mockery": "1.6.12", "phpunit/phpunit": "^10" }, "type": "library", @@ -39,14 +39,14 @@ "license": [ "BSD-3-Clause" ], - "description": "Appwrite is an open-source self-hosted backend server that abstract and simplify complex and repetitive development tasks behind a very simple REST API", + "description": "Appwrite is an open-source self-hosted backend server that abstracts and simplifies complex and repetitive development tasks behind a very simple REST API", "support": { "email": "team@appwrite.io", "issues": "https://github.com/appwrite/sdk-for-php/issues", - "source": "https://github.com/appwrite/sdk-for-php/tree/19.1.0", + "source": "https://github.com/appwrite/sdk-for-php/tree/23.1.0", "url": "https://appwrite.io/support" }, - "time": "2025-12-18T08:07:43+00:00" + "time": "2026-05-08T13:44:58+00:00" }, { "name": "brick/math", @@ -187,23 +187,23 @@ }, { "name": "google/protobuf", - "version": "v4.33.5", + "version": "v4.33.6", "source": { "type": "git", "url": "https://github.com/protocolbuffers/protobuf-php.git", - "reference": "ebe8010a61b2ae0cff0d246fe1c4d44e9f7dfa6d" + "reference": "84b008c23915ed94536737eae46f41ba3bccfe67" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/protocolbuffers/protobuf-php/zipball/ebe8010a61b2ae0cff0d246fe1c4d44e9f7dfa6d", - "reference": "ebe8010a61b2ae0cff0d246fe1c4d44e9f7dfa6d", + "url": "https://api.github.com/repos/protocolbuffers/protobuf-php/zipball/84b008c23915ed94536737eae46f41ba3bccfe67", + "reference": "84b008c23915ed94536737eae46f41ba3bccfe67", "shasum": "" }, "require": { "php": ">=8.1.0" }, "require-dev": { - "phpunit/phpunit": ">=5.0.0 <8.5.27" + "phpunit/phpunit": ">=10.5.62 <11.0.0" }, "suggest": { "ext-bcmath": "Need to support JSON deserialization" @@ -225,9 +225,9 @@ "proto" ], "support": { - "source": "https://github.com/protocolbuffers/protobuf-php/tree/v4.33.5" + "source": "https://github.com/protocolbuffers/protobuf-php/tree/v4.33.6" }, - "time": "2026-01-29T20:49:00+00:00" + "time": "2026-03-18T17:32:05+00:00" }, { "name": "halaxa/json-machine", @@ -514,16 +514,16 @@ }, { "name": "open-telemetry/api", - "version": "1.8.0", + "version": "1.9.0", "source": { "type": "git", "url": "https://github.com/opentelemetry-php/api.git", - "reference": "df5197c6fd0ddd8e9883b87de042d9341300e2ad" + "reference": "6f8d237ce2c304ca85f31970f788e7f074d147be" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/opentelemetry-php/api/zipball/df5197c6fd0ddd8e9883b87de042d9341300e2ad", - "reference": "df5197c6fd0ddd8e9883b87de042d9341300e2ad", + "url": "https://api.github.com/repos/opentelemetry-php/api/zipball/6f8d237ce2c304ca85f31970f788e7f074d147be", + "reference": "6f8d237ce2c304ca85f31970f788e7f074d147be", "shasum": "" }, "require": { @@ -580,20 +580,20 @@ "issues": "https://github.com/open-telemetry/opentelemetry-php/issues", "source": "https://github.com/open-telemetry/opentelemetry-php" }, - "time": "2026-01-21T04:14:03+00:00" + "time": "2026-02-25T13:24:05+00:00" }, { "name": "open-telemetry/context", - "version": "1.4.0", + "version": "1.5.0", "source": { "type": "git", "url": "https://github.com/opentelemetry-php/context.git", - "reference": "d4c4470b541ce72000d18c339cfee633e4c8e0cf" + "reference": "3c414b246e0dabb7d6145404e6a5e4536ca18d07" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/opentelemetry-php/context/zipball/d4c4470b541ce72000d18c339cfee633e4c8e0cf", - "reference": "d4c4470b541ce72000d18c339cfee633e4c8e0cf", + "url": "https://api.github.com/repos/opentelemetry-php/context/zipball/3c414b246e0dabb7d6145404e6a5e4536ca18d07", + "reference": "3c414b246e0dabb7d6145404e6a5e4536ca18d07", "shasum": "" }, "require": { @@ -635,11 +635,11 @@ ], "support": { "chat": "https://app.slack.com/client/T08PSQ7BQ/C01NFPCV44V", - "docs": "https://opentelemetry.io/docs/php", + "docs": "https://opentelemetry.io/docs/languages/php", "issues": "https://github.com/open-telemetry/opentelemetry-php/issues", "source": "https://github.com/open-telemetry/opentelemetry-php" }, - "time": "2025-09-19T00:05:49+00:00" + "time": "2025-10-19T06:44:33+00:00" }, { "name": "open-telemetry/exporter-otlp", @@ -707,16 +707,16 @@ }, { "name": "open-telemetry/gen-otlp-protobuf", - "version": "1.8.0", + "version": "1.9.0", "source": { "type": "git", "url": "https://github.com/opentelemetry-php/gen-otlp-protobuf.git", - "reference": "673af5b06545b513466081884b47ef15a536edde" + "reference": "a229cf161d42001d64c8f21e8f678581fe1c66b9" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/opentelemetry-php/gen-otlp-protobuf/zipball/673af5b06545b513466081884b47ef15a536edde", - "reference": "673af5b06545b513466081884b47ef15a536edde", + "url": "https://api.github.com/repos/opentelemetry-php/gen-otlp-protobuf/zipball/a229cf161d42001d64c8f21e8f678581fe1c66b9", + "reference": "a229cf161d42001d64c8f21e8f678581fe1c66b9", "shasum": "" }, "require": { @@ -762,30 +762,30 @@ ], "support": { "chat": "https://app.slack.com/client/T08PSQ7BQ/C01NFPCV44V", - "docs": "https://opentelemetry.io/docs/php", + "docs": "https://opentelemetry.io/docs/languages/php", "issues": "https://github.com/open-telemetry/opentelemetry-php/issues", "source": "https://github.com/open-telemetry/opentelemetry-php" }, - "time": "2025-09-17T23:10:12+00:00" + "time": "2025-10-19T06:44:33+00:00" }, { "name": "open-telemetry/sdk", - "version": "1.13.0", + "version": "1.14.0", "source": { "type": "git", "url": "https://github.com/opentelemetry-php/sdk.git", - "reference": "c76f91203bf7ef98ab3f4e0a82ca21699af185e1" + "reference": "6e3d0ce93e76555dd5e2f1d19443ff45b990e410" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/opentelemetry-php/sdk/zipball/c76f91203bf7ef98ab3f4e0a82ca21699af185e1", - "reference": "c76f91203bf7ef98ab3f4e0a82ca21699af185e1", + "url": "https://api.github.com/repos/opentelemetry-php/sdk/zipball/6e3d0ce93e76555dd5e2f1d19443ff45b990e410", + "reference": "6e3d0ce93e76555dd5e2f1d19443ff45b990e410", "shasum": "" }, "require": { "ext-json": "*", "nyholm/psr7-server": "^1.1", - "open-telemetry/api": "^1.7", + "open-telemetry/api": "^1.8", "open-telemetry/context": "^1.4", "open-telemetry/sem-conv": "^1.0", "php": "^8.1", @@ -863,7 +863,7 @@ "issues": "https://github.com/open-telemetry/opentelemetry-php/issues", "source": "https://github.com/open-telemetry/opentelemetry-php" }, - "time": "2026-01-28T11:38:11+00:00" + "time": "2026-03-21T11:50:01+00:00" }, { "name": "open-telemetry/sem-conv", @@ -1420,16 +1420,16 @@ }, { "name": "symfony/deprecation-contracts", - "version": "v3.6.0", + "version": "v3.7.0", "source": { "type": "git", "url": "https://github.com/symfony/deprecation-contracts.git", - "reference": "63afe740e99a13ba87ec199bb07bbdee937a5b62" + "reference": "50f59d1f3ca46d41ac911f97a78626b6756af35b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/deprecation-contracts/zipball/63afe740e99a13ba87ec199bb07bbdee937a5b62", - "reference": "63afe740e99a13ba87ec199bb07bbdee937a5b62", + "url": "https://api.github.com/repos/symfony/deprecation-contracts/zipball/50f59d1f3ca46d41ac911f97a78626b6756af35b", + "reference": "50f59d1f3ca46d41ac911f97a78626b6756af35b", "shasum": "" }, "require": { @@ -1442,7 +1442,7 @@ "name": "symfony/contracts" }, "branch-alias": { - "dev-main": "3.6-dev" + "dev-main": "3.7-dev" } }, "autoload": { @@ -1467,7 +1467,7 @@ "description": "A generic function and convention to trigger deprecation notices", "homepage": "https://symfony.com", "support": { - "source": "https://github.com/symfony/deprecation-contracts/tree/v3.6.0" + "source": "https://github.com/symfony/deprecation-contracts/tree/v3.7.0" }, "funding": [ { @@ -1478,25 +1478,29 @@ "url": "https://github.com/fabpot", "type": "github" }, + { + "url": "https://github.com/nicolas-grekas", + "type": "github" + }, { "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", "type": "tidelift" } ], - "time": "2024-09-25T14:21:43+00:00" + "time": "2026-04-13T15:52:40+00:00" }, { "name": "symfony/http-client", - "version": "v7.4.5", + "version": "v7.4.9", "source": { "type": "git", "url": "https://github.com/symfony/http-client.git", - "reference": "84bb634857a893cc146cceb467e31b3f02c5fe9f" + "reference": "7e941c6abf4e3bf7dca160bf0e11ef36a9f832f6" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/http-client/zipball/84bb634857a893cc146cceb467e31b3f02c5fe9f", - "reference": "84bb634857a893cc146cceb467e31b3f02c5fe9f", + "url": "https://api.github.com/repos/symfony/http-client/zipball/7e941c6abf4e3bf7dca160bf0e11ef36a9f832f6", + "reference": "7e941c6abf4e3bf7dca160bf0e11ef36a9f832f6", "shasum": "" }, "require": { @@ -1564,7 +1568,7 @@ "http" ], "support": { - "source": "https://github.com/symfony/http-client/tree/v7.4.5" + "source": "https://github.com/symfony/http-client/tree/v7.4.9" }, "funding": [ { @@ -1584,20 +1588,20 @@ "type": "tidelift" } ], - "time": "2026-01-27T16:16:02+00:00" + "time": "2026-04-29T13:25:15+00:00" }, { "name": "symfony/http-client-contracts", - "version": "v3.6.0", + "version": "v3.7.0", "source": { "type": "git", "url": "https://github.com/symfony/http-client-contracts.git", - "reference": "75d7043853a42837e68111812f4d964b01e5101c" + "reference": "4a2d00c37651c0bdc2b9e1c773487a8bf4edb12d" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/http-client-contracts/zipball/75d7043853a42837e68111812f4d964b01e5101c", - "reference": "75d7043853a42837e68111812f4d964b01e5101c", + "url": "https://api.github.com/repos/symfony/http-client-contracts/zipball/4a2d00c37651c0bdc2b9e1c773487a8bf4edb12d", + "reference": "4a2d00c37651c0bdc2b9e1c773487a8bf4edb12d", "shasum": "" }, "require": { @@ -1610,7 +1614,7 @@ "name": "symfony/contracts" }, "branch-alias": { - "dev-main": "3.6-dev" + "dev-main": "3.7-dev" } }, "autoload": { @@ -1646,7 +1650,7 @@ "standards" ], "support": { - "source": "https://github.com/symfony/http-client-contracts/tree/v3.6.0" + "source": "https://github.com/symfony/http-client-contracts/tree/v3.7.0" }, "funding": [ { @@ -1657,25 +1661,29 @@ "url": "https://github.com/fabpot", "type": "github" }, + { + "url": "https://github.com/nicolas-grekas", + "type": "github" + }, { "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", "type": "tidelift" } ], - "time": "2025-04-29T11:18:49+00:00" + "time": "2026-03-06T13:17:50+00:00" }, { "name": "symfony/polyfill-mbstring", - "version": "v1.33.0", + "version": "v1.37.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-mbstring.git", - "reference": "6d857f4d76bd4b343eac26d6b539585d2bc56493" + "reference": "6a21eb99c6973357967f6ce3708cd55a6bec6315" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/6d857f4d76bd4b343eac26d6b539585d2bc56493", - "reference": "6d857f4d76bd4b343eac26d6b539585d2bc56493", + "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/6a21eb99c6973357967f6ce3708cd55a6bec6315", + "reference": "6a21eb99c6973357967f6ce3708cd55a6bec6315", "shasum": "" }, "require": { @@ -1727,7 +1735,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-mbstring/tree/v1.33.0" + "source": "https://github.com/symfony/polyfill-mbstring/tree/v1.37.0" }, "funding": [ { @@ -1747,20 +1755,20 @@ "type": "tidelift" } ], - "time": "2024-12-23T08:48:59+00:00" + "time": "2026-04-10T17:25:58+00:00" }, { "name": "symfony/polyfill-php82", - "version": "v1.33.0", + "version": "v1.37.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-php82.git", - "reference": "5d2ed36f7734637dacc025f179698031951b1692" + "reference": "34808efe3e68f69685796f7c253a2f1d8ea9df59" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-php82/zipball/5d2ed36f7734637dacc025f179698031951b1692", - "reference": "5d2ed36f7734637dacc025f179698031951b1692", + "url": "https://api.github.com/repos/symfony/polyfill-php82/zipball/34808efe3e68f69685796f7c253a2f1d8ea9df59", + "reference": "34808efe3e68f69685796f7c253a2f1d8ea9df59", "shasum": "" }, "require": { @@ -1807,7 +1815,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-php82/tree/v1.33.0" + "source": "https://github.com/symfony/polyfill-php82/tree/v1.37.0" }, "funding": [ { @@ -1827,20 +1835,20 @@ "type": "tidelift" } ], - "time": "2024-09-09T11:45:10+00:00" + "time": "2026-04-10T16:19:22+00:00" }, { "name": "symfony/polyfill-php83", - "version": "v1.33.0", + "version": "v1.37.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-php83.git", - "reference": "17f6f9a6b1735c0f163024d959f700cfbc5155e5" + "reference": "3600c2cb22399e25bb226e4a135ce91eeb2a6149" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-php83/zipball/17f6f9a6b1735c0f163024d959f700cfbc5155e5", - "reference": "17f6f9a6b1735c0f163024d959f700cfbc5155e5", + "url": "https://api.github.com/repos/symfony/polyfill-php83/zipball/3600c2cb22399e25bb226e4a135ce91eeb2a6149", + "reference": "3600c2cb22399e25bb226e4a135ce91eeb2a6149", "shasum": "" }, "require": { @@ -1887,7 +1895,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-php83/tree/v1.33.0" + "source": "https://github.com/symfony/polyfill-php83/tree/v1.37.0" }, "funding": [ { @@ -1907,20 +1915,20 @@ "type": "tidelift" } ], - "time": "2025-07-08T02:45:35+00:00" + "time": "2026-04-10T17:25:58+00:00" }, { "name": "symfony/polyfill-php85", - "version": "v1.33.0", + "version": "v1.37.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-php85.git", - "reference": "d4e5fcd4ab3d998ab16c0db48e6cbb9a01993f91" + "reference": "fcfa4973a9917cef23f2e38774da74a2b7d115ee" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-php85/zipball/d4e5fcd4ab3d998ab16c0db48e6cbb9a01993f91", - "reference": "d4e5fcd4ab3d998ab16c0db48e6cbb9a01993f91", + "url": "https://api.github.com/repos/symfony/polyfill-php85/zipball/fcfa4973a9917cef23f2e38774da74a2b7d115ee", + "reference": "fcfa4973a9917cef23f2e38774da74a2b7d115ee", "shasum": "" }, "require": { @@ -1967,7 +1975,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-php85/tree/v1.33.0" + "source": "https://github.com/symfony/polyfill-php85/tree/v1.37.0" }, "funding": [ { @@ -1987,20 +1995,20 @@ "type": "tidelift" } ], - "time": "2025-06-23T16:12:55+00:00" + "time": "2026-04-26T13:10:57+00:00" }, { "name": "symfony/service-contracts", - "version": "v3.6.1", + "version": "v3.7.0", "source": { "type": "git", "url": "https://github.com/symfony/service-contracts.git", - "reference": "45112560a3ba2d715666a509a0bc9521d10b6c43" + "reference": "d25d82433a80eba6aa0e6c24b61d7370d99e444a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/service-contracts/zipball/45112560a3ba2d715666a509a0bc9521d10b6c43", - "reference": "45112560a3ba2d715666a509a0bc9521d10b6c43", + "url": "https://api.github.com/repos/symfony/service-contracts/zipball/d25d82433a80eba6aa0e6c24b61d7370d99e444a", + "reference": "d25d82433a80eba6aa0e6c24b61d7370d99e444a", "shasum": "" }, "require": { @@ -2018,7 +2026,7 @@ "name": "symfony/contracts" }, "branch-alias": { - "dev-main": "3.6-dev" + "dev-main": "3.7-dev" } }, "autoload": { @@ -2054,7 +2062,7 @@ "standards" ], "support": { - "source": "https://github.com/symfony/service-contracts/tree/v3.6.1" + "source": "https://github.com/symfony/service-contracts/tree/v3.7.0" }, "funding": [ { @@ -2074,7 +2082,7 @@ "type": "tidelift" } ], - "time": "2025-07-15T11:30:57+00:00" + "time": "2026-03-28T09:44:51+00:00" }, { "name": "tbachert/spi", @@ -2130,16 +2138,16 @@ }, { "name": "utopia-php/cache", - "version": "1.0.0", + "version": "1.0.2", "source": { "type": "git", "url": "https://github.com/utopia-php/cache.git", - "reference": "7068870c086a6aea16173563a26b93ef3e408439" + "reference": "d36f9050c39c02e09a7763389c9e71258e74af1f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/cache/zipball/7068870c086a6aea16173563a26b93ef3e408439", - "reference": "7068870c086a6aea16173563a26b93ef3e408439", + "url": "https://api.github.com/repos/utopia-php/cache/zipball/d36f9050c39c02e09a7763389c9e71258e74af1f", + "reference": "d36f9050c39c02e09a7763389c9e71258e74af1f", "shasum": "" }, "require": { @@ -2176,22 +2184,22 @@ ], "support": { "issues": "https://github.com/utopia-php/cache/issues", - "source": "https://github.com/utopia-php/cache/tree/1.0.0" + "source": "https://github.com/utopia-php/cache/tree/1.0.2" }, - "time": "2026-01-28T10:55:44+00:00" + "time": "2026-05-08T11:40:20+00:00" }, { - "name": "utopia-php/compression", - "version": "0.1.3", + "name": "utopia-php/console", + "version": "0.1.1", "source": { "type": "git", - "url": "https://github.com/utopia-php/compression.git", - "reference": "66f093557ba66d98245e562036182016c7dcfe8a" + "url": "https://github.com/utopia-php/console.git", + "reference": "d298e43960780e6d76e66de1228c75dc81220e3e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/compression/zipball/66f093557ba66d98245e562036182016c7dcfe8a", - "reference": "66f093557ba66d98245e562036182016c7dcfe8a", + "url": "https://api.github.com/repos/utopia-php/console/zipball/d298e43960780e6d76e66de1228c75dc81220e3e", + "reference": "d298e43960780e6d76e66de1228c75dc81220e3e", "shasum": "" }, "require": { @@ -2199,56 +2207,60 @@ }, "require-dev": { "laravel/pint": "1.2.*", + "phpstan/phpstan": "^1.10", "phpunit/phpunit": "^9.3", - "vimeo/psalm": "4.0.1" + "squizlabs/php_codesniffer": "^3.6", + "swoole/ide-helper": "4.8.8" }, "type": "library", "autoload": { "psr-4": { - "Utopia\\Compression\\": "src/Compression" + "Utopia\\": "src/" } }, "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], - "description": "A simple Compression library to handle file compression", + "description": "Console helpers for logging, prompting, and executing commands", "keywords": [ - "compression", - "framework", + "cli", + "console", "php", - "upf", + "terminal", "utopia" ], "support": { - "issues": "https://github.com/utopia-php/compression/issues", - "source": "https://github.com/utopia-php/compression/tree/0.1.3" + "issues": "https://github.com/utopia-php/console/issues", + "source": "https://github.com/utopia-php/console/tree/0.1.1" }, - "time": "2025-01-15T15:15:51+00:00" + "time": "2026-02-10T10:20:29+00:00" }, { "name": "utopia-php/database", - "version": "5.2.1", + "version": "5.7.0", "source": { "type": "git", "url": "https://github.com/utopia-php/database.git", - "reference": "adfdf201144353a1d2ce14bb197ab746079894e0" + "reference": "eb35e68f7f90932d5a60bd72e70158ae7a4e0511" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/database/zipball/adfdf201144353a1d2ce14bb197ab746079894e0", - "reference": "adfdf201144353a1d2ce14bb197ab746079894e0", + "url": "https://api.github.com/repos/utopia-php/database/zipball/eb35e68f7f90932d5a60bd72e70158ae7a4e0511", + "reference": "eb35e68f7f90932d5a60bd72e70158ae7a4e0511", "shasum": "" }, "require": { "ext-mbstring": "*", "ext-mongodb": "*", "ext-pdo": "*", + "ext-redis": "*", "php": ">=8.4", "utopia-php/cache": "1.*", - "utopia-php/framework": "0.33.*", + "utopia-php/console": "0.1.*", "utopia-php/mongo": "1.*", - "utopia-php/pools": "1.*" + "utopia-php/pools": "1.*", + "utopia-php/validators": "0.2.*" }, "require-dev": { "fakerphp/faker": "1.23.*", @@ -2258,7 +2270,7 @@ "phpunit/phpunit": "9.*", "rregeer/phpunit-coverage-check": "0.3.*", "swoole/ide-helper": "5.1.3", - "utopia-php/cli": "0.14.*" + "utopia-php/cli": "0.22.*" }, "type": "library", "autoload": { @@ -2280,9 +2292,9 @@ ], "support": { "issues": "https://github.com/utopia-php/database/issues", - "source": "https://github.com/utopia-php/database/tree/5.2.1" + "source": "https://github.com/utopia-php/database/tree/5.7.0" }, - "time": "2026-02-16T11:01:13+00:00" + "time": "2026-05-06T01:04:08+00:00" }, { "name": "utopia-php/dsn", @@ -2331,67 +2343,18 @@ }, "time": "2024-05-07T02:01:25+00:00" }, - { - "name": "utopia-php/framework", - "version": "0.33.39", - "source": { - "type": "git", - "url": "https://github.com/utopia-php/http.git", - "reference": "409a258814d664d3a50fa2f48b6695679334d30b" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/utopia-php/http/zipball/409a258814d664d3a50fa2f48b6695679334d30b", - "reference": "409a258814d664d3a50fa2f48b6695679334d30b", - "shasum": "" - }, - "require": { - "php": ">=8.3", - "utopia-php/compression": "0.1.*", - "utopia-php/telemetry": "0.2.*", - "utopia-php/validators": "0.2.*" - }, - "require-dev": { - "laravel/pint": "1.*", - "phpbench/phpbench": "1.*", - "phpstan/phpstan": "1.*", - "phpunit/phpunit": "9.*", - "swoole/ide-helper": "^6.0" - }, - "type": "library", - "autoload": { - "psr-4": { - "Utopia\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "description": "A simple, light and advanced PHP framework", - "keywords": [ - "framework", - "php", - "upf" - ], - "support": { - "issues": "https://github.com/utopia-php/http/issues", - "source": "https://github.com/utopia-php/http/tree/0.33.39" - }, - "time": "2026-02-11T06:33:42+00:00" - }, { "name": "utopia-php/mongo", - "version": "1.0.0", + "version": "1.1.0", "source": { "type": "git", "url": "https://github.com/utopia-php/mongo.git", - "reference": "45bedf36c2c946ec7a0a3e59b9f12f772de0b01d" + "reference": "73593682deee4696525a04e26524c1c1226e1530" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/mongo/zipball/45bedf36c2c946ec7a0a3e59b9f12f772de0b01d", - "reference": "45bedf36c2c946ec7a0a3e59b9f12f772de0b01d", + "url": "https://api.github.com/repos/utopia-php/mongo/zipball/73593682deee4696525a04e26524c1c1226e1530", + "reference": "73593682deee4696525a04e26524c1c1226e1530", "shasum": "" }, "require": { @@ -2437,22 +2400,22 @@ ], "support": { "issues": "https://github.com/utopia-php/mongo/issues", - "source": "https://github.com/utopia-php/mongo/tree/1.0.0" + "source": "https://github.com/utopia-php/mongo/tree/1.1.0" }, - "time": "2026-02-12T05:54:06+00:00" + "time": "2026-04-24T06:15:10+00:00" }, { "name": "utopia-php/pools", - "version": "1.0.2", + "version": "1.0.3", "source": { "type": "git", "url": "https://github.com/utopia-php/pools.git", - "reference": "b7d8dd00306cdd8bf3ff6f1dc90caeaf27dabeb1" + "reference": "74de7c5457a2c447f27e7ec4d72e8412a7d68c10" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/pools/zipball/b7d8dd00306cdd8bf3ff6f1dc90caeaf27dabeb1", - "reference": "b7d8dd00306cdd8bf3ff6f1dc90caeaf27dabeb1", + "url": "https://api.github.com/repos/utopia-php/pools/zipball/74de7c5457a2c447f27e7ec4d72e8412a7d68c10", + "reference": "74de7c5457a2c447f27e7ec4d72e8412a7d68c10", "shasum": "" }, "require": { @@ -2490,22 +2453,22 @@ ], "support": { "issues": "https://github.com/utopia-php/pools/issues", - "source": "https://github.com/utopia-php/pools/tree/1.0.2" + "source": "https://github.com/utopia-php/pools/tree/1.0.3" }, - "time": "2026-01-28T13:12:36+00:00" + "time": "2026-02-26T08:42:40+00:00" }, { "name": "utopia-php/storage", - "version": "1.0.0", + "version": "2.0.2", "source": { "type": "git", "url": "https://github.com/utopia-php/storage.git", - "reference": "f672e8865938e5d1d6dc3bd149ceba4e5582199e" + "reference": "64e132a3768e22243eda36fe4262da22fd204f3c" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/storage/zipball/f672e8865938e5d1d6dc3bd149ceba4e5582199e", - "reference": "f672e8865938e5d1d6dc3bd149ceba4e5582199e", + "url": "https://api.github.com/repos/utopia-php/storage/zipball/64e132a3768e22243eda36fe4262da22fd204f3c", + "reference": "64e132a3768e22243eda36fe4262da22fd204f3c", "shasum": "" }, "require": { @@ -2519,9 +2482,8 @@ "utopia-php/validators": "0.2.*" }, "require-dev": { - "laravel/pint": "1.2.*", - "phpunit/phpunit": "^9.3", - "vimeo/psalm": "4.0.1" + "laravel/pint": "^1.21", + "phpunit/phpunit": "^9.3" }, "type": "library", "autoload": { @@ -2543,22 +2505,22 @@ ], "support": { "issues": "https://github.com/utopia-php/storage/issues", - "source": "https://github.com/utopia-php/storage/tree/1.0.0" + "source": "https://github.com/utopia-php/storage/tree/2.0.2" }, - "time": "2026-02-17T04:37:10+00:00" + "time": "2026-05-01T15:06:16+00:00" }, { "name": "utopia-php/system", - "version": "0.10.0", + "version": "0.10.2", "source": { "type": "git", "url": "https://github.com/utopia-php/system.git", - "reference": "6441a9c180958a373e5ddb330264dd638539dfdb" + "reference": "04229a822b147c1abaf1a92fb42c2d7aad4625df" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/system/zipball/6441a9c180958a373e5ddb330264dd638539dfdb", - "reference": "6441a9c180958a373e5ddb330264dd638539dfdb", + "url": "https://api.github.com/repos/utopia-php/system/zipball/04229a822b147c1abaf1a92fb42c2d7aad4625df", + "reference": "04229a822b147c1abaf1a92fb42c2d7aad4625df", "shasum": "" }, "require": { @@ -2599,9 +2561,9 @@ ], "support": { "issues": "https://github.com/utopia-php/system/issues", - "source": "https://github.com/utopia-php/system/tree/0.10.0" + "source": "https://github.com/utopia-php/system/tree/0.10.2" }, - "time": "2025-10-15T19:12:00+00:00" + "time": "2026-05-05T14:33:41+00:00" }, { "name": "utopia-php/telemetry", @@ -2660,16 +2622,16 @@ }, { "name": "utopia-php/validators", - "version": "0.2.0", + "version": "0.2.2", "source": { "type": "git", "url": "https://github.com/utopia-php/validators.git", - "reference": "30b6030a5b100fc1dff34506e5053759594b2a20" + "reference": "5d7d494e64457cd4eb67fdcfd9481f2c89796aa6" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/validators/zipball/30b6030a5b100fc1dff34506e5053759594b2a20", - "reference": "30b6030a5b100fc1dff34506e5053759594b2a20", + "url": "https://api.github.com/repos/utopia-php/validators/zipball/5d7d494e64457cd4eb67fdcfd9481f2c89796aa6", + "reference": "5d7d494e64457cd4eb67fdcfd9481f2c89796aa6", "shasum": "" }, "require": { @@ -2699,9 +2661,9 @@ ], "support": { "issues": "https://github.com/utopia-php/validators/issues", - "source": "https://github.com/utopia-php/validators/tree/0.2.0" + "source": "https://github.com/utopia-php/validators/tree/0.2.2" }, - "time": "2026-01-13T09:16:51+00:00" + "time": "2026-04-27T16:30:24+00:00" } ], "packages-dev": [ @@ -2769,16 +2731,16 @@ }, { "name": "laravel/pint", - "version": "v1.27.1", + "version": "v1.29.1", "source": { "type": "git", "url": "https://github.com/laravel/pint.git", - "reference": "54cca2de13790570c7b6f0f94f37896bee4abcb5" + "reference": "0770e9b7fafd50d4586881d456d6eb41c9247a80" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/laravel/pint/zipball/54cca2de13790570c7b6f0f94f37896bee4abcb5", - "reference": "54cca2de13790570c7b6f0f94f37896bee4abcb5", + "url": "https://api.github.com/repos/laravel/pint/zipball/0770e9b7fafd50d4586881d456d6eb41c9247a80", + "reference": "0770e9b7fafd50d4586881d456d6eb41c9247a80", "shasum": "" }, "require": { @@ -2789,13 +2751,14 @@ "php": "^8.2.0" }, "require-dev": { - "friendsofphp/php-cs-fixer": "^3.93.1", - "illuminate/view": "^12.51.0", - "larastan/larastan": "^3.9.2", - "laravel-zero/framework": "^12.0.5", + "friendsofphp/php-cs-fixer": "^3.95.1", + "illuminate/view": "^12.56.0", + "larastan/larastan": "^3.9.6", + "laravel-zero/framework": "^12.1.0", "mockery/mockery": "^1.6.12", - "nunomaduro/termwind": "^2.3.3", - "pestphp/pest": "^3.8.5" + "nunomaduro/termwind": "^2.4.0", + "pestphp/pest": "^3.8.6", + "shipfastlabs/agent-detector": "^1.1.3" }, "bin": [ "builds/pint" @@ -2832,7 +2795,7 @@ "issues": "https://github.com/laravel/pint/issues", "source": "https://github.com/laravel/pint" }, - "time": "2026-02-10T20:00:20+00:00" + "time": "2026-04-20T15:26:14+00:00" }, { "name": "myclabs/deep-copy", @@ -3147,11 +3110,11 @@ }, { "name": "phpstan/phpstan", - "version": "1.12.32", + "version": "1.12.33", "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/2770dcdf5078d0b0d53f94317e06affe88419aa8", - "reference": "2770dcdf5078d0b0d53f94317e06affe88419aa8", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/37982d6fc7cbb746dda7773530cda557cdf119e1", + "reference": "37982d6fc7cbb746dda7773530cda557cdf119e1", "shasum": "" }, "require": { @@ -3196,7 +3159,7 @@ "type": "github" } ], - "time": "2025-09-30T10:16:31+00:00" + "time": "2026-02-28T20:30:03+00:00" }, { "name": "phpunit/php-code-coverage", @@ -3547,16 +3510,16 @@ }, { "name": "phpunit/phpunit", - "version": "11.5.53", + "version": "11.5.55", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "a997a653a82845f1240d73ee73a8a4e97e4b0607" + "reference": "adc7262fccc12de2b30f12a8aa0b33775d814f00" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/a997a653a82845f1240d73ee73a8a4e97e4b0607", - "reference": "a997a653a82845f1240d73ee73a8a4e97e4b0607", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/adc7262fccc12de2b30f12a8aa0b33775d814f00", + "reference": "adc7262fccc12de2b30f12a8aa0b33775d814f00", "shasum": "" }, "require": { @@ -3629,7 +3592,7 @@ "support": { "issues": "https://github.com/sebastianbergmann/phpunit/issues", "security": "https://github.com/sebastianbergmann/phpunit/security/policy", - "source": "https://github.com/sebastianbergmann/phpunit/tree/11.5.53" + "source": "https://github.com/sebastianbergmann/phpunit/tree/11.5.55" }, "funding": [ { @@ -3653,7 +3616,7 @@ "type": "tidelift" } ], - "time": "2026-02-10T12:28:25+00:00" + "time": "2026-02-18T12:37:06+00:00" }, { "name": "sebastian/cli-parser", @@ -4695,16 +4658,16 @@ }, { "name": "symfony/polyfill-ctype", - "version": "v1.33.0", + "version": "v1.37.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-ctype.git", - "reference": "a3cc8b044a6ea513310cbd48ef7333b384945638" + "reference": "141046a8f9477948ff284fa65be2095baafb94f2" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/a3cc8b044a6ea513310cbd48ef7333b384945638", - "reference": "a3cc8b044a6ea513310cbd48ef7333b384945638", + "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/141046a8f9477948ff284fa65be2095baafb94f2", + "reference": "141046a8f9477948ff284fa65be2095baafb94f2", "shasum": "" }, "require": { @@ -4754,7 +4717,7 @@ "portable" ], "support": { - "source": "https://github.com/symfony/polyfill-ctype/tree/v1.33.0" + "source": "https://github.com/symfony/polyfill-ctype/tree/v1.37.0" }, "funding": [ { @@ -4774,20 +4737,20 @@ "type": "tidelift" } ], - "time": "2024-09-09T11:45:10+00:00" + "time": "2026-04-10T16:19:22+00:00" }, { "name": "symfony/polyfill-php80", - "version": "v1.33.0", + "version": "v1.37.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-php80.git", - "reference": "0cc9dd0f17f61d8131e7df6b84bd344899fe2608" + "reference": "dfb55726c3a76ea3b6459fcfda1ec2d80a682411" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-php80/zipball/0cc9dd0f17f61d8131e7df6b84bd344899fe2608", - "reference": "0cc9dd0f17f61d8131e7df6b84bd344899fe2608", + "url": "https://api.github.com/repos/symfony/polyfill-php80/zipball/dfb55726c3a76ea3b6459fcfda1ec2d80a682411", + "reference": "dfb55726c3a76ea3b6459fcfda1ec2d80a682411", "shasum": "" }, "require": { @@ -4838,7 +4801,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-php80/tree/v1.33.0" + "source": "https://github.com/symfony/polyfill-php80/tree/v1.37.0" }, "funding": [ { @@ -4858,7 +4821,7 @@ "type": "tidelift" } ], - "time": "2025-01-02T08:10:11+00:00" + "time": "2026-04-10T16:19:22+00:00" }, { "name": "theseer/tokenizer", @@ -5001,12 +4964,12 @@ "prefer-stable": false, "prefer-lowest": false, "platform": { - "php": ">=8.1", + "php": ">=8.2", "ext-curl": "*", "ext-openssl": "*" }, "platform-dev": { "ext-pdo": "*" }, - "plugin-api-version": "2.6.0" + "plugin-api-version": "2.9.0" } diff --git a/src/Migration/Destinations/Appwrite.php b/src/Migration/Destinations/Appwrite.php index 32de97586b..d176bf6034 100644 --- a/src/Migration/Destinations/Appwrite.php +++ b/src/Migration/Destinations/Appwrite.php @@ -64,6 +64,29 @@ use Utopia\Migration\Transfer; class Appwrite extends Destination { + /** Names of the project-DB collections holding Appwrite schema metadata. */ + private const META_DATABASES = 'databases'; + private const META_ATTRIBUTES = 'attributes'; + private const META_INDEXES = 'indexes'; + + /** Attribute fields the SDK can't update in place (no per-type updateX endpoint exposes them); a change here forces drop+recreate. */ + private const ATTRIBUTE_IMMUTABLE_FIELDS = [ + 'type', + 'array', + 'signed', + 'format', + 'formatOptions', + 'filters', + ]; + + /** Relationship options fields the SDK can't update in place (only onDelete/newKey are SDK-reachable); a change here forces drop+recreate. */ + private const RELATIONSHIP_IMMUTABLE_FIELDS = [ + 'relationType', + 'twoWay', + 'twoWayKey', + 'relatedCollection', + ]; + protected Client $client; protected string $project; @@ -81,11 +104,47 @@ class Appwrite extends Destination */ protected $getDatabasesDB; + /** + * Resolves the DSN written into the destination's `_databases.database` + * for a migrated database. When the source and destination projects don't + * share the same DSN — e.g. one project is on a host the other isn't — + * pass a resolver so the destination metadata carries its own DSN instead + * of the source's. When unset, the attribute is left blank and the + * runtime falls back to the destination project's DSN at read time, which + * is safe for single-host single-type setups. + * + * @var (callable(Database $resource): string)|null + */ + protected $getDatabaseDSN; + /** * @var array */ private array $rowBuffer = []; + /** + * Overwrite-mode orphan tracking, keyed by (database, table). Orphans are + * destination keys not in `attributeKeys` / `indexKeys`. Entries removed + * after their cleanup runs so the end-of-run sweep only visits tables + * that had no rows. + * + * @var array, + * indexKeys: list, + * }> + */ + private array $orphansByTable = []; + + /** + * Two-way pairs already reconciled this run; partner pass short-circuits. + * + * @var array + */ + private array $processedTwoWayPairs = []; + /** * @param string $project * @param string $endpoint @@ -93,6 +152,8 @@ class Appwrite extends Destination * @param UtopiaDatabase $dbForProject * @param callable(UtopiaDocument $database):UtopiaDatabase $getDatabasesDB * @param array> $collectionStructure + * @param OnDuplicate $onDuplicate Behavior when a row with an existing $id is encountered. + * @param (callable(Database $resource): string)|null $getDatabaseDSN Resolver for the destination's `_databases.database` value. Pass when the destination project's DSN differs from the source's, so the destination row carries its own DSN instead of inheriting the source's. */ public function __construct( string $project, @@ -103,6 +164,8 @@ class Appwrite extends Destination protected array $collectionStructure, protected UtopiaDatabase $dbForPlatform, protected string $projectInternalId, + protected OnDuplicate $onDuplicate = OnDuplicate::Fail, + ?callable $getDatabaseDSN = null, ) { $this->project = $project; $this->endpoint = $endpoint; @@ -121,6 +184,43 @@ class Appwrite extends Destination $this->users = new Users($this->client); $this->getDatabasesDB = $getDatabasesDB; + $this->getDatabaseDSN = $getDatabaseDSN; + } + + /** + * Resolve the DSN written into the destination's `_databases.database`. + * Without a resolver, leave it blank — the source DSN must never be + * propagated as the default, since when source and destination DSNs + * differ propagation routes destination reads to the wrong host (the + * regression PR #151 introduced). + */ + private function resolveDestinationDsn(Database $resource): string + { + if ($this->getDatabaseDSN === null) { + return ''; + } + return ($this->getDatabaseDSN)($resource); + } + + /** Orphan cleanup runs only after a successful migration — a mid-run throw preserves the destination as-is. */ + #[Override] + public function run( + array $resources, + callable $callback, + string $rootResourceId = '', + string $rootResourceType = '', + ): void { + $this->resetRunState(); + parent::run($resources, $callback, $rootResourceId, $rootResourceType); + $this->cleanupOverwriteOrphans(); + } + + /** Per-run state must not leak across run() invocations on a reused instance. */ + private function resetRunState(): void + { + $this->rowBuffer = []; + $this->orphansByTable = []; + $this->processedTwoWayPairs = []; } public static function getName(): string @@ -175,6 +275,9 @@ class Appwrite extends Destination // Integrations Resource::TYPE_PLATFORM, + + // Backups + Resource::TYPE_BACKUP_POLICY, ]; } @@ -332,6 +435,7 @@ class Appwrite extends Destination Transfer::GROUP_MESSAGING => $this->importMessagingResource($resource), Transfer::GROUP_SITES => $this->importSiteResource($resource), Transfer::GROUP_INTEGRATIONS => $this->importIntegrationsResource($resource), + Transfer::GROUP_BACKUPS => $this->importBackupResource($resource), default => throw new \Exception('Invalid resource group', Exception::CODE_VALIDATION), }; } catch (\Throwable $e) { @@ -417,18 +521,58 @@ class Appwrite extends Destination $validator = new UID(); if (!$validator->isValid($resource->getId())) { - throw new Exception( + $resource->setStatus(Resource::STATUS_ERROR, $validator->getDescription()); + $this->addError(new Exception( resourceName: $resource->getName(), resourceGroup: $resource->getGroup(), resourceId: $resource->getId(), message: $validator->getDescription(), - ); + )); + return false; } $createdAt = $this->normalizeDateTime($resource->getCreatedAt()); $updatedAt = $this->normalizeDateTime($resource->getUpdatedAt(), $createdAt); - $database = $this->dbForProject->createDocument('databases', new UtopiaDocument([ + if ($this->onDuplicate !== OnDuplicate::Fail) { + $existing = $this->dbForProject->getDocument(self::META_DATABASES, $resource->getId()); + $action = $this->onDuplicate->resolveSchemaAction( + !$existing->isEmpty(), + $updatedAt, + $existing->getUpdatedAt(), + ); + // Spec match → skip work. Create excluded; nothing on dest to match against. + if ($action !== SchemaAction::Create && $this->databaseSpecMatches($existing, $resource)) { + $action = SchemaAction::Skip; + } + + $earlyReturn = match ($action) { + SchemaAction::Skip => (function () use ($resource, $existing): bool { + $resource->setSequence($existing->getSequence()); + $resource->setStatus(Resource::STATUS_SKIPPED, 'Already exists on destination'); + return false; + })(), + SchemaAction::Overwrite => (function () use ($resource, $existing, $updatedAt): bool { + $this->dbForProject->updateDocument(self::META_DATABASES, $existing->getId(), new UtopiaDocument([ + 'name' => $resource->getDatabaseName(), + 'search' => implode(' ', [$resource->getId(), $resource->getDatabaseName()]), + 'enabled' => $resource->getEnabled(), + 'type' => empty($resource->getType()) ? 'legacy' : $resource->getType(), + 'originalId' => empty($resource->getOriginalId()) ? null : $resource->getOriginalId(), + 'database' => $this->resolveDestinationDsn($resource), + '$updatedAt' => $updatedAt, + ])); + $resource->setSequence($existing->getSequence()); + return true; + })(), + SchemaAction::Create => null, + }; + if ($earlyReturn !== null) { + return $earlyReturn; + } + } + + $database = $this->dbForProject->createDocument(self::META_DATABASES, new UtopiaDocument([ '$id' => $resource->getId(), 'name' => $resource->getDatabaseName(), 'enabled' => $resource->getEnabled(), @@ -437,8 +581,8 @@ class Appwrite extends Destination '$updatedAt' => $updatedAt, 'originalId' => empty($resource->getOriginalId()) ? null : $resource->getOriginalId(), 'type' => empty($resource->getType()) ? 'legacy' : $resource->getType(), - // source and destination can be in different location - 'database' => $resource->getDatabase() + // Resolved by the destination's resolver (or left blank); never copy the source's DSN by default. + 'database' => $this->resolveDestinationDsn($resource), ])); $resource->setSequence($database->getSequence()); @@ -454,7 +598,7 @@ class Appwrite extends Destination ); $this->dbForProject->createCollection( - 'database_' . $database->getSequence(), + $this->databaseCollectionId($database), $columns, $indexes ); @@ -477,26 +621,30 @@ class Appwrite extends Destination $validator = new UID(); if (!$validator->isValid($resource->getId())) { - throw new Exception( + $resource->setStatus(Resource::STATUS_ERROR, $validator->getDescription()); + $this->addError(new Exception( resourceName: $resource->getName(), resourceGroup: $resource->getGroup(), resourceId: $resource->getId(), message: $validator->getDescription(), - ); + )); + return false; } $database = $this->dbForProject->getDocument( - 'databases', + self::META_DATABASES, $resource->getDatabase()->getId() ); if ($database->isEmpty()) { - throw new Exception( + $resource->setStatus(Resource::STATUS_ERROR, 'Database not found'); + $this->addError(new Exception( resourceName: $resource->getName(), resourceGroup: $resource->getGroup(), resourceId: $resource->getId(), message: 'Database not found', - ); + )); + return false; } $createdAt = $this->normalizeDateTime($resource->getCreatedAt()); @@ -509,7 +657,51 @@ class Appwrite extends Destination $dbForDatabases->create(); } - $table = $this->dbForProject->createDocument('database_' . $database->getSequence(), new UtopiaDocument([ + if ($this->onDuplicate !== OnDuplicate::Fail) { + $existing = $this->dbForProject->getDocument( + $this->databaseCollectionId($database), + $resource->getId() + ); + $action = $this->onDuplicate->resolveSchemaAction( + !$existing->isEmpty(), + $updatedAt, + $existing->getUpdatedAt(), + ); + // Spec match → skip work. Create excluded; nothing on dest to match against. + if ($action !== SchemaAction::Create && $this->tableSpecMatches($existing, $resource)) { + $action = SchemaAction::Skip; + } + + $earlyReturn = match ($action) { + SchemaAction::Skip => (function () use ($resource, $existing): bool { + $resource->setSequence($existing->getSequence()); + $resource->setStatus(Resource::STATUS_SKIPPED, 'Already exists on destination'); + return false; + })(), + SchemaAction::Overwrite => (function () use ($resource, $existing, $database, $updatedAt): bool { + $this->dbForProject->updateDocument( + $this->databaseCollectionId($database), + $existing->getId(), + new UtopiaDocument([ + 'name' => $resource->getTableName(), + 'search' => implode(' ', [$resource->getId(), $resource->getTableName()]), + 'enabled' => $resource->getEnabled(), + '$permissions' => Permission::aggregate($resource->getPermissions()), + 'documentSecurity' => $resource->getRowSecurity(), + '$updatedAt' => $updatedAt, + ]) + ); + $resource->setSequence($existing->getSequence()); + return true; + })(), + SchemaAction::Create => null, + }; + if ($earlyReturn !== null) { + return $earlyReturn; + } + } + + $table = $this->dbForProject->createDocument($this->databaseCollectionId($database), new UtopiaDocument([ '$id' => $resource->getId(), 'databaseInternalId' => $database->getSequence(), 'databaseId' => $resource->getDatabase()->getId(), @@ -525,7 +717,7 @@ class Appwrite extends Destination $resource->setSequence($table->getSequence()); $dbForDatabases->createCollection( - 'database_' . $database->getSequence() . '_collection_' . $resource->getSequence(), + $this->tableCollectionId($database, $table), permissions: $resource->getPermissions(), documentSecurity: $resource->getRowSecurity() ); @@ -550,6 +742,7 @@ class Appwrite extends Destination Column::TYPE_DATETIME => UtopiaDatabase::VAR_DATETIME, Column::TYPE_BOOLEAN => UtopiaDatabase::VAR_BOOLEAN, Column::TYPE_INTEGER => UtopiaDatabase::VAR_INTEGER, + Column::TYPE_BIG_INT => UtopiaDatabase::VAR_BIGINT, Column::TYPE_FLOAT => UtopiaDatabase::VAR_FLOAT, Column::TYPE_RELATIONSHIP => UtopiaDatabase::VAR_RELATIONSHIP, @@ -573,84 +766,148 @@ class Appwrite extends Destination }; $database = $this->dbForProject->getDocument( - 'databases', + self::META_DATABASES, $resource->getTable()->getDatabase()->getId(), ); if ($database->isEmpty()) { - throw new Exception( + $resource->setStatus(Resource::STATUS_ERROR, 'Database not found'); + $this->addError(new Exception( resourceName: $resource->getName(), resourceGroup: $resource->getGroup(), resourceId: $resource->getId(), message: 'Database not found', - ); + )); + return false; } $table = $this->dbForProject->getDocument( - 'database_' . $database->getSequence(), + $this->databaseCollectionId($database), $resource->getTable()->getId(), ); if ($table->isEmpty()) { - throw new Exception( + $resource->setStatus(Resource::STATUS_ERROR, 'Table not found'); + $this->addError(new Exception( resourceName: $resource->getName(), resourceGroup: $resource->getGroup(), resourceId: $resource->getId(), message: 'Table not found', - ); + )); + return false; } if (!empty($resource->getFormat())) { if (!Structure::hasFormat($resource->getFormat(), $type)) { - throw new Exception( + $resource->setStatus(Resource::STATUS_ERROR, "Format {$resource->getFormat()} not available for column type {$type}"); + $this->addError(new Exception( resourceName: $resource->getName(), resourceGroup: $resource->getGroup(), resourceId: $resource->getId(), message: "Format {$resource->getFormat()} not available for column type {$type}", - ); + )); + return false; } } if ($resource->isRequired() && $resource->getDefault() !== null) { - throw new Exception( + $resource->setStatus(Resource::STATUS_ERROR, 'Cannot set default value for required column'); + $this->addError(new Exception( resourceName: $resource->getName(), resourceGroup: $resource->getGroup(), resourceId: $resource->getId(), message: 'Cannot set default value for required column', - ); + )); + return false; } if ($resource->isArray() && $resource->getDefault() !== null) { - throw new Exception( + $resource->setStatus(Resource::STATUS_ERROR, 'Cannot set default value for array column'); + $this->addError(new Exception( resourceName: $resource->getName(), resourceGroup: $resource->getGroup(), resourceId: $resource->getId(), message: 'Cannot set default value for array column', - ); + )); + return false; } if ($type === UtopiaDatabase::VAR_RELATIONSHIP) { $resource->getOptions()['side'] = UtopiaDatabase::RELATION_SIDE_PARENT; $relatedTable = $this->dbForProject->getDocument( - 'database_' . $database->getSequence(), + $this->databaseCollectionId($database), $resource->getOptions()['relatedCollection'] ); if ($relatedTable->isEmpty()) { - throw new Exception( + $resource->setStatus(Resource::STATUS_ERROR, 'Related table not found'); + $this->addError(new Exception( resourceName: $resource->getName(), resourceGroup: $resource->getGroup(), resourceId: $resource->getId(), message: 'Related table not found', - ); + )); + return false; } } $createdAt = $this->normalizeDateTime($resource->getCreatedAt()); $updatedAt = $this->normalizeDateTime($resource->getUpdatedAt(), $createdAt); $dbForDatabases = ($this->getDatabasesDB)($database); + + $this->trackOrphanCandidate($database, $table, 'attributeKeys', $resource->getKey(), $dbForDatabases); + + $isRelationship = $type === UtopiaDatabase::VAR_RELATIONSHIP; + + // Source emits both sides of a two-way; processing one side reconciles both. Partner skip. + $twoWayPairKey = $this->twoWayPairKey($database, $table, $resource, $type); + if ($twoWayPairKey !== null && isset($this->processedTwoWayPairs[$twoWayPairKey])) { + $resource->setStatus(Resource::STATUS_SKIPPED, 'Two-way partner already reconciled'); + return false; + } + + $attributeMetaId = $this->attributeIndexMetaId($database, $table, $resource->getKey()); + if ($this->onDuplicate !== OnDuplicate::Fail) { + $existingAttr = $this->dbForProject->getDocument(self::META_ATTRIBUTES, $attributeMetaId); + $action = $this->onDuplicate->resolveSchemaAction( + !$existingAttr->isEmpty(), + $updatedAt, + $existingAttr->getUpdatedAt(), + ); + // Spec match → skip work. Create excluded; nothing on dest to match against. + if ($action !== SchemaAction::Create && $this->attributeSpecMatches($existingAttr, $resource, $type, $isRelationship)) { + $action = SchemaAction::Skip; + } + + $earlyReturn = match ($action) { + SchemaAction::Skip => (function () use ($resource, $database, $table, $dbForDatabases): bool { + $this->purgeTableCaches($database, $table, $dbForDatabases); + $resource->setStatus(Resource::STATUS_SKIPPED, 'Already exists on destination'); + return false; + })(), + SchemaAction::Overwrite => ($isRelationship + ? $this->updateRelationshipInPlace($database, $table, $resource, $type, $updatedAt, $existingAttr, $dbForDatabases) + : $this->updateAttributeInPlace($database, $table, $resource, $type, $updatedAt, $existingAttr, $dbForDatabases)) + ? true + : null, + SchemaAction::Create => null, + }; + if ($earlyReturn !== null) { + if ($twoWayPairKey !== null) { + $this->processedTwoWayPairs[$twoWayPairKey] = true; + } + return $earlyReturn; + } + + if ($action === SchemaAction::Overwrite) { + $this->dropAttributeForRecreate($database, $table, $resource, $dbForDatabases, $existingAttr); + // Reload $table — in-memory copy still holds the dropped attribute, so checkAttribute would over-count. + $table = $this->dbForProject->getDocument($this->databaseCollectionId($database), $table->getId()); + } + } + try { $column = new UtopiaDocument([ - '$id' => ID::custom($database->getSequence() . '_' . $table->getSequence() . '_' . $resource->getKey()), + '$id' => ID::custom($attributeMetaId), 'key' => $resource->getKey(), 'databaseInternalId' => $database->getSequence(), 'databaseId' => $database->getId(), @@ -673,29 +930,33 @@ class Appwrite extends Destination $this->dbForProject->checkAttribute($table, $column); - $column = $this->dbForProject->createDocument('attributes', $column); - } catch (DuplicateException) { - throw new Exception( + $column = $this->dbForProject->createDocument(self::META_ATTRIBUTES, $column); + } catch (DuplicateException $e) { + $resource->setStatus(Resource::STATUS_ERROR, 'Attribute already exists'); + $this->addError(new Exception( resourceName: $resource->getName(), resourceGroup: $resource->getGroup(), resourceId: $resource->getId(), message: 'Attribute already exists', - ); - } catch (LimitException) { - throw new Exception( + previous: $e, + )); + return false; + } catch (LimitException $e) { + $resource->setStatus(Resource::STATUS_ERROR, 'Attribute limit exceeded'); + $this->addError(new Exception( resourceName: $resource->getName(), resourceGroup: $resource->getGroup(), resourceId: $resource->getId(), message: 'Attribute limit exceeded', - ); + previous: $e, + )); + return false; } catch (\Throwable $e) { - $this->dbForProject->purgeCachedDocument('database_' . $database->getSequence(), $table->getId()); - $dbForDatabases->purgeCachedCollection('database_' . $database->getSequence() . '_collection_' . $table->getSequence()); + $this->purgeTableCaches($database, $table, $dbForDatabases); throw $e; } - $this->dbForProject->purgeCachedDocument('database_' . $database->getSequence(), $table->getId()); - $dbForDatabases->purgeCachedCollection('database_' . $database->getSequence() . '_collection_' . $table->getSequence()); + $this->purgeTableCaches($database, $table, $dbForDatabases); $options = $resource->getOptions(); $twoWayKey = null; @@ -708,7 +969,7 @@ class Appwrite extends Destination try { $twoWayAttribute = new UtopiaDocument([ - '$id' => ID::custom($database->getSequence() . '_' . $relatedTable->getSequence() . '_' . $twoWayKey), + '$id' => ID::custom($this->attributeIndexMetaId($database, $relatedTable, $twoWayKey)), 'key' => $twoWayKey, 'databaseInternalId' => $database->getSequence(), 'databaseId' => $database->getId(), @@ -729,28 +990,34 @@ class Appwrite extends Destination '$updatedAt' => $updatedAt, ]); - $this->dbForProject->createDocument('attributes', $twoWayAttribute); - } catch (DuplicateException) { - $this->dbForProject->deleteDocument('attributes', $column->getId()); + $this->dbForProject->createDocument(self::META_ATTRIBUTES, $twoWayAttribute); + $this->trackOrphanCandidate($database, $relatedTable, 'attributeKeys', $twoWayKey, $dbForDatabases); + } catch (DuplicateException $e) { + $this->dbForProject->deleteDocument(self::META_ATTRIBUTES, $column->getId()); - throw new Exception( + $resource->setStatus(Resource::STATUS_ERROR, 'Attribute already exists'); + $this->addError(new Exception( resourceName: $resource->getName(), resourceGroup: $resource->getGroup(), resourceId: $resource->getId(), message: 'Attribute already exists', - ); - } catch (LimitException) { - $this->dbForProject->deleteDocument('attributes', $column->getId()); + previous: $e, + )); + return false; + } catch (LimitException $e) { + $this->dbForProject->deleteDocument(self::META_ATTRIBUTES, $column->getId()); - throw new Exception( + $resource->setStatus(Resource::STATUS_ERROR, 'Attribute limit exceeded'); + $this->addError(new Exception( resourceName: $resource->getName(), resourceGroup: $resource->getGroup(), resourceId: $resource->getId(), - message: 'Column limit exceeded', - ); + message: 'Attribute limit exceeded', + previous: $e, + )); + return false; } catch (\Throwable $e) { - $this->dbForProject->purgeCachedDocument('database_' . $database->getSequence(), $relatedTable->getId()); - $dbForDatabases->purgeCachedCollection('database_' . $database->getSequence() . '_collection_' . $relatedTable->getSequence()); + $this->purgeTableCaches($database, $relatedTable, $dbForDatabases); throw $e; } } @@ -759,8 +1026,9 @@ class Appwrite extends Destination switch ($type) { case UtopiaDatabase::VAR_RELATIONSHIP: if (!$dbForDatabases->createRelationship( - collection: 'database_' . $database->getSequence() . '_collection_' . $table->getSequence(), - relatedCollection: 'database_' . $database->getSequence() . '_collection_' . $relatedTable->getSequence(), + collection: $this->tableCollectionId($database, $table), + // @phpstan-ignore-next-line — $relatedTable is set when type is VAR_RELATIONSHIP. + relatedCollection: $this->tableCollectionId($database, $relatedTable), type: $options['relationType'], twoWay: $options['twoWay'], id: $resource->getKey(), @@ -777,7 +1045,7 @@ class Appwrite extends Destination break; default: if (!$dbForDatabases->createAttribute( - 'database_' . $database->getSequence() . '_collection_' . $table->getSequence(), + $this->tableCollectionId($database, $table), $resource->getKey(), $type, $resource->getSize(), @@ -792,27 +1060,26 @@ class Appwrite extends Destination throw new \Exception('Failed to create Column', Exception::CODE_INTERNAL); } } - } catch (\Throwable) { - $this->dbForProject->deleteDocument('attributes', $column->getId()); + } catch (\Throwable $e) { + $this->dbForProject->deleteDocument(self::META_ATTRIBUTES, $column->getId()); if (isset($twoWayAttribute)) { - $this->dbForProject->deleteDocument('attributes', $twoWayAttribute->getId()); + $this->dbForProject->deleteDocument(self::META_ATTRIBUTES, $twoWayAttribute->getId()); } - throw new Exception( - resourceName: $resource->getName(), - resourceGroup: $resource->getGroup(), - resourceId: $resource->getId(), - message: 'Failed to create column', - ); + throw $e; } if ($type === UtopiaDatabase::VAR_RELATIONSHIP && $options['twoWay']) { - $this->dbForProject->purgeCachedDocument('database_' . $database->getSequence(), $relatedTable->getId()); + // @phpstan-ignore-next-line — $relatedTable is set when type is VAR_RELATIONSHIP. + $this->dbForProject->purgeCachedDocument($this->databaseCollectionId($database), $relatedTable->getId()); } - $this->dbForProject->purgeCachedDocument('database_' . $database->getSequence(), $table->getId()); - $dbForDatabases->purgeCachedCollection('database_' . $database->getSequence() . '_collection_' . $table->getSequence()); + $this->purgeTableCaches($database, $table, $dbForDatabases); + + if ($twoWayPairKey !== null) { + $this->processedTwoWayPairs[$twoWayPairKey] = true; + } return true; } @@ -824,44 +1091,94 @@ class Appwrite extends Destination protected function createIndex(Index $resource): bool { $database = $this->dbForProject->getDocument( - 'databases', + self::META_DATABASES, $resource->getTable()->getDatabase()->getId(), ); if ($database->isEmpty()) { - throw new Exception( + $resource->setStatus(Resource::STATUS_ERROR, 'Database not found'); + $this->addError(new Exception( resourceName: $resource->getName(), resourceGroup: $resource->getGroup(), resourceId: $resource->getId(), message: 'Database not found', - ); + )); + return false; } $table = $this->dbForProject->getDocument( - 'database_' . $database->getSequence(), + $this->databaseCollectionId($database), $resource->getTable()->getId(), ); if ($table->isEmpty()) { - throw new Exception( + $resource->setStatus(Resource::STATUS_ERROR, 'Table not found'); + $this->addError(new Exception( resourceName: $resource->getName(), resourceGroup: $resource->getGroup(), resourceId: $resource->getId(), message: 'Table not found', - ); + )); + return false; } $dbForDatabases = ($this->getDatabasesDB)($database); - $count = $this->dbForProject->count('indexes', [ + $createdAt = $this->normalizeDateTime($resource->getCreatedAt()); + $updatedAt = $this->normalizeDateTime($resource->getUpdatedAt(), $createdAt); + + $this->trackOrphanCandidate($database, $table, 'indexKeys', $resource->getKey(), $dbForDatabases); + + $indexMetaId = $this->attributeIndexMetaId($database, $table, $resource->getKey()); + + // Pre-check + drop runs BEFORE count/validator so the to-be-dropped index isn't included in + // the limit count or in IndexValidator's $tableIndexes (otherwise an Overwrite recreate at the + // ceiling throws "Index limit reached" or "Invalid index" even though the net change is zero). + if ($this->onDuplicate !== OnDuplicate::Fail) { + $existingIdx = $this->dbForProject->getDocument(self::META_INDEXES, $indexMetaId); + $action = $this->onDuplicate->resolveSchemaAction( + !$existingIdx->isEmpty(), + $updatedAt, + $existingIdx->getUpdatedAt(), + ); + // Spec match → skip work. Create excluded; nothing on dest to match against. + if ($action !== SchemaAction::Create && $this->indexSpecMatches($existingIdx, $resource)) { + $action = SchemaAction::Skip; + } + + // Indexes have no in-place primitive — any action other than Skip falls through to drop+recreate. + $earlyReturn = match ($action) { + SchemaAction::Skip => (function () use ($resource, $database, $table): bool { + $this->dbForProject->purgeCachedDocument($this->databaseCollectionId($database), $table->getId()); + $resource->setStatus(Resource::STATUS_SKIPPED, 'Already exists on destination'); + return false; + })(), + SchemaAction::Overwrite, SchemaAction::Create => null, + }; + if ($earlyReturn !== null) { + return $earlyReturn; + } + + if ($action === SchemaAction::Overwrite) { + $dbForDatabases->deleteIndex($this->tableCollectionId($database, $table), $resource->getKey()); + $this->dbForProject->deleteDocument(self::META_INDEXES, $indexMetaId); + $this->dbForProject->purgeCachedDocument($this->databaseCollectionId($database), $table->getId()); + // Reload $table — in-memory copy still holds the dropped index, so IndexValidator below would over-count. + $table = $this->dbForProject->getDocument($this->databaseCollectionId($database), $table->getId()); + } + } + + $count = $this->dbForProject->count(self::META_INDEXES, [ Query::equal('collectionInternalId', [$table->getSequence()]), Query::equal('databaseInternalId', [$database->getSequence()]) ], $dbForDatabases->getLimitForIndexes()); if ($count >= $dbForDatabases->getLimitForIndexes()) { - throw new Exception( + $resource->setStatus(Resource::STATUS_ERROR, 'Index limit reached for table'); + $this->addError(new Exception( resourceName: $resource->getName(), resourceGroup: $resource->getGroup(), resourceId: $resource->getId(), message: 'Index limit reached for table', - ); + )); + return false; } // Lengths hidden by default @@ -871,11 +1188,8 @@ class Appwrite extends Destination $this->validateFieldsForIndexes($resource, $table, $lengths); } - $createdAt = $this->normalizeDateTime($resource->getCreatedAt()); - $updatedAt = $this->normalizeDateTime($resource->getUpdatedAt(), $createdAt); - $index = new UtopiaDocument([ - '$id' => ID::custom($database->getSequence() . '_' . $table->getSequence() . '_' . $resource->getKey()), + '$id' => ID::custom($indexMetaId), 'key' => $resource->getKey(), 'status' => 'available', // processing, available, failed, deleting, stuck 'databaseInternalId' => $database->getSequence(), @@ -916,21 +1230,22 @@ class Appwrite extends Destination $dbForDatabases->getAdapter()->getSupportForFulltextIndex() ); - if (!$validator->isValid($index)) { - throw new Exception( + $resource->setStatus(Resource::STATUS_ERROR, 'Invalid index: ' . $validator->getDescription()); + $this->addError(new Exception( resourceName: $resource->getName(), resourceGroup: $resource->getGroup(), resourceId: $resource->getId(), message: 'Invalid index: ' . $validator->getDescription(), - ); + )); + return false; } - $index = $this->dbForProject->createDocument('indexes', $index); + $index = $this->dbForProject->createDocument(self::META_INDEXES, $index); try { $result = $dbForDatabases->createIndex( - 'database_' . $database->getSequence() . '_collection_' . $table->getSequence(), + $this->tableCollectionId($database, $table), $resource->getKey(), $resource->getType(), $resource->getColumns(), @@ -947,20 +1262,12 @@ class Appwrite extends Destination ); } } catch (\Throwable $th) { - $this->dbForProject->deleteDocument('indexes', $index->getId()); + $this->dbForProject->deleteDocument(self::META_INDEXES, $index->getId()); - throw new Exception( - resourceName: $resource->getName(), - resourceGroup: $resource->getGroup(), - resourceId: $resource->getId(), - message: 'Failed to create index', - ); + throw $th; } - $this->dbForProject->purgeCachedDocument( - 'database_' . $database->getSequence(), - $table->getId() - ); + $this->dbForProject->purgeCachedDocument($this->databaseCollectionId($database), $table->getId()); return true; } @@ -980,12 +1287,14 @@ class Appwrite extends Destination $validator = new UID(); if (!$validator->isValid($resource->getId())) { - throw new Exception( + $resource->setStatus(Resource::STATUS_ERROR, $validator->getDescription()); + $this->addError(new Exception( resourceName: $resource->getName(), resourceGroup: $resource->getGroup(), resourceId: $resource->getId(), message: $validator->getDescription(), - ); + )); + return false; } // Check if document has already been created @@ -1037,22 +1346,25 @@ class Appwrite extends Destination if ($isLast) { try { $database = $this->dbForProject->getDocument( - 'databases', + self::META_DATABASES, $resource->getTable()->getDatabase()->getId(), ); $table = $this->dbForProject->getDocument( - 'database_' . $database->getSequence(), + $this->databaseCollectionId($database), $resource->getTable()->getId(), ); - $databaseInternalId = $database->getSequence(); - $tableInternalId = $table->getSequence(); $dbForDatabases = ($this->getDatabasesDB)($database); - /** - * This is in case an attribute was deleted from Appwrite attributes collection but was not deleted from the table - * When creating an archive we select * which will include orphan attribute from the schema - */ + + // Drop schema orphans before rows land so the Structure validator doesn't reject on orphan required columns. + $this->cleanupOverwriteOrphansForTable($this->tableIdentity($database, $table)); + // Reload $table — in-memory copy still holds the dropped attributes, so the strip loop below would over-keep. + $table = $this->dbForProject->getDocument( + $this->databaseCollectionId($database), + $resource->getTable()->getId(), + ); + // Strip row payload fields the table doesn't declare — guards against orphans surviving in source archives. if ($dbForDatabases->getAdapter()->getSupportForAttributes()) { foreach ($this->rowBuffer as $row) { foreach ($row as $key => $value) { @@ -1075,10 +1387,43 @@ class Appwrite extends Destination } } } - $dbForDatabases->skipRelationshipsExistCheck(fn () => $dbForDatabases->createDocuments( - 'database_' . $databaseInternalId . '_collection_' . $tableInternalId, - $this->rowBuffer - )); + $collectionId = $this->tableCollectionId($database, $table); + + try { + match ($this->onDuplicate) { + OnDuplicate::Overwrite => $dbForDatabases->skipRelationshipsExistCheck( + fn () => $dbForDatabases->upsertDocuments($collectionId, $this->rowBuffer) + ), + OnDuplicate::Skip => $dbForDatabases->skipDuplicates( + fn () => $dbForDatabases->skipRelationshipsExistCheck( + fn () => $dbForDatabases->createDocuments($collectionId, $this->rowBuffer) + ) + ), + OnDuplicate::Fail => $dbForDatabases->skipRelationshipsExistCheck( + fn () => $dbForDatabases->createDocuments($collectionId, $this->rowBuffer) + ), + }; + } catch (DuplicateException $e) { + $resource->setStatus(Resource::STATUS_ERROR, 'Document already exists'); + $this->addError(new Exception( + resourceName: $resource->getName(), + resourceGroup: $resource->getGroup(), + resourceId: $resource->getId(), + message: 'Document already exists', + previous: $e, + )); + return false; + } catch (StructureException $e) { + $resource->setStatus(Resource::STATUS_ERROR, $e->getMessage()); + $this->addError(new Exception( + resourceName: $resource->getName(), + resourceGroup: $resource->getGroup(), + resourceId: $resource->getId(), + message: $e->getMessage(), + previous: $e, + )); + return false; + } } finally { $this->rowBuffer = []; } @@ -1088,6 +1433,516 @@ class Appwrite extends Destination return true; } + /** Relationships route through deleteRelationship since deleteAttribute throws for VAR_RELATIONSHIP. */ + private function dropAttributeForRecreate( + UtopiaDocument $database, + UtopiaDocument $table, + Column|Attribute $resource, + UtopiaDatabase $dbForDatabases, + UtopiaDocument $existingAttr, + ): void { + $collectionId = $this->tableCollectionId($database, $table); + $attributeMetaId = $this->attributeIndexMetaId($database, $table, $resource->getKey()); + $isRelationship = $resource->getType() === Column::TYPE_RELATIONSHIP; + + if ($isRelationship) { + $dbForDatabases->deleteRelationship($collectionId, $resource->getKey()); + } else { + $dbForDatabases->deleteAttribute($collectionId, $resource->getKey()); + } + + $this->dbForProject->deleteDocument(self::META_ATTRIBUTES, $attributeMetaId); + + // Use dest's options for partner lookup — drop fires when relatedCollection/twoWayKey differ, so source points to the NEW partner. + if ($isRelationship) { + $partner = $this->resolveTwoWayPartner($database, (array) $existingAttr->getAttribute('options', [])); + if ($partner !== null) { + $this->bestEffort(fn () => $this->dbForProject->deleteDocument(self::META_ATTRIBUTES, $partner['partnerMetaId'])); + } + } + + $this->purgeTableCaches($database, $table, $dbForDatabases); + } + + /** Returns false when the source change isn't SDK-expressible — caller falls through to drop+recreate. */ + private function updateAttributeInPlace( + UtopiaDocument $database, + UtopiaDocument $table, + Column|Attribute $resource, + string $type, + string $updatedAt, + UtopiaDocument $existingAttr, + UtopiaDatabase $dbForDatabases, + ): bool { + $sourceFields = [ + 'type' => $type, + 'array' => $resource->isArray(), + 'signed' => $resource->isSigned(), + 'format' => $resource->getFormat(), + 'formatOptions' => $resource->getFormatOptions(), + 'filters' => $resource->getFilters(), + ]; + + $existingFields = []; + foreach (self::ATTRIBUTE_IMMUTABLE_FIELDS as $field) { + $existingFields[$field] = $existingAttr->getAttribute($field); + } + + if ($this->arraysDifferOnKeys($sourceFields, $existingFields, self::ATTRIBUTE_IMMUTABLE_FIELDS)) { + return false; + } + + // Pass existing values for non-SDK fields so utopia doesn't trigger an ALTER for unchanged fields. + $dbForDatabases->updateAttribute( + collection: $this->tableCollectionId($database, $table), + id: $resource->getKey(), + type: $type, + size: $resource->getSize(), + required: $resource->isRequired(), + default: $resource->getDefault(), + signed: $existingAttr->getAttribute('signed'), + array: $existingAttr->getAttribute('array'), + format: $resource->getFormat(), + formatOptions: $resource->getFormatOptions(), + filters: $existingAttr->getAttribute('filters'), + ); + + $this->dbForProject->updateDocument(self::META_ATTRIBUTES, $existingAttr->getId(), new UtopiaDocument([ + 'key' => $resource->getKey(), + 'type' => $type, + 'size' => $resource->getSize(), + 'required' => $resource->isRequired(), + 'signed' => $resource->isSigned(), + 'default' => $resource->getDefault(), + 'array' => $resource->isArray(), + 'format' => $resource->getFormat(), + 'formatOptions' => $resource->getFormatOptions(), + 'filters' => $resource->getFilters(), + '$updatedAt' => $updatedAt, + ])); + + $this->purgeTableCaches($database, $table, $dbForDatabases); + return true; + } + + /** + * Returns false when the source change isn't SDK-expressible — caller falls through to drop+recreate. + * One-way + onDelete change is also rejected: utopia's updateRelationship partner-cascade throws on one-way. + */ + private function updateRelationshipInPlace( + UtopiaDocument $database, + UtopiaDocument $table, + Column|Attribute $resource, + string $type, + string $updatedAt, + UtopiaDocument $existingAttr, + UtopiaDatabase $dbForDatabases, + ): bool { + $sourceOptions = $resource->getOptions(); + $destOptions = $existingAttr->getAttribute('options', []); + + if ($this->arraysDifferOnKeys($sourceOptions, $destOptions, self::RELATIONSHIP_IMMUTABLE_FIELDS)) { + return false; + } + + $isTwoWay = (bool) ($destOptions['twoWay'] ?? false); + $onDeleteChanged = ($sourceOptions['onDelete'] ?? null) !== ($destOptions['onDelete'] ?? null); + + if (!$isTwoWay && $onDeleteChanged) { + return false; + } + + if ($onDeleteChanged) { + $dbForDatabases->updateRelationship( + collection: $this->tableCollectionId($database, $table), + id: $resource->getKey(), + onDelete: (string) ($sourceOptions['onDelete'] ?? ''), + ); + } + + $this->dbForProject->updateDocument(self::META_ATTRIBUTES, $existingAttr->getId(), new UtopiaDocument([ + 'key' => $resource->getKey(), + 'type' => $type, + 'options' => array_merge($destOptions, [ + 'onDelete' => $sourceOptions['onDelete'] ?? $destOptions['onDelete'] ?? null, + ]), + '$updatedAt' => $updatedAt, + ])); + + $this->purgeTableCaches($database, $table, $dbForDatabases); + + // utopia syncs both physical sides; partner's Appwrite-level meta doc has to be refreshed by hand. + if ($isTwoWay) { + $this->refreshTwoWayPartnerOnDelete($database, $destOptions, $sourceOptions, $updatedAt, $dbForDatabases); + } + + return true; + } + + /** + * @param array $destOptions + * @param array $sourceOptions + */ + private function refreshTwoWayPartnerOnDelete( + UtopiaDocument $database, + array $destOptions, + array $sourceOptions, + string $updatedAt, + UtopiaDatabase $dbForDatabases, + ): void { + $partner = $this->resolveTwoWayPartner($database, $destOptions); + if ($partner === null) { + return; + } + + $partnerMeta = $this->dbForProject->getDocument(self::META_ATTRIBUTES, $partner['partnerMetaId']); + if ($partnerMeta->isEmpty()) { + return; + } + + $partnerOptions = $partnerMeta->getAttribute('options', []); + $this->dbForProject->updateDocument(self::META_ATTRIBUTES, $partnerMeta->getId(), new UtopiaDocument([ + 'options' => array_merge($partnerOptions, [ + 'onDelete' => $sourceOptions['onDelete'] ?? $partnerOptions['onDelete'] ?? null, + ]), + '$updatedAt' => $updatedAt, + ])); + $this->purgeTableCaches($database, $partner['relatedTable'], $dbForDatabases); + } + + private function databaseSpecMatches(UtopiaDocument $existing, Database $resource): bool + { + $sourceType = empty($resource->getType()) ? 'legacy' : $resource->getType(); + $sourceOriginalId = empty($resource->getOriginalId()) ? null : $resource->getOriginalId(); + + return $existing->getAttribute('name') === $resource->getDatabaseName() + && $existing->getAttribute('enabled') === $resource->getEnabled() + && $existing->getAttribute('type') === $sourceType + && $existing->getAttribute('originalId') === $sourceOriginalId + && $existing->getAttribute('database') === $this->resolveDestinationDsn($resource); + } + + private function tableSpecMatches(UtopiaDocument $existing, Table $resource): bool + { + if ($existing->getAttribute('name') !== $resource->getTableName() + || $existing->getAttribute('enabled') !== $resource->getEnabled() + || $existing->getAttribute('documentSecurity') !== $resource->getRowSecurity()) { + return false; + } + + $sourcePerms = Permission::aggregate($resource->getPermissions()); + /** @var list $destPerms */ + $destPerms = $existing->getAttribute('$permissions', []); + \sort($sourcePerms); + \sort($destPerms); + return $sourcePerms === $destPerms; + } + + /** Full-spec equality: short-circuits Overwrite to Skip when nothing changed. */ + private function attributeSpecMatches(UtopiaDocument $existing, Column|Attribute $resource, string $type, bool $isRelationship): bool + { + if ($existing->getAttribute('type') !== $type) { + return false; + } + if ($isRelationship) { + $sourceOptions = $resource->getOptions(); + /** @var array $destOptions */ + $destOptions = $existing->getAttribute('options', []); + foreach (self::RELATIONSHIP_IMMUTABLE_FIELDS as $field) { + if (!$this->valuesMatch($sourceOptions[$field] ?? null, $destOptions[$field] ?? null)) { + return false; + } + } + return $this->valuesMatch($sourceOptions['onDelete'] ?? null, $destOptions['onDelete'] ?? null); + } + + return $existing->getAttribute('size') === $resource->getSize() + && $existing->getAttribute('required') === $resource->isRequired() + && $existing->getAttribute('default') === $resource->getDefault() + && $existing->getAttribute('array') === $resource->isArray() + && $existing->getAttribute('signed') === $resource->isSigned() + && $existing->getAttribute('format') === $resource->getFormat() + && $this->valuesMatch($existing->getAttribute('formatOptions'), $resource->getFormatOptions()) + && $existing->getAttribute('filters') === $resource->getFilters(); + } + + /** + * `lengths` is intentionally not compared: it's derived per-column from + * the adapter's index validator and not settable on the Index resource. + */ + private function indexSpecMatches(UtopiaDocument $existingIdx, Index $resource): bool + { + return $existingIdx->getAttribute('type') === $resource->getType() + && $existingIdx->getAttribute('attributes') === $resource->getColumns() + && $existingIdx->getAttribute('orders') === $resource->getOrders(); + } + + private function tableIdentity(UtopiaDocument $database, UtopiaDocument $table): string + { + return $database->getSequence() . ':' . $table->getSequence(); + } + + /** + * @param array $options + * @return array{relatedTable: UtopiaDocument, partnerKey: string, partnerMetaId: string}|null + */ + private function resolveTwoWayPartner(UtopiaDocument $database, array $options): ?array + { + if (empty($options['twoWay'])) { + return null; + } + $relatedTableId = (string) ($options['relatedCollection'] ?? ''); + $partnerKey = (string) ($options['twoWayKey'] ?? ''); + if ($relatedTableId === '' || $partnerKey === '') { + return null; + } + $relatedTable = $this->dbForProject->getDocument( + $this->databaseCollectionId($database), + $relatedTableId, + ); + if ($relatedTable->isEmpty()) { + return null; + } + return [ + 'relatedTable' => $relatedTable, + 'partnerKey' => $partnerKey, + 'partnerMetaId' => $this->attributeIndexMetaId($database, $relatedTable, $partnerKey), + ]; + } + + /** Canonical pair-key — same string regardless of which side is being processed. */ + private function twoWayPairKey( + UtopiaDocument $database, + UtopiaDocument $table, + Column|Attribute $resource, + string $type, + ): ?string { + if ($type !== UtopiaDatabase::VAR_RELATIONSHIP) { + return null; + } + $options = $resource->getOptions(); + if (empty($options['twoWay'])) { + return null; + } + $twoWayKey = (string) ($options['twoWayKey'] ?? ''); + $relatedTableId = (string) ($options['relatedCollection'] ?? ''); + if ($twoWayKey === '' || $relatedTableId === '') { + return null; + } + + $thisSide = $table->getId() . '::' . $resource->getKey(); + $partnerSide = $relatedTableId . '::' . $twoWayKey; + $pair = [$thisSide, $partnerSide]; + \sort($pair); + return $database->getSequence() . '@' . \implode('<->', $pair); + } + + private function databaseCollectionId(UtopiaDocument $database): string + { + return 'database_' . $database->getSequence(); + } + + private function tableCollectionId(UtopiaDocument $database, UtopiaDocument $table): string + { + return $this->databaseCollectionId($database) . '_collection_' . $table->getSequence(); + } + + private function attributeIndexMetaId(UtopiaDocument $database, UtopiaDocument $table, string $key): string + { + return $database->getSequence() . '_' . $table->getSequence() . '_' . $key; + } + + /** Stale platform/per-database cache shows the pre-change schema; evict after every structural change. */ + private function purgeTableCaches( + UtopiaDocument $database, + UtopiaDocument $table, + UtopiaDatabase $dbForDatabases, + ): void { + $this->dbForProject->purgeCachedDocument($this->databaseCollectionId($database), $table->getId()); + $dbForDatabases->purgeCachedCollection($this->tableCollectionId($database, $table)); + } + + /** + * @param array $a + * @param array $b + * @param list $keys + */ + private function arraysDifferOnKeys(array $a, array $b, array $keys): bool + { + foreach ($keys as $key) { + if (!$this->valuesMatch($a[$key] ?? null, $b[$key] ?? null)) { + return true; + } + } + return false; + } + + /** + * `===` on associative arrays is order-sensitive on keys; ksort both sides + * before comparing so {min, max} matches {max, min}. Lists (numeric keys) + * are left alone — order is semantically meaningful for filters/columns. + */ + private function valuesMatch(mixed $a, mixed $b): bool + { + if (\is_array($a) && \is_array($b) && !\array_is_list($a) && !\array_is_list($b)) { + \ksort($a); + \ksort($b); + } + return $a === $b; + } + + /** $kind is 'attributeKeys' or 'indexKeys'. */ + private function trackOrphanCandidate( + UtopiaDocument $database, + UtopiaDocument $table, + string $kind, + string $key, + UtopiaDatabase $dbForDatabases, + ): void { + if ($this->onDuplicate !== OnDuplicate::Overwrite) { + return; + } + $tableId = $this->tableIdentity($database, $table); + if (!isset($this->orphansByTable[$tableId])) { + $this->orphansByTable[$tableId] = [ + 'database' => $database, + 'table' => $table, + 'dbForDatabases' => $dbForDatabases, + 'attributeKeys' => [], + 'indexKeys' => [], + ]; + } + $this->orphansByTable[$tableId][$kind][] = $key; + } + + /** End-of-migration sweep — only visits tables that had no rows (rest were cleaned per-table in createRecord). */ + private function cleanupOverwriteOrphans(): void + { + foreach (\array_keys($this->orphansByTable) as $tableId) { + $this->cleanupOverwriteOrphansForTable($tableId); + } + } + + /** Called per-table from createRecord before rows land so Structure validator sees the post-cleanup schema. */ + private function cleanupOverwriteOrphansForTable(string $tableId): void + { + if ($this->onDuplicate !== OnDuplicate::Overwrite) { + return; + } + if (!isset($this->orphansByTable[$tableId])) { + return; + } + + $tracked = $this->orphansByTable[$tableId]; + $database = $tracked['database']; + $table = $tracked['table']; + $dbForDatabases = $tracked['dbForDatabases']; + + $this->dropOrphansByKind( + self::META_ATTRIBUTES, + $tracked['attributeKeys'], + $database, + $table, + fn (UtopiaDocument $doc) => $this->dropOrphanAttribute($database, $table, $doc, $dbForDatabases), + ); + + $this->dropOrphansByKind( + self::META_INDEXES, + $tracked['indexKeys'], + $database, + $table, + fn (UtopiaDocument $doc) => $this->dropOrphanIndex( + $database, + $table, + (string) $doc->getAttribute('key'), + $dbForDatabases, + ), + ); + + unset($this->orphansByTable[$tableId]); + } + + /** + * @param list $processedKeys + * @param callable(UtopiaDocument): void $drop + */ + private function dropOrphansByKind( + string $metaCollection, + array $processedKeys, + UtopiaDocument $database, + UtopiaDocument $table, + callable $drop, + ): void { + $destDocs = $this->dbForProject->find($metaCollection, [ + Query::equal('databaseInternalId', [$database->getSequence()]), + Query::equal('collectionInternalId', [$table->getSequence()]), + Query::limit(PHP_INT_MAX), + ]); + foreach ($destDocs as $destDoc) { + if (!\in_array($destDoc->getAttribute('key'), $processedKeys, true)) { + $drop($destDoc); + } + } + } + + /** Reads dest's own meta doc as source of truth — there's no source resource for an orphan. */ + private function dropOrphanAttribute( + UtopiaDocument $database, + UtopiaDocument $table, + UtopiaDocument $attrDoc, + UtopiaDatabase $dbForDatabases, + ): void { + $key = (string) $attrDoc->getAttribute('key'); + $type = (string) $attrDoc->getAttribute('type'); + $options = $attrDoc->getAttribute('options', []); + $collectionId = $this->tableCollectionId($database, $table); + + if ($type === UtopiaDatabase::VAR_RELATIONSHIP) { + $this->bestEffort(fn () => $dbForDatabases->deleteRelationship($collectionId, $key)); + } else { + $this->bestEffort(fn () => $dbForDatabases->deleteAttribute($collectionId, $key)); + } + $this->bestEffort(fn () => $this->dbForProject->deleteDocument(self::META_ATTRIBUTES, $attrDoc->getId())); + $this->dbForProject->purgeCachedDocument($this->databaseCollectionId($database), $table->getId()); + $dbForDatabases->purgeCachedCollection($collectionId); + + if ($type !== UtopiaDatabase::VAR_RELATIONSHIP) { + return; + } + $partner = $this->resolveTwoWayPartner($database, $options); + if ($partner === null) { + return; + } + + // deleteRelationship already dropped the partner physical column; only the Appwrite-level meta doc remains. + $this->bestEffort(fn () => $this->dbForProject->deleteDocument(self::META_ATTRIBUTES, $partner['partnerMetaId'])); + $this->purgeTableCaches($database, $partner['relatedTable'], $dbForDatabases); + } + + private function dropOrphanIndex( + UtopiaDocument $database, + UtopiaDocument $table, + string $indexKey, + UtopiaDatabase $dbForDatabases, + ): void { + $collectionId = $this->tableCollectionId($database, $table); + $indexMetaId = $this->attributeIndexMetaId($database, $table, $indexKey); + + $this->bestEffort(fn () => $dbForDatabases->deleteIndex($collectionId, $indexKey)); + $this->bestEffort(fn () => $this->dbForProject->deleteDocument(self::META_INDEXES, $indexMetaId)); + $this->dbForProject->purgeCachedDocument($this->databaseCollectionId($database), $table->getId()); + } + + /** Swallows deletion errors — a prior run or relationship cascade may have already removed the target. */ + private function bestEffort(callable $fn): void + { + try { + $fn(); + } catch (\Throwable) { + // already gone + } + } + /** * @throws \Exception */ @@ -1135,6 +1990,7 @@ class Appwrite extends Destination 'none' => Compression::NONE(), 'gzip' => Compression::GZIP(), 'zstd' => Compression::ZSTD(), + // no break default => throw new \Exception('Invalid Compression: ' . $resource->getCompression(), Exception::CODE_VALIDATION), }; @@ -1148,10 +2004,11 @@ class Appwrite extends Destination $resource->getAllowedFileExtensions(), $compression, $resource->getEncryption(), - $resource->getAntiVirus() + $resource->getAntiVirus(), + $resource->getTransformations() ); - $resource->setId($response['$id']); + $resource->setId($response->id); } $resource->setStatus(Resource::STATUS_SUCCESS); @@ -1303,11 +2160,11 @@ class Appwrite extends Destination /** * @param User $user - * @return array|null + * @return \Appwrite\Models\User|null * @throws AppwriteException * @throws \Exception */ - public function importPasswordUser(User $user): ?array + public function importPasswordUser(User $user): ?\Appwrite\Models\User { $hash = $user->getPasswordHash(); $result = null; @@ -1429,9 +2286,6 @@ class Appwrite extends Destination 'dart-2.17' => Runtime::DART217(), 'dart-2.18' => Runtime::DART218(), 'dart-2.19' => Runtime::DART219(), - 'deno-1.21' => Runtime::DENO121(), - 'deno-1.24' => Runtime::DENO124(), - 'deno-1.35' => Runtime::DENO135(), 'deno-1.40' => Runtime::DENO140(), 'deno-1.46' => Runtime::DENO146(), 'deno-2.0' => Runtime::DENO20(), @@ -1457,27 +2311,34 @@ class Appwrite extends Destination 'bun-1.0' => Runtime::BUN10(), 'bun-1.1' => Runtime::BUN11(), 'go-1.23' => Runtime::GO123(), + // no break default => throw new \Exception('Invalid Runtime: ' . $resource->getRuntime(), Exception::CODE_VALIDATION), }; $this->functions->create( - $resource->getId(), - $resource->getFunctionName(), - $runtime, - $resource->getExecute(), - $resource->getEvents(), - $resource->getSchedule(), - $resource->getTimeout(), - $resource->getEnabled(), + functionId: $resource->getId(), + name: $resource->getFunctionName(), + runtime: $runtime, + execute: $resource->getExecute(), + events: $resource->getEvents(), + schedule: $resource->getSchedule(), + timeout: $resource->getTimeout(), + enabled: $resource->getEnabled(), + logging: $resource->getLogging(), entrypoint: $resource->getEntrypoint(), + commands: $resource->getCommands(), + scopes: $resource->getScopes(), + buildSpecification: $resource->getSpecification() ?: null, + runtimeSpecification: $resource->getSpecification() ?: null, ); break; case Resource::TYPE_ENVIRONMENT_VARIABLE: /** @var EnvVar $resource */ $this->functions->createVariable( - $resource->getFunc()->getId(), - $resource->getKey(), - $resource->getValue() + functionId: $resource->getFunc()->getId(), + variableId: $resource->getId(), + key: $resource->getKey(), + value: $resource->getValue(), ); break; case Resource::TYPE_DEPLOYMENT: @@ -1490,6 +2351,13 @@ class Appwrite extends Destination return $resource; } + public function importBackupResource(Resource $resource): Resource + { + $resource->setStatus(Resource::STATUS_SKIPPED); + + return $resource; + } + /** * @throws AppwriteException * @throws \Exception @@ -1638,9 +2506,6 @@ class Appwrite extends Destination 'dart-2.17' => BuildRuntime::DART217(), 'dart-2.18' => BuildRuntime::DART218(), 'dart-2.19' => BuildRuntime::DART219(), - 'deno-1.21' => BuildRuntime::DENO121(), - 'deno-1.24' => BuildRuntime::DENO124(), - 'deno-1.35' => BuildRuntime::DENO135(), 'deno-1.40' => BuildRuntime::DENO140(), 'deno-1.46' => BuildRuntime::DENO146(), 'deno-2.0' => BuildRuntime::DENO20(), @@ -1672,6 +2537,7 @@ class Appwrite extends Destination 'flutter-3.29' => BuildRuntime::FLUTTER329(), 'flutter-3.32' => BuildRuntime::FLUTTER332(), 'flutter-3.35' => BuildRuntime::FLUTTER335(), + // no break default => throw new \Exception('Invalid Build Runtime: ' . $resource->getBuildRuntime(), Exception::CODE_VALIDATION), }; @@ -1700,27 +2566,29 @@ class Appwrite extends Destination }; $this->sites->create( - $resource->getId(), - $resource->getSiteName(), - $framework, - $buildRuntime, - $resource->getEnabled(), - $resource->getLogging(), - $resource->getTimeout(), - $resource->getInstallCommand(), - $resource->getBuildCommand(), - $resource->getOutputDirectory(), - $adapter, + siteId: $resource->getId(), + name: $resource->getSiteName(), + framework: $framework, + buildRuntime: $buildRuntime, + enabled: $resource->getEnabled(), + logging: $resource->getLogging(), + timeout: $resource->getTimeout(), + installCommand: $resource->getInstallCommand(), + buildCommand: $resource->getBuildCommand(), + outputDirectory: $resource->getOutputDirectory(), + adapter: $adapter, fallbackFile: $resource->getFallbackFile(), - specification: $resource->getSpecification(), + buildSpecification: $resource->getSpecification() ?: null, + runtimeSpecification: $resource->getSpecification() ?: null, ); break; case Resource::TYPE_SITE_VARIABLE: /** @var SiteEnvVar $resource */ $this->sites->createVariable( - $resource->getSite()->getId(), - $resource->getKey(), - $resource->getValue() + siteId: $resource->getSite()->getId(), + variableId: $resource->getId(), + key: $resource->getKey(), + value: $resource->getValue(), ); break; case Resource::TYPE_SITE_DEPLOYMENT: diff --git a/src/Migration/Destinations/JSON.php b/src/Migration/Destinations/JSON.php index 91016ef589..6660ab5a06 100644 --- a/src/Migration/Destinations/JSON.php +++ b/src/Migration/Destinations/JSON.php @@ -316,5 +316,4 @@ class JSON extends Destination return $this->convertValueToJSON($item); }, $value); } - } diff --git a/src/Migration/Destinations/OnDuplicate.php b/src/Migration/Destinations/OnDuplicate.php new file mode 100644 index 0000000000..a88ebf8033 --- /dev/null +++ b/src/Migration/Destinations/OnDuplicate.php @@ -0,0 +1,70 @@ + + */ + public static function values(): array + { + return \array_values(\array_map(fn (self $case) => $case->value, self::cases())); + } + + /** + * Coarse routing for re-migration. The caller follows up with a spec-match + * check that overrides Overwrite to Skip when source and dest + * already have identical spec — see DestinationAppwrite for the full flow. + */ + public function resolveSchemaAction( + bool $exists, + ?string $sourceUpdatedAt = null, + ?string $destUpdatedAt = null, + ): SchemaAction { + if (!$exists) { + return SchemaAction::Create; + } + return match ($this) { + self::Fail => SchemaAction::Create, + self::Skip => SchemaAction::Skip, + self::Overwrite => $this->sourceIsNewer($sourceUpdatedAt, $destUpdatedAt) + ? SchemaAction::Overwrite + : SchemaAction::Skip, + }; + } + + private function sourceIsNewer(?string $source, ?string $dest): bool + { + $src = $this->parseTimestamp($source); + $dst = $this->parseTimestamp($dest); + return $src !== null && $dst !== null && $src > $dst; + } + + /** + * strtotime accepts '0000-00-00' leniently (returns a large negative epoch, + * not false), so non-positive epochs are rejected too. + */ + private function parseTimestamp(?string $value): ?int + { + if ($value === null || $value === '') { + return null; + } + $epoch = \strtotime($value); + if ($epoch === false || $epoch <= 0) { + return null; + } + return $epoch; + } +} diff --git a/src/Migration/Resource.php b/src/Migration/Resource.php index e624fe2cac..eb579ebe77 100644 --- a/src/Migration/Resource.php +++ b/src/Migration/Resource.php @@ -77,6 +77,9 @@ abstract class Resource implements \JsonSerializable public const TYPE_MESSAGE = 'message'; + // Backups + public const TYPE_BACKUP_POLICY = 'backup-policy'; + // legacy terminologies public const TYPE_DOCUMENT = 'document'; public const TYPE_ATTRIBUTE = 'attribute'; @@ -113,6 +116,7 @@ abstract class Resource implements \JsonSerializable self::TYPE_TOPIC, self::TYPE_SUBSCRIBER, self::TYPE_MESSAGE, + self::TYPE_BACKUP_POLICY, // legacy self::TYPE_DOCUMENT, diff --git a/src/Migration/Resources/Backups/Policy.php b/src/Migration/Resources/Backups/Policy.php new file mode 100644 index 0000000000..d5358b4287 --- /dev/null +++ b/src/Migration/Resources/Backups/Policy.php @@ -0,0 +1,115 @@ + $services + * @param int $retention + * @param string $schedule + * @param bool $enabled + * @param string $resourceId + * @param string $resourceType + */ + public function __construct( + string $id = '', + private readonly string $name = '', + private readonly array $services = [], + private readonly int $retention = 0, + private readonly string $schedule = '', + private readonly bool $enabled = true, + private readonly string $resourceId = '', + private readonly string $resourceType = '', + ) { + $this->id = $id; + } + + /** + * @param array $array + * @return self + */ + public static function fromArray(array $array): self + { + return new self( + $array['id'], + $array['name'] ?? '', + $array['services'] ?? [], + $array['retention'] ?? 0, + $array['schedule'] ?? '', + $array['enabled'] ?? true, + $array['resourceId'] ?? '', + $array['resourceType'] ?? '', + ); + } + + /** + * @return array + */ + public function jsonSerialize(): array + { + return [ + 'id' => $this->id, + 'name' => $this->name, + 'services' => $this->services, + 'retention' => $this->retention, + 'schedule' => $this->schedule, + 'enabled' => $this->enabled, + 'resourceId' => $this->resourceId, + 'resourceType' => $this->resourceType, + ]; + } + + public static function getName(): string + { + return Resource::TYPE_BACKUP_POLICY; + } + + public function getGroup(): string + { + return Transfer::GROUP_BACKUPS; + } + + public function getPolicyName(): string + { + return $this->name; + } + + /** + * @return array + */ + public function getServices(): array + { + return $this->services; + } + + public function getRetention(): int + { + return $this->retention; + } + + public function getSchedule(): string + { + return $this->schedule; + } + + public function getEnabled(): bool + { + return $this->enabled; + } + + public function getResourceId(): string + { + return $this->resourceId; + } + + public function getResourceType(): string + { + return $this->resourceType; + } +} diff --git a/src/Migration/Resources/Database/Column.php b/src/Migration/Resources/Database/Column.php index 45aea6d4c8..3a3ef4faa6 100644 --- a/src/Migration/Resources/Database/Column.php +++ b/src/Migration/Resources/Database/Column.php @@ -14,6 +14,7 @@ abstract class Column extends Resource public const TYPE_LONGTEXT = 'longtext'; public const TYPE_INTEGER = 'integer'; + public const TYPE_BIG_INT = 'bigint'; public const TYPE_FLOAT = 'double'; public const TYPE_BOOLEAN = 'boolean'; public const TYPE_DATETIME = 'datetime'; diff --git a/src/Migration/Resources/Database/Columns/BigInt.php b/src/Migration/Resources/Database/Columns/BigInt.php new file mode 100644 index 0000000000..a6d9045068 --- /dev/null +++ b/src/Migration/Resources/Database/Columns/BigInt.php @@ -0,0 +1,108 @@ + $min, + 'max' => $max, + ], + createdAt: $createdAt, + updatedAt: $updatedAt + ); + } + + /** + * @param array{ + * key: string, + * collection?: array{ + * database: array{ + * id: string, + * name: string, + * }, + * name: string, + * id: string, + * documentSecurity: bool, + * permissions: ?array + * }, + * table?: array{ + * database: array{ + * id: string, + * name: string, + * }, + * name: string, + * id: string, + * rowSecurity: bool, + * permissions: ?array + * }, + * required: bool, + * array: bool, + * default: ?int, + * formatOptions: array{ + * min: ?int, + * max: ?int + * }, + * createdAt: string, + * updatedAt: string, + * } $array + * @return self + */ + public static function fromArray(array $array): self + { + return new self( + $array['key'], + Table::fromArray($array['table'] ?? $array['collection']), + required: $array['required'], + default: $array['default'], + array: $array['array'], + min: $array['formatOptions']['min'] ?? null, + max: $array['formatOptions']['max'] ?? null, + signed: $array['signed'] ?? true, + createdAt: $array['createdAt'] ?? '', + updatedAt: $array['updatedAt'] ?? '', + ); + } + + public function getType(): string + { + return Column::TYPE_BIG_INT; + } + + public function getMin(): ?int + { + return (int)$this->formatOptions['min']; + } + + public function getMax(): ?int + { + return (int)$this->formatOptions['max']; + } +} diff --git a/src/Migration/Resources/Database/Database.php b/src/Migration/Resources/Database/Database.php index 05afab2c48..6abfaac055 100644 --- a/src/Migration/Resources/Database/Database.php +++ b/src/Migration/Resources/Database/Database.php @@ -26,7 +26,7 @@ class Database extends Resource protected bool $enabled = true, protected string $originalId = '', protected string $type = '', - protected string $database = '' + protected ?string $database = '' ) { $this->id = $id; } @@ -52,7 +52,7 @@ class Database extends Resource enabled: $array['enabled'] ?? true, originalId: $array['originalId'] ?? '', type: $array['type'] ?? 'legacy', - database: $array['database'] + database: $array['database'] ?? null ); } @@ -97,7 +97,7 @@ class Database extends Resource return $this->type; } - public function getDatabase(): string + public function getDatabase(): ?string { return $this->database; } diff --git a/src/Migration/Resources/Database/Table.php b/src/Migration/Resources/Database/Table.php index 889d90fe1f..0ba04a5cda 100644 --- a/src/Migration/Resources/Database/Table.php +++ b/src/Migration/Resources/Database/Table.php @@ -53,7 +53,7 @@ class Table extends Resource Database::fromArray($array['database']), name: $array['name'], id: $array['id'], - rowSecurity: $array['rowSecurity'] ?? $array['documentSecurity'], + rowSecurity: (bool) ($array['rowSecurity'] ?? $array['documentSecurity'] ?? false), permissions: $array['permissions'] ?? [], createdAt: $array['createdAt'] ?? '', updatedAt: $array['updatedAt'] ?? '', diff --git a/src/Migration/Resources/Functions/Func.php b/src/Migration/Resources/Functions/Func.php index 930f92191a..ef5e69341f 100644 --- a/src/Migration/Resources/Functions/Func.php +++ b/src/Migration/Resources/Functions/Func.php @@ -18,6 +18,10 @@ class Func extends Resource * @param int $timeout * @param string $activeDeployment * @param string $entrypoint + * @param string $commands + * @param bool $logging + * @param array $scopes + * @param string $specification */ public function __construct( string $id, @@ -29,7 +33,11 @@ class Func extends Resource private readonly string $schedule = '', private readonly int $timeout = 0, private readonly string $activeDeployment = '', - private readonly string $entrypoint = '' + private readonly string $entrypoint = '', + private readonly string $commands = '', + private readonly bool $logging = true, + private readonly array $scopes = [], + private readonly string $specification = '' ) { $this->id = $id; } @@ -50,7 +58,11 @@ class Func extends Resource $array['schedule'] ?? '', $array['timeout'] ?? 0, $array['activeDeployment'] ?? '', - $array['entrypoint'] ?? '' + $array['entrypoint'] ?? '', + $array['commands'] ?? '', + $array['logging'] ?? true, + $array['scopes'] ?? [], + $array['specification'] ?? '' ); } @@ -70,6 +82,10 @@ class Func extends Resource 'timeout' => $this->timeout, 'activeDeployment' => $this->activeDeployment, 'entrypoint' => $this->entrypoint, + 'commands' => $this->commands, + 'logging' => $this->logging, + 'scopes' => $this->scopes, + 'specification' => $this->specification, ]; } @@ -133,4 +149,27 @@ class Func extends Resource { return $this->entrypoint; } + + public function getCommands(): string + { + return $this->commands; + } + + public function getLogging(): bool + { + return $this->logging; + } + + /** + * @return array + */ + public function getScopes(): array + { + return $this->scopes; + } + + public function getSpecification(): string + { + return $this->specification; + } } diff --git a/src/Migration/Resources/Sites/EnvVar.php b/src/Migration/Resources/Sites/EnvVar.php index 78fce7e18b..a2300f3030 100644 --- a/src/Migration/Resources/Sites/EnvVar.php +++ b/src/Migration/Resources/Sites/EnvVar.php @@ -67,5 +67,4 @@ class EnvVar extends Resource { return $this->value; } - } diff --git a/src/Migration/Resources/Storage/Bucket.php b/src/Migration/Resources/Storage/Bucket.php index cd6fdf0621..a667ffdc17 100644 --- a/src/Migration/Resources/Storage/Bucket.php +++ b/src/Migration/Resources/Storage/Bucket.php @@ -19,6 +19,7 @@ class Bucket extends Resource * @param bool $encryption * @param bool $antiVirus * @param bool $updateLimits + * @param bool $transformations */ public function __construct( string $id = '', @@ -32,6 +33,7 @@ class Bucket extends Resource private readonly bool $encryption = false, private readonly bool $antiVirus = false, private readonly bool $updateLimits = false, + private readonly bool $transformations = false, ) { $this->id = $id; $this->permissions = $permissions; @@ -54,7 +56,8 @@ class Bucket extends Resource $array['compression'] ?? 'none', $array['encryption'] ?? false, $array['antiVirus'] ?? false, - $array['updateLimits'] ?? false + $array['updateLimits'] ?? false, + $array['transformations'] ?? false ); } @@ -74,6 +77,7 @@ class Bucket extends Resource 'encryption' => $this->encryption, 'antiVirus' => $this->antiVirus, 'updateLimits' => $this->updateLimits, + 'transformations' => $this->transformations, ]; } @@ -131,4 +135,9 @@ class Bucket extends Resource { return $this->antiVirus; } + + public function getTransformations(): bool + { + return $this->transformations; + } } diff --git a/src/Migration/Source.php b/src/Migration/Source.php index 2dcf205fae..bf129b2bc9 100644 --- a/src/Migration/Source.php +++ b/src/Migration/Source.php @@ -51,6 +51,11 @@ abstract class Source extends Target return static::$defaultBatchSize; } + public function getBackupsBatchSize(): int + { + return static::$defaultBatchSize; + } + /** * @param array $resources * @return void @@ -115,6 +120,7 @@ abstract class Source extends Target Transfer::GROUP_MESSAGING => Transfer::GROUP_MESSAGING_RESOURCES, Transfer::GROUP_SITES => Transfer::GROUP_SITES_RESOURCES, Transfer::GROUP_INTEGRATIONS => Transfer::GROUP_INTEGRATIONS_RESOURCES, + Transfer::GROUP_BACKUPS => Transfer::GROUP_BACKUPS_RESOURCES, ]; foreach ($mapping as $group => $resources) { @@ -152,6 +158,9 @@ abstract class Source extends Target case Transfer::GROUP_INTEGRATIONS: $this->exportGroupIntegrations($this->getIntegrationsBatchSize(), $resources); break; + case Transfer::GROUP_BACKUPS: + $this->exportGroupBackups($this->getBackupsBatchSize(), $resources); + break; } } } @@ -211,4 +220,12 @@ abstract class Source extends Target * @param array $resources Resources to export */ abstract protected function exportGroupIntegrations(int $batchSize, array $resources): void; + + /** + * Export Backups Group + * + * @param int $batchSize + * @param array $resources Resources to export + */ + abstract protected function exportGroupBackups(int $batchSize, array $resources): void; } diff --git a/src/Migration/Sources/Appwrite.php b/src/Migration/Sources/Appwrite.php index 1f51fa6b97..b1de4654c7 100644 --- a/src/Migration/Sources/Appwrite.php +++ b/src/Migration/Sources/Appwrite.php @@ -5,11 +5,11 @@ namespace Utopia\Migration\Sources; use Appwrite\AppwriteException; use Appwrite\Client; use Appwrite\Query; -use Appwrite\Services\Databases; use Appwrite\Services\Functions; use Appwrite\Services\Messaging; use Appwrite\Services\Sites; use Appwrite\Services\Storage; +use Appwrite\Services\TablesDB; use Appwrite\Services\Teams; use Appwrite\Services\Users; use Utopia\Database\Database as UtopiaDatabase; @@ -24,6 +24,7 @@ use Utopia\Migration\Resources\Auth\User; use Utopia\Migration\Resources\Database\Attribute; use Utopia\Migration\Resources\Database\Collection; use Utopia\Migration\Resources\Database\Column; +use Utopia\Migration\Resources\Database\Columns\BigInt; use Utopia\Migration\Resources\Database\Columns\Boolean; use Utopia\Migration\Resources\Database\Columns\DateTime; use Utopia\Migration\Resources\Database\Columns\Decimal; @@ -132,7 +133,7 @@ class Appwrite extends Source switch ($this->source) { case static::SOURCE_API: - $this->reader = new APIReader(new Databases($this->client)); + $this->reader = new APIReader(new TablesDB($this->client)); break; case static::SOURCE_DATABASE: @@ -149,7 +150,6 @@ class Appwrite extends Source default: throw new \Exception('Unknown source', Exception::CODE_VALIDATION); } - } public function setConsoleKey(string $key): void @@ -239,6 +239,11 @@ class Appwrite extends Source // Integrations Resource::TYPE_PLATFORM, + + // Backups + Resource::TYPE_BACKUP_POLICY, + + // Settings ]; } @@ -278,6 +283,7 @@ class Appwrite extends Source $this->reportMessaging($resources, $report, $resourceIds); $this->reportSites($resources, $report, $resourceIds); $this->reportIntegrations($resources, $report, $resourceIds); + $this->reportBackups($resources, $report, $resourceIds); $report['version'] = $this->call( 'GET', @@ -323,7 +329,7 @@ class Appwrite extends Source limit: 1 ); $userList = $this->users->list($userQueries); - $report[Resource::TYPE_USER] = $userList['total']; + $report[Resource::TYPE_USER] = $userList->total; } if ($needTeams) { @@ -339,11 +345,11 @@ class Appwrite extends Source ); $teamList = $this->teams->list($params); - $totalTeams = $teamList['total']; - $currentTeams = $teamList['teams']; + $totalTeams = $teamList->total; + $currentTeams = $teamList->teams; $allTeams = array_merge($allTeams, $currentTeams); - $lastTeam = $currentTeams[count($currentTeams) - 1]['$id'] ?? null; + $lastTeam = empty($currentTeams) ? null : end($currentTeams)->id; if (count($currentTeams) < self::DEFAULT_PAGE_LIMIT) { break; @@ -357,7 +363,7 @@ class Appwrite extends Source limit: 1 ); $teamList = $this->teams->list($params); - $teams = ['total' => $teamList['total'], 'teams' => []]; + $teams = ['total' => $teamList->total, 'teams' => []]; } } @@ -369,9 +375,9 @@ class Appwrite extends Source $report[Resource::TYPE_MEMBERSHIP] = 0; foreach ($teams['teams'] as $team) { $report[Resource::TYPE_MEMBERSHIP] += $this->teams->listMemberships( - $team['$id'], + $team->id, [Query::limit(1)] - )['total']; + )->total; } } } @@ -393,14 +399,13 @@ class Appwrite extends Source */ private function reportStorage(array $resources, array &$report, array $resourceIds = []): void { - if (\in_array(Resource::TYPE_BUCKET, $resources)) { $bucketQueries = $this->buildQueries( resourceType: Resource::TYPE_BUCKET, resourceIds: $resourceIds, limit: 1 ); - $report[Resource::TYPE_BUCKET] = $this->storage->listBuckets($bucketQueries)['total']; + $report[Resource::TYPE_BUCKET] = $this->storage->listBuckets($bucketQueries)->total; } if (\in_array(Resource::TYPE_FILE, $resources)) { @@ -415,10 +420,10 @@ class Appwrite extends Source resourceIds: $resourceIds, cursor: $lastBucket, ); - $currentBuckets = $this->storage->listBuckets($queries)['buckets']; + $currentBuckets = $this->storage->listBuckets($queries)->buckets; $buckets = array_merge($buckets, $currentBuckets); - $lastBucket = $buckets[count($buckets) - 1]['$id'] ?? null; + $lastBucket = $buckets[count($buckets) - 1]->id ?? null; if (count($currentBuckets) < self::DEFAULT_PAGE_LIMIT) { break; @@ -427,12 +432,12 @@ class Appwrite extends Source foreach ($buckets as $bucket) { $filesResponse = $this->storage->listFiles( - $bucket['$id'], + $bucket->id, [Query::limit(1)] ); - $report['size'] += $bucket['totalSize'] ?? 0; - $report[Resource::TYPE_FILE] += $filesResponse['total']; + $report['size'] += $bucket->totalSize ?? 0; + $report[Resource::TYPE_FILE] += $filesResponse->total; } $report['size'] = $report['size'] / 1000 / 1000; // MB @@ -455,7 +460,7 @@ class Appwrite extends Source resourceIds: $resourceIds, limit: 1 ); - $report[Resource::TYPE_FUNCTION] = $this->functions->list($functionQueries)['total']; + $report[Resource::TYPE_FUNCTION] = $this->functions->list($functionQueries)->total; return; } @@ -469,11 +474,11 @@ class Appwrite extends Source ); $funcList = $this->functions->list($params); - $totalFunctions = $funcList['total']; - $currentFunctions = $funcList['functions']; + $totalFunctions = $funcList->total; + $currentFunctions = $funcList->functions; $functions = array_merge($functions, $currentFunctions); - $lastFunction = $currentFunctions[count($currentFunctions) - 1]['$id'] ?? null; + $lastFunction = $currentFunctions[count($currentFunctions) - 1]->id ?? null; if (count($currentFunctions) < self::DEFAULT_PAGE_LIMIT) { break; } @@ -487,7 +492,7 @@ class Appwrite extends Source if (\in_array(Resource::TYPE_DEPLOYMENT, $resources)) { $report[Resource::TYPE_DEPLOYMENT] = 0; foreach ($functions as $function) { - if (!empty($function['deploymentId'])) { + if (!empty($function->deploymentId)) { $report[Resource::TYPE_DEPLOYMENT] += 1; } } @@ -497,7 +502,7 @@ class Appwrite extends Source $report[Resource::TYPE_ENVIRONMENT_VARIABLE] = 0; foreach ($functions as $function) { // function model contains `vars`, we don't need to fetch the list again. - $report[Resource::TYPE_ENVIRONMENT_VARIABLE] += count($function['vars'] ?? []); + $report[Resource::TYPE_ENVIRONMENT_VARIABLE] += count($function->vars ?? []); } } } @@ -518,7 +523,7 @@ class Appwrite extends Source resourceIds: $resourceIds, limit: 1 ); - $report[Resource::TYPE_SITE] = $this->sites->list($siteQueries)['total']; + $report[Resource::TYPE_SITE] = $this->sites->list($siteQueries)->total; return; } @@ -532,15 +537,15 @@ class Appwrite extends Source ); $siteList = $this->sites->list($params); - $totalSites = $siteList['total']; - $currentSites = $siteList['sites']; + $totalSites = $siteList->total; + $currentSites = $siteList->sites; $sites = array_merge($sites, $currentSites); if (count($currentSites) === 0 || count($currentSites) < self::DEFAULT_PAGE_LIMIT) { break; } - $lastSite = $currentSites[count($currentSites) - 1]['$id']; + $lastSite = $currentSites[count($currentSites) - 1]->id; } } @@ -551,7 +556,7 @@ class Appwrite extends Source if (\in_array(Resource::TYPE_SITE_DEPLOYMENT, $resources)) { $report[Resource::TYPE_SITE_DEPLOYMENT] = 0; foreach ($sites as $site) { - if (!empty($site['deploymentId'])) { + if (!empty($site->deploymentId)) { $report[Resource::TYPE_SITE_DEPLOYMENT] += 1; } } @@ -560,8 +565,8 @@ class Appwrite extends Source if (\in_array(Resource::TYPE_SITE_VARIABLE, $resources)) { $report[Resource::TYPE_SITE_VARIABLE] = 0; foreach ($sites as $site) { - $variables = $this->sites->listVariables($site['$id']); - $report[Resource::TYPE_SITE_VARIABLE] += $variables['total'] ?? 0; + $variables = $this->sites->listVariables($site->id); + $report[Resource::TYPE_SITE_VARIABLE] += $variables->total ?? 0; } } } @@ -639,27 +644,27 @@ class Appwrite extends Source } $response = $this->users->list($queries); - if ($response['total'] == 0) { + if ($response->total == 0) { break; } - foreach ($response['users'] as $user) { + foreach ($response->users as $user) { $users[] = new User( - $user['$id'], - empty($user['email']) ? null : $user['email'], - empty($user['name']) ? null : $user['name'], - $user['password'] ? new Hash($user['password'], algorithm: $user['hash']) : null, - empty($user['phone']) ? null : $user['phone'], - $user['labels'] ?? [], + $user->id, + empty($user->email) ? null : $user->email, + empty($user->name) ? null : $user->name, + $user->password ? new Hash($user->password, algorithm: $user->hash) : null, + empty($user->phone) ? null : $user->phone, + $user->labels ?? [], '', - $user['emailVerification'] ?? false, - $user['phoneVerification'] ?? false, - !$user['status'], - $user['prefs'] ?? [], - $user['targets'] ?? [], + $user->emailVerification ?? false, + $user->phoneVerification ?? false, + !$user->status, + $user->prefs->data ?? [], + \array_map(fn ($target) => $target->toArray(), $user->targets ?? []), ); - $lastDocument = $user['$id']; + $lastDocument = $user->id; } $this->callback($users); @@ -693,18 +698,18 @@ class Appwrite extends Source } $response = $this->teams->list($queries); - if ($response['total'] == 0) { + if ($response->total == 0) { break; } - foreach ($response['teams'] as $team) { + foreach ($response->teams as $team) { $teams[] = new Team( - $team['$id'], - $team['name'], - $team['prefs'], + $team->id, + $team->name, + $team->prefs->data, ); - $lastDocument = $team['$id']; + $lastDocument = $team->id; } $this->callback($teams); @@ -745,25 +750,25 @@ class Appwrite extends Source $response = $this->teams->listMemberships($team->getId(), $queries); - if ($response['total'] == 0) { + if ($response->total == 0) { break; } - foreach ($response['memberships'] as $membership) { - $user = $cacheUsers[$membership['userId']] ?? null; + foreach ($response->memberships as $membership) { + $user = $cacheUsers[$membership->userId] ?? null; if ($user === null) { throw new \Exception('User not found', Exception::CODE_NOT_FOUND); } $memberships[] = new Membership( - $membership['$id'], + $membership->id, $team, $user, - $membership['roles'], - $membership['confirm'] + $membership->roles, + $membership->confirm ); - $lastDocument = $membership['$id']; + $lastDocument = $membership->id; } $this->callback($memberships); @@ -908,7 +913,6 @@ class Appwrite extends Source 'enabled' => $database['enabled'] ?? true, ]); $databases[] = $newDatabase; - } } @@ -967,10 +971,12 @@ class Appwrite extends Source $response = $this->reader->listTables($database, $queries); foreach ($response as $table) { + $rowSecurity = $table['rowSecurity'] ?? $table['documentSecurity'] ?? false; $newTable = self::getEntity($databaseName, [ 'id' => $table['$id'], 'name' => $table['name'], - 'documentSecurity' => $table['documentSecurity'], + 'rowSecurity' => $rowSecurity, + 'documentSecurity' => $rowSecurity, 'permissions' => $table['$permissions'], 'createdAt' => $table['$createdAt'], 'updatedAt' => $table['$updatedAt'], @@ -1034,7 +1040,7 @@ class Appwrite extends Source } /** @var Table $table */ - $col = match($table->getDatabase()->getType()) { + $col = match ($table->getDatabase()->getType()) { Resource::TYPE_DATABASE_VECTORSDB => self::getColumn($table, $column)->getAttribute(), default => self::getColumn($table, $column), }; @@ -1192,10 +1198,11 @@ class Appwrite extends Source unset($row['$id']); unset($row['$permissions']); - unset($row['$collectionId']); unset($row['$databaseId']); unset($row['$sequence']); unset($row['$collection']); + unset($row['$tableId']); + unset($row['$table']); $row = self::getRecord($table->getDatabase()->getType(), [ 'id' => $id, @@ -1279,18 +1286,20 @@ class Appwrite extends Source $convertedBuckets = []; - foreach ($buckets['buckets'] as $bucket) { + foreach ($buckets->buckets as $bucket) { $bucket = new Bucket( - $bucket['$id'], - $bucket['name'], - $bucket['$permissions'], - $bucket['fileSecurity'], - $bucket['enabled'], - $bucket['maximumFileSize'], - $bucket['allowedFileExtensions'], - $bucket['compression'], - $bucket['encryption'], - $bucket['antivirus'], + $bucket->id, + $bucket->name, + $bucket->permissions, + $bucket->fileSecurity, + $bucket->enabled, + $bucket->maximumFileSize, + $bucket->allowedFileExtensions, + $bucket->compression, + $bucket->encryption, + $bucket->antivirus, + false, + $bucket->transformations ?? false, ); $convertedBuckets[] = $bucket; } @@ -1325,31 +1334,31 @@ class Appwrite extends Source $queries ); - foreach ($response['files'] as $file) { + foreach ($response->files as $file) { try { $this->exportFileData(new File( - $file['$id'], + $file->id, $bucket, - $file['name'], - $file['signature'], - $file['mimeType'], - $file['$permissions'], - $file['sizeOriginal'], + $file->name, + $file->signature, + $file->mimeType, + $file->permissions, + $file->sizeOriginal, )); } catch (\Throwable $e) { $this->addError(new Exception( resourceName: Resource::TYPE_FILE, resourceGroup: Transfer::GROUP_STORAGE, - resourceId: $file['$id'], + resourceId: $file->id, message: $e->getMessage(), code: $e->getCode() )); } - $lastDocument = $file['$id']; + $lastDocument = $file->id; } - if (count($response['files']) < $batchSize) { + if (count($response->files) < $batchSize) { break; } } @@ -1458,6 +1467,18 @@ class Appwrite extends Source } } + protected function exportGroupBackups(int $batchSize, array $resources): void + { + // No-op: backup policies are Cloud-only + } + + protected function reportBackups(array $resources, array &$report, array $resourceIds = []): void + { + if (\in_array(Resource::TYPE_BACKUP_POLICY, $resources)) { + $report[Resource::TYPE_BACKUP_POLICY] = 0; + } + } + /** * @throws AppwriteException */ @@ -1483,36 +1504,40 @@ class Appwrite extends Source $response = $this->functions->list($queries); - if ($response['total'] === 0) { + if ($response->total === 0) { return; } $functions = []; $convertedResources = []; - foreach ($response['functions'] as $function) { + foreach ($response->functions as $function) { $convertedFunc = new Func( - $function['$id'], - $function['name'], - $function['runtime'], - $function['execute'], - $function['enabled'], - $function['events'], - $function['schedule'], - $function['timeout'], - $function['deploymentId'] ?? '', - $function['entrypoint'] + $function->id, + $function->name, + $function->runtime, + $function->execute, + $function->enabled, + $function->events, + $function->schedule, + $function->timeout, + $function->deploymentId ?? '', + $function->entrypoint, + $function->commands ?? '', + $function->logging ?? true, + $function->scopes ?? [], + $function->runtimeSpecification ?: $function->buildSpecification ?: '', ); $functions[] = $convertedFunc; $convertedResources[] = $convertedFunc; - foreach ($function['vars'] as $var) { + foreach ($function->vars as $var) { $convertedResources[] = new EnvVar( - $var['$id'], + $var->id, $convertedFunc, - $var['key'], - $var['value'], + $var->key, + $var->value, ); } } @@ -1567,17 +1592,17 @@ class Appwrite extends Source $queries ); - foreach ($response['deployments'] as $deployment) { + foreach ($response->deployments as $deployment) { try { $this->exportDeploymentData($func, $deployment); } catch (\Throwable $e) { $func->setStatus(Resource::STATUS_ERROR, $e->getMessage()); } - $lastDocument = $deployment['$id']; + $lastDocument = $deployment->id; } - if (count($response['deployments']) < $batchSize) { + if (count($response->deployments) < $batchSize) { break; } } @@ -1587,7 +1612,7 @@ class Appwrite extends Source /** * @throws \Exception */ - private function exportDeploymentData(Func $func, array $deployment): void + private function exportDeploymentData(Func $func, \Appwrite\Models\Deployment $deployment): void { // Set the chunk size (5MB) $start = 0; @@ -1598,7 +1623,7 @@ class Appwrite extends Source $this->call( 'HEAD', - "/functions/{$func->getId()}/deployments/{$deployment['$id']}/download", + "/functions/{$func->getId()}/deployments/{$deployment->id}/download", [], [], $responseHeaders @@ -1608,7 +1633,7 @@ class Appwrite extends Source if (!array_key_exists('content-length', $responseHeaders)) { $file = $this->call( 'GET', - "/functions/{$func->getId()}/deployments/{$deployment['$id']}/download", + "/functions/{$func->getId()}/deployments/{$deployment->id}/download", [], [], $responseHeaders @@ -1621,14 +1646,14 @@ class Appwrite extends Source } $deployment = new Deployment( - $deployment['$id'], + $deployment->id, $func, $size, - $deployment['entrypoint'], + $deployment->entrypoint, $start, $end, $file, - $deployment['activate'] + $deployment->activate ); $deployment->setSequence($deployment->getId()); @@ -1644,14 +1669,14 @@ class Appwrite extends Source } $deployment = new Deployment( - $deployment['$id'], + $deployment->id, $func, $fileSize, - $deployment['entrypoint'], + $deployment->entrypoint, $start, $end, '', - $deployment['activate'] + $deployment->activate ); $deployment->setSequence($deployment->getId()); @@ -1696,7 +1721,7 @@ class Appwrite extends Source resourceIds: $resourceIds, limit: 1 ); - $report[Resource::TYPE_PROVIDER] = $this->messaging->listProviders($providerQueries)['total']; + $report[Resource::TYPE_PROVIDER] = $this->messaging->listProviders($providerQueries)->total; } if (\in_array(Resource::TYPE_TOPIC, $resources)) { @@ -1705,7 +1730,7 @@ class Appwrite extends Source resourceIds: $resourceIds, limit: 1 ); - $report[Resource::TYPE_TOPIC] = $this->messaging->listTopics($topicQueries)['total']; + $report[Resource::TYPE_TOPIC] = $this->messaging->listTopics($topicQueries)->total; } if (\in_array(Resource::TYPE_SUBSCRIBER, $resources)) { @@ -1719,16 +1744,16 @@ class Appwrite extends Source } $topicResponse = $this->messaging->listTopics($topicQueries); - if ($topicResponse['total'] == 0 || empty($topicResponse['topics'])) { + if ($topicResponse->total == 0 || empty($topicResponse->topics)) { break; } - foreach ($topicResponse['topics'] as $topic) { - $subscriberTotal += $this->messaging->listSubscribers($topic['$id'], [Query::limit(1)])['total']; - $lastTopic = $topic['$id']; + foreach ($topicResponse->topics as $topic) { + $subscriberTotal += $this->messaging->listSubscribers($topic->id, [Query::limit(1)])->total; + $lastTopic = $topic->id; } - if (\count($topicResponse['topics']) < self::DEFAULT_PAGE_LIMIT) { + if (\count($topicResponse->topics) < self::DEFAULT_PAGE_LIMIT) { break; } } @@ -1742,7 +1767,7 @@ class Appwrite extends Source resourceIds: $resourceIds, limit: 1 ); - $report[Resource::TYPE_MESSAGE] = $this->messaging->listMessages($messageQueries)['total']; + $report[Resource::TYPE_MESSAGE] = $this->messaging->listMessages($messageQueries)->total; } } @@ -1828,24 +1853,24 @@ class Appwrite extends Source $response = $this->messaging->listProviders($queries); - if ($response['total'] == 0) { + if ($response->total == 0) { break; } - foreach ($response['providers'] as $provider) { + foreach ($response->providers as $provider) { $providers[] = new Provider( - $provider['$id'], - $provider['name'], - $provider['provider'], - $provider['type'], - $provider['enabled'], - $provider['credentials'] ?? [], - $provider['options'] ?? [], - $provider['$createdAt'] ?? '', - $provider['$updatedAt'] ?? '', + $provider->id, + $provider->name, + $provider->provider, + $provider->type, + $provider->enabled, + $provider->credentials ?? [], + $provider->options ?? [], + $provider->createdAt ?? '', + $provider->updatedAt ?? '', ); - $lastDocument = $provider['$id']; + $lastDocument = $provider->id; } $this->callback($providers); @@ -1879,20 +1904,20 @@ class Appwrite extends Source $response = $this->messaging->listTopics($queries); - if ($response['total'] == 0) { + if ($response->total == 0) { break; } - foreach ($response['topics'] as $topic) { + foreach ($response->topics as $topic) { $topics[] = new Topic( - $topic['$id'], - $topic['name'], - $topic['subscribe'] ?? [], - $topic['$createdAt'] ?? '', - $topic['$updatedAt'] ?? '', + $topic->id, + $topic->name, + $topic->subscribe ?? [], + $topic->createdAt ?? '', + $topic->updatedAt ?? '', ); - $lastDocument = $topic['$id']; + $lastDocument = $topic->id; } $this->callback($topics); @@ -1925,23 +1950,23 @@ class Appwrite extends Source $response = $this->messaging->listSubscribers($topic->getId(), $queries); - if ($response['total'] == 0) { + if ($response->total == 0) { break; } - foreach ($response['subscribers'] as $subscriber) { + foreach ($response->subscribers as $subscriber) { $subscribers[] = new Subscriber( - $subscriber['$id'], - $subscriber['topicId'], - $subscriber['targetId'], - $subscriber['userId'] ?? '', - $subscriber['userName'] ?? '', - $subscriber['providerType'] ?? '', - $subscriber['$createdAt'] ?? '', - $subscriber['$updatedAt'] ?? '', + $subscriber->id, + $subscriber->topicId, + $subscriber->targetId, + $subscriber->userId ?? '', + $subscriber->userName ?? '', + $subscriber->providerType ?? '', + $subscriber->createdAt ?? '', + $subscriber->updatedAt ?? '', ); - $lastDocument = $subscriber['$id']; + $lastDocument = $subscriber->id; } $this->callback($subscribers); @@ -1976,28 +2001,28 @@ class Appwrite extends Source $response = $this->messaging->listMessages($queries); - if ($response['total'] == 0) { + if ($response->total == 0) { break; } - foreach ($response['messages'] as $message) { + foreach ($response->messages as $message) { $messages[] = new Message( - $message['$id'], - $message['providerType'] ?? '', - $message['topics'] ?? [], - $message['users'] ?? [], - $message['targets'] ?? [], - $message['data'] ?? [], - $message['status'] ?? '', - $message['scheduledAt'] ?? '', - $message['deliveredAt'] ?? '', - $message['deliveryErrors'] ?? [], - $message['deliveredTotal'] ?? 0, - $message['$createdAt'] ?? '', - $message['$updatedAt'] ?? '', + $message->id, + $message->providerType ?? '', + $message->topics ?? [], + $message->users ?? [], + $message->targets ?? [], + $message->data ?? [], + (string) $message->status, + $message->scheduledAt ?? '', + $message->deliveredAt ?? '', + $message->deliveryErrors ?? [], + $message->deliveredTotal ?? 0, + $message->createdAt ?? '', + $message->updatedAt ?? '', ); - $lastDocument = $message['$id']; + $lastDocument = $message->id; } $this->callback($messages); @@ -2031,40 +2056,40 @@ class Appwrite extends Source $response = $this->sites->list($queries); - if ($response['total'] === 0) { + if ($response->total === 0) { return; } $sites = []; $convertedResources = []; - foreach ($response['sites'] as $site) { + foreach ($response->sites as $site) { $convertedSite = new Site( - $site['$id'], - $site['name'], - $site['framework'], - $site['buildRuntime'], - $site['enabled'], - $site['logging'], - $site['timeout'], - $site['installCommand'] ?? '', - $site['buildCommand'] ?? '', - $site['outputDirectory'] ?? '', - $site['adapter'] ?? 'static', - $site['fallbackFile'] ?? '', - $site['specification'] ?? '', - $site['deploymentId'] ?? '' + $site->id, + $site->name, + $site->framework, + $site->buildRuntime, + $site->enabled, + $site->logging, + $site->timeout, + $site->installCommand ?? '', + $site->buildCommand ?? '', + $site->outputDirectory ?? '', + $site->adapter ?? 'static', + $site->fallbackFile ?? '', + $site->runtimeSpecification ?: $site->buildSpecification ?: '', + $site->deploymentId ?? '' ); $sites[] = $convertedSite; $convertedResources[] = $convertedSite; - $variables = $this->sites->listVariables($site['$id']); - foreach ($variables['variables'] ?? [] as $var) { + $variables = $this->sites->listVariables($site->id); + foreach ($variables->variables ?? [] as $var) { $convertedResources[] = new SiteEnvVar( - $var['$id'], + $var->id, $convertedSite, - $var['key'], - $var['value'] + $var->key, + $var->value ); } } @@ -2120,17 +2145,17 @@ class Appwrite extends Source $queries ); - foreach ($response['deployments'] as $deployment) { + foreach ($response->deployments as $deployment) { try { $this->exportSiteDeploymentData($site, $deployment); } catch (\Throwable $e) { $site->setStatus(Resource::STATUS_ERROR, $e->getMessage()); } - $lastDocument = $deployment['$id']; + $lastDocument = $deployment->id; } - if (count($response['deployments']) < $batchSize) { + if (count($response->deployments) < $batchSize) { break; } } @@ -2140,7 +2165,7 @@ class Appwrite extends Source /** * @throws \Exception */ - private function exportSiteDeploymentData(Site $site, array $deployment): void + private function exportSiteDeploymentData(Site $site, \Appwrite\Models\Deployment $deployment): void { $start = 0; $end = Transfer::STORAGE_MAX_CHUNK_SIZE - 1; @@ -2149,7 +2174,7 @@ class Appwrite extends Source $this->call( 'HEAD', - "/sites/{$site->getId()}/deployments/{$deployment['$id']}/download", + "/sites/{$site->getId()}/deployments/{$deployment->id}/download", [], [], $responseHeaders @@ -2158,7 +2183,7 @@ class Appwrite extends Source if (!\array_key_exists('content-length', $responseHeaders)) { $file = $this->call( 'GET', - "/sites/{$site->getId()}/deployments/{$deployment['$id']}/download", + "/sites/{$site->getId()}/deployments/{$deployment->id}/download", [], [], $responseHeaders @@ -2171,13 +2196,13 @@ class Appwrite extends Source } $siteDeployment = new SiteDeployment( - $deployment['$id'], + $deployment->id, $site, $size, $start, $end, $file, - $deployment['$id'] === $site->getActiveDeployment() + $deployment->id === $site->getActiveDeployment() ); $siteDeployment->setSequence($siteDeployment->getId()); @@ -2193,13 +2218,13 @@ class Appwrite extends Source } $siteDeployment = new SiteDeployment( - $deployment['$id'], + $deployment->id, $site, $fileSize, $start, $end, '', - $deployment['$id'] === $site->getActiveDeployment() + $deployment->id === $site->getActiveDeployment() ); $siteDeployment->setSequence($siteDeployment->getId()); @@ -2494,6 +2519,19 @@ class Appwrite extends Source updatedAt: $column['$updatedAt'] ?? '', ), + Column::TYPE_BIG_INT => new BigInt( + $column['key'], + $table, + required: $column['required'], + default: $column['default'], + array: $column['array'], + min: $column['min'] ?? null, + max: $column['max'] ?? null, + signed: $column['signed'] ?? true, + createdAt: $column['$createdAt'] ?? '', + updatedAt: $column['$updatedAt'] ?? '', + ), + Column::TYPE_FLOAT => new Decimal( $column['key'], $table, @@ -2622,7 +2660,6 @@ class Appwrite extends Source default => throw new \InvalidArgumentException("Unsupported column type: {$column['type']}"), }; - } /** diff --git a/src/Migration/Sources/Appwrite/Reader.php b/src/Migration/Sources/Appwrite/Reader.php index d7a18027a3..fb6efd98d4 100644 --- a/src/Migration/Sources/Appwrite/Reader.php +++ b/src/Migration/Sources/Appwrite/Reader.php @@ -108,5 +108,4 @@ interface Reader */ public function queryLimit(int $limit): mixed; public function getSupportForAttributes(): bool; - } diff --git a/src/Migration/Sources/Appwrite/Reader/API.php b/src/Migration/Sources/Appwrite/Reader/API.php index b42835bd70..a187e8c927 100644 --- a/src/Migration/Sources/Appwrite/Reader/API.php +++ b/src/Migration/Sources/Appwrite/Reader/API.php @@ -4,21 +4,23 @@ namespace Utopia\Migration\Sources\Appwrite\Reader; use Appwrite\AppwriteException; use Appwrite\Query; -use Appwrite\Services\Databases; -/* use Appwrite\Services\Tables; */ +use Appwrite\Services\TablesDB; use Utopia\Migration\Resource; use Utopia\Migration\Resources\Database\Database; use Utopia\Migration\Resources\Database\Table; use Utopia\Migration\Sources\Appwrite\Reader; /** + * Adapts Appwrite SDK typed models to the Reader's plain-array contract so that + * downstream callers can use array-offset syntax interchangeably with the + * Database reader (which returns utopia-php Document objects). + * * @implements Reader */ class API implements Reader { public function __construct( - private readonly Databases $database, - /* private readonly Tables $table, */ + private readonly TablesDB $database, ) { } @@ -53,10 +55,10 @@ class API implements Reader } $databasesResponse = $this->database->list($databaseQueries); - $databases = $databasesResponse['databases']; + $databases = \array_map(fn ($database) => $database->toArray(), $databasesResponse->databases); if (in_array(Resource::TYPE_DATABASE, $resources)) { - $report[Resource::TYPE_DATABASE] = $databasesResponse['total']; + $report[Resource::TYPE_DATABASE] = $databasesResponse->total; } if (count(array_intersect($resources, $relevantResources)) === 1 && @@ -73,13 +75,13 @@ class API implements Reader $lastTable = null; while (true) { - /* $currentTables = $this->tables->list(...); */ - $currentTables = $this->database->listCollections( + $tablesResponse = $this->database->listTables( $databaseId, $lastTable ? [Query::cursorAfter($lastTable)] : [Query::limit($pageLimit)] - )['collections']; /* ['tables'] */ + ); + $currentTables = \array_map(fn ($table) => $table->toArray(), $tablesResponse->tables); $tables = \array_merge($tables, $currentTables); $lastTable = $tables[count($tables) - 1]['$id'] ?? null; @@ -108,14 +110,13 @@ class API implements Reader } if (Resource::isSupported(Resource::TYPE_ROW, $resources)) { - /* $rowsResponse = $this->tables->listRows(...) */ - $rowsResponse = $this->database->listDocuments( + $rowsResponse = $this->database->listRows( $databaseId, $tableId, [Query::limit(1)] ); - $report[Resource::TYPE_ROW] += $rowsResponse['total']; + $report[Resource::TYPE_ROW] += $rowsResponse->total; } } } @@ -125,90 +126,89 @@ class API implements Reader } /** + * @return array> * @throws AppwriteException */ public function listDatabases(array $queries = []): array { - return $this->database->list($queries)['databases']; + return \array_map( + fn ($database) => $database->toArray(), + $this->database->list($queries)->databases + ); } /** + * @return array> * @throws AppwriteException */ public function listTables(Database $resource, array $queries = []): array { - /* $this->tables->list(...)['tables'] */ - return $this->database->listCollections( - $resource->getId(), - $queries - )['collections']; + return \array_map( + fn ($table) => $table->toArray(), + $this->database->listTables($resource->getId(), $queries)->tables + ); } /** - * @param Table $resource - * @param array $queries - * @return array + * @return array> * @throws AppwriteException */ public function listColumns(Table $resource, array $queries = []): array { - /* $this->tables->listColumns(...)['columns'] */ - return $this->database->listAttributes( - $resource->getDatabase()->getId(), - $resource->getId(), - $queries - )['attributes']; + return \array_map( + fn ($column) => $column->toArray(), + $this->database->listColumns( + $resource->getDatabase()->getId(), + $resource->getId(), + $queries + )->columns + ); } /** - * @param Table $resource - * @param array $queries - * @return array + * @return array> * @throws AppwriteException */ public function listIndexes(Table $resource, array $queries = []): array { - /* $this->tables->listIndexes(...)['indexes'] */ - return $this->database->listIndexes( - $resource->getDatabase()->getId(), - $resource->getId(), - $queries - )['indexes']; + return \array_map( + fn ($index) => $index->toArray(), + $this->database->listIndexes( + $resource->getDatabase()->getId(), + $resource->getId(), + $queries + )->indexes + ); } - /** - * @param Table $resource - * @param array $queries - * @return array + * @return array> * @throws AppwriteException */ public function listRows(Table $resource, array $queries = []): array { - /* $this->tables->listRows(...)['rows'] */ - return $this->database->listDocuments( - $resource->getDatabase()->getId(), - $resource->getId(), - $queries - )['documents']; + return \array_map( + fn ($row) => $row->toArray(), + $this->database->listRows( + $resource->getDatabase()->getId(), + $resource->getId(), + $queries + )->rows + ); } /** - * @param Table $resource - * @param string $rowId - * @param array $queries - * @return array + * @return array * @throws AppwriteException */ public function getRow(Table $resource, string $rowId, array $queries = []): array { - /* $this->tables->getRow(...) */ - return $this->database->getDocument( + return $this->database->getRow( $resource->getDatabase()->getId(), $resource->getId(), $rowId, $queries - ); + )->toArray(); } /** diff --git a/src/Migration/Sources/CSV.php b/src/Migration/Sources/CSV.php index 3454f4a1a3..678b3b9eec 100644 --- a/src/Migration/Sources/CSV.php +++ b/src/Migration/Sources/CSV.php @@ -301,7 +301,7 @@ class CSV extends Source $parsedData[$key] = array_map(function ($item) use ($type) { return match ($type) { - Column::TYPE_INTEGER => is_numeric($item) ? (int)$item : null, + Column::TYPE_INTEGER,Column::TYPE_BIG_INT => is_numeric($item) ? (int)$item : null, Column::TYPE_FLOAT => is_numeric($item) ? (float)$item : null, Column::TYPE_BOOLEAN => filter_var($item, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE), default => $item, @@ -323,6 +323,7 @@ class CSV extends Source 'null' => null, // 'null' string is converted to null '' => match ($type) { Column::TYPE_INTEGER, + Column::TYPE_BIG_INT, Column::TYPE_FLOAT, Column::TYPE_BOOLEAN, Column::TYPE_DATETIME, @@ -330,7 +331,7 @@ class CSV extends Source default => '', // but empty string stays empty string for compatibility }, default => match ($type) { - Column::TYPE_INTEGER => \is_numeric($parsedValue) ? (int)$parsedValue : null, + Column::TYPE_INTEGER, Column::TYPE_BIG_INT => \is_numeric($parsedValue) ? (int)$parsedValue : null, Column::TYPE_FLOAT => \is_numeric($parsedValue) ? (float)$parsedValue : null, Column::TYPE_BOOLEAN => \filter_var( $parsedValue, @@ -434,6 +435,11 @@ class CSV extends Source throw new \Exception('Not Implemented'); } + protected function exportGroupBackups(int $batchSize, array $resources): void + { + throw new \Exception('Not Implemented'); + } + /** * @param callable(resource $stream, string $delimiter): void $callback * @return void diff --git a/src/Migration/Sources/Firebase.php b/src/Migration/Sources/Firebase.php index 94615746ed..007797b5b6 100644 --- a/src/Migration/Sources/Firebase.php +++ b/src/Migration/Sources/Firebase.php @@ -822,4 +822,9 @@ class Firebase extends Source { throw new \Exception('Not implemented'); } + + protected function exportGroupBackups(int $batchSize, array $resources): void + { + throw new \Exception('Not implemented'); + } } diff --git a/src/Migration/Sources/JSON.php b/src/Migration/Sources/JSON.php index 2a2ff81f2e..0b3e7829f1 100644 --- a/src/Migration/Sources/JSON.php +++ b/src/Migration/Sources/JSON.php @@ -209,6 +209,11 @@ class JSON extends Source throw new \Exception('Not Implemented'); } + protected function exportGroupBackups(int $batchSize, array $resources): void + { + throw new \Exception('Not Implemented'); + } + /** * @throws \Exception */ diff --git a/src/Migration/Sources/NHost.php b/src/Migration/Sources/NHost.php index c6bfa41985..c72ca81223 100644 --- a/src/Migration/Sources/NHost.php +++ b/src/Migration/Sources/NHost.php @@ -8,6 +8,7 @@ use Utopia\Migration\Resource; use Utopia\Migration\Resources\Auth\Hash; use Utopia\Migration\Resources\Auth\User; use Utopia\Migration\Resources\Database\Column; +use Utopia\Migration\Resources\Database\Columns\BigInt; use Utopia\Migration\Resources\Database\Columns\Boolean; use Utopia\Migration\Resources\Database\Columns\DateTime; use Utopia\Migration\Resources\Database\Columns\Decimal; @@ -679,7 +680,7 @@ class NHost extends Source $column['column_default'] = null; } - return new Integer( + return new BigInt( $column['column_name'], $table, required: $column['is_nullable'] === 'NO', @@ -956,4 +957,9 @@ class NHost extends Source { throw new \Exception('Not Implemented'); } + + protected function exportGroupBackups(int $batchSize, array $resources): void + { + throw new \Exception('Not Implemented'); + } } diff --git a/src/Migration/Target.php b/src/Migration/Target.php index 8dbc5a87f2..777609f22a 100644 --- a/src/Migration/Target.php +++ b/src/Migration/Target.php @@ -150,7 +150,6 @@ abstract class Target throw new \Exception(\curl_error($ch), Exception::CODE_INTERNAL); } - \curl_close($ch); if ($responseStatus >= 400) { if (\is_array($responseBody)) { diff --git a/src/Migration/Transfer.php b/src/Migration/Transfer.php index 59ed486c80..7925fb3203 100644 --- a/src/Migration/Transfer.php +++ b/src/Migration/Transfer.php @@ -26,6 +26,8 @@ class Transfer public const GROUP_MESSAGING = 'messaging'; + public const GROUP_BACKUPS = 'backups'; + public const GROUP_AUTH_RESOURCES = [ Resource::TYPE_USER, Resource::TYPE_TEAM, @@ -91,6 +93,10 @@ class Transfer public const GROUP_SETTINGS_RESOURCES = []; + public const GROUP_BACKUPS_RESOURCES = [ + Resource::TYPE_BACKUP_POLICY, + ]; + public const GROUP_MESSAGING_RESOURCES = [ Resource::TYPE_PROVIDER, Resource::TYPE_TOPIC, @@ -119,6 +125,7 @@ class Transfer Resource::TYPE_TOPIC, Resource::TYPE_SUBSCRIBER, Resource::TYPE_MESSAGE, + Resource::TYPE_BACKUP_POLICY, // Integrations Resource::TYPE_PLATFORM, @@ -406,6 +413,7 @@ class Transfer self::GROUP_DATABASES_DOCUMENTS_DB => array_merge($resources, self::GROUP_DOCUMENTSDB_RESOURCES), self::GROUP_DATABASES_VECTOR_DB => array_merge($resources, self::GROUP_VECTORSDB_RESOURCES), self::GROUP_MESSAGING => array_merge($resources, self::GROUP_MESSAGING_RESOURCES), + self::GROUP_BACKUPS => array_merge($resources, self::GROUP_BACKUPS_RESOURCES), default => throw new \Exception('No service group found'), }; } diff --git a/tests/Migration/E2E/Sources/Base.php b/tests/Migration/E2E/Sources/Base.php index 7b5bd403e8..9563344e3a 100644 --- a/tests/Migration/E2E/Sources/Base.php +++ b/tests/Migration/E2E/Sources/Base.php @@ -124,7 +124,6 @@ abstract class Base extends TestCase throw new \Exception(curl_error($ch)); } - curl_close($ch); if ($responseStatus >= 400) { if (is_array($responseBody)) { diff --git a/tests/Migration/Unit/Adapters/MockSource.php b/tests/Migration/Unit/Adapters/MockSource.php index 1524dea8c7..a309f91e27 100644 --- a/tests/Migration/Unit/Adapters/MockSource.php +++ b/tests/Migration/Unit/Adapters/MockSource.php @@ -85,6 +85,7 @@ class MockSource extends Source Resource::TYPE_TOPIC, Resource::TYPE_SUBSCRIBER, Resource::TYPE_MESSAGE, + Resource::TYPE_BACKUP_POLICY, // legacy Resource::TYPE_DOCUMENT, @@ -216,4 +217,15 @@ class MockSource extends Source $this->handleResourceTransfer(Transfer::GROUP_INTEGRATIONS, $resource); } } + + protected function exportGroupBackups(int $batchSize, array $resources): void + { + foreach (Transfer::GROUP_BACKUPS_RESOURCES as $resource) { + if (!\in_array($resource, $resources)) { + continue; + } + + $this->handleResourceTransfer(Transfer::GROUP_BACKUPS, $resource); + } + } } diff --git a/tests/Migration/Unit/Destinations/AppwriteAttributeSdkBoundaryTest.php b/tests/Migration/Unit/Destinations/AppwriteAttributeSdkBoundaryTest.php new file mode 100644 index 0000000000..2e5c182a5c --- /dev/null +++ b/tests/Migration/Unit/Destinations/AppwriteAttributeSdkBoundaryTest.php @@ -0,0 +1,96 @@ + + */ + private const PARAM_TO_META_FIELD = [ + 'xdefault' => 'default', + 'min' => null, + 'max' => null, + 'elements' => null, + 'onDelete' => null, + ]; + + public function testSdkExposedFieldsAreNotInNonSdkConstant(): void + { + $sdkExposed = $this->collectSdkExposedMetaFields(); + $nonSdk = $this->readNonSdkConstant(); + + $overlap = \array_intersect($sdkExposed, $nonSdk); + $this->assertSame( + [], + \array_values($overlap), + 'ATTRIBUTE_IMMUTABLE_FIELDS marks these fields as non-SDK, but the appwrite/appwrite ' + . 'SDK exposes them via updateXColumn/updateXAttribute: ' . \implode(', ', $overlap) + . '. Either drop these from ATTRIBUTE_IMMUTABLE_FIELDS or update PARAM_TO_META_FIELD ' + . 'in this test if the SDK param doesn\'t correspond to a top-level meta-doc field.', + ); + } + + /** + * @return list meta-doc field names the SDK can mutate + */ + private function collectSdkExposedMetaFields(): array + { + $fields = []; + foreach ([TablesDB::class, Databases::class] as $service) { + $service = new ReflectionClass($service); + foreach ($service->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { + if (!\preg_match('/^update.+(Column|Attribute)$/', $method->getName())) { + continue; + } + foreach ($method->getParameters() as $param) { + $name = $param->getName(); + if (\in_array($name, self::IGNORED_PARAMS, true)) { + continue; + } + $mapped = \array_key_exists($name, self::PARAM_TO_META_FIELD) + ? self::PARAM_TO_META_FIELD[$name] + : $name; + if ($mapped === null) { + continue; + } + $fields[$mapped] = true; + } + } + } + return \array_keys($fields); + } + + /** + * @return list + */ + private function readNonSdkConstant(): array + { + /** @var list $value */ + $value = (new ReflectionClass(AppwriteDestination::class))->getConstant('ATTRIBUTE_IMMUTABLE_FIELDS'); + return $value; + } +} diff --git a/tests/Migration/Unit/Destinations/AppwriteDestinationDsnTest.php b/tests/Migration/Unit/Destinations/AppwriteDestinationDsnTest.php new file mode 100644 index 0000000000..e6e9e4d8f9 --- /dev/null +++ b/tests/Migration/Unit/Destinations/AppwriteDestinationDsnTest.php @@ -0,0 +1,96 @@ +__metadata' doesn't exist`. + */ +class AppwriteDestinationDsnTest extends TestCase +{ + public function testWithoutResolverReturnsEmptyString(): void + { + $destination = $this->makeDestination(getDatabaseDSN: null); + $resource = $this->makeResource(sourceDsn: 'database_db_fra1_self_hosted_11_0'); + + $resolved = $this->invokeResolver($destination, $resource); + + $this->assertSame('', $resolved, 'Without a resolver the destination must not propagate the source DSN.'); + } + + public function testWithResolverUsesItsReturnValue(): void + { + $expected = 'appwrite://database_db_fra1_self_hosted_17_0?database=appwrite&namespace=_1'; + $destination = $this->makeDestination( + getDatabaseDSN: fn (DatabaseResource $r): string => $expected, + ); + $resource = $this->makeResource(sourceDsn: 'database_db_fra1_self_hosted_11_0'); + + $resolved = $this->invokeResolver($destination, $resource); + + $this->assertSame($expected, $resolved); + $this->assertNotSame($resource->getDatabase(), $resolved, 'Source DSN must not leak through the resolver path.'); + } + + public function testResolverReceivesTheResource(): void + { + $captured = null; + $destination = $this->makeDestination( + getDatabaseDSN: function (DatabaseResource $r) use (&$captured): string { + $captured = $r; + return 'resolved'; + }, + ); + $resource = $this->makeResource(sourceDsn: 'src'); + + $this->invokeResolver($destination, $resource); + + $this->assertSame($resource, $captured); + } + + private function makeDestination(?callable $getDatabaseDSN): AppwriteDestination + { + return new AppwriteDestination( + project: 'destination-project', + endpoint: 'http://example.test/v1', + key: 'test-key', + dbForProject: $this->createStub(UtopiaDatabase::class), + getDatabasesDB: fn (UtopiaDocument $database): UtopiaDatabase => $this->createStub(UtopiaDatabase::class), + collectionStructure: ['attributes' => [], 'indexes' => []], + onDuplicate: OnDuplicate::Fail, + getDatabaseDSN: $getDatabaseDSN, + ); + } + + private function makeResource(string $sourceDsn): DatabaseResource + { + return new DatabaseResource( + id: 'src-database', + name: 'src', + type: 'legacy', + database: $sourceDsn, + ); + } + + private function invokeResolver(AppwriteDestination $destination, DatabaseResource $resource): string + { + $method = (new ReflectionClass(AppwriteDestination::class))->getMethod('resolveDestinationDsn'); + /** @var string $value */ + $value = $method->invoke($destination, $resource); + return $value; + } +} diff --git a/tests/Migration/Unit/General/CacheTest.php b/tests/Migration/Unit/General/CacheTest.php index 36ae697561..adc31c09f2 100644 --- a/tests/Migration/Unit/General/CacheTest.php +++ b/tests/Migration/Unit/General/CacheTest.php @@ -1,6 +1,6 @@ assertSame( + SchemaAction::Create, + $mode->resolveSchemaAction(exists: false), + "{$mode->value} on non-existing resource must return Create", + ); + } + } + + public function testFailExistsReturnsCreateSoCallerDDLThrows(): void + { + // Fail is routed through Create (not Skip) so the caller's normal + // create flow runs and the library surfaces DuplicateException as + // designed. Returning Skip here would silently hide the error. + $this->assertSame( + SchemaAction::Create, + OnDuplicate::Fail->resolveSchemaAction(exists: true), + ); + } + + public function testSkipAlwaysSkipsExisting(): void + { + // Skip must ignore updatedAt entirely — it's the "don't touch" contract. + $this->assertSame( + SchemaAction::Skip, + OnDuplicate::Skip->resolveSchemaAction( + exists: true, + sourceUpdatedAt: '2026-01-01T00:00:00.000+00:00', + destUpdatedAt: '2020-01-01T00:00:00.000+00:00', + ), + ); + $this->assertSame( + SchemaAction::Skip, + OnDuplicate::Skip->resolveSchemaAction( + exists: true, + sourceUpdatedAt: '2020-01-01T00:00:00.000+00:00', + destUpdatedAt: '2026-01-01T00:00:00.000+00:00', + ), + ); + } + + public function testOverwriteSourceNewerUpdatesInPlace(): void + { + // Source updatedAt strictly newer than dest → Overwrite. The + // caller (DestinationAppwrite) follows up with attribute/index spec + // checks and falls through to drop+recreate when the SDK can't + // express the change. + $this->assertSame( + SchemaAction::Overwrite, + OnDuplicate::Overwrite->resolveSchemaAction( + exists: true, + sourceUpdatedAt: '2026-04-23T10:00:00.000+00:00', + destUpdatedAt: '2026-04-23T09:59:59.000+00:00', + ), + ); + } + + public function testOverwriteSourceEqualSkips(): void + { + $when = '2026-04-23T10:00:00.000+00:00'; + $this->assertSame( + SchemaAction::Skip, + OnDuplicate::Overwrite->resolveSchemaAction( + exists: true, + sourceUpdatedAt: $when, + destUpdatedAt: $when, + ), + ); + } + + public function testOverwriteSourceOlderSkips(): void + { + // Dest is ahead — don't roll back. Avoids overwriting newer + // destination edits with stale source data. + $this->assertSame( + SchemaAction::Skip, + OnDuplicate::Overwrite->resolveSchemaAction( + exists: true, + sourceUpdatedAt: '2026-04-23T09:00:00.000+00:00', + destUpdatedAt: '2026-04-23T10:00:00.000+00:00', + ), + ); + } + + public function testOverwriteNoTimestampsSkips(): void + { + // No timestamps provided at all → no information to act on. Conservative: + // Skip rather than risk a destructive update on uncertain input. + $this->assertSame( + SchemaAction::Skip, + OnDuplicate::Overwrite->resolveSchemaAction(exists: true), + ); + } + + /** + * @return array + */ + public static function unparseableTimestampPairs(): array + { + return [ + 'both empty' => ['', ''], + 'both null' => [null, null], + 'source empty' => ['', '2026-04-23T10:00:00.000+00:00'], + 'dest empty' => ['2026-04-23T10:00:00.000+00:00', ''], + 'source null' => [null, '2026-04-23T10:00:00.000+00:00'], + 'dest null' => ['2026-04-23T10:00:00.000+00:00', null], + 'source zero-date' => ['0000-00-00 00:00:00', '2026-04-23T10:00:00.000+00:00'], + 'dest zero-date' => ['2026-04-23T10:00:00.000+00:00', '0000-00-00 00:00:00'], + 'source garbage' => ['not-a-date', '2026-04-23T10:00:00.000+00:00'], + 'dest garbage' => ['2026-04-23T10:00:00.000+00:00', 'not-a-date'], + ]; + } + + #[DataProvider('unparseableTimestampPairs')] + public function testOverwriteUnparseableUpdatedAtSkips(?string $source, ?string $dest): void + { + // Conservative: unparseable updatedAt preserves existing destination + // rather than risk a destructive update on garbage input. + $this->assertSame( + SchemaAction::Skip, + OnDuplicate::Overwrite->resolveSchemaAction( + exists: true, + sourceUpdatedAt: $source, + destUpdatedAt: $dest, + ), + ); + } + + public function testValuesListsAllCasesInDeclarationOrder(): void + { + // The values() helper is consumed by API/SDK param validators; this + // protects against an accidental case-rename or reorder. + $this->assertSame(['fail', 'skip', 'overwrite'], OnDuplicate::values()); + } +}