Skip to content

Commit

Permalink
Merge pull request #7 from byjg/2.0.4
Browse files Browse the repository at this point in the history
2.0.4
  • Loading branch information
byjg authored Mar 20, 2018
2 parents 424df69 + be3b9a3 commit 6d7ac3c
Show file tree
Hide file tree
Showing 35 changed files with 333 additions and 161 deletions.
24 changes: 22 additions & 2 deletions .idea/runConfigurations/Test_Postgres.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 26 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,38 @@
language: php

addons:
hosts:
- mysql-container
- postgres-container
- dblib-container

php:
- "7.2"
- "7.1"
- "7.0"
- "5.6"

services:
- docker

before_install:
- sudo service mysql stop || echo "mysql not stopped"
- sudo service postgresql stop || echo "postgresql not stopped"
- npm i @usdocker/usdocker @usdocker/postgres @usdocker/mysql @usdocker/mssql
- node_modules/.bin/usdocker --refresh
# - node_modules/.bin/usdocker mssql up
- node_modules/.bin/usdocker postgres up
- node_modules/.bin/usdocker mysql up

install:
- composer install
- node_modules/.bin/usdocker mssql status
- node_modules/.bin/usdocker postgres status
- node_modules/.bin/usdocker mysql status

script:
- phpunit
- vendor/bin/phpunit
# - vendor/bin/phpunit tests/SqlServerDatabaseTest.php
- vendor/bin/phpunit tests/PostgresDatabaseTest.php
- vendor/bin/phpunit tests/MysqlDatabaseTest.php

105 changes: 58 additions & 47 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,26 @@ Simple library in PHP for database version control. Supports Sqlite, MySql, Sql
Database Migration is a set of commands for upgrade or downgrade a database.
This library uses only SQL commands.

## Introduction

The basic usage is
## Installing

- Create a connection a ConnectionManagement object. For more information see the "byjg/anydataset" component
- Create a Migration object with this connection and the folder where the scripts sql are located.
- Use the proper command for "reset", "up" or "down" the migrations scripts.
```
composer require 'byjg/migration=2.0.*'
```

See an example:
## Supported databases:

```php
$connectionUri = new \ByJG\Util\Uri('mysql://migrateuser:migratepwd@localhost/migratedatabase');
$migration = new Migration($connectionUri, '.');
* Sqlite
* Mysql / MariaDB
* Postgres
* SqlServer

// Restore the database using the "base.sql" script and run ALL existing scripts for up the database version
// and run the up() method to maintain the database updated;
$migration->reset();
## How It Works?

// Run ALL existing scripts for up the database version from the current version to the last version;
$migration->up();
```
The Database Migration uses PURE SQL to manage the database versioning.
In order to get working you need to:

The Migration object controls the database version.
- Create the SQL Scripts
- Manage using Command Line or the API.

### The SQL Scripts

Expand Down Expand Up @@ -59,23 +56,23 @@ The directory scripts is :
+-- 00001.sql
```

- "base.sql" is the base script
- "up" folder contains the scripts for migrate up the version.
For example: 00002.sql is the script for move the database from version '1' to '2'.
- "down" folder contains the scripts for migrate down the version.
For example: 00001.sql is the script for move the database from version '2' to '1'.
The "down" folder is optional.
- "base.sql" is the base script
- "up" folder contains the scripts for migrate up the version.
For example: 00002.sql is the script for move the database from version '1' to '2'.
- "down" folder contains the scripts for migrate down the version.
For example: 00001.sql is the script for move the database from version '2' to '1'.
The "down" folder is optional.

### Multi Development environment
**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.
- 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.
Expand All @@ -86,7 +83,7 @@ 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
### Running in the command line

Migration library creates the 'migrate' script. It has the follow syntax:

Expand Down Expand Up @@ -115,9 +112,9 @@ Available commands:
version Get the current database version
```

## Commands
#### Commands

### Basic Usage
##### Basic Usage

The basic usage is:

Expand All @@ -140,7 +137,7 @@ You can omit the uri parameter if you define it in the
export MIGRATE_CONNECTION=sqlite:///path/to/my.db
```

### Command: create
##### Command: create

Create a empty directory structure with base.sql and migrations/up and migrations/down for migrations. This is
useful for create from scratch a migration scheme.
Expand All @@ -151,7 +148,7 @@ Ex.
migrate create /path/to/sql
```

### Command: install
##### Command: install

If you already have a database but it is not controlled by the migration system you can use this method for
install the required tables for migration.
Expand All @@ -160,7 +157,7 @@ install the required tables for migration.
migrate install mysql://server/database
```

### Command: update
##### Command: update

Will apply all necessary migrations to keep your database updated.

Expand All @@ -175,7 +172,7 @@ You can also specify a version:
migrate update --up-to=34
```

### Command: reset
##### Command: reset

Creates/replace a database with the "base.sql" and apply ALL migrations

Expand All @@ -193,28 +190,42 @@ migrate reset --yes # reset the database without ask anything. Be careful!!
export MIGRATE_DISABLE_RESET=true
```

## Supported databases:
### Using the API

The basic usage is

* Sqlite
* Mysql / MariaDB
* Postgres
* SqlServer
- Create a connection a ConnectionManagement object. For more information see the "byjg/anydataset" component
- Create a Migration object with this connection and the folder where the scripts sql are located.
- Use the proper command for "reset", "up" or "down" the migrations scripts.

## Installing
See an example:

### At your project level
```php
<?php
// Create the Connection URI
// See more: https://github.com/byjg/anydataset#connection-based-on-uri
$connectionUri = new \ByJG\Util\Uri('mysql://migrateuser:migratepwd@localhost/migratedatabase');

