From 7d3147d4e1ac4c90cb5c87650bcf15dcef71f1cb Mon Sep 17 00:00:00 2001 From: Ian Morland <16573496+imorland@users.noreply.github.com> Date: Tue, 2 Aug 2022 10:43:31 +0100 Subject: [PATCH] feat: add createTableIfNotExists migration helper (#3576) --- framework/core/src/Database/Migration.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/framework/core/src/Database/Migration.php b/framework/core/src/Database/Migration.php index 1fb0bcb2f..7e51f68eb 100644 --- a/framework/core/src/Database/Migration.php +++ b/framework/core/src/Database/Migration.php @@ -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. */