|
90 | 90 | expect(config("filesystems.disks.{$location->disk}.secret"))->toBe('super-secret-access-key'); |
91 | 91 | }); |
92 | 92 |
|
93 | | -test('apply() is a no-op when not fully configured', function () { |
| 93 | +test('apply() leaves a bucket that is not fully configured unregistered', function () { |
94 | 94 | $location = bucketLocation(); |
95 | 95 | DB::table('storage_locations')->where('id', $location->id)->update(['secret' => null]); |
96 | 96 |
|
|
158 | 158 | ->and(resolvedDiskFor(User::factory()->client()->create()))->toBe($location->disk); |
159 | 159 | }); |
160 | 160 |
|
161 | | -test('a real upload lands on the external disk once the backend is active, and local uploads made before activation are unaffected', function () { |
| 161 | +test('a real upload lands on the bucket once it is the default, and local uploads made before are unaffected', function () { |
162 | 162 | Storage::fake('files'); |
163 | 163 |
|
164 | 164 | uploadImageFile($this->admin); |
|
178 | 178 | ->and($externalFile->disk)->toBe($location->disk); |
179 | 179 | Storage::disk($location->disk)->assertExists($externalFile->path); |
180 | 180 |
|
181 | | - // The first file, uploaded before activation, still resolves to local |
182 | | - // disk and is unaffected by the backend switch (no migration tool). |
| 181 | + // The first file, uploaded before the switch, still resolves to local |
| 182 | + // disk and is unaffected by it (no migration tool). |
183 | 183 | Storage::disk('files')->assertExists($localFile->refresh()->path); |
184 | 184 | expect($localFile->disk)->toBe('files'); |
185 | 185 | }); |
|
254 | 254 | expect(resolvedDiskFor($this->admin))->toBe('files_external'); |
255 | 255 | }); |
256 | 256 |
|
| 257 | +test('before the storage locations migration, the old external storage row is read as before', function () { |
| 258 | + // Boot happens before `artisan migrate`, so every upgrade passes |
| 259 | + // through a window where this code meets the previous schema. Files |
| 260 | + // already in the bucket must keep resolving, and new uploads must not |
| 261 | + // quietly start landing on the local disk. |
| 262 | + withoutStorageLocationsTable(); |
| 263 | + ExternalStorageSettings::query()->create([ |
| 264 | + 'active' => true, |
| 265 | + 'key' => 'AKIAEXAMPLE', |
| 266 | + 'secret' => 'shh', |
| 267 | + 'bucket' => 'my-bucket', |
| 268 | + 'region' => 'us-east-1', |
| 269 | + ]); |
| 270 | + |
| 271 | + applyStorage(); |
| 272 | + |
| 273 | + expect(config('filesystems.disks.files_external.bucket'))->toBe('my-bucket') |
| 274 | + ->and(config('filesystems.disks.files_external.secret'))->toBe('shh') |
| 275 | + ->and(resolvedDiskFor($this->admin))->toBe('files_external'); |
| 276 | +}); |
| 277 | + |
257 | 278 | test('the migration carries the old row over on its own disk name, as the default only if it was switched on', function (bool $active) { |
258 | 279 | resetStorageLocationsMigration(function () use ($active): void { |
259 | 280 | ExternalStorageSettings::query()->create([ |
|
280 | 301 | ->and($local->is_default)->toBe(! $active); |
281 | 302 | })->with(['switched on' => true, 'switched off' => false]); |
282 | 303 |
|
| 304 | +test('the migration does not leave the pre-migration picture in the boot cache', function () { |
| 305 | + // The real upgrade: the `migrate` process boots first, against the old |
| 306 | + // schema, and caches what it sees there. Nobody flushes afterwards. |
| 307 | + withoutStorageLocationsTable(); |
| 308 | + ExternalStorageSettings::query()->create([ |
| 309 | + 'active' => false, |
| 310 | + 'key' => 'AKIAEXAMPLE', |
| 311 | + 'secret' => 'shh', |
| 312 | + 'bucket' => 'my-bucket', |
| 313 | + 'region' => 'us-east-1', |
| 314 | + ]); |
| 315 | + app(ExternalStorageConfigApplier::class)->apply(); |
| 316 | + |
| 317 | + $migration = require database_path('migrations/2026_10_09_090000_create_storage_locations_tables.php'); |
| 318 | + $migration->up(); |
| 319 | + |
| 320 | + // The next process boots on whatever the cache now holds. |
| 321 | + app(ExternalStorageConfigApplier::class)->apply(); |
| 322 | + |
| 323 | + expect(array_column(app(ExternalStorageConfigApplier::class)->externalLocations(), 'disk'))->toContain('files_external') |
| 324 | + ->and(config('filesystems.disks.files_external.bucket'))->toBe('my-bucket'); |
| 325 | +}); |
| 326 | + |
283 | 327 | // ---- Where a client's uploads go --------------------------------------- |
284 | 328 |
|
285 | 329 | test('a client uploads to their first location, or the one they asked for if it is theirs', function () { |
|