Skip to content

Add environment annotation to SQL #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions Migrate/Command/AbstractEnvCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ class AbstractEnvCommand extends AbstractCommand
*/
private $config;

/**
* @var string
*/
private $environment;

/**
* @return \PDO
*/
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
}
15 changes: 13 additions & 2 deletions Migrate/Command/CreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <info>(default ANY)</info>: ";
$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 <info>(default vim)</info>: ", "vim");
$questions->ask($input, $output, $editorQuestion);
$editor = $questions->ask($input, $output, $editorQuestion);

$slugger = new Slugify();
$filename = $slugger->slugify($description);
Expand All @@ -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("<info>$migrationFullPath created</info>");

if (!defined('PHPUNIT')) {
system("vim $migrationFullPath > `tty`");
system("{$editor} $migrationFullPath > `tty`");
}
}
}
36 changes: 32 additions & 4 deletions Migrate/Migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Migration
private $version;
private $sqlUp;
private $sqlDown;
private $allowedEnvironments = ['ANY'];

/**
* @return mixed
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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]);
}
}
}
}
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
"config": {
"bin-dir": "bin"
},
"scripts": {
"phpcs": "bin/phpcs --standard=PSR2 Migrate/"
},
"autoload": {
"psr-4": {
"Migrate\\": "Migrate/"
Expand Down
1 change: 1 addition & 0 deletions templates/migration.tpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
-- // {DESCRIPTION}
-- @ENVIRONMENTS [{ENVIRONMENTS}]
-- Migration SQL that makes the change goes here.

-- @UNDO
Expand Down
2 changes: 1 addition & 1 deletion tests/Command/CreateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function testExecute()
$this->assertFileExists($fileName);
$content = file_get_contents($fileName);
$expected =<<<EXPECTED
-- // je suis une super migration &&&ééé\n-- Migration SQL that makes the change goes here.\n\n-- @UNDO\n-- SQL to undo the change goes here.\n
-- // je suis une super migration &&&ééé\n-- @ENVIRONMENTS [ANY]\n-- Migration SQL that makes the change goes here.\n\n-- @UNDO\n-- SQL to undo the change goes here.\n
EXPECTED;

$this->assertEquals($expected, $content);
Expand Down