Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migration for upgrade vom v8 to v9 fails #2601

Closed
Anticom opened this issue Sep 29, 2021 · 2 comments
Closed

Migration for upgrade vom v8 to v9 fails #2601

Anticom opened this issue Sep 29, 2021 · 2 comments

Comments

@Anticom
Copy link

Anticom commented Sep 29, 2021

I've recently upgraded my laravel application from Laravel 7 to Laravel 8. Hence I've also had to upgrade spatie/media-library to be compatible with laravel 8 again. For this I've strictly followed the upgrade guide (UPGRADING.md) and added the following migration to my project:

// ...
class AddGeneratedConversionsToMediaTable extends Migration {
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up() {
        if ( ! Schema::hasColumn( 'media', 'generated_conversions' ) ) {
            Schema::table( 'media', function ( Blueprint $table ) {
                $table->json( 'generated_conversions' );
            } );
        }

        Media::query()
                ->whereNull('generated_conversions')
                ->orWhere('generated_conversions', '')
                ->orWhereRaw("JSON_TYPE(generated_conversions) = 'NULL'")
                ->update([
                    'generated_conversions' => DB::raw('custom_properties->"$.generated_conversions"'),
                    // OPTIONAL: Remove the generated conversions from the custom_properties field as well:
                    'custom_properties'     => DB::raw("JSON_REMOVE(custom_properties, '$.generated_conversions')")
                ]);
    }

    // ...
}

Now when I try to run this migration I get:

$ php artisan migrate
Migrating: 2021_05_07_143222_add_generated_conversions_to_media_table

   Illuminate\Database\QueryException 

  SQLSTATE[01000]: Warning: 1265 Data truncated for column 'generated_conversions' at row 1 (SQL: update `media` set `generated_conversions` = custom_properties->"$.generated_conversions", `custom_properties` = JSON_REMOVE(custom_properties, '$.generated_conversions'), `media`.`updated_at` = 2021-09-29 09:31:13 where `generated_conversions` is null or `generated_conversions` =  or JSON_TYPE(generated_conversions) = 'NULL')

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:703
    699|         // If an exception occurs when attempting to run a query, we'll format the error
    700|         // message to include the bindings with SQL, which will make this exception a
    701|         // lot more helpful to the developer instead of just the database's errors.
    702|         catch (Exception $e) {
  > 703|             throw new QueryException(
    704|                 $query, $this->prepareBindings($bindings), $e
    705|             );
    706|         }
    707|     }

      +8 vendor frames 
  9   path/to/database/migrations/2021_05_07_143222_add_generated_conversions_to_media_table.php:29
      Illuminate\Database\Eloquent\Builder::update()

      +21 vendor frames 
  31  artisan:37
      Illuminate\Foundation\Console\Kernel::handle()

As far as I know json columns are similar to text columns in that they can be of arbitrary size so I don't understand why anything would be truncated during the migration.


Here are my versions:

$ php --version
PHP 8.0.5 (cli) (built: May  3 2021 11:30:19) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.5, Copyright (c) Zend Technologies
    with Zend OPcache v8.0.5, Copyright (c), by Zend Technologies
    with Xdebug v3.0.3, Copyright (c) 2002-2021, by Derick Rethans
$ php artisan --version
Laravel Framework 8.62.0

$ composer show laravel/*
laravel/framework            v8.62.0 The Laravel Framework.
laravel/nova                 3.29.0  A wonderful administration interface for Laravel.
laravel/sanctum              v2.11.2 Laravel Sanctum provides a featherweight authentication system for SPAs and simple APIs.
laravel/serializable-closure v1.0.0  Laravel Serializable Closure provides an easy and secure way to serialize closures in PHP.
laravel/socialite            v5.2.5  Laravel wrapper around OAuth 1 & OAuth 2 libraries.
laravel/tinker               v2.6.1  Powerful REPL for the Laravel framework.
laravel/ui                   v3.3.0  Laravel UI utilities and presets.
$ composer show spatie/*
spatie/eloquent-sortable          3.11.0 Sortable behaviour for eloquent models
spatie/image                      1.10.5 Manipulate images with an expressive API
spatie/image-optimizer            1.4.0  Easily optimize images using PHP
spatie/laravel-medialibrary       9.7.4  Associate files with Eloquent models
spatie/laravel-permission         3.18.0 Permission handling for Laravel 5.8 and up
spatie/laravel-translatable       4.6.0  A trait to make an Eloquent model hold translations
spatie/laravel-translation-loader 2.7.0  Store your language lines in the database, yaml or other sources
spatie/once                       3.0.1  A magic memoization function
spatie/temporary-directory        2.0.0  Easily create, use and destroy temporary directories


$ mysql --version
mysql  Ver 8.0.21 for Linux on x86_64 (MySQL Community Server - GPL)
$ mysqld --version
/usr/sbin/mysqld  Ver 8.0.21 for Linux on x86_64 (MySQL Community Server - GPL)
@oliveready7
Copy link

Hey, I'm guessing you got past this?

If anyone else comes across this issue allow the "generated_conversions" column to be null (you may need to remove the generated_conversions column first).

 if ( ! Schema::hasColumn( 'media', 'generated_conversions' ) ) {
            Schema::table( 'media', function ( Blueprint $table ) {
                $table->json( 'generated_conversions' )->nullable();
            } );
        }

@freekmurze
Copy link
Member

Thanks, feel free to PR and improvement to the upgrade guide 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants