diff --git a/Migrate/Command/AbstractEnvCommand.php b/Migrate/Command/AbstractEnvCommand.php
index 4ac7f42..a7ecd3d 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,11 @@ protected function filterMigrationsToExecute(InputInterface $input, OutputInterf
return $toExecute;
}
+
+ protected function isEnvironmentAllowed(Migration $migration)
+ {
+ $allowed = $migration->getAllowedEnvironments();
+
+ 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..f5a882b 100644
--- a/tests/Command/CreateCommandTest.php
+++ b/tests/Command/CreateCommandTest.php
@@ -53,7 +53,7 @@ public function testExecute()
$this->assertFileExists($fileName);
$content = file_get_contents($fileName);
$expected =<<assertEquals($expected, $content);