From 340032f05493c48fde111d75d591810f85436b81 Mon Sep 17 00:00:00 2001 From: flashwave Date: Wed, 18 Jul 2018 05:06:27 +0200 Subject: [PATCH] Added command to drop a migration template into the database folder. --- misuzu.php | 39 +++++++++++++++++++++++++++++++++++++++ utility.php | 10 ++++++++++ 2 files changed, 49 insertions(+) diff --git a/misuzu.php b/misuzu.php index 1a54c38d..cd8eae00 100644 --- a/misuzu.php +++ b/misuzu.php @@ -137,6 +137,45 @@ if (PHP_SAPI === 'cli') { } break; + case 'new-mig': + if (empty($argv[2])) { + echo 'Specify a migration name.' . PHP_EOL; + return; + } + + if (!preg_match('#^([a-z_]+)$#', $argv[2])) { + echo 'Migration name may only contain alpha and _ characters.' . PHP_EOL; + return; + } + + $filename = date('Y_m_d_His_') . trim($argv[2], '_') . '.php'; + $filepath = __DIR__ . '/database/' . $filename; + $namespace = snake_to_camel($argv[2]); + $template = <<exec(' + CREATE TABLE ... + '); +} + +function migrate_down(PDO \$conn): void +{ + \$conn->exec('DROP TABLE ...'); +} + +MIG; + + file_put_contents($filepath, $template); + + echo "Template for '{$namespace}' has been created." . PHP_EOL; + break; + default: echo 'Unknown command.' . PHP_EOL; break; diff --git a/utility.php b/utility.php index 807cdea5..62f2d0a1 100644 --- a/utility.php +++ b/utility.php @@ -303,3 +303,13 @@ function url_construct(string $path, array $query = [], string $host = ''): stri return substr($url, 0, -1); } + +function camel_to_snake(string $camel): string +{ + return trim(strtolower(preg_replace('#([A-Z][a-z]+)#', '$1_', $camel)), '_'); +} + +function snake_to_camel(string $snake): string +{ + return str_replace('_', '', ucwords($snake, '_')); +}