Skip to content

Commit

Permalink
Changed commands and simplified code
Browse files Browse the repository at this point in the history
rougin committed Sep 19, 2015
1 parent a750f5b commit 5677a81
Showing 8 changed files with 349 additions and 344 deletions.
49 changes: 26 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -12,10 +12,19 @@ Install ```Refinery``` via [Composer](https://getcomposer.org):

# Examples

### Keywords

```Refinery``` also provides a *ready-to-eat* migration based on the following keywords below:

* ```create_(table)_table```
* ```add_(column)_in_(table)_table```
* ```modify_(column)_in_(table)_table```
* ```delete_(column)_in_(table)_table```

### Creating a table named "user"

```bash
$ php vendor/bin/refinery create:migration create_user_table
$ php vendor/bin/refinery create create_user_table
"20150607123241_create_user_table.php" has been created.
```

@@ -41,7 +50,7 @@ class Migration_create_user_table extends CI_Migration {
### Adding column named "name" in "user" table

```bash
$ php vendor/bin/refinery create:migration add_name_in_user_table
$ php vendor/bin/refinery create add_name_in_user_table
"20150607123510_add_name_in_user_table.php" has been created.
```

@@ -71,14 +80,6 @@ class Migration_add_name_in_user_table extends CI_Migration {
}
```

#### Available keywords

* create_*table*_table
* add_*column*_in_*table*_table
* modify_*column*_in_*table*_table
* delete_*column*_in_*table*_table


### Migrating all files in ```application/migrations``` directory

```bash
@@ -90,14 +91,14 @@ $ php vendor/bin/refinery migrate
### You can also revert back if you want

```bash
$ php vendor/bin/refinery migrate --revert=1
$ php vendor/bin/refinery rollback
Database is reverted back to version 20150607123241. (20150607123241_create_user_table)
```

### Or reset them back

```bash
$ php vendor/bin/refinery migrate:reset
$ php vendor/bin/refinery reset
Database has been resetted.
```

@@ -107,37 +108,39 @@ Database has been resetted.

#### Description:

Migrate the database
Migrates the database

#### Arguments:
#### ```rollback```

* ```version``` (**Optional**) - Migrate to a specified version of the database
#### Description:

#### Options:
Returns to a previous/specified migration

* ```--revert``` - Number of times to revert from the list of migrations
#### Arguments:

**NOTE**: If an error occurs about URI, just include an equal sign ```=``` in ```$config['permitted_uri_chars']``` on ```application/config.php```
* ```version``` - Specified version of the migration

#### ```migrate:reset```
#### ```reset```

#### Description:

Rollback all migrations back to start
Resets all migrations

#### ```create:migration```
#### ```create```

#### Description:

Create a new migration file
Creates a new migration file

#### Arguments:

* ```name``` - Name of the migration file

#### Options:

**NOTE**: The following options below are only available when you use the ```add_```, ```delete_```, or ```modify_``` text
**NOTE**: The following options below are only available when you use the ```add_```, ```delete_```, or ```modify_``` keywords

* ```--from-database``` - Generates a migration file that is based from the database

* ```--sequential``` - Generates a migration file with a sequential identifier

43 changes: 33 additions & 10 deletions src/Commands/CreateMigrationCommand.php
Original file line number Diff line number Diff line change
@@ -30,12 +30,17 @@ class CreateMigrationCommand extends AbstractCommand
*/
protected function configure()
{
$this->setName('create:migration')
->setDescription('Create a new migration file')
$this->setName('create')
->setDescription('Creates a new migration file')
->addArgument(
'name',
InputArgument::REQUIRED,
'Name of the migration file'
)->addOption(
'from-database',
NULL,
InputOption::VALUE_NONE,
'Generates a migration based from the database'
)->addOption(
'sequential',
NULL,
@@ -113,33 +118,53 @@ protected function execute(InputInterface $input, OutputInterface $output)
);

if ($files != '') {
$number = iterator_count($files) + 1;
$number += iterator_count($files);
}

$sequence = sprintf('%03d', $number);
$fileName = $path . '/' . $sequence .
'_' . $name . '.php';
$fileName = $path . '/' . $sequence . '_' . $name . '.php';
}

$keywords = explode('_', $name);

if (
$input->getOption('from-database') &&
$keywords[0] != 'create' &&
count($keywords) != 3
) {
$message = '"--from-database" is only available to ' .
'"create_*table*_table" keyword.';

return $output->writeln('<error>' . $message . '</error>');
}

$data['columns'] = [];
$data['command'] = $keywords[0];
$data['defaultColumns'] = [];
$data['description'] = str_replace($path . '/', '', $fileName);
$data['name'] = $name;
$data['table'] = (isset($keywords[1])) ? $keywords[1] : '';
$data['columns'] = [];
$data['defaultColumns'] = [];

$data['dataTypes'] = [
'string' => 'VARCHAR',
'integer' => 'INT'
];

switch ($data['command']) {
case 'create':
if ( ! $input->getOption('from-database')) {
break;
}

$describe = $this->getDescribe();
$data['columns'] = $describe->getInformationFromTable(
$data['table']
);

break;
case 'add':
case 'delete':
$field = $keywords[1];

$data['table'] = (isset($keywords[3])) ? $keywords[3] : '';

$column = new Column();
@@ -168,8 +193,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
}

break;
default:
break;
}

Loading

0 comments on commit 5677a81

Please sign in to comment.