Skip to content

Commit bd40b37

Browse files
author
Filipe Gonçalves
committed
First commit
0 parents  commit bd40b37

33 files changed

+4126
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.vscode
2+
bin/phpunit
3+
vendor

README.md

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
PhpMySQLMigration - PHP-MySQL database migration tool
2+
=================================================
3+
Project created to run migrations on multiple environments
4+
5+
This is a full standalone PHP tool based on [Symfony Console](http://symfony.com/doc/current/components/console).
6+
It's a fork from https://github.com/alwex/php-database-migration
7+
8+
Usage
9+
-----
10+
```
11+
$ ./bin/migrate
12+
Console Tool
13+
14+
Usage:
15+
command [options] [arguments]
16+
17+
Options:
18+
-h, --help Display this help message
19+
-q, --quiet Do not output any message
20+
-V, --version Display this application version
21+
--ansi Force ANSI output
22+
--no-ansi Disable ANSI output
23+
-n, --no-interaction Do not ask any interactive question
24+
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
25+
26+
Available commands:
27+
help Displays help for a command
28+
list Lists commands
29+
migrate
30+
migrate:init Create the changelog file and file directories
31+
migrate:addenv Add an environment to work with php db migrate
32+
migrate:reset Reset database given a clean file
33+
migrate:create Create a SQL migration
34+
migrate:up Execute all waiting migration up to [to] option if precised
35+
migrate:down Rollback all waiting migration down to [to] option if precised
36+
migrate:status Display the current status of the specified environment
37+
migrate:seed Seed database with given file.
38+
```
39+
40+
Installing it in your project
41+
-----------------------------
42+
Just run composer command (don't forget to specify your bin directory)
43+
44+
```
45+
composer require filipe07/php-database-migration
46+
```
47+
48+
49+
Initialization
50+
--------------
51+
Choose folder for migrations and configurations and creates a new database table for tracking the current database changes.
52+
Warning, all migrate commands must be executed on your root folder like `bin/migrate migrate:command...`
53+
54+
```
55+
$ ./bin/migrate migrate:init
56+
```
57+
58+
59+
Adding an environment
60+
---------------------
61+
The first thing to do before playing with MySQL migrations is to add an environment, let's add the dev one.
62+
63+
```
64+
$ ./bin/migrate migrate:addenv
65+
```
66+
67+
You will be prompted to answer a series of questions about your environment, and then a config file will be saved
68+
in `.[environments]/[env].yml`.
69+
70+
71+
Create a migration
72+
------------------
73+
It is time to create our first migration file.
74+
75+
```
76+
$ ./bin/migrate migrate:create
77+
```
78+
79+
Migrations file are like this:
80+
-- // add table users
81+
-- Migration SQL that makes the change goes here.
82+
create table users (id integer, name text);
83+
-- @UNDO
84+
-- SQL to undo the change goes here.
85+
drop table users;
86+
87+
88+
List status of migrations
89+
------------------
90+
View all available migrations and their status.
91+
92+
```
93+
$ ./bin/migrate migrate:status [env]
94+
+----------------+---------+------------------+--------------------+
95+
| id | version | applied at | description |
96+
+----------------+---------+------------------+--------------------+
97+
| 14679010838251 | | | create table users |
98+
+----------------+---------+------------------+--------------------+
99+
```
100+
101+
102+
Up and down
103+
-----------
104+
You can now up all the pending migrations. If you decide to down a migration, the last one will be downed alone to
105+
prevent mistakes. You will be asked to confirm the downgrade of your database before running the real SQL script.
106+
107+
```
108+
$ ./bin/migrate migrate:up [env]
109+
```
110+
111+
For development purposes, it is also possible to up a single migration without taking care of the other ones:
112+
113+
```
114+
$ ./bin/migrate migrate:up [env] --only=[migrationid]
115+
```
116+
117+
or migrate to specific migration (it will run all migrations, including the specified migration)
118+
119+
```
120+
$ ./bin/migrate migrate:up [env] --to=[migrationid]
121+
```
122+
123+
Same thing for down:
124+
125+
```
126+
$ ./bin/migrate migrate:down [env] --only=[migrationid]
127+
```
128+
or
129+
130+
```
131+
$ ./bin/migrate migrate:down [env] --to=[migrationid]
132+
```
133+
134+
135+
Seed file to database
136+
------------------
137+
If you need to seed database with given file
138+
139+
```
140+
$ ./bin/migrate migrate:seed [env] {file_location}
141+
```

bin/migrate

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
if (file_exists(__DIR__ . '/../vendor/autoload.php')) {
5+
require_once __DIR__ . '/../vendor/autoload.php';
6+
} elseif (file_exists(__DIR__ . '/../../../autoload.php')) {
7+
require_once __DIR__ . '/../../../autoload.php';
8+
}
9+
10+
use Symfony\Component\Console\Application;
11+
use Migrate\Command\AddEnvCommand;
12+
use Migrate\Command\CreateCommand;
13+
use Migrate\Command\DownCommand;
14+
use Migrate\Command\InitCommand;
15+
use Migrate\Command\SeedCommand;
16+
use Migrate\Command\StatusCommand;
17+
use Migrate\Command\UpCommand;
18+
19+
$application = new Application();
20+
$application->add(new AddEnvCommand());
21+
$application->add(new CreateCommand());
22+
$application->add(new DownCommand());
23+
$application->add(new InitCommand());
24+
$application->add(new SeedCommand());
25+
$application->add(new StatusCommand());
26+
$application->add(new UpCommand());
27+
$application->run();

composer.json

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "filipe07/php-database-migration",
3+
"description": "SQL migration tool forked from https://github.com/alwex/php-database-migration",
4+
"homepage": "https://github.com/filipe07/php-database-migration",
5+
"keywords": ["migration", "sql"],
6+
"type": "library",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "Filipe Gçalves",
11+
"email": "[email protected]"
12+
}
13+
],
14+
"require": {
15+
"php": ">=5.3.0",
16+
"symfony/console": "~2.6|~3.0",
17+
"symfony/process": "~2.6|~3.0",
18+
"symfony/config": "~2.6|~3.0",
19+
"cocur/slugify": "~1.0",
20+
"symfony/yaml": "~2.6|~3.0"
21+
},
22+
"require-dev": {
23+
"phpunit/phpunit": "*"
24+
},
25+
"bin": [
26+
"bin/migrate"
27+
],
28+
"config": {
29+
"bin-dir": "bin"
30+
},
31+
"autoload": {
32+
"psr-4": {"Migrate\\": "src/Migrate/"}
33+
},
34+
"autoload-dev": {
35+
"psr-4": { "Migrate\\Test\\": "tests/" }
36+
}
37+
}

0 commit comments

Comments
 (0)