diff --git a/src/Providers/ExtensionServiceProvider.php b/src/Providers/ExtensionServiceProvider.php index 2d1043f343f..3ae1bc8789b 100644 --- a/src/Providers/ExtensionServiceProvider.php +++ b/src/Providers/ExtensionServiceProvider.php @@ -252,6 +252,7 @@ class ExtensionServiceProvider extends ServiceProvider Updates\AddTimezoneConfigOptions::class, Updates\RemoveParentField::class, Updates\UpdateGlobalVariables::class, + Updates\PublishMigrationForNocacheUrlColumn::class, Updates\PublishMigrationForTwoFactorColumns::class, Updates\PublishMigrationForWebauthnTable::class, Updates\AddAddonSettingsToGitConfig::class, diff --git a/src/StaticCaching/NoCache/DatabaseSession.php b/src/StaticCaching/NoCache/DatabaseSession.php index 08029ff51ad..c5b14b76851 100644 --- a/src/StaticCaching/NoCache/DatabaseSession.php +++ b/src/StaticCaching/NoCache/DatabaseSession.php @@ -11,7 +11,7 @@ public function write() public function restore() { - $regions = DatabaseRegion::where('url', $this->url)->get(['key']); + $regions = DatabaseRegion::where('url', md5($this->url))->get(['key']); $this->regions = $regions->map->key; @@ -38,7 +38,7 @@ protected function cacheRegion(Region $region) DatabaseRegion::updateOrCreate([ 'key' => $region->key(), ], [ - 'url' => $this->url, + 'url' => md5($this->url), 'region' => serialize($region), ]); } diff --git a/src/UpdateScripts/PublishMigrationForNocacheUrlColumn.php b/src/UpdateScripts/PublishMigrationForNocacheUrlColumn.php new file mode 100644 index 00000000000..900259b70e8 --- /dev/null +++ b/src/UpdateScripts/PublishMigrationForNocacheUrlColumn.php @@ -0,0 +1,40 @@ +isUpdatingTo('6.0.0') + && config('statamic.static_caching.nocache', 'cache') === 'database'; + } + + public function update() + { + if (! $this->migrationExists('update_nocache_url_column')) { + $stub = __DIR__.'/stubs/update_nocache_url_column.php.stub'; + $filename = date('Y_m_d_His').'_update_nocache_url_column.php'; + + File::put(database_path("migrations/$filename"), File::get($stub)); + + $this->console->line(sprintf( + 'Migration database/migrations/%s created successfully.', + $filename + )); + + $this->console->line('Run php artisan migrate to apply the migration.'); + } + } + + protected function migrationExists(string $name): bool + { + return collect(File::allFiles(database_path('migrations'))) + ->map->getFilename() + ->filter(fn (string $filename) => Str::contains($filename, $name)) + ->isNotEmpty(); + } +} diff --git a/src/UpdateScripts/stubs/update_nocache_url_column.php.stub b/src/UpdateScripts/stubs/update_nocache_url_column.php.stub new file mode 100644 index 00000000000..f1d954d05ae --- /dev/null +++ b/src/UpdateScripts/stubs/update_nocache_url_column.php.stub @@ -0,0 +1,29 @@ +table('nocache_regions') + ->whereLike('url', 'http%') + ->orderBy('key') + ->chunk(100, function ($regions) { + foreach ($regions as $region) { + DB::connection(config('statamic.static_caching.nocache_db_connection')) + ->table('nocache_regions') + ->where('key', $region->key) + ->where('url', $region->url) + ->update(['url' => md5($region->url)]); + } + }); + }); + } +};