|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Appwrite\Migration\Version; |
| 4 | + |
| 5 | +use Appwrite\Auth\Auth; |
| 6 | +use Appwrite\Migration\Migration; |
| 7 | +use Utopia\CLI\Console; |
| 8 | +use Utopia\Database\Database; |
| 9 | +use Utopia\Database\Document; |
| 10 | + |
| 11 | +class V16 extends Migration |
| 12 | +{ |
| 13 | + public function execute(): void |
| 14 | + { |
| 15 | + /** |
| 16 | + * Disable SubQueries for Performance. |
| 17 | + */ |
| 18 | + foreach (['subQueryIndexes', 'subQueryPlatforms', 'subQueryDomains', 'subQueryKeys', 'subQueryWebhooks', 'subQuerySessions', 'subQueryTokens', 'subQueryMemberships', 'subqueryVariables'] as $name) { |
| 19 | + Database::addFilter( |
| 20 | + $name, |
| 21 | + fn () => null, |
| 22 | + fn () => [] |
| 23 | + ); |
| 24 | + } |
| 25 | + |
| 26 | + Console::log('Migrating Project: ' . $this->project->getAttribute('name') . ' (' . $this->project->getId() . ')'); |
| 27 | + |
| 28 | + Console::info('Migrating Collections'); |
| 29 | + $this->migrateCollections(); |
| 30 | + |
| 31 | + Console::info('Migrating Documents'); |
| 32 | + $this->forEachDocument([$this, 'fixDocument']); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Migrate all Collections. |
| 37 | + * |
| 38 | + * @return void |
| 39 | + */ |
| 40 | + protected function migrateCollections(): void |
| 41 | + { |
| 42 | + foreach ($this->collections as $collection) { |
| 43 | + $id = $collection['$id']; |
| 44 | + |
| 45 | + Console::log("Migrating Collection \"{$id}\""); |
| 46 | + |
| 47 | + $this->projectDB->setNamespace("_{$this->project->getInternalId()}"); |
| 48 | + |
| 49 | + switch ($id) { |
| 50 | + case 'sessions': |
| 51 | + try { |
| 52 | + /** |
| 53 | + * Create 'compression' attribute |
| 54 | + */ |
| 55 | + $this->projectDB->deleteAttribute($id, 'expire'); |
| 56 | + } catch (\Throwable $th) { |
| 57 | + Console::warning("'expire' from {$id}: {$th->getMessage()}"); |
| 58 | + } |
| 59 | + |
| 60 | + break; |
| 61 | + |
| 62 | + case 'projects': |
| 63 | + try { |
| 64 | + /** |
| 65 | + * Create 'region' attribute |
| 66 | + */ |
| 67 | + $this->createAttributeFromCollection($this->projectDB, $id, 'region'); |
| 68 | + } catch (\Throwable $th) { |
| 69 | + Console::warning("'region' from {$id}: {$th->getMessage()}"); |
| 70 | + } |
| 71 | + |
| 72 | + try { |
| 73 | + /** |
| 74 | + * Create '_key_team' index |
| 75 | + */ |
| 76 | + $this->createIndexFromCollection($this->projectDB, $id, '_key_team'); |
| 77 | + } catch (\Throwable $th) { |
| 78 | + Console::warning("'_key_team' from {$id}: {$th->getMessage()}"); |
| 79 | + } |
| 80 | + |
| 81 | + default: |
| 82 | + break; |
| 83 | + } |
| 84 | + |
| 85 | + usleep(50000); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Fix run on each document |
| 91 | + * |
| 92 | + * @param \Utopia\Database\Document $document |
| 93 | + * @return \Utopia\Database\Document |
| 94 | + */ |
| 95 | + protected function fixDocument(Document $document) |
| 96 | + { |
| 97 | + switch ($document->getCollection()) { |
| 98 | + case 'projects': |
| 99 | + /** |
| 100 | + * Bump version number. |
| 101 | + */ |
| 102 | + $document->setAttribute('version', '1.1.0'); |
| 103 | + |
| 104 | + /** |
| 105 | + * Set default authDuration |
| 106 | + */ |
| 107 | + $document->setAttribute('auths', array_merge($document->getAttribute('auths', []), [ |
| 108 | + 'duration' => Auth::TOKEN_EXPIRATION_LOGIN_LONG |
| 109 | + ])); |
| 110 | + break; |
| 111 | + } |
| 112 | + |
| 113 | + return $document; |
| 114 | + } |
| 115 | +} |
0 commit comments