Skip to content

Commit a21c419

Browse files
authored
Merge pull request #2 from byjg/2.0
2.0
2 parents 9bd1b42 + e1dfd71 commit a21c419

28 files changed

+461
-131
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ install:
88
- composer install
99

1010
script:
11-
- phpunit tests/SqliteCommandTest.php
11+
- phpunit tests/SqliteDatabaseTest.php
1212

README.md

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![SensioLabsInsight](https://insight.sensiolabs.com/projects/571cb412-7018-4938-a4e5-0f9ce44956d7/mini.png)](https://insight.sensiolabs.com/projects/571cb412-7018-4938-a4e5-0f9ce44956d7)
44
[![Build Status](https://travis-ci.org/byjg/migration.svg?branch=master)](https://travis-ci.org/byjg/migration)
55

6-
A micro framework in PHP for managing a set of database migrations using pure Sql.
6+
Simple library in PHP for database version control. Supports Sqlite, MySql, Sql Server and Postgres.
77

88
Database Migration is a set of commands for upgrade or downgrade a database.
99
This library uses only SQL commands.
@@ -66,6 +66,25 @@ For example: 00002.sql is the script for move the database from version '1' to '
6666
For example: 00001.sql is the script for move the database from version '2' to '1'.
6767
The "down" folder is optional.
6868

69+
### Multi Development environment
70+
71+
If you work with multiple developers and multiple branches it is to difficult to determine what is the next number.
72+
73+
In that case you have the suffix "-dev" after the version number.
74+
75+
See the scenario:
76+
77+
- Developer 1 create a branch and the most recent version in e.g. 42.
78+
- Developer 2 create a branch at the same time and have the same database version number.
79+
80+
In both case the developers will create a file called 43-dev.sql. Both developers will migrate UP and DOWN with
81+
no problem and your local version will be 43.
82+
83+
But developer 1 merged your changes and created a final version 43.sql (`git mv 43-dev.sql 43.sql`). If the developer 2
84+
update your local branch he will have a file 43.sql (from dev 1) and your file 43-dev.sql.
85+
If he is try to migrate UP or DOWN
86+
the migration script will down and alert him there a TWO versions 43. In that case, developer 2 will have to update your
87+
file do 44-dev.sql and continue to work until merge your changes and generate a final version.
6988

7089
## Running in the command line
7190

@@ -76,9 +95,12 @@ Usage:
7695
command [options] [arguments]
7796
7897
Available commands:
98+
create Create the directory structure FROM a pre-existing database
99+
install Install or upgrade the migrate version in a existing database
79100
down Migrate down the database version.
80101
reset Create a fresh new database
81102
up Migrate Up the database version
103+
version Get the current database version
82104
83105
Arguments:
84106
connection The connection string. Ex. mysql://root:password@server/database [default: false]
@@ -91,7 +113,7 @@ Example:
91113
migrate down --up-to=3 --path=/somepath mysql://root:password@server/database
92114
```
93115

94-
## Suportted databases:
116+
## Supported databases:
95117

96118
* Sqlite
97119
* Mysql / MariaDB
@@ -101,7 +123,7 @@ Example:
101123
## Installing Globally
102124

103125
```bash
104-
composer global require 'byjg/migration=1.1.*'
126+
composer global require 'byjg/migration=2.0.*'
105127
sudo ln -s $HOME/.composer/vendor/bin/migrate /usr/local/bin
106128
```
107129

@@ -112,8 +134,8 @@ This library has integrated tests and need to be setup for each database you wan
112134
Basiclly you have the follow tests:
113135

114136
```
115-
phpunit tests/SqliteCommandTest.php
116-
phpunit tests/MysqlCommandTest.php
117-
phpunit tests/PostgresCommandTest.php
118-
phpunit tests/SqlServerCommandTest.php
137+
phpunit tests/SqliteDatabaseTest.php
138+
phpunit tests/MysqlDatabaseTest.php
139+
phpunit tests/PostgresDatabaseTest.php
140+
phpunit tests/SqlServerDatabaseTest.php
119141
```

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "byjg/migration",
3-
"description": "A micro framework in PHP for managing a set of database migrations using pure Sql.",
3+
"description": "Simple library in PHP for database version control. Supports Sqlite, MySql, Sql Server and Postgres.",
44
"authors": [
55
{
66
"name": "jg",

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ and open the template in the editor.
2222
<testsuites>
2323
<testsuite name="Test Suite">
2424
<!--<directory>./tests/</directory>-->
25-
<file>./tests/SqliteCommandTest.php</file>
25+
<file>./tests/SqliteDatabaseTest.php</file>
2626
</testsuite>
2727
</testsuites>
2828

scripts/migrate

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ require_once($autoload);
1313

1414
use Symfony\Component\Console\Application;
1515

16-
$application = new Application('Migrate Script by JG', '1.1.1');
16+
$application = new Application('Migrate Script by JG', '2.0.0');
1717
$application->add(new \ByJG\DbMigration\Console\ResetCommand());
1818
$application->add(new \ByJG\DbMigration\Console\UpCommand());
1919
$application->add(new \ByJG\DbMigration\Console\DownCommand());
2020
$application->add(new \ByJG\DbMigration\Console\CreateCommand());
2121
$application->add(new \ByJG\DbMigration\Console\DatabaseVersionCommand());
22+
$application->add(new \ByJG\DbMigration\Console\InstallCommand());
23+
$application->add(new \ByJG\DbMigration\Console\UpdateCommand());
2224
$application->run();

src/Commands/AbstractCommand.php

Lines changed: 0 additions & 54 deletions
This file was deleted.

src/Console/ConsoleCommand.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace ByJG\DbMigration\Console;
44

5-
use ByJG\AnyDataset\ConnectionManagement;
65
use ByJG\DbMigration\Migration;
76
use ByJG\Util\Uri;
87
use Symfony\Component\Console\Command\Command;
@@ -89,5 +88,18 @@ protected function execute(InputInterface $input, OutputInterface $output)
8988
}
9089
}
9190

91+
/**
92+
* @param \Exception|\Error $ex
93+
* @param \Symfony\Component\Console\Output\OutputInterface $output
94+
*/
95+
protected function handleError($ex, OutputInterface $output)
96+
{
97+
$output->writeln('-- Error migrating tables --');
98+
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
99+
$output->writeln(get_class($ex));
100+
$output->writeln($ex->getMessage());
101+
}
102+
}
103+
92104

93105
}

src/Console/DatabaseVersionCommand.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ protected function configure()
2525
protected function execute(InputInterface $input, OutputInterface $output)
2626
{
2727
parent::execute($input, $output);
28-
$output->writeln('version: ' . $this->migration->getCurrentVersion());
28+
try {
29+
$versionInfo = $this->migration->getCurrentVersion();
30+
$output->writeln('version: ' . $versionInfo['version']);
31+
$output->writeln('status.: ' . $versionInfo['status']);
32+
} catch (\Exception $ex) {
33+
$this->handleError($ex, $output);
34+
}
2935
}
3036
}

src/Console/DownCommand.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use Symfony\Component\Console\Input\InputInterface;
1212
use Symfony\Component\Console\Output\OutputInterface;
13+
use Symfony\Component\Console\Question\ConfirmationQuestion;
1314

1415
class DownCommand extends ConsoleCommand
1516
{
@@ -24,8 +25,27 @@ protected function configure()
2425

2526
protected function execute(InputInterface $input, OutputInterface $output)
2627
{
27-
parent::execute($input, $output);
28-
$this->migration->down($this->upTo);
28+
try {
29+
$versionInfo = $this->migration->getCurrentVersion();
30+
if (strpos($versionInfo['status'], 'partial') !== false) {
31+
$helper = $this->getHelper('question');
32+
$question = new ConfirmationQuestion(
33+
'The database was not fully updated and maybe be unstable. Did you really want migrate the version? (y/N)',
34+
false
35+
);
36+
37+
if (!$helper->ask($input, $output, $question)) {
38+
$output->writeln('Aborted.');
39+
40+
return;
41+
}
42+
}
43+
44+
parent::execute($input, $output);
45+
$this->migration->down($this->upTo, true);
46+
} catch (\Exception $ex) {
47+
$this->handleError($ex, $output);
48+
}
2949
}
3050

3151
}

src/Console/InstallCommand.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: jg
5+
* Date: 17/06/16
6+
* Time: 21:52
7+
*/
8+
9+
namespace ByJG\DbMigration\Console;
10+
11+
use ByJG\DbMigration\Exception\DatabaseNotVersionedException;
12+
use ByJG\DbMigration\Exception\OldVersionSchemaException;
13+
use Symfony\Component\Console\Input\InputInterface;
14+
use Symfony\Component\Console\Output\OutputInterface;
15+
16+
class InstallCommand extends ConsoleCommand
17+
{
18+
protected function configure()
19+
{
20+
parent::configure();
21+
$this
22+
->setName('install')
23+
->setDescription('Install or upgrade the migrate version in a existing database');
24+
25+
}
26+
27+
protected function execute(InputInterface $input, OutputInterface $output)
28+
{
29+
try {
30+
parent::execute($input, $output);
31+
32+
$action = 'Database is already versioned. ';
33+
try {
34+
$this->migration->getCurrentVersion();
35+
} catch (DatabaseNotVersionedException $ex) {
36+
$action = 'Created the version table';
37+
$this->migration->createVersion();
38+
} catch (OldVersionSchemaException $ex) {
39+
$action = 'Updated the version table';
40+
$this->migration->updateTableVersion();
41+
}
42+
43+
$version = $this->migration->getCurrentVersion();
44+
$output->writeln($action);
45+
$output->writeln('current version: ' . $version['version']);
46+
$output->writeln('current status.: ' . $version['status']);
47+
} catch (\Exception $ex) {
48+
$this->handleError($ex, $output);
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)