Skip to content

Commit 2cd06d0

Browse files
author
Alexandre GUIDET
committed
Merge branch 'release-3.4.0' into stable
2 parents 8bb91c3 + 0774599 commit 2cd06d0

13 files changed

Lines changed: 682 additions & 78 deletions

Migrate/Command/AbstractEnvCommand.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
class AbstractEnvCommand extends AbstractComand
2020
{
2121

22-
protected static $progressBarFormat = '%current%/%max% [%bar%] %percent% % %memory% [%message%]';
22+
protected static $progressBarFormat = '%current%/%max% [%bar%] %percent% % [%message%]';
2323

2424
/**
2525
* @var \PDO
@@ -76,10 +76,14 @@ protected function init(InputInterface $input, OutputInterface $output, $env = n
7676
$password = ArrayUtil::get($conf['connection'], 'password');
7777

7878
$uri = $driver;
79-
$uri .= ($dbname == null) ?: ":dbname=$dbname";
80-
$uri .= ($host == null) ?: ";host=$host";
81-
$uri .= ($port == null) ?: ";port=$port";
8279

80+
if ($driver == 'sqlite') {
81+
$uri .= ":$dbname";
82+
} else {
83+
$uri .= ($dbname == null) ?: ":dbname=$dbname";
84+
$uri .= ($host == null) ?: ";host=$host";
85+
$uri .= ($port == null) ?: ";port=$port";
86+
}
8387
$this->db = new \PDO(
8488
$uri,
8589
$username,
@@ -98,6 +102,7 @@ public function getLocalMigrations()
98102
$fileList = scandir($this->getMigrationDir());
99103
$fileList = ArrayUtil::filter($fileList);
100104

105+
$migrations = array();
101106
foreach ($fileList as $file) {
102107
$migration = Migration::createFromFile($file, $this->getMigrationDir());
103108
$migrations[$migration->getId()] = $migration;

Migrate/Command/CreateCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
4949
file_put_contents($migrationFullPath, $templateFile);
5050
$output->writeln("<info>$migrationFullPath created</info>");
5151

52-
system("vim $migrationFullPath > `tty`");
52+
if (!defined('PHPUNIT')) {
53+
system("vim $migrationFullPath > `tty`");
54+
}
5355
}
5456

5557
}

Migrate/Enum/Directory.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: aguidet
5+
* Date: 01/03/15
6+
* Time: 01:41
7+
*/
8+
9+
namespace Migrate\Enum;
10+
11+
12+
class Directory {
13+
14+
public static $appDirectory = '.php-database-migration';
15+
16+
public static function getEnvPath()
17+
{
18+
return self::$appDirectory . '/environments';
19+
}
20+
21+
public static function getMigrationsPath()
22+
{
23+
return self::$appDirectory . '/migrations';
24+
}
25+
}

Migrate/Utils/InputStreamUtil.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: aguidet
5+
* Date: 01/03/15
6+
* Time: 01:45
7+
*/
8+
9+
namespace Migrate\Utils;
10+
11+
12+
class InputStreamUtil {
13+
14+
public static function type($input)
15+
{
16+
$stream = fopen('php://memory', 'r+', false);
17+
fputs($stream, $input);
18+
rewind($stream);
19+
20+
return $stream;
21+
}
22+
23+
}

README.md

Lines changed: 50 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,67 @@
11
PhpDbMigration - full PHP database migration tool
22
=================================================
33

4-
This is a full standalone PHP tool inspired by the rails database migration tool and MyBatis.
4+
This is a full standalone PHP tool based on symfony console and inspired by the rails database migration tool and MyBatis.
55
It merge the functionnality of the two tools and has been desined to be as flexible as possible.
66

7+
Installing it to your project
8+
-----------------------------
9+
Just add it to your composer.json (don't forget to specify your bin directory)
10+
Warning, all migrate commands must be executed on your root folder like `bin/migrate migrate:command...`
11+
12+
{
13+
"name": "jdoe/testproject",
14+
"authors": [
15+
{
16+
"name": "Jhon DOE",
17+
"email": "jdoe@gmail.com"
18+
}
19+
],
20+
"require": {
21+
"php-database-migration/php-database-migration" :"3.4.*"
22+
},
23+
"config": {
24+
"bin-dir": "bin"
25+
}
26+
}
27+
28+
29+
Adding an environment
30+
---------------------
31+
The first thing to do before playing with SQL migrations is to add an environment, let's add the dev one.
32+
33+
![alt tag](http://tikotepadventure.com/files/php-database-migration/addenv.gif)
34+
735
Initialization
836
--------------
37+
Once the environment is added, you have to initialize it (create the changelog table on the good database)
938

10-
The first time the tool is run, it needs a first initialization like the following:
11-
12-
./migrate --init --driver=<driver> --database=<database> --host=<host> --login=<db_login> --password=<db_password> --changelog=<changelog_table_name>
39+
![alt tag](http://tikotepadventure.com/files/php-database-migration/init.gif)
1340

14-
Example:
41+
Create a migration
42+
------------------
43+
It is time to create our first migration file.
1544

16-
./migrate --init --driver=pgsql --database=php_migration_test --host=localhost --login=my_login --password=my_password --changelog=changelog
45+
![alt tag](http://tikotepadventure.com/files/php-database-migration/create.gif)
1746

18-
wich will create the following directories/files
47+
Migrations file are like this
1948

20-
./environments
21-
|----development.ini
22-
|----preproduction.ini
23-
`----production.ini
49+
--// add table users
50+
-- Migration SQL that makes the change goes here.
51+
create table users (id integer, name text);
52+
-- @UNDO
53+
-- SQL to undo the change goes here.
54+
drop table users;
2455

25-
./migrations
56+
Up and down
57+
------------------
58+
You can now up all the pending migrations. If you decided to down a migration, the last one will be downed alone to prevent from mistake. You will be asked to confirm the downgrade of your database before runing the real SQL script.
59+
![alt tag](http://tikotepadventure.com/files/php-database-migration/status.gif)
2660

27-
just edit/change the environment ini files in order to match with your database access.
61+
For developement purpose, it is also possible to up a single migration without taking care of the other ones.
2862

29-
Usage
30-
-----
63+
![alt tag](http://tikotepadventure.com/files/php-database-migration/uponly.gif)
3164

32-
Usage: ./migrate command [parameters] [--env=<environment>]
33-
34-
Commands:
35-
--env=<environment> Environment to configure. Default environment is 'dev'.
36-
--generate <description> Creates a new migration with the provided description.
37-
--up Run unapplied migrations, ALL by default.
38-
--up=<version> Run unapplied migrations up to version (included).
39-
--down Undoes migrations applied to the database. ONE by default.
40-
--down=<version> Undoes migrations applied to the database. Down to version (included).
41-
--force Run or undoes only specified migration (not recommended).
42-
--transactional Rollback all applied migration up or down on error.
43-
--status Show migrations status (applied, unapplied ect...).
65+
Same thing for down
4466

45-
Examples:
46-
./migrate [--generate <migration_name>]
47-
./migrate [--up | --up=<version> | --down | --down=<version>] [--transactional] [--force] [--env=<environment>]
48-
./migrate [--status] [--env=<environment>]
67+
![alt tag](http://tikotepadventure.com/files/php-database-migration/downonly.gif)

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,8 @@
3030
},
3131
"autoload": {
3232
"psr-4": {"Migrate\\": "Migrate/"}
33+
},
34+
"autoload-dev": {
35+
"psr-4": { "Migrate\\Test\\": "tests/" }
3336
}
3437
}

composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
/**
3+
* User: aguidet
4+
* Date: 02/03/15
5+
* Time: 15:19
6+
*/
7+
8+
namespace Migrate\Test\Command;
9+
10+
use Migrate\Command\AddEnvCommand;
11+
use Migrate\Command\InitCommand;
12+
use Migrate\Enum\Directory;
13+
use Migrate\Utils\InputStreamUtil;
14+
use Symfony\Component\Console\Application;
15+
use Symfony\Component\Console\Helper\QuestionHelper;
16+
use Symfony\Component\Console\Tester\CommandTester;
17+
18+
class AbstractCommandTester extends \PHPUnit_Framework_TestCase
19+
{
20+
public static $env = 'testing';
21+
public static $driver = 'sqlite';
22+
public static $bddName = 'migrate_test';
23+
public static $username = 'aguidet';
24+
public static $password = 'aguidet';
25+
public static $host = 'localhost';
26+
public static $port = '5432';
27+
28+
public function cleanEnv()
29+
{
30+
exec("rm -rf .php-database-migration");
31+
32+
if (file_exists('test.sqlite')) {
33+
exec("rm test.sqlite");
34+
}
35+
}
36+
37+
public function createEnv()
38+
{
39+
$application = new Application();
40+
$application->add(new AddEnvCommand());
41+
42+
$command = $application->find('migrate:addenv');
43+
$commandTester = new CommandTester($command);
44+
45+
/* @var $question QuestionHelper */
46+
$question = $command->getHelper('question');
47+
$question->setInputStream(InputStreamUtil::type("testing\n1\ntest.sqlite\n\n\n\n\nchangelog\nvim\n"));
48+
49+
$commandTester->execute(array('command' => $command->getName()));
50+
}
51+
52+
public function initEnv()
53+
{
54+
$application = new Application();
55+
$application->add(new InitCommand());
56+
57+
$command = $application->find('migrate:init');
58+
$commandTester = new CommandTester($command);
59+
60+
$commandTester->execute(array(
61+
'command' => $command->getName(),
62+
'env' => 'testing'
63+
));
64+
}
65+
66+
public function createMigration($timestamp, $sqlUp, $sqlDown)
67+
{
68+
$filename = Directory::getMigrationsPath() . '/' . $timestamp . '_migration.sql';
69+
70+
$content =<<<SQL
71+
--// unit testing migration
72+
-- Migration SQL that makes the change goes here.
73+
$sqlUp
74+
75+
-- @UNDO
76+
-- SQL to undo the change goes here.
77+
$sqlDown
78+
79+
SQL;
80+
81+
file_put_contents($filename, $content);
82+
}
83+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
/**
3+
* User: aguidet
4+
* Date: 27/02/15
5+
* Time: 17:21
6+
*/
7+
8+
namespace Migrate\Command;
9+
10+
use Migrate\Enum\Directory;
11+
use Migrate\Utils\InputStreamUtil;
12+
use Symfony\Component\Console\Application;
13+
use Symfony\Component\Console\Helper\QuestionHelper;
14+
use Symfony\Component\Console\Tester\CommandTester;
15+
use Migrate\Test\Command\AbstractCommandTester;
16+
17+
class AddenvCommandTest extends AbstractCommandTester
18+
{
19+
public function setUp()
20+
{
21+
$this->cleanEnv();
22+
}
23+
24+
public function tearDown()
25+
{
26+
$this->cleanEnv();
27+
}
28+
29+
public function testExecute()
30+
{
31+
$application = new Application();
32+
$application->add(new AddEnvCommand());
33+
34+
$command = $application->find('migrate:addenv');
35+
$commandTester = new CommandTester($command);
36+
37+
/* @var $question QuestionHelper */
38+
$question = $command->getHelper('question');
39+
$question->setInputStream(InputStreamUtil::type("testing\n1\nmigrate_test\nlocalhost\n5432\naguidet\naguidet\nchangelog\nvim\n"));
40+
41+
$commandTester->execute(array('command' => $command->getName()));
42+
43+
$expected = "Please enter the name of the new environment (default dev): Please chose your pdo driver\n [0] pgsql\n [1] sqlite\n > Please enter the database name (or the database file location): Please enter the database host (if needed): Please enter the database port (if needed): Please enter the database user name (if needed): Please enter the database user password (if needed): Please enter the changelog table (default changelog): Please enter the text editor to use by default (default vim): ";
44+
$this->assertEquals($expected, $commandTester->getDisplay());
45+
46+
$envDir = Directory::getEnvPath();
47+
48+
$expected = <<<EXPECTED
49+
connection:
50+
host: localhost
51+
driver: sqlite
52+
port: 5432
53+
username: aguidet
54+
password: aguidet
55+
database: migrate_test
56+
57+
changelog: changelog
58+
default_editor: vim
59+
EXPECTED;
60+
61+
$fileContent = file_get_contents($envDir . '/testing.yml');
62+
63+
$this->assertEquals($expected, $fileContent);
64+
}
65+
66+
}

0 commit comments

Comments
 (0)