From cccfa31206272a6bf729b3104f6b40fcbf224727 Mon Sep 17 00:00:00 2001 From: Rougin Gutib Date: Sat, 19 Oct 2024 13:40:18 +0800 Subject: [PATCH] Add "--sequential" option in "create" command --- CHANGELOG.md | 3 --- README.md | 33 +++++++++++++++++++++++++++++++++ src/Commands/Create.php | 21 +++++++++++++-------- tests/ManagerTest.php | 2 ++ tests/PlateTest.php | 11 ++++++----- 5 files changed, 54 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 78a3f3b..4794c52 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,9 +18,6 @@ All notable changes to `Refinery` will be documented in this file. - Reworked entire code structure - Workflow provider to `Github Actions` -### Removed -- `sequential` option in `create` command - ## [0.3.0](https://github.com/rougin/refinery/compare/v0.2.1...v0.3.0) - 2017-01-07 ### Added diff --git a/README.md b/README.md index 9a538ff..5815e89 100644 --- a/README.md +++ b/README.md @@ -240,6 +240,39 @@ class Migration_create_users_table extends Migration > [!NOTE] > The `--from-database` option only exists when creating files under the `create_*_table` prefix. +### Creating sequential migrations + +By default, this uses a timestamp prefix as its numbering when creating migration files. To enable sequential numbering, kindly add the `--sequential` option in the `create` command: + +``` bash +$ vendor/bin/refinery create create_users_table --sequential +[PASS] "001_create_users_table.php" successfully created! +``` + +When using the `--sequential` option, kindly ensure as well that the value of `$config['migration_type']` in `migration.php` was set to `sequential`: + +``` php +// ciacme/application/config/migration.php + +/* +|-------------------------------------------------------------------------- +| Migration Type +|-------------------------------------------------------------------------- +| +| Migration file names may be based on a sequential identifier or on +| a timestamp. Options are: +| +| 'sequential' = Sequential migration naming (001_add_blog.php) +| 'timestamp' = Timestamp migration naming (20121031104401_add_blog.php) +| Use timestamp format YYYYMMDDHHIISS. +| +| Note: If this configuration value is missing the Migration library +| defaults to 'sequential' for backward compatibility with CI2. +| +*/ +$config['migration_type'] = 'sequential'; +``` + ## Changelog Please see [CHANGELOG][link-changelog] for more information what has changed recently. diff --git a/src/Commands/Create.php b/src/Commands/Create.php index e859f4c..e237cb1 100644 --- a/src/Commands/Create.php +++ b/src/Commands/Create.php @@ -58,6 +58,7 @@ public function init() $this->addValueOption('auto-increment', 'Sets the "auto_increment" value', false); $this->addValueOption('default', 'Sets the "default" value of the column'); $this->addOption('from-database', 'Creates a migration from the database'); + $this->addOption('sequential', 'Use a sequential identifier (e.g., 001, 002)'); $this->addValueOption('length', 'Sets the "constraint" value of the column', 50); $this->addValueOption('null', 'Sets the column with a nullable value', false); $this->addValueOption('primary', 'Sets the column as the primary key', false); @@ -135,13 +136,14 @@ protected function createFile($name, $class) $style = $this->getNumberStyle(); - // TODO: Get latest sequence from config --- - $prefix = null; - // ----------------------------------------- + $prefix = date('YmdHis'); - if ($style === Migration::STYLE_TIMESTAMP) + if ($style === Migration::STYLE_SEQUENCE) { - $prefix = date('YmdHis'); + /** @var string[] */ + $files = glob($path . '*.php'); + + $prefix = sprintf('%03d', count($files) + 1); } $file = $path . $prefix . '_' . $name; @@ -178,11 +180,14 @@ protected function getNumberStyle() { $type = $this->getConfig('migration_type'); - $style = Migration::STYLE_SEQUENCE; + $style = Migration::STYLE_TIMESTAMP; + + /** @var boolean */ + $sequential = $this->getOption('sequential'); - if ($type === '\'timestamp\'') + if ($sequential || $type === '\'sequential\'') { - $style = Migration::STYLE_TIMESTAMP; + $style = Migration::STYLE_SEQUENCE; } return $style; diff --git a/tests/ManagerTest.php b/tests/ManagerTest.php index 7d4acb8..ec19ef3 100644 --- a/tests/ManagerTest.php +++ b/tests/ManagerTest.php @@ -219,6 +219,8 @@ public function test_07_create_with_database() $actual = $this->getActualFile($input['name']); + $this->clearFiles(); + $this->assertEquals($expected, $actual); } diff --git a/tests/PlateTest.php b/tests/PlateTest.php index ee8929d..0d0689f 100644 --- a/tests/PlateTest.php +++ b/tests/PlateTest.php @@ -34,6 +34,7 @@ public function test_creating_column() $test = $this->findCommand('create'); $input = array('name' => 'add_name_in_users_table'); + $input['--sequential'] = true; $input['--length'] = 100; $input['--null'] = true; @@ -56,6 +57,7 @@ public function test_creating_table() $test = $this->findCommand('create'); $input = array('name' => 'create_users_table'); + $input['--sequential'] = true; $test->execute($input); @@ -78,6 +80,7 @@ public function test_deleting_column() $test = $this->findCommand('create'); $input = array('name' => 'remove_name_in_users_table'); + $input['--sequential'] = true; $input['--length'] = 100; $input['--null'] = true; @@ -100,6 +103,7 @@ public function test_deleting_table() $test = $this->findCommand('create'); $input = array('name' => 'delete_users_table'); + $input['--sequential'] = true; $test->execute($input); @@ -122,10 +126,7 @@ protected function clearFiles() /** @var string[] */ $files = glob($path . '/migrations/*.php'); - foreach ($files as $file) - { - unlink($file); - } + array_map('unlink', $files); } /** @@ -171,7 +172,7 @@ protected function getActualFile($name) { $base = basename($file); - $parsed = substr($base, 15, strlen($base)); + $parsed = substr($base, 4, strlen($base)); if ($parsed === $name . '.php') {