Skip to content

Commit 8e3086d

Browse files
authored
Merge pull request #17 from byjg/4.0.1
4.0.1
2 parents bfd6b19 + 66f82bf commit 8e3086d

File tree

12 files changed

+131
-642
lines changed

12 files changed

+131
-642
lines changed

README.md

Lines changed: 77 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Database Migrations
1+
# Database Migrations PHP
22

33
[![Opensource ByJG](https://img.shields.io/badge/opensource-byjg.com-brightgreen.svg)](http://opensource.byjg.com)
44
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/byjg/migration/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/byjg/migration/?branch=master)
@@ -27,28 +27,43 @@ But at the end despite these good features the reality in big projects someone w
2727

2828
Because of that this is an agnostic project (independent of framework and Programming Language) and use pure and native SQL commands for migrate your database.
2929

30-
## Installing
30+
# Installing
31+
32+
## PHP Library
33+
34+
If you want to use only the PHP Library in your project:
3135

3236
```
3337
composer require 'byjg/migration=4.0.*'
3438
```
39+
## Command Line Interface
40+
41+
The command line interface is standalone and does not require you install with your project.
42+
43+
You can install global and create a symbolic lynk
44+
45+
```
46+
composer require 'byjg/migration-cli=4.0.*'
47+
```
48+
49+
Please visit https://github.com/byjg/migration-cli to get more informations about Migration CLI.
3550

36-
## Supported databases:
51+
# Supported databases:
3752

3853
* Sqlite
3954
* Mysql / MariaDB
4055
* Postgres
4156
* SqlServer
4257

43-
## How It Works?
58+
# How It Works?
4459

4560
The Database Migration uses PURE SQL to manage the database versioning.
4661
In order to get working you need to:
4762

4863
- Create the SQL Scripts
4964
- Manage using Command Line or the API.
5065

51-
### The SQL Scripts
66+
## The SQL Scripts
5267

5368
The scripts are divided in three set of scripts:
5469

@@ -102,114 +117,8 @@ If he is try to migrate UP or DOWN
102117
the migration script will down and alert him there a TWO versions 43. In that case, developer 2 will have to update your
103118
file do 44-dev.sql and continue to work until merge your changes and generate a final version.
104119

105-
### Running in the command line
106-
107-
Migration library creates the 'migrate' script. It has the follow syntax:
108-
109-
```
110-
Usage:
111-
command [options] [arguments]
112-
113-
Options:
114-
-h, --help Display this help message
115-
-q, --quiet Do not output any message
116-
-V, --version Display this application version
117-
--ansi Force ANSI output
118-
--no-ansi Disable ANSI output
119-
-n, --no-interaction Do not ask any interactive question
120-
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
121-
122-
Available commands:
123-
create Create the directory structure FROM a pre-existing database
124-
down Migrate down the database version.
125-
help Displays help for a command
126-
install Install or upgrade the migrate version in a existing database
127-
list Lists commands
128-
reset Create a fresh new database
129-
up Migrate Up the database version
130-
update Migrate Up or Down the database version based on the current database version and the migration scripts available
131-
version Get the current database version
132-
```
133-
134-
#### Commands
135-
136-
##### Basic Usage
137-
138-
The basic usage is:
139-
140-
```text
141-
migrate <COMMAND> --path=<scripts> uri://connection
142-
```
143-
144-
The `--path` specify where the base.sql and migrate scripts are located.
145-
If you omitted the `--path` it will assume the current directory. You can also
146-
set the `MIGRATE_PATH` environment variable with the base path
147-
148-
The uri://connection is the uri that represents the connection to the database.
149-
You can see [here](https://github.com/byjg/anydataset#connection-based-on-uri)
150-
to know more about the connection string.
151-
152-
You can omit the uri parameter if you define it in the
153-
`MIGRATE_CONNECTION` environment variable
154-
155-
```bash
156-
export MIGRATE_CONNECTION=sqlite:///path/to/my.db
157-
```
158-
159-
##### Command: create
160-
161-
Create a empty directory structure with base.sql and migrations/up and migrations/down for migrations. This is
162-
useful for create from scratch a migration scheme.
163-
164-
Ex.
165-
166-
```bash
167-
migrate create /path/to/sql
168-
```
169-
170-
##### Command: install
171-
172-
If you already have a database but it is not controlled by the migration system you can use this method for
173-
install the required tables for migration.
174-
175-
```bash
176-
migrate install mysql://server/database
177-
```
178-
179-
##### Command: update
180-
181-
Will apply all necessary migrations to keep your database updated.
182-
183-
```bash
184-
migrate update mysql://server/database
185-
```
186-
187-
Update command can choose if up or down your database depending on your current database version.
188-
You can also specify a version:
189120

190-
```bash
191-
migrate update --up-to=34
192-
```
193-
194-
##### Command: reset
195-
196-
Creates/replace a database with the "base.sql" and apply ALL migrations
197-
198-
```bash
199-
migrate reset # reset the database and apply all migrations scripts.
200-
migrate reset --up-to=5 # reset the database and apply the migration from the
201-
# start up to the version 5.
202-
migrate reset --yes # reset the database without ask anything. Be careful!!
203-
```
204-
205-
**Note on reset:** You can disable the reset command by setting the environment variable
206-
`MIGRATE_DISABLE_RESET` to true:
207-
208-
```bash
209-
export MIGRATE_DISABLE_RESET=true
210-
```
211-
212-
### Using the PHP API and Integrate it into your projects.
121+
# Using the PHP API and Integrate it into your projects.
213122

214123
The basic usage is
215124

@@ -236,17 +145,58 @@ $migration->registerDatabase('maria', \ByJG\DbMigration\Database\MySqlDatabase::
236145
// and run ALL existing scripts for up the database version to the latest version
237146
$migration->reset();
238147

239-
// Run ALL existing scripts for up the database version
240-
// from the current version to the last version;
241-
$migration->up();
148+
// Run ALL existing scripts for up or down the database version
149+
// from the current version until the $version number;
150+
// If the version number is not specified migrate until the last database version
151+
$migration->update($version = null);
242152
```
243153

244154
The Migration object controls the database version.
245155

246156

247-
### Tips on writing SQL migrations
157+
## Creating a version control in your project:
158+
159+
```php
160+
<?php
161+
// Create the Migration instance
162+
$migration = new \ByJG\DbMigration\Migration($connectionUri, '.');
163+
164+
// Register the Database or Databases can handle that URI:
165+
$migration->registerDatabase('mysql', \ByJG\DbMigration\Database\MySqlDatabase::class);
166+
167+
// This command will create the version table in your database
168+
$migration->createVersion();
169+
```
170+
171+
## Getting the current version
172+
173+
```php
174+
<?php
175+
$migration->getCurrentVersion();
176+
```
177+
178+
## Add Callback to control the progress
179+
180+
```php
181+
<?php
182+
$migration->addCallbackProgress(function ($command, $version) {
183+
echo "Doing Command: $command at version $version";
184+
});
185+
```
186+
187+
## Getting the Db Driver instance
188+
189+
```php
190+
<?php
191+
$migration->getDbDriver();
192+
```
193+
194+
To use it, please visit: https://github.com/byjg/anydataset-db
195+
196+
197+
# Tips on writing SQL migrations
248198

249-
#### Rely on explicit transactions
199+
## Rely on explicit transactions
250200

251201
```sql
252202
-- DO
@@ -278,7 +228,7 @@ and warn you when you attempt to run it again. The difference is that with expli
278228
transactions you know that the database cannot be in an inconsistent state after an
279229
unexpected failure.
280230

281-
#### On creating triggers and SQL functions
231+
## On creating triggers and SQL functions
282232

283233
```sql
284234
-- DO
@@ -341,7 +291,7 @@ comment after every inner semicolon of a function definition `byjg/migration` wi
341291
Unfortunately, if you forget to add any of these comments the library will split the `CREATE FUNCTION` statement in
342292
multiple parts and the migration will fail.
343293

344-
#### Avoid the colon character (`:`)
294+
## Avoid the colon character (`:`)
345295

346296
```sql
347297
-- DO
@@ -369,14 +319,14 @@ read this as an invalid named parameter in an invalid context and fail when it t
369319
The only way to fix this inconsistency is avoiding colons altogether (in this case, PostgreSQL also has an alternative
370320
syntax: `CAST(value AS type)`).
371321

372-
#### Use an SQL editor
322+
## Use an SQL editor
373323

374324
Finally, writing manual SQL migrations can be tiresome, but it is significantly easier if
375325
you use an editor capable of understanding the SQL syntax, providing autocomplete,
376326
introspecting your current database schema and/or autoformatting your code.
377327

378328

379-
### Handle different migration inside one schema
329+
# Handle different migration inside one schema
380330

381331
If you need to create different migration scripts and version inside the same schema it is possible
382332
but is too risky and I do not recommend at all.
@@ -394,7 +344,7 @@ For security reasons, this feature is not available at command line, but you can
394344
We really recommend do not use this feature. The recommendation is one migration for one schema.
395345

396346

397-
## Unit Tests
347+
# Unit Tests
398348

399349
This library has integrated tests and need to be setup for each database you want to test.
400350

@@ -407,9 +357,9 @@ vendor/bin/phpunit tests/PostgresDatabaseTest.php
407357
vendor/bin/phpunit tests/SqlServerDatabaseTest.php
408358
```
409359

410-
### Using Docker for testing
360+
## Using Docker for testing
411361

412-
#### MySql
362+
### MySql
413363

414364
```bash
415365
npm i @usdocker/usdocker @usdocker/mysql
@@ -424,7 +374,7 @@ docker run -it --rm \
424374
phpunit tests/MysqlDatabaseTest
425375
```
426376

427-
#### Postgresql
377+
### Postgresql
428378

429379
```bash
430380
npm i @usdocker/usdocker @usdocker/postgres
@@ -439,7 +389,7 @@ docker run -it --rm \
439389
phpunit tests/PostgresDatabaseTest
440390
```
441391

442-
#### Microsoft SqlServer
392+
### Microsoft SqlServer
443393

444394
```bash
445395
npm i @usdocker/usdocker @usdocker/mssql
@@ -454,7 +404,7 @@ docker run -it --rm \
454404
phpunit tests/SqlServerDatabaseTest
455405
```
456406

457-
## Related Projects
407+
# Related Projects
458408

459409
- [Micro ORM](https://github.com/byjg/micro-orm)
460410
- [Anydataset](https://github.com/byjg/anydataset)

_config.yml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
name: migration
2+
3+
project:
4+
version: 4.0.0
5+
download_url: https://github.com/byjg/migration/releases
6+
7+
license:
8+
software: MIT
9+
software_url: https://opensource.org/licenses/MIT
10+
11+
docs: MIT
12+
docs_url: https://opensource.org/licenses/MIT
13+
14+
git_edit_address: https://github.com/byjg/migration/blob/master/
15+
16+
links:
17+
header:
18+
- title: GitHub
19+
url: https://github.com/byjg/migration
20+
- title: ByJG
21+
url: https://opensource.byjg.com/
22+
footer:
23+
- title: GitHub
24+
url: https://github.com/byjg/migration
25+
- title: Issues
26+
url: https://github.com/byjg/migration/issues
27+
28+
ui:
29+
header:
30+
color1: "#080331"
31+
color2: "#0033cc"
32+
trianglify: true
33+
34+
social:
35+
github:
36+
user: byjg
37+
repo: migration
38+
twitter:
39+
enabled: false
40+
via:
41+
hash: opensourcebyjg
42+
account:
43+
facebook:
44+
enabled: false
45+
profileUrl:
46+
47+
analytics:
48+
google: UA-130014324-1
49+
50+
# Build settings
51+
markdown: kramdown
52+
remote_theme: allejo/jekyll-docs-theme
53+

composer.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
"prefer-stable": true,
1212
"require": {
1313
"byjg/anydataset-db": "4.0.*",
14-
"byjg/uri": "^1.0",
15-
"symfony/console": "^3.1"
14+
"byjg/uri": "^1.0"
1615
},
1716
"require-dev": {
1817
"phpunit/phpunit": ">=5.7"
@@ -22,8 +21,5 @@
2221
"ByJG\\DbMigration\\": "src/"
2322
}
2423
},
25-
"bin": [
26-
"scripts/migrate"
27-
],
2824
"license": "MIT"
2925
}

0 commit comments

Comments
 (0)