Skip to content

Commit

Permalink
Splitting Database Migration and Database Migration Cli into differen…
Browse files Browse the repository at this point in the history
…t projects.
  • Loading branch information
byjg committed Nov 26, 2018
1 parent bf31594 commit a2fc736
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 622 deletions.
170 changes: 60 additions & 110 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,24 @@ Because of that this is an agnostic project (independent of framework and Progra

## Installing

### PHP Library

If you want to use only the PHP Library in your project:

```
composer require 'byjg/migration=4.0.*'
```
### Command Line Interface

The command line interface is standalone and does not require you install with your project.

You can install global and create a symbolic lynk

```
composer require 'byjg/migration-cli=4.0.*'
```

Please visit https://github.com/byjg/migration-cli to get more informations about Migration CLI.

## Supported databases:

Expand Down Expand Up @@ -102,114 +117,8 @@ 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

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

```
Usage:
command [options] [arguments]
Options:
-h, --help Display this help message
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi Force ANSI output
--no-ansi Disable ANSI output
-n, --no-interaction Do not ask any interactive question
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
Available commands:
create Create the directory structure FROM a pre-existing database
down Migrate down the database version.
help Displays help for a command
install Install or upgrade the migrate version in a existing database
list Lists commands
reset Create a fresh new database
up Migrate Up the database version
update Migrate Up or Down the database version based on the current database version and the migration scripts available
version Get the current database version
```

#### Commands

##### 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.

Ex.

```bash
migrate create /path/to/sql
```

##### 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.

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

##### 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.
You can also specify a version:

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

##### 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 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
```

### Using the PHP API and Integrate it into your projects.
## Using the PHP API and Integrate it into your projects.

The basic usage is

Expand All @@ -236,14 +145,55 @@ $migration->registerDatabase('maria', \ByJG\DbMigration\Database\MySqlDatabase::
// 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();
// Run ALL existing scripts for up or down the database version
// from the current version until the $version number;
// If the version number is not specified migrate until the last database version
$migration->update($version = null);
```

The Migration object controls the database version.


### Creating a version control in your project:

```php
<?php
// Create the Migration instance
$migration = new \ByJG\DbMigration\Migration($connectionUri, '.');

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

// This command will create the version table in your database
$migration->createVersion();
```

### Getting the current version

```php
<?php
$migration->getCurrentVersion();
```

### Add Callback to control the progress

```php
<?php
$migration->addCallbackProgress(function ($command, $version) {
echo "Doing Command: $command at version $version";
});
```

### Getting the Db Driver instance

```php
<?php
$migration->getDbDriver();
```

To use it, please visit: https://github.com/byjg/anydataset-db


### Tips on writing SQL migrations

#### Rely on explicit transactions
Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
"prefer-stable": true,
"require": {
"byjg/anydataset-db": "4.0.*",
"byjg/uri": "^1.0",
"symfony/console": "^4.0"
"byjg/uri": "^1.0"
},
"require-dev": {
"phpunit/phpunit": ">=5.7"
Expand Down
23 changes: 0 additions & 23 deletions scripts/migrate

This file was deleted.

129 changes: 0 additions & 129 deletions src/Console/ConsoleCommand.php

This file was deleted.

Loading

0 comments on commit a2fc736

Please sign in to comment.