feat: add createTableIfNotExists migration helper (#3576)

This commit is contained in:
Ian Morland 2022-08-02 10:43:31 +01:00 committed by GitHub
parent 61c4421bd2
commit 7d3147d4e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -38,6 +38,25 @@ abstract class Migration
];
}
/**
* Create a table if it doesn't already exist.
*/
public static function createTableIfNotExists($name, callable $definition)
{
return [
'up' => function (Builder $schema) use ($name, $definition) {
if (! $schema->hasTable($name)) {
$schema->create($name, function (Blueprint $table) use ($definition) {
$definition($table);
});
}
},
'down' => function (Builder $schema) use ($name) {
$schema->dropIfExists($name);
}
];
}
/**
* Rename a table.
*/