Skip to content

Instantly share code, notes, and snippets.

@theconsolelogger
Created April 12, 2020 03:17
Show Gist options
  • Select an option

  • Save theconsolelogger/c48b22960ce13bf303c8e04a591c0ef3 to your computer and use it in GitHub Desktop.

Select an option

Save theconsolelogger/c48b22960ce13bf303c8e04a591c0ef3 to your computer and use it in GitHub Desktop.

Revisions

  1. Jonathan Staniforth created this gist Apr 12, 2020.
    36 changes: 36 additions & 0 deletions change-enum.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    <?php

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

    class ChangeEnum extends Migration
    {
    /**
    * Run the migrations.
    *
    * @return void
    */
    public function up()
    {
    DB::statement("ALTER TABLE messages DROP CONSTRAINT messages_status_check");

    $types = ['draft', 'sent', 'read', 'replied'];
    $result = join( ', ', array_map(function ($value){
    return sprintf("'%s'::character varying", $value);
    }, $types));

    DB::statement("ALTER TABLE messages ADD CONSTRAINT messages_status_check CHECK (status::text = ANY (ARRAY[$result]::text[]))");
    }

    /**
    * Reverse the migrations.
    *
    * @return void
    */
    public function down()
    {
    //
    }
    }