Skip to content

Commit

Permalink
Add "--sequential" option in "create" command
Browse files Browse the repository at this point in the history
  • Loading branch information
rougin committed Oct 19, 2024
1 parent 4f9f95c commit cccfa31
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 16 deletions.
3 changes: 0 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
21 changes: 13 additions & 8 deletions src/Commands/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions tests/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ public function test_07_create_with_database()

$actual = $this->getActualFile($input['name']);

$this->clearFiles();

$this->assertEquals($expected, $actual);
}

Expand Down
11 changes: 6 additions & 5 deletions tests/PlateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);

Expand All @@ -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;

Expand All @@ -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);

Expand All @@ -122,10 +126,7 @@ protected function clearFiles()
/** @var string[] */
$files = glob($path . '/migrations/*.php');

foreach ($files as $file)
{
unlink($file);
}
array_map('unlink', $files);
}

/**
Expand Down Expand Up @@ -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')
{
Expand Down

0 comments on commit cccfa31

Please sign in to comment.