statamic / cms

The core Laravel CMS Composer package
https://statamic.com
Other
4.09k stars 534 forks source link

NoCache database driver - Long URL's causing 500 errors #11034

Open JorisOrangeStudio opened 3 weeks ago

JorisOrangeStudio commented 3 weeks ago

Bug description

When using nocache database driver and accessing urls that are longer than the max length of the column it throws a 500 error for that page. This happend to advertisement links, where Meta/Google adds a lot of parameters to the url, causing it to exceed the varchar limit.

Only changing the column type to text was not possible due to the existing index on url. We have fixed this issue temporarily by creating a migration like this:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up()
    {
        // Drop index first
        Schema::table('nocache_regions', function (Blueprint $table) {
            $table->dropIndex('nocache_regions_url_index');
        });

        // Change column type
        Schema::table('nocache_regions', function (Blueprint $table) {
            $table->text('url')->change();
        });

        // Add index with length using raw SQL
        DB::statement('ALTER TABLE nocache_regions ADD INDEX nocache_regions_url_index (url(255))');
    }

    public function down()
    {
        // Remove index
        DB::statement('ALTER TABLE nocache_regions DROP INDEX nocache_regions_url_index');

        // Change column back to varchar
        Schema::table('nocache_regions', function (Blueprint $table) {
            $table->string('url', 255)->change();
        });

        // Recreate original index
        Schema::table('nocache_regions', function (Blueprint $table) {
            $table->index('url', 'nocache_regions_url_index');
        });
    }
};

How to reproduce

  1. Set up half static caching
  2. Use nocache tags on a page
  3. Make use of nocache database driver
  4. Visit a page with long url (for example with a lot of parameters)
  5. See error 500 showing up as below:

Logs

SQLSTATE: String data, right truncated: 1406 Data too long for column 'url' at row 1 (Connection: mysql, SQL: insert into `nocache_regions` (`key`, `url`, `region`, `updated_at`, `created_at`)

Environment

Environment
Application Name: Local
Laravel Version: 10.48.22
PHP Version: 8.3.7
Composer Version: 2.5.5
Environment: staging
Debug Mode: ENABLED
URL: localhost/
Maintenance Mode: OFF

Cache
Config: NOT CACHED
Events: NOT CACHED
Routes: NOT CACHED
Views: CACHED

Drivers
Cache: file
Database: mysql
Logs: stack / single
Mail: mailgun
Queue: sync
Session: file

Statamic
Addons: 6
Sites: 3 (Dutch, English, German)
Stache Watcher: Enabled
Static Caching: Disabled
Version: 5.29.0 PRO

Statamic Addons
aerni/advanced-seo: 2.9.1
heidkaemper/statamic-toolbar: 1.2.1
jacksleight/statamic-bard-texstyle: 3.3.0
mattrothenberg/statamic-mapbox-address: 0.9.0
mitydigital/statamic-logger: 2.3.0
statamic/collaboration: 1.0.0

Installation

Starter Kit using via CLI

Additional details

No response

jasonvarga commented 3 weeks ago

What about making the string longer? By default its 255.


$table->string('url', 500);