Added command to drop a migration template into the database folder.

This commit is contained in:
flash 2018-07-18 05:06:27 +02:00
parent bf903a38d0
commit 340032f054
2 changed files with 49 additions and 0 deletions

View file

@ -137,6 +137,45 @@ if (PHP_SAPI === 'cli') {
} }
break; 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 = <<<MIG
<?php
namespace Misuzu\DatabaseMigrations\\$namespace;
use PDO;
function migrate_up(PDO \$conn): void
{
\$conn->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: default:
echo 'Unknown command.' . PHP_EOL; echo 'Unknown command.' . PHP_EOL;
break; break;

View file

@ -303,3 +303,13 @@ function url_construct(string $path, array $query = [], string $host = ''): stri
return substr($url, 0, -1); 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, '_'));
}