```
composer require 'byjg/migration=2.0.*'
```
// Create the Migration instance
$migration = new \ByJG\DbMigration\Migration($connectionUri, '.');

### Globally
// Register the Database or Databases can handle that URI:
$migration->registerDatabase('mysql', \ByJG\DbMigration\Database\MySqlDatabase::class);
$migration->registerDatabase('maria', \ByJG\DbMigration\Database\MySqlDatabase::class);

```bash
composer global require 'byjg/migration=2.0.*'
sudo ln -s $HOME/.composer/vendor/bin/migrate /usr/local/bin
// Restore the database using the "base.sql" script
// and run ALL existing scripts for up the database version to the latest version
$migration->reset();

// Run ALL existing scripts for up the database version
// from the current version to the last version;
$migration->up();
```

The Migration object controls the database version.



## Unit Tests

This library has integrated tests and need to be setup for each database you want to test.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"symfony/console": "^3.1"
},
"require-dev": {
"phpunit/phpunit": "^5.7|^6.5"
"phpunit/phpunit": ">=5.7"
},
"autoload": {
"psr-4": {
Expand Down
1 change: 1 addition & 0 deletions example/mysql/test_mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
$uri = new \ByJG\Util\Uri('mysql://root:password@mysql-container/migratedatabase');

$migration = new \ByJG\DbMigration\Migration($uri, __DIR__);
$migration->registerDatabase('mysql', \ByJG\DbMigration\Database\MySqlDatabase::class);

$migration->prepareEnvironment();

Expand Down
1 change: 1 addition & 0 deletions example/postgres/test_postgres.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
$uri = new \ByJG\Util\Uri('pgsql://postgres:password@postgres-container/migratedatabase');

$migration = new \ByJG\DbMigration\Migration($uri, __DIR__);
$migration->registerDatabase('pgsql', \ByJG\DbMigration\Database\PgsqlDatabase::class);

$migration->prepareEnvironment();

Expand Down
1 change: 1 addition & 0 deletions example/sql_server/test_sqlserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
$uri = new \ByJG\Util\Uri('dblib://sa:Pa$$word!@mssql-container/migratedatabase');

$migration = new \ByJG\DbMigration\Migration($uri, __DIR__);
$migration->registerDatabase('dblib', \ByJG\DbMigration\Database\DblibDatabase::class);

$migration->prepareEnvironment();

Expand Down
1 change: 1 addition & 0 deletions example/sqlite/test_sqlite.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
$uri = new \ByJG\Util\Uri('sqlite:///tmp/teste.sqlite');

$migration = new \ByJG\DbMigration\Migration($uri, __DIR__);
$migration->registerDatabase('sqlite', \ByJG\DbMigration\Database\SqliteDatabase::class);

$migration->reset();

2 changes: 1 addition & 1 deletion scripts/migrate
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require_once($autoload);

use Symfony\Component\Console\Application;

$application = new Application('Migrate Script by JG', '2.0.1');
$application = new Application('Migrate Script by JG', '2.0.4');
$application->add(new \ByJG\DbMigration\Console\ResetCommand());
$application->add(new \ByJG\DbMigration\Console\UpCommand());
$application->add(new \ByJG\DbMigration\Console\DownCommand());
Expand Down
26 changes: 23 additions & 3 deletions src/Console/ConsoleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace ByJG\DbMigration\Console;

use ByJG\DbMigration\Database\DblibDatabase;
use ByJG\DbMigration\Database\MySqlDatabase;
use ByJG\DbMigration\Database\PgsqlDatabase;
use ByJG\DbMigration\Database\SqliteDatabase;
use ByJG\DbMigration\Migration;
use ByJG\Util\Uri;
use Symfony\Component\Console\Command\Command;
Expand Down Expand Up @@ -34,6 +38,12 @@ protected function configure()
InputOption::VALUE_OPTIONAL,
'Run up to the specified version'
)
->addOption(
'no-base',
null,
InputOption::VALUE_NONE,
'Remove the check for base.sql file'
)
->addUsage('')
->addUsage('Example: ')
->addUsage(' migrate reset mysql://root:password@server/database')
Expand All @@ -55,6 +65,11 @@ protected function configure()

protected $path;

/**
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @throws \ByJG\DbMigration\Exception\InvalidMigrationFile
*/
protected function initialize(InputInterface $input, OutputInterface $output)
{
$this->connection = $input->getArgument('connection');
Expand All @@ -72,8 +87,15 @@ protected function initialize(InputInterface $input, OutputInterface $output)

$this->upTo = $input->getOption('up-to');

$requiredBase = !$input->getOption('no-base');

$uri = new Uri($this->connection);
$this->migration = new Migration($uri, $this->path);
$this->migration = new Migration($uri, $this->path, $requiredBase);
$this->migration
->registerDatabase('sqlite', SqliteDatabase::class)
->registerDatabase('mysql', MySqlDatabase::class)
->registerDatabase('pgsql', PgsqlDatabase::class)
->registerDatabase('dblib', DblibDatabase::class);
}

protected function execute(InputInterface $input, OutputInterface $output)
Expand Down Expand Up @@ -102,6 +124,4 @@ protected function handleError($exception, OutputInterface $output)
$output->writeln($exception->getMessage());
}
}


}
8 changes: 1 addition & 7 deletions src/Console/DatabaseVersionCommand.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
<?php
/**
* Created by PhpStorm.
* User: jg
* Date: 17/06/16
* Time: 21:52
*/

namespace ByJG\DbMigration\Console;

Expand All @@ -15,7 +9,7 @@ class DatabaseVersionCommand extends ConsoleCommand
{
protected function configure()
{
parent::configure();
parent::configure();
$this
->setName('version')
->setDescription('Get the current database version');
Expand Down
Loading

0 comments on commit 6d7ac3c

Please sign in to comment.