From a335968c47530c01d33ace6fa3d5f55092bd9f9b Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Thu, 9 Jul 2026 09:03:19 +0100 Subject: [PATCH 1/2] Save migrated global variables through the repository Writing the variables file directly with File::put left the Stache holding the empty variables created by the global set's save, so anything saving those variables later in the same process (like an addon update script) would clobber the migrated data. Co-Authored-By: Claude Fable 5 --- src/UpdateScripts/UpdateGlobalVariables.php | 2 +- .../UpdateGlobalVariablesTest.php | 28 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/UpdateScripts/UpdateGlobalVariables.php b/src/UpdateScripts/UpdateGlobalVariables.php index 414261c2bc5..8fe0755f904 100644 --- a/src/UpdateScripts/UpdateGlobalVariables.php +++ b/src/UpdateScripts/UpdateGlobalVariables.php @@ -76,7 +76,7 @@ private function migrateFileStructure(): void $globalSet->save(); - File::put($variablesPath, YAML::dump($data)); + $globalSet->inDefaultSite()->data($data)->saveQuietly(); }); } } diff --git a/tests/UpdateScripts/UpdateGlobalVariablesTest.php b/tests/UpdateScripts/UpdateGlobalVariablesTest.php index 2cc3fc6bf21..00082fc352e 100644 --- a/tests/UpdateScripts/UpdateGlobalVariablesTest.php +++ b/tests/UpdateScripts/UpdateGlobalVariablesTest.php @@ -4,6 +4,7 @@ use Illuminate\Support\Facades\File; use PHPUnit\Framework\Attributes\Test; +use Statamic\Facades\GlobalSet; use Statamic\Facades\YAML; use Statamic\UpdateScripts\UpdateGlobalVariables; use Tests\PreventSavingStacheItemsToDisk; @@ -79,6 +80,33 @@ public function it_migrates_global_variables_with_empty_data_in_a_single_site_in unlink($this->globalsPath.'/en/test.yaml'); } + #[Test] + public function migrated_variables_survive_being_saved_later_in_the_same_process() + { + File::put($this->globalsPath.'/test.yaml', Yaml::dump([ + 'title' => 'Test', + 'data' => [ + 'foo' => 'Bar', + ], + ])); + + $this->runUpdateScript(UpdateGlobalVariables::class); + + // Simulates an addon update script saving variables after the migration, + // like Peak SEO's AddRobots script does. + $variables = GlobalSet::findByHandle('test')->inDefaultSite(); + $variables->set('baz', 'Qux'); + $variables->save(); + + $this->assertEquals( + ['foo' => 'Bar', 'baz' => 'Qux'], + YAML::parse(File::get($this->globalsPath.'/en/test.yaml')) + ); + + unlink($this->globalsPath.'/test.yaml'); + unlink($this->globalsPath.'/en/test.yaml'); + } + #[Test] public function it_builds_the_sites_array_in_a_multi_site_install() { From 40bf1ebb5821be26c0975b894fbe12a08690f409 Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Thu, 9 Jul 2026 09:07:35 +0100 Subject: [PATCH 2/2] wip --- tests/UpdateScripts/UpdateGlobalVariablesTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/UpdateScripts/UpdateGlobalVariablesTest.php b/tests/UpdateScripts/UpdateGlobalVariablesTest.php index 00082fc352e..9d74711bc03 100644 --- a/tests/UpdateScripts/UpdateGlobalVariablesTest.php +++ b/tests/UpdateScripts/UpdateGlobalVariablesTest.php @@ -92,8 +92,7 @@ public function migrated_variables_survive_being_saved_later_in_the_same_process $this->runUpdateScript(UpdateGlobalVariables::class); - // Simulates an addon update script saving variables after the migration, - // like Peak SEO's AddRobots script does. + // Simulates an addon update script saving variables after the migration $variables = GlobalSet::findByHandle('test')->inDefaultSite(); $variables->set('baz', 'Qux'); $variables->save();