Created
March 12, 2019 04:04
-
-
Save tai-sho/a04ce4fb5f5533fdbd6e0c663c5174cc to your computer and use it in GitHub Desktop.
CakePHP3.6でデータベース初期化+マイグレーションを行う。 rails db:migrate:resetと同様
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Command; | |
use Cake\Console\Arguments; | |
use Cake\Console\Command; | |
use Cake\Console\ConsoleIo; | |
use Cake\Console\ConsoleOptionParser; | |
use Cake\Datasource\ConnectionManager; | |
/** | |
* DatabaseReset command. | |
*/ | |
class DatabaseMigrateResetCommand extends Command | |
{ | |
/** | |
* Hook method for defining this command's option parser. | |
* | |
* @see https://book.cakephp.org/3.0/en/console-and-shells/commands.html#defining-arguments-and-options | |
* | |
* @param \Cake\Console\ConsoleOptionParser $parser The parser to be defined | |
* @return \Cake\Console\ConsoleOptionParser The built parser. | |
*/ | |
public function buildOptionParser(ConsoleOptionParser $parser) | |
{ | |
$parser = parent::buildOptionParser($parser); | |
return $parser; | |
} | |
/** | |
* Implement this method with your command's logic. | |
* | |
* @param \Cake\Console\Arguments $args The command arguments. | |
* @param \Cake\Console\ConsoleIo $io The console io | |
* @return null|int The exit code or null for success | |
*/ | |
public function execute(Arguments $args, ConsoleIo $io) | |
{ | |
$connection = ConnectionManager::get('default'); | |
$config = $connection->config(); | |
$connection->execute("DROP DATABASE {$config['database']};"); | |
$connection->execute("CREATE DATABASE {$config['database']};"); | |
system(ROOT . DS . 'bin' . DS . 'cake migrations migrate'); | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment