From a733932c40dd9dbfe9a72589b875959c46a56dc8 Mon Sep 17 00:00:00 2001 From: Ian Morland Date: Mon, 1 Aug 2022 23:41:44 +0100 Subject: [PATCH] feat: add createTableIfNotExists migration helper --- 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 1fb0bcb2f8..7e51f68eb3 100644 --- a/framework/core/src/Database/Migration.php +++ b/framework/core/src/Database/Migration.php @@ -38,6 +38,25 @@ public static function createTable($name, callable $definition) ]; } + /** + * 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. */