Skip to content

Commit

Permalink
Added new environment variables and update documentation:
Browse files Browse the repository at this point in the history
- MIGRATE_DISABLE_RESET
- MIGRATE_PATH
- MIGRATE_CONNECTION (docs only)
byjg committed Feb 5, 2018
1 parent fb3a3cd commit d941774
Showing 6 changed files with 61 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
language: php
php:
- "7.2"
- "7.1"
- "7.0"
- "5.6"
@@ -8,5 +9,5 @@ install:
- composer install

script:
- phpunit tests/SqliteDatabaseTest.php
- phpunit

46 changes: 39 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -117,7 +117,30 @@ Available commands:

## Commands

### migrate create
### Basic Usage

The basic usage is:

```text
migrate <COMMAND> --path=<scripts> uri://connection
```

The `--path` specify where the base.sql and migrate scripts are located.
If you omitted the `--path` it will assume the current directory. You can also
set the `MIGRATE_PATH` environment variable with the base path

The uri://connection is the uri that represents the connection to the database.
You can see [here](https://github.com/byjg/anydataset#connection-based-on-uri)
to know more about the connection string.

You can omit the uri parameter if you define it in the
`MIGRATE_CONNECTION` environment variable

```bash
export MIGRATE_CONNECTION=sqlite:///path/to/my.db
```

### 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.
@@ -128,7 +151,7 @@ Ex.
migrate create /path/to/sql
```

### migrate 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.
@@ -137,30 +160,39 @@ install the required tables for migration.
migrate install mysql://server/database
```

### migrate update
### Command: update

Will apply all necessary migrations to keep your database updated.

```bash
migrate update mysql://server/database
```

Update command can choose if up or down your database depending on your current database version.
Update command can choose if up or down your database depending on your current database version.
You can also specify a version:

```bash
migrate update --up-to=34
```

### migrate reset
### Command: reset

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

```bash
migrate reset # reset the database and apply all migrations scripts.
migrate reset --up-to=5 # reset the database and apply the migration version up to 5.
migrate reset --up-to=5 # reset the database and apply the migration from the
# start up to the version 5.
migrate reset --yes # reset the database without ask anything. Be careful!!
```

**Note on reset:** You can disable the reset command by setting the environment variable
`MIGRATE_DISABLE_RESET` to true:

```bash
export MIGRATE_DISABLE_RESET=true
```

## Supported databases:

* Sqlite
@@ -194,4 +226,4 @@ phpunit tests/SqliteDatabaseTest.php
phpunit tests/MysqlDatabaseTest.php
phpunit tests/PostgresDatabaseTest.php
phpunit tests/SqlServerDatabaseTest.php
```
```
9 changes: 6 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -10,9 +10,12 @@
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"byjg/anydataset": "3.0.*",
"byjg/uri": "1.0.*",
"symfony/console": "3.1.*"
"byjg/anydataset": "^3.0",
"byjg/uri": "^1.0",
"symfony/console": "^3.1"
},
"require-dev": {
"phpunit/phpunit": "^5.7|^6.5"
},
"autoload": {
"psr-4": {
2 changes: 1 addition & 1 deletion src/Console/ConsoleCommand.php
Original file line number Diff line number Diff line change
@@ -66,7 +66,7 @@ protected function initialize(InputInterface $input, OutputInterface $output)

$this->path = $input->getOption('path');
if (!$this->path) {
$this->path = ".";
$this->path = (!empty(getenv('MIGRATE_PATH')) ? getenv('MIGRATE_PATH') : ".");
}
$this->path = realpath($this->path);

5 changes: 5 additions & 0 deletions src/Console/ResetCommand.php
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@

namespace ByJG\DbMigration\Console;

use ByJG\DbMigration\Exception\ResetDisabledException;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
@@ -25,6 +26,10 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
if (getenv('MIGRATE_DISABLE_RESET') === "true") {
throw new ResetDisabledException('Reset was disabled by MIGRATE_DISABLE_RESET environment variable. Cannot continue.');
}

try {
$helper = $this->getHelper('question');
if (!$input->getOption('yes')) {
8 changes: 8 additions & 0 deletions src/Exception/ResetDisabledException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace ByJG\DbMigration\Exception;

class ResetDisabledException extends \Exception
{

}

0 comments on commit d941774

Please sign in to comment.