Skip to content

Instantly share code, notes, and snippets.

@tjimenez
Forked from theconsolelogger/change-enum.php
Created June 22, 2021 20:10
Show Gist options
  • Select an option

  • Save tjimenez/0a1b7b0eb8a5c06b955ec83b52623f43 to your computer and use it in GitHub Desktop.

Select an option

Save tjimenez/0a1b7b0eb8a5c06b955ec83b52623f43 to your computer and use it in GitHub Desktop.
An example of how to update a Laravel enum column with a migration script.
<?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()
{
//
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment