Skip to content

Commit 9e563e6

Browse files
committed
Merge pull request #6 from joegreen88/master
Teardown + readme
2 parents 42430e9 + 5126e89 commit 9e563e6

File tree

6 files changed

+178
-8
lines changed

6 files changed

+178
-8
lines changed

README.md

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,84 @@
11
mysql-version-control
22
=====================
33

4-
A crude version control system for mysql
4+
A crude version control system for a mysql database.
5+
6+
## Installation
7+
Use composer.
8+
Add `"smrtr/mysql-version-control": "~1.0"` to the `requires` section of your project's composer.json then run
9+
`composer update`.
10+
11+
## Configuration
12+
Your database configuration will be stored at `<project_root>/db/db.ini`. In this file you will define a list of
13+
environments and then define database configurations for each environment.
14+
15+
### environments
16+
Define a list of environments under the `[environments]` tag using the format `environments[] = "development"`. The
17+
CLI tool will add commands for each environment listed here.
18+
19+
You may also define a list of testing environments using the format `testing_environments[] = "testing"`. The CLI tool
20+
will only apply test data on the environments listed here.
21+
22+
### databases
23+
You must define two database configurations for each environment using the name of the environment as a tag.
24+
The two configurations are called `buildtime` and `runtime` and they are used for processing schemas and data
25+
respectively. Each configuration requires a `host`, `user`, `password` and `database` entry.
26+
27+
### Example db.ini
28+
29+
[environments]
30+
environments[] = "development"
31+
environments[] = "production"
32+
33+
testing_environments[] = "development"
34+
35+
[development]
36+
runtime.host = "localhost"
37+
runtime.user = "buzz"
38+
runtime.password = "lightyear"
39+
runtime.database = "buzz"
40+
41+
buildtime.host = "localhost"
42+
buildtime.user = "buzz"
43+
buildtime.password = "lightyear"
44+
buildtime.database = "buzz"
45+
46+
[production]
47+
runtime.host = "localhost"
48+
runtime.user = "root"
49+
runtime.password = "root"
50+
runtime.database = "buzz"
51+
52+
buildtime.host = "localhost"
53+
buildtime.user = "buzz"
54+
buildtime.password = "lightyear"
55+
buildtime.database = "buzz"
56+
57+
## Versioning
58+
Your database versions will be stored in `<project_root>/db/versions`. The sql for each version is stored in a directory
59+
directly under this directory. So the directories are named `db/versions/1`, `db/versions/2` and so on.
60+
Each version must contain at least one of the following files:
61+
62+
- `schema.sql` - always runs first, contains `CREATE TABLE IF NOT EXISTS` and `ALTER` statements.
63+
- `data.sql` - contains `REPLACE INTO`, `INSERT`, `UPDATE` and `DELETE` statements and the like.
64+
- `testing.sql` - same as `data.sql` but with test data which doesn't need to exist outside of testing environments.
65+
- `runme.php` - a custom php hook for running, for example, import tasks.
66+
67+
The files are run in the order specified above.
68+
69+
## Usage
70+
This package will put two CLI scripts into your project's `vendor/bin` directory.
71+
72+
### up
73+
Run `vendor/bin/up <environment>` to install or update the database on the given environment.
74+
This command looks at the available versions in the `db/versions` directory and applies new versions sequentially
75+
from the current version.
76+
77+
If this is the first run on the given environment, then a table called `db_config` is created and used to store the
78+
current database version.
79+
80+
### teardown
81+
Run `vendor/bin/teardown <environment>` to tear down the tables on the given environment.
82+
83+
This command is useful for development & testing developments where you may wish to, for example, tear down your
84+
database between test runs.

bin/teardown

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env php
2+
<?php
3+
include('teardown.php');

bin/teardown.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
require_once realpath(dirname(__FILE__).'/../../../autoload.php');
4+
5+
$app = new \Symfony\Component\Console\Application('teardown');
6+
7+
foreach (\Smrtr\MysqlVersionControl\DbConfig::getEnvironments() as $env) {
8+
9+
$app->add(
10+
new \Smrtr\MysqlVersionControl\TeardownCommand($env)
11+
);
12+
}
13+
14+
$app->run();

composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
namespace Smrtr\MysqlVersionControl;
4+
5+
use Symfony\Component\Console\Command\Command;
6+
use Symfony\Component\Console\Input\InputInterface;
7+
use Symfony\Component\Console\Output\OutputInterface;
8+
9+
/**
10+
* Class TeardownCommand
11+
* @package Smrtr\MysqlVersionControl
12+
* @author Joe Green
13+
*/
14+
class TeardownCommand extends Command
15+
{
16+
protected $env;
17+
18+
public function __construct($env)
19+
{
20+
$this->env = $env;
21+
parent::__construct();
22+
}
23+
24+
protected function configure()
25+
{
26+
$this
27+
->setName($this->env)
28+
->setDescription('Install the '.$this->env.' versions')
29+
->addArgument(
30+
'mysqlbin',
31+
InputArgument::OPTIONAL,
32+
'Where is the MySQL binary located?'
33+
)
34+
->addOption(
35+
'confirm',
36+
null,
37+
InputOption::VALUE_NONE,
38+
'If set, the command will bypass the confirmation prompt'
39+
)
40+
;
41+
}
42+
/**
43+
* Confirmation prompt then tear down the database tables
44+
*
45+
* @param InputInterface $input
46+
* @param OutputInterface $output
47+
* @return int|null|void
48+
*/
49+
protected function execute(InputInterface $input, OutputInterface $output)
50+
{
51+
$con = DbConfig::getPDO($this->env, true);
52+
$stmt = $con->query("SHOW TABLES");
53+
$result = $stmt->fetchAll();
54+
55+
$tables = array();
56+
foreach ($result as $row) {
57+
$tables[] = array_shift($row);
58+
}
59+
60+
$dialogue = $this->getHelperSet()->get('dialog');
61+
if (!$input->getOption('confirm')) {
62+
$tableCount = count($tables);
63+
$answer = $dialogue->askConfirmation(
64+
$output,
65+
"<question>Tear down $tableCount tables(y/n)?</question>",
66+
false
67+
);
68+
if (!$answer) {
69+
exit;
70+
}
71+
}
72+
73+
foreach ($tables as $table) {
74+
$con->exec("DROP TABLE IF EXISTS `$table`");
75+
$output->writeln("Dropped table $table");
76+
}
77+
}
78+
79+
}

src/Smrtr/MysqlVersionControl/UpCommand.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,6 @@ protected function configure()
3434
InputArgument::OPTIONAL,
3535
'Where is the MySQL binary located?'
3636
)
37-
->addOption(
38-
'confirm',
39-
null,
40-
InputOption::VALUE_NONE,
41-
'If set, the command will bypass the confirmation prompt'
42-
)
4337
;
4438
}
4539

0 commit comments

Comments
 (0)