Daursu / laravel-zero-downtime-migration

Zero downtime migrations with Laravel and pt-online-schema-change
MIT License
77 stars 13 forks source link

Let pipeline fail if migration fails #45

Open mdbw3 opened 1 month ago

mdbw3 commented 1 month ago

Hi,

First of all, thanks for this amazing plugin. We use it for 1.5 years now and it works flawlessly. There is however one issue we encounter when migrations fail. What we want is that the command 'php artisan migrate' returns a real error when a migration fails. Currently the error is shown in the console, but as no error is returned our (Bitbucket) deployment pipeline continues to run even when a migration has failed.

Is there any option / param / setting that can be used or set to achieve this behavior?

Thanks!

MdB

Daursu commented 1 month ago

Thanks for reaching out. I've been trying to reproduce this behavior locally, however every time I attempt to fail the migration, the exit code in my terminal is 1 instead of 0. This is the expected behavior.

Example below

root@0174c92a47f8:/app# php artisan migrate

   INFO  Running migrations.

  2024_07_22_201338_convert_city_to_uuid_users_table ..................................................................................... 27ms FAIL

   Doctrine\DBAL\Schema\SchemaException

  There is no column with name 'cit' on table 'users'.

  at vendor/doctrine/dbal/src/Schema/SchemaException.php:87
     83▕      * @return SchemaException
     84▕      */
     85▕     public static function columnDoesNotExist($columnName, $table)
     86▕     {
  ➜  87▕         return new self(
     88▕             sprintf("There is no column with name '%s' on table '%s'.", $columnName, $table),
     89▕             self::COLUMN_DOESNT_EXIST
     90▕         );
     91▕     }

      +8 vendor frames
  9   /laravel-zero-downtime-migration/src/BatchableBlueprint.php:19
      Illuminate\Database\Schema\Blueprint::toSql(Object(Daursu\ZeroDowntimeMigration\Connections\PtOnlineSchemaChangeConnection), Object(Illuminate\Database\Schema\Grammars\MySqlGrammar))

      +2 vendor frames
  12  /laravel-zero-downtime-migration/src/ZeroDowntimeSchema.php:51
      Illuminate\Database\Schema\Builder::table("users", Object(Closure))

root@0174c92a47f8:/app# echo $?
1

Are you able to confirm what status code are you seeing in your pipeline?

mdbw3 commented 1 month ago

Ah you are right. It turns out to be an issue with using SSH within the Bitbucket pipeline. These commands returned statuscode 0:

- ssh -t -T user@host << EOF
- php artisan migrate --force
- exit $?
- EOF

Even when echoing $? within the EOF part it showed code 0, but when running from my own bash (via ssh) it showed code 1.

Changing the Bitbucket pipeline code to this fixed the issue, it returns statuscode 1 on error: ssh user@host 'php artisan migrate --force'

Thanks for your help!