From 89e598cfb572ccec8d0cfea4e6efc56d77d41539 Mon Sep 17 00:00:00 2001 From: five07 Date: Fri, 11 Oct 2019 10:09:49 -0700 Subject: [PATCH 1/3] Add environment annotation to SQL This change allows filtering migrations for specific environments. This is useful mock data for dev/qa, environment specific settings, etc. Signed-off-by: five07 --- Migrate/Command/AbstractEnvCommand.php | 20 ++++++++++++-- Migrate/Command/CreateCommand.php | 15 +++++++++-- Migrate/Migration.php | 36 +++++++++++++++++++++++--- README.md | 4 +++ composer.json | 3 +++ templates/migration.tpl | 1 + tests/Command/CreateCommandTest.php | 4 ++- 7 files changed, 74 insertions(+), 9 deletions(-) diff --git a/Migrate/Command/AbstractEnvCommand.php b/Migrate/Command/AbstractEnvCommand.php index 4ac7f42..0e15db8 100644 --- a/Migrate/Command/AbstractEnvCommand.php +++ b/Migrate/Command/AbstractEnvCommand.php @@ -29,6 +29,11 @@ class AbstractEnvCommand extends AbstractCommand */ private $config; + /** + * @var string + */ + private $environment; + /** * @return \PDO */ @@ -66,6 +71,8 @@ protected function init(InputInterface $input, OutputInterface $output, $env = n $env = $input->getArgument('env'); } + $this->environment = $env; + $parser = $configLocator->locate($env); $conf = $parser->parse(); @@ -212,7 +219,7 @@ public function executeUpMigration(Migration $migration, $changeLogOnly = false) { $this->getDb()->beginTransaction(); - if ($changeLogOnly === false) { + if ($changeLogOnly === false && $this->isEnvironmentAllowed($migration)) { $result = $this->getDb()->exec($migration->getSqlUp()); if ($result === false) { @@ -244,7 +251,7 @@ public function executeDownMigration(Migration $migration, $changeLogOnly = fals { $this->getDb()->beginTransaction(); - if ($changeLogOnly === false) { + if ($changeLogOnly === false && $this->isEnvironmentAllowed($migration)) { $result = $this->getDb()->exec($migration->getSqlDown()); if ($result === false) { @@ -313,4 +320,13 @@ protected function filterMigrationsToExecute(InputInterface $input, OutputInterf return $toExecute; } + + protected function isEnvironmentAllowed(Migration $migration) + { + $allowed = $migration->getAllowedEnvironments(); + + print_r([$this->environment, $allowed]); + + return in_array(strtoupper($this->environment), $allowed) || in_array('ANY', $allowed); + } } diff --git a/Migrate/Command/CreateCommand.php b/Migrate/Command/CreateCommand.php index 458887d..b8c4c97 100644 --- a/Migrate/Command/CreateCommand.php +++ b/Migrate/Command/CreateCommand.php @@ -32,8 +32,18 @@ protected function execute(InputInterface $input, OutputInterface $output) $descriptionQuestion = new Question("Please enter a description: "); $description = $questions->ask($input, $output, $descriptionQuestion); + $question = "Please comma separate list the environments to allow or (default ANY): "; + $descriptionQuestion = new Question($question, "ANY"); + $environments = $questions->ask($input, $output, $descriptionQuestion); + + $tokens = explode(' ', trim($environments)); + if (count($tokens) > 1) { + $environments = implode(',', array_map('trim', $tokens)); + } + $environments = strtoupper(str_replace(',,', ',', $environments)); + $editorQuestion = new Question("Please chose which editor to use (default vim): ", "vim"); - $questions->ask($input, $output, $editorQuestion); + $editor = $questions->ask($input, $output, $editorQuestion); $slugger = new Slugify(); $filename = $slugger->slugify($description); @@ -42,13 +52,14 @@ protected function execute(InputInterface $input, OutputInterface $output) $templateFile = file_get_contents(__DIR__ . '/../../templates/migration.tpl'); $templateFile = str_replace('{DESCRIPTION}', $description, $templateFile); + $templateFile = str_replace('{ENVIRONMENTS}', $environments, $templateFile); $migrationFullPath = $this->getMigrationDir() . '/' . $filename; file_put_contents($migrationFullPath, $templateFile); $output->writeln("$migrationFullPath created"); if (!defined('PHPUNIT')) { - system("vim $migrationFullPath > `tty`"); + system("{$editor} $migrationFullPath > `tty`"); } } } diff --git a/Migrate/Migration.php b/Migrate/Migration.php index 1efeef3..f3ec556 100644 --- a/Migrate/Migration.php +++ b/Migrate/Migration.php @@ -20,6 +20,7 @@ class Migration private $version; private $sqlUp; private $sqlDown; + private $allowedEnvironments = ['ANY']; /** * @return mixed @@ -133,6 +134,23 @@ public function setSqlDown($sqlDown) $this->sqlDown = $sqlDown; } + /** + * @return array + */ + public function getAllowedEnvironments() + { + return $this->allowedEnvironments; + } + + /** + * @param array + */ + public function setAllowedEnvironments($allowedEnvironments) + { + $items = array_map('strtoupper', $allowedEnvironments); + $this->allowedEnvironments = $items; + } + public static function createFromFile($filename, $migrationDir) { $data = explode('_', $filename); @@ -178,10 +196,20 @@ public function toArray() public function load($migrationDir) { $content = file_get_contents($migrationDir . '/' . $this->getFile()); - if ($content && strpos($content, '@UNDO') > 0) { - $sql = explode('-- @UNDO', $content); - $this->setSqlUp($sql[0]); - $this->setSqlDown($sql[1]); + + if ($content) { + if (strpos($content, '@ENVIRONMENTS') > 0) { + if (preg_match('/-- @ENVIRONMENTS \[(.+)\]/', $content, $matches)) { + $tokens = explode(',', $matches[1]); + $this->setAllowedEnvironments(array_map('strtoupper', $tokens)); + } + } + + if (strpos($content, '@UNDO') > 0) { + $sql = explode('-- @UNDO', $content); + $this->setSqlUp($sql[0]); + $this->setSqlDown($sql[1]); + } } } } diff --git a/README.md b/README.md index ec51786..f196ec2 100644 --- a/README.md +++ b/README.md @@ -95,12 +95,16 @@ $ ./bin/migrate migrate:create Migrations file are like this -- // add table users + -- @ENVIRONMENTS [DEV,QA] -- Migration SQL that makes the change goes here. create table users (id integer, name text); -- @UNDO -- SQL to undo the change goes here. drop table users; +You can restrict which environment uses this SQL with "@ENVIRONMENT" annotation. Values inside array brackets can be +one or more comma separated environment values OR "ANY" + List migrations ------------------ View all available migrations and their status. diff --git a/composer.json b/composer.json index e8cd882..a44b004 100644 --- a/composer.json +++ b/composer.json @@ -34,6 +34,9 @@ "config": { "bin-dir": "bin" }, + "scripts": { + "phpcs": "bin/phpcs --standard=PSR2 Migrate/" + }, "autoload": { "psr-4": { "Migrate\\": "Migrate/" diff --git a/templates/migration.tpl b/templates/migration.tpl index d60a527..bcc0bde 100644 --- a/templates/migration.tpl +++ b/templates/migration.tpl @@ -1,4 +1,5 @@ -- // {DESCRIPTION} +-- @ENVIRONMENTS [{ENVIRONMENTS}] -- Migration SQL that makes the change goes here. -- @UNDO diff --git a/tests/Command/CreateCommandTest.php b/tests/Command/CreateCommandTest.php index 5718f44..a9a57a2 100644 --- a/tests/Command/CreateCommandTest.php +++ b/tests/Command/CreateCommandTest.php @@ -42,6 +42,8 @@ public function testExecute() /* @var $question QuestionHelper */ $question = $command->getHelper('question'); $question->setInputStream(InputStreamUtil::type("je suis une super migration &&&ééé\n\n:x\n")); + $question = $command->getHelper('question'); + $question->setInputStream(InputStreamUtil::type("testing\n\n:x\n")); $commandTester->execute(array('command' => $command->getName())); @@ -53,7 +55,7 @@ public function testExecute() $this->assertFileExists($fileName); $content = file_get_contents($fileName); $expected =<<assertEquals($expected, $content); From f186ae7d5767485905347a5a03477e2971b6c1fd Mon Sep 17 00:00:00 2001 From: five07 Date: Fri, 11 Oct 2019 10:16:29 -0700 Subject: [PATCH 2/3] Remove debug Signed-off-by: five07 --- Migrate/Command/AbstractEnvCommand.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/Migrate/Command/AbstractEnvCommand.php b/Migrate/Command/AbstractEnvCommand.php index 0e15db8..a7ecd3d 100644 --- a/Migrate/Command/AbstractEnvCommand.php +++ b/Migrate/Command/AbstractEnvCommand.php @@ -325,8 +325,6 @@ protected function isEnvironmentAllowed(Migration $migration) { $allowed = $migration->getAllowedEnvironments(); - print_r([$this->environment, $allowed]); - return in_array(strtoupper($this->environment), $allowed) || in_array('ANY', $allowed); } } From 439822558fb2a19e12ea565da5ec085f464f05d1 Mon Sep 17 00:00:00 2001 From: five07 Date: Fri, 11 Oct 2019 10:18:55 -0700 Subject: [PATCH 3/3] Updating test for CI Signed-off-by: five07 --- tests/Command/CreateCommandTest.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/Command/CreateCommandTest.php b/tests/Command/CreateCommandTest.php index a9a57a2..f5a882b 100644 --- a/tests/Command/CreateCommandTest.php +++ b/tests/Command/CreateCommandTest.php @@ -42,8 +42,6 @@ public function testExecute() /* @var $question QuestionHelper */ $question = $command->getHelper('question'); $question->setInputStream(InputStreamUtil::type("je suis une super migration &&&ééé\n\n:x\n")); - $question = $command->getHelper('question'); - $question->setInputStream(InputStreamUtil::type("testing\n\n:x\n")); $commandTester->execute(array('command' => $command->getName())); @@ -55,7 +53,7 @@ public function testExecute() $this->assertFileExists($fileName); $content = file_get_contents($fileName); $expected =<<assertEquals($expected, $content);