Skip to content
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

2.0 #2

Merged
merged 12 commits into from
May 27, 2017
Merged
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ install:
- composer install

script:
- phpunit tests/SqliteCommandTest.php
- phpunit tests/SqliteDatabaseTest.php

36 changes: 29 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![SensioLabsInsight](https://insight.sensiolabs.com/projects/571cb412-7018-4938-a4e5-0f9ce44956d7/mini.png)](https://insight.sensiolabs.com/projects/571cb412-7018-4938-a4e5-0f9ce44956d7)
[![Build Status](https://travis-ci.org/byjg/migration.svg?branch=master)](https://travis-ci.org/byjg/migration)

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

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

### Multi Development environment

If you work with multiple developers and multiple branches it is to difficult to determine what is the next number.

In that case you have the suffix "-dev" after the version number.

See the scenario:

- Developer 1 create a branch and the most recent version in e.g. 42.
- Developer 2 create a branch at the same time and have the same database version number.

In both case the developers will create a file called 43-dev.sql. Both developers will migrate UP and DOWN with
no problem and your local version will be 43.

But developer 1 merged your changes and created a final version 43.sql (`git mv 43-dev.sql 43.sql`). If the developer 2
update your local branch he will have a file 43.sql (from dev 1) and your file 43-dev.sql.
If he is try to migrate UP or DOWN
the migration script will down and alert him there a TWO versions 43. In that case, developer 2 will have to update your
file do 44-dev.sql and continue to work until merge your changes and generate a final version.

## Running in the command line

Expand All @@ -76,9 +95,12 @@ Usage:
command [options] [arguments]

Available commands:
create Create the directory structure FROM a pre-existing database
install Install or upgrade the migrate version in a existing database
down Migrate down the database version.
reset Create a fresh new database
up Migrate Up the database version
version Get the current database version

Arguments:
connection The connection string. Ex. mysql://root:password@server/database [default: false]
Expand All @@ -91,7 +113,7 @@ Example:
migrate down --up-to=3 --path=/somepath mysql://root:password@server/database
```

## Suportted databases:
## Supported databases:

* Sqlite
* Mysql / MariaDB
Expand All @@ -101,7 +123,7 @@ Example:
## Installing Globally

```bash
composer global require 'byjg/migration=1.1.*'
composer global require 'byjg/migration=2.0.*'
sudo ln -s $HOME/.composer/vendor/bin/migrate /usr/local/bin
```

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

```
phpunit tests/SqliteCommandTest.php
phpunit tests/MysqlCommandTest.php
phpunit tests/PostgresCommandTest.php
phpunit tests/SqlServerCommandTest.php
phpunit tests/SqliteDatabaseTest.php
phpunit tests/MysqlDatabaseTest.php
phpunit tests/PostgresDatabaseTest.php
phpunit tests/SqlServerDatabaseTest.php
```
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "byjg/migration",
"description": "A micro framework in PHP for managing a set of database migrations using pure Sql.",
"description": "Simple library in PHP for database version control. Supports Sqlite, MySql, Sql Server and Postgres.",
"authors": [
{
"name": "jg",
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ and open the template in the editor.
<testsuites>
<testsuite name="Test Suite">
<!--<directory>./tests/</directory>-->
<file>./tests/SqliteCommandTest.php</file>
<file>./tests/SqliteDatabaseTest.php</file>
</testsuite>
</testsuites>

Expand Down
4 changes: 3 additions & 1 deletion scripts/migrate
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ require_once($autoload);

use Symfony\Component\Console\Application;

$application = new Application('Migrate Script by JG', '1.1.1');
$application = new Application('Migrate Script by JG', '2.0.0');
$application->add(new \ByJG\DbMigration\Console\ResetCommand());
$application->add(new \ByJG\DbMigration\Console\UpCommand());
$application->add(new \ByJG\DbMigration\Console\DownCommand());
$application->add(new \ByJG\DbMigration\Console\CreateCommand());
$application->add(new \ByJG\DbMigration\Console\DatabaseVersionCommand());
$application->add(new \ByJG\DbMigration\Console\InstallCommand());
$application->add(new \ByJG\DbMigration\Console\UpdateCommand());
$application->run();
54 changes: 0 additions & 54 deletions src/Commands/AbstractCommand.php

This file was deleted.

14 changes: 13 additions & 1 deletion src/Console/ConsoleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace ByJG\DbMigration\Console;

use ByJG\AnyDataset\ConnectionManagement;
use ByJG\DbMigration\Migration;
use ByJG\Util\Uri;
use Symfony\Component\Console\Command\Command;
Expand Down Expand Up @@ -89,5 +88,18 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
}

/**
* @param \Exception|\Error $ex
* @param \Symfony\Component\Console\Output\OutputInterface $output
*/
protected function handleError($ex, OutputInterface $output)
{
$output->writeln('-- Error migrating tables --');
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
$output->writeln(get_class($ex));
$output->writeln($ex->getMessage());
}
}


}
8 changes: 7 additions & 1 deletion src/Console/DatabaseVersionCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output)
{
parent::execute($input, $output);
$output->writeln('version: ' . $this->migration->getCurrentVersion());
try {
$versionInfo = $this->migration->getCurrentVersion();
$output->writeln('version: ' . $versionInfo['version']);
$output->writeln('status.: ' . $versionInfo['status']);
} catch (\Exception $ex) {
$this->handleError($ex, $output);
}
}
}
24 changes: 22 additions & 2 deletions src/Console/DownCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;

class DownCommand extends ConsoleCommand
{
Expand All @@ -24,8 +25,27 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
parent::execute($input, $output);
$this->migration->down($this->upTo);
try {
$versionInfo = $this->migration->getCurrentVersion();
if (strpos($versionInfo['status'], 'partial') !== false) {
$helper = $this->getHelper('question');
$question = new ConfirmationQuestion(
'The database was not fully updated and maybe be unstable. Did you really want migrate the version? (y/N)',
false
);

if (!$helper->ask($input, $output, $question)) {
$output->writeln('Aborted.');

return;
}
}

parent::execute($input, $output);
$this->migration->down($this->upTo, true);
} catch (\Exception $ex) {
$this->handleError($ex, $output);
}
}

}
51 changes: 51 additions & 0 deletions src/Console/InstallCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* Created by PhpStorm.
* User: jg
* Date: 17/06/16
* Time: 21:52
*/

namespace ByJG\DbMigration\Console;

use ByJG\DbMigration\Exception\DatabaseNotVersionedException;
use ByJG\DbMigration\Exception\OldVersionSchemaException;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class InstallCommand extends ConsoleCommand
{
protected function configure()
{
parent::configure();
$this
->setName('install')
->setDescription('Install or upgrade the migrate version in a existing database');

}

protected function execute(InputInterface $input, OutputInterface $output)
{
try {
parent::execute($input, $output);

$action = 'Database is already versioned. ';
try {
$this->migration->getCurrentVersion();
} catch (DatabaseNotVersionedException $ex) {
$action = 'Created the version table';
$this->migration->createVersion();
} catch (OldVersionSchemaException $ex) {
$action = 'Updated the version table';
$this->migration->updateTableVersion();
}

$version = $this->migration->getCurrentVersion();
$output->writeln($action);
$output->writeln('current version: ' . $version['version']);
$output->writeln('current status.: ' . $version['status']);
} catch (\Exception $ex) {
$this->handleError($ex, $output);
}
}
}
26 changes: 16 additions & 10 deletions src/Console/ResetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,23 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$helper = $this->getHelper('question');
$question = new ConfirmationQuestion('This will ERASE all of data in your data. Continue with this action? (y/N) ', false);

if (!$helper->ask($input, $output, $question)) {
$output->writeln('Aborted.');
return;
try {
$helper = $this->getHelper('question');
$question = new ConfirmationQuestion('This will ERASE all of data in your data. Continue with this action? (y/N) ',
false);

if (!$helper->ask($input, $output, $question)) {
$output->writeln('Aborted.');

return;
}

parent::execute($input, $output);
$this->migration->prepareEnvironment();
$this->migration->reset($this->upTo);
} catch (\Exception $ex) {
$this->handleError($ex, $output);
}

parent::execute($input, $output);
$this->migration->prepareEnvironment();
$this->migration->reset($this->upTo);
}

}
24 changes: 22 additions & 2 deletions src/Console/UpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;

class UpCommand extends ConsoleCommand
{
Expand All @@ -24,7 +25,26 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
parent::execute($input, $output);
$this->migration->up($this->upTo);
try {
$versionInfo = $this->migration->getCurrentVersion();
if (strpos($versionInfo['status'], 'partial') !== false) {
$helper = $this->getHelper('question');
$question = new ConfirmationQuestion(
'The database was not fully updated and maybe be unstable. Did you really want migrate the version? (y/N) ',
false
);

if (!$helper->ask($input, $output, $question)) {
$output->writeln('Aborted.');

return;
}
}

parent::execute($input, $output);
$this->migration->up($this->upTo, true);
} catch (\Exception $ex) {
$this->handleError($ex, $output);
}
}
}
Loading