Skip to content

Commit

Permalink
The create command has been added to the console application to creat…
Browse files Browse the repository at this point in the history
…e a new migration file.
  • Loading branch information
muhammetsafak committed Jul 10, 2022
1 parent 3ec331d commit 1b8c772
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 14 deletions.
20 changes: 13 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,44 +94,50 @@ To use the CLI interface, first create the "`barbarian.json`" file in your worki
Or you can try to create this file with the command below.

```
php barbarian json
vendor/bin/barbarian json
```

You can use the command below to create a new migration class.

```
vendor/bin/barbarian create
```

You can use the command below to up all migrations.

```
php barbarian up
vendor/bin/barbarian up
```

You can use the `-version` flag to only up a migration.

```
php barbarian up -version=20221230153013
vendor/bin/barbarian up -version=20221230153013
```

or

```
php barbarian up -version=Migration_20221230153013
vendor/bin/barbarian up -version=Migration_20221230153013
```


You can use the command below to down all migrations.

```
php barbarian down
vendor/bin/barbarian down
```

You can use the `-version` flag to only up a migration.

```
php barbarian down -version=20221230153013
vendor/bin/barbarian down -version=20221230153013
```

or

```
php barbarian down -version=Migration_20221230153013
vendor/bin/barbarian down -version=Migration_20221230153013
```

## Credits
Expand Down
29 changes: 28 additions & 1 deletion barbarian
Original file line number Diff line number Diff line change
Expand Up @@ -180,5 +180,32 @@ $console->register('down', function (Console $console) {
}
}, 'Runs the down() method of the Migration class.');

$console->register('create', function (Console $console) {
$migration = barbarian_json_migration_start();
$dir = $migration->getOption('folder');
$namespace = $migration->getOption('namespace');
$name = 'Migration_' . date("YmdHis");

$content = '<?php' . PHP_EOL . 'declare(strict_types=1);' . PHP_EOL . PHP_EOL;

if(!empty($namespace)){
$content .= 'namespace ' . $namespace . ';' . PHP_EOL . PHP_EOL;
}

$content .= 'use InitPHP\\Barbarian\\QueryInterface;' . PHP_EOL . PHP_EOL . 'class ' . $name . ' extends \\InitPHP\\Barbarian\\MigrationAbstract' . PHP_EOL . '{' . PHP_EOL . PHP_EOL . ' public function up(QueryInterface $query) : bool' . PHP_EOL . ' {' . PHP_EOL . ' return true;' . PHP_EOL . ' }' . PHP_EOL . PHP_EOL . ' public function down(QueryInterface $query) : bool' . PHP_EOL . ' {' . PHP_EOL . ' return true;' . PHP_EOL . ' }' . PHP_EOL . PHP_EOL . '}' . PHP_EOL;

$path = rtrim($dir, "/\\") . '/' . $name . '.php';
if(file_exists($path)){
$console->warning('"' . $name . '" already exists.');
exit;
}

if((@file_put_contents($path, $content)) === FALSE){
$console->warning('Failed to create "' . $name . '".');
exit;
}
$console->success('"'.$name.'" migration created.');
}, 'Creates a new Migration class.');


$console->run();
$console->run();
2 changes: 1 addition & 1 deletion src/MigrationAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author Muhammet ŞAFAK <[email protected]>
* @copyright Copyright © 2022 Muhammet ŞAFAK
* @license ./LICENSE MIT
* @version 1.0
* @version 1.0.1
* @link https://www.muhammetsafak.com.tr
*/

Expand Down
2 changes: 1 addition & 1 deletion src/MigrationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author Muhammet ŞAFAK <[email protected]>
* @copyright Copyright © 2022 Muhammet ŞAFAK
* @license ./LICENSE MIT
* @version 1.0
* @version 1.0.1
* @link https://www.muhammetsafak.com.tr
*/

Expand Down
2 changes: 1 addition & 1 deletion src/MigrationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author Muhammet ŞAFAK <[email protected]>
* @copyright Copyright © 2022 Muhammet ŞAFAK
* @license ./LICENSE MIT
* @version 1.0
* @version 1.0.1
* @link https://www.muhammetsafak.com.tr
*/

Expand Down
8 changes: 6 additions & 2 deletions src/Migrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author Muhammet ŞAFAK <[email protected]>
* @copyright Copyright © 2022 Muhammet ŞAFAK
* @license ./LICENSE MIT
* @version 1.0
* @version 1.0.1
* @link https://www.muhammetsafak.com.tr
*/

Expand Down Expand Up @@ -127,6 +127,11 @@ public function downMigration(MigrationInterface $migration): bool
return false;
}

public function getOption(string $key, $default = null)
{
return $this->options[$key] ?? $default;
}

protected function setMigrationTable(string $table): self
{
if(((bool)preg_match('/^[a-zA-Z_]+$/', $table)) === FALSE){
Expand Down Expand Up @@ -176,7 +181,6 @@ protected function folder_scan()
}
}


private function migration_versions_table_exists()
{
switch ($this->driver) {
Expand Down
2 changes: 1 addition & 1 deletion src/QueryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author Muhammet ŞAFAK <[email protected]>
* @copyright Copyright © 2022 Muhammet ŞAFAK
* @license ./LICENSE MIT
* @version 1.0
* @version 1.0.1
* @link https://www.muhammetsafak.com.tr
*/

Expand Down

0 comments on commit 1b8c772

Please sign in to comment.