Skip to content

Commit 37689c4

Browse files
committed
Added the command line "migrate" script
1 parent 1122642 commit 37689c4

File tree

6 files changed

+226
-17
lines changed

6 files changed

+226
-17
lines changed

composer.json

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
{
2-
"name": "byjg/migration",
3-
"description": "A micro framework in PHP for managing a set of database migrations using pure Sql.",
4-
"authors": [
5-
{
6-
"name": "jg",
7-
"email": "joao@byjg.com.br"
8-
}
9-
],
10-
"require": {
11-
"byjg/anydataset": "2.1.*"
12-
},
13-
"autoload": {
14-
"psr-4": {
15-
"ByJG\\DbMigration\\": "src/"
16-
}
17-
},
18-
"license": "MIT"
2+
"name": "byjg/migration",
3+
"description": "A micro framework in PHP for managing a set of database migrations using pure Sql.",
4+
"authors": [
5+
{
6+
"name": "jg",
7+
"email": "joao@byjg.com.br"
8+
}
9+
],
10+
"require": {
11+
"byjg/anydataset": "2.1.*",
12+
"symfony/console": "3.1.*"
13+
},
14+
"autoload": {
15+
"psr-4": {
16+
"ByJG\\DbMigration\\": "src/"
17+
}
18+
},
19+
"bin": [
20+
"scripts/migrate"
21+
],
22+
"license": "MIT"
1923
}

scripts/migrate

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
$autoload = realpath(__DIR__."/../vendor/autoload.php");
5+
if (!file_exists($autoload)) {
6+
$autoload = realpath(__DIR__."/../../../autoload.php");
7+
if (!file_exists($autoload)) {
8+
throw new \Exception('Autoload not found. Did you run `composer dump-autload`?');
9+
}
10+
}
11+
require_once($autoload);
12+
13+
14+
use Symfony\Component\Console\Application;
15+
16+
$application = new Application('Migrate Script by JG', '1.0.2');
17+
$application->add(new \ByJG\DbMigration\Console\ResetCommand());
18+
$application->add(new \ByJG\DbMigration\Console\UpCommand());
19+
$application->add(new \ByJG\DbMigration\Console\DownCommand());
20+
$application->run();

src/Console/ConsoleCommand.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
namespace ByJG\DbMigration\Console;
4+
5+
use ByJG\AnyDataset\ConnectionManagement;
6+
use ByJG\DbMigration\Migration;
7+
use Symfony\Component\Console\Command\Command;
8+
use Symfony\Component\Console\Exception\InvalidArgumentException;
9+
use Symfony\Component\Console\Input\InputArgument;
10+
use Symfony\Component\Console\Input\InputInterface;
11+
use Symfony\Component\Console\Input\InputOption;
12+
use Symfony\Component\Console\Output\OutputInterface;
13+
14+
abstract class ConsoleCommand extends Command
15+
{
16+
protected function configure()
17+
{
18+
$this
19+
->addArgument(
20+
'connection',
21+
InputArgument::OPTIONAL,
22+
'The connection string. Ex. mysql://root:password@server/database',
23+
getenv('MIGRATE_CONNECTION')
24+
)
25+
->addOption(
26+
'path',
27+
'p',
28+
InputOption::VALUE_OPTIONAL,
29+
'Define the path where the base.sql resides. If not set assumes the current folder'
30+
)
31+
->addOption(
32+
'up-to',
33+
'u',
34+
InputOption::VALUE_OPTIONAL,
35+
'Run up to the specified version'
36+
)
37+
->addUsage('')
38+
->addUsage('Example: ')
39+
->addUsage(' migrate reset mysql://root:password@server/database')
40+
->addUsage(' migrate up mysql://root:password@server/database')
41+
->addUsage(' migrate down mysql://root:password@server/database')
42+
->addUsage(' migrate up --up-to=10 --path=/somepath mysql://root:password@server/database')
43+
->addUsage(' migrate down --up-to=3 --path=/somepath mysql://root:password@server/database')
44+
;
45+
}
46+
47+
/**
48+
* @var Migration
49+
*/
50+
protected $migration;
51+
52+
protected $upTo;
53+
54+
protected $connection;
55+
56+
protected $path;
57+
58+
protected function initialize(InputInterface $input, OutputInterface $output)
59+
{
60+
$this->connection = $input->getArgument('connection');
61+
if (!$this->connection) {
62+
throw new InvalidArgumentException('You need to setup the connection in the argument or setting the environment MIGRATE_CONNECTION');
63+
}
64+
65+
$this->path = $input->getOption('path');
66+
if (!$this->path) {
67+
$this->path = ".";
68+
}
69+
$this->path = realpath($this->path);
70+
71+
$this->upTo = $input->getOption('up-to');
72+
73+
$connectionObject = new ConnectionManagement($this->connection);
74+
$this->migration = new Migration($connectionObject, $this->path);
75+
}
76+
77+
protected function execute(InputInterface $input, OutputInterface $output)
78+
{
79+
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
80+
$output->writeln('Connection String: ' . $this->connection);
81+
$output->writeln('Path: ' . $this->path);
82+
}
83+
84+
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERY_VERBOSE) {
85+
$this->migration->addCallbackProgress(function($command, $version) use ($output) {
86+
$output->writeln('Doing: ' . $command . " to " . $version);
87+
});
88+
}
89+
}
90+
91+
92+
}

src/Console/DownCommand.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: jg
5+
* Date: 17/06/16
6+
* Time: 21:52
7+
*/
8+
9+
namespace ByJG\DbMigration\Console;
10+
11+
use Symfony\Component\Console\Input\InputInterface;
12+
use Symfony\Component\Console\Output\OutputInterface;
13+
14+
class DownCommand extends ConsoleCommand
15+
{
16+
protected function configure()
17+
{
18+
parent::configure();
19+
$this
20+
->setName('down')
21+
->setDescription('Migrate down the database version.');
22+
23+
}
24+
25+
protected function execute(InputInterface $input, OutputInterface $output)
26+
{
27+
parent::execute($input, $output);
28+
$this->migration->down($this->upTo);
29+
}
30+
31+
}

src/Console/ResetCommand.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: jg
5+
* Date: 17/06/16
6+
* Time: 21:52
7+
*/
8+
9+
namespace ByJG\DbMigration\Console;
10+
11+
use Symfony\Component\Console\Input\InputInterface;
12+
use Symfony\Component\Console\Output\OutputInterface;
13+
14+
class ResetCommand extends ConsoleCommand
15+
{
16+
protected function configure()
17+
{
18+
parent::configure();
19+
$this
20+
->setName('reset')
21+
->setDescription('Create a fresh new database');
22+
23+
}
24+
25+
protected function execute(InputInterface $input, OutputInterface $output)
26+
{
27+
parent::execute($input, $output);
28+
$this->migration->prepareEnvironment();
29+
$this->migration->reset($this->upTo);
30+
}
31+
32+
}

src/Console/UpCommand.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: jg
5+
* Date: 17/06/16
6+
* Time: 21:52
7+
*/
8+
9+
namespace ByJG\DbMigration\Console;
10+
11+
use Symfony\Component\Console\Input\InputInterface;
12+
use Symfony\Component\Console\Output\OutputInterface;
13+
14+
class UpCommand extends ConsoleCommand
15+
{
16+
protected function configure()
17+
{
18+
parent::configure();
19+
$this
20+
->setName('up')
21+
->setDescription('Migrate Up the database version');
22+
23+
}
24+
25+
protected function execute(InputInterface $input, OutputInterface $output)
26+
{
27+
parent::execute($input, $output);
28+
$this->migration->up($this->upTo);
29+
}
30+
}

0 commit comments

Comments
 (0)