Skip to content

Commit 5c5bf66

Browse files
committed
Merge pull request #12 from joegreen88/master
1.1.0
2 parents 9104e0e + 05faab3 commit 5c5bf66

3 files changed

Lines changed: 49 additions & 10 deletions

File tree

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,9 @@ respectively. Each configuration requires a `host`, `user`, `password` and `data
5555
buildtime.database = "buzz"
5656

5757
## 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.
58+
Your database versions will be stored in `<project_root>/db/versions` by default.
59+
The sql for each version is stored in a directory directly under this directory.
60+
So the directories are named `db/versions/1`, `db/versions/2` and so on.
6061
Each version must contain at least one of the following files:
6162

6263
- `schema.sql` - always runs first, contains `CREATE TABLE IF NOT EXISTS` and `ALTER` statements.
@@ -77,6 +78,12 @@ from the current version.
7778
If this is the first run on the given environment, then a table called `db_config` is created and used to store the
7879
current database version.
7980

81+
You can use the flag `--no-schema` to skip the schema files. This can be useful if you use an ORM to build the database
82+
schema.
83+
84+
You can also use the option `--versions-path`, or `-p` for short, to provide a custom versions path.
85+
This allows you to override the default versions path which is `<project_root>/db/versions`.
86+
8087
### teardown
8188
Run `vendor/bin/teardown <environment>` to tear down the tables on the given environment.
8289

src/Smrtr/MysqlVersionControl/TeardownCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,16 @@ protected function execute(InputInterface $input, OutputInterface $output)
6868
false
6969
);
7070
if (!$answer) {
71-
exit;
71+
return 0;
7272
}
7373
}
7474

7575
foreach ($tables as $table) {
7676
$con->exec("DROP TABLE IF EXISTS `$table`");
7777
$output->writeln("Dropped table $table");
7878
}
79+
80+
return 0;
7981
}
8082

8183
}

src/Smrtr/MysqlVersionControl/UpCommand.php

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ protected function configure()
3434
InputArgument::OPTIONAL,
3535
'Where is the MySQL binary located?'
3636
)
37+
->addOption(
38+
'no-schema',
39+
null,
40+
InputOption::VALUE_NONE,
41+
'Skip execution of the schema files'
42+
)
43+
->addOption(
44+
'versions-path',
45+
'p',
46+
InputOption::VALUE_REQUIRED,
47+
'Optional custom path to database versions directory'
48+
)
3749
;
3850
}
3951

@@ -59,7 +71,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
5971

6072
if (!$buildConf instanceof \PDO) {
6173
$output->writeln('<error>Failed: unable to obtain a database connection.</error>');
62-
return false;
74+
return 1;
6375
}
6476

6577
if ($buildConf->query("SHOW TABLES LIKE 'db_config'")->rowCount()) {
@@ -84,14 +96,29 @@ protected function execute(InputInterface $input, OutputInterface $output)
8496

8597
if (!$result) {
8698
$output->writeln('<error>Installing version control failed.</error>');
87-
return false;
99+
return 1;
88100
}
89101

90102
$output->writeln('<info>Installed version control successfully.</info>');
91103
}
92104

93105
// 2. Check for current version and available version
94106

107+
// what is the versions path?
108+
if ($input->getOption('versions-path')) {
109+
$versionsPath = $input->getOption('versions-path');
110+
} else {
111+
$versionsPath = realpath(dirname(__FILE__).'/../../../../../../db/versions');
112+
}
113+
if (!is_readable($versionsPath)) {
114+
$output->writeln('<error>Versions path is not readable: '.$versionsPath.'</error>');
115+
return 1;
116+
}
117+
if (!is_dir($versionsPath)) {
118+
$output->writeln('<error>Versions path is not a directory: '.$versionsPath.'</error>');
119+
return 1;
120+
}
121+
95122
// what is the current version?
96123
$query = $runConf->query("SELECT `value` FROM `db_config` WHERE `key`='version'");
97124
if ($query->rowCount()) {
@@ -100,19 +127,20 @@ protected function execute(InputInterface $input, OutputInterface $output)
100127
} else {
101128
$currentVersion = 0;
102129
}
130+
$output->writeln('Current version: '.$currentVersion);
103131

104132
// what is the available version?
105133
$availableVersion = 0;
106-
$versionsPath = realpath(dirname(__FILE__).'/../../../../../../db/versions');
107134
foreach (scandir($versionsPath) as $path) {
108135
if (preg_match("/^(\\d)+$/", $path) && (int) $path > $availableVersion) {
109136
$availableVersion = (int) $path;
110137
}
111138
}
139+
$output->writeln('Available version: '.$availableVersion);
112140

113141
if ($currentVersion >= $availableVersion) {
114142
$output->writeln('<info>Database version is already up to date.</info>');
115-
return true;
143+
return 0;
116144
}
117145

118146
$noun = ($availableVersion - $currentVersion > 1) ? 'updates' : 'update';
@@ -122,7 +150,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
122150

123151
// go from current to latest version, building stack of SQL files
124152
$filesToLookFor = [];
125-
$filesToLookFor[] = 'schema.sql'; // structural changes, alters, creates, drops
153+
if (!$input->getOption('no-schema')) {
154+
$filesToLookFor[] = 'schema.sql'; // structural changes, alters, creates, drops
155+
}
126156
$filesToLookFor[] = 'data.sql'; // core data, inserts, replaces, updates, deletes
127157
if (in_array($this->env, DbConfig::getTestingEnvironments())) {
128158
$filesToLookFor[] = 'testing.sql'; // extra data on top of data.sql for the testing environment(s)
@@ -225,11 +255,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
225255

226256
if ($result) {
227257
$output->writeln('<info>Database updates installed successfully.</info>');
228-
return true;
258+
return 0;
229259

230260
} else {
231261
$output->writeln('<error>Installing database updates failed.</error>');
232-
return false;
262+
return 1;
233263
}
234264
}
235265
}

0 commit comments

Comments
 (0)