Skip to content

Commit 05faab3

Browse files
committed
Added option for custom versions path
1 parent 1846557 commit 05faab3

2 files changed

Lines changed: 29 additions & 3 deletions

File tree

README.md

Lines changed: 6 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.
@@ -80,6 +81,9 @@ current database version.
8081
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
8182
schema.
8283

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+
8387
### teardown
8488
Run `vendor/bin/teardown <environment>` to tear down the tables on the given environment.
8589

src/Smrtr/MysqlVersionControl/UpCommand.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ protected function configure()
4040
InputOption::VALUE_NONE,
4141
'Skip execution of the schema files'
4242
)
43+
->addOption(
44+
'versions-path',
45+
'p',
46+
InputOption::VALUE_REQUIRED,
47+
'Optional custom path to database versions directory'
48+
)
4349
;
4450
}
4551

@@ -98,6 +104,21 @@ protected function execute(InputInterface $input, OutputInterface $output)
98104

99105
// 2. Check for current version and available version
100106

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+
101122
// what is the current version?
102123
$query = $runConf->query("SELECT `value` FROM `db_config` WHERE `key`='version'");
103124
if ($query->rowCount()) {
@@ -106,15 +127,16 @@ protected function execute(InputInterface $input, OutputInterface $output)
106127
} else {
107128
$currentVersion = 0;
108129
}
130+
$output->writeln('Current version: '.$currentVersion);
109131

110132
// what is the available version?
111133
$availableVersion = 0;
112-
$versionsPath = realpath(dirname(__FILE__).'/../../../../../../db/versions');
113134
foreach (scandir($versionsPath) as $path) {
114135
if (preg_match("/^(\\d)+$/", $path) && (int) $path > $availableVersion) {
115136
$availableVersion = (int) $path;
116137
}
117138
}
139+
$output->writeln('Available version: '.$availableVersion);
118140

119141
if ($currentVersion >= $availableVersion) {
120142
$output->writeln('<info>Database version is already up to date.</info>');

0 commit comments

Comments
 (0)