From 508b1f9348addd77117fc780d2fe287de67a4632 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joao=20Gilberto=20Magalh=C3=A3es?= Date: Fri, 10 Nov 2017 13:57:31 -0200 Subject: [PATCH 1/2] Fix problem with versions greater than 10. --- phpunit.xml.dist | 1 + src/Exception/InvalidMigrationFile.php | 8 ++ src/Migration.php | 63 ++++++------ tests/MigrationTest.php | 98 +++++++++++++++++++ tests/dirstructure/base.sql | 0 tests/dirstructure/migrations/down/0000.sql | 0 .../dirstructure/migrations/down/0000013.sql | 0 tests/dirstructure/migrations/down/00001.sql | 0 tests/dirstructure/migrations/down/00002.sql | 0 tests/dirstructure/migrations/down/00003.sql | 0 tests/dirstructure/migrations/down/00004.sql | 0 tests/dirstructure/migrations/down/00005.sql | 0 tests/dirstructure/migrations/down/00006.sql | 0 tests/dirstructure/migrations/down/00007.sql | 0 tests/dirstructure/migrations/down/00008.sql | 0 tests/dirstructure/migrations/down/00009.sql | 0 tests/dirstructure/migrations/down/00010.sql | 0 tests/dirstructure/migrations/down/00011.sql | 0 .../migrations/down/00012-dev.sql | 0 tests/dirstructure/migrations/down/00013.sql | 0 tests/dirstructure/migrations/up/0000013.sql | 0 tests/dirstructure/migrations/up/00001.sql | 0 tests/dirstructure/migrations/up/00002.sql | 0 tests/dirstructure/migrations/up/00003.sql | 0 tests/dirstructure/migrations/up/00004.sql | 0 tests/dirstructure/migrations/up/00005.sql | 0 tests/dirstructure/migrations/up/00006.sql | 0 tests/dirstructure/migrations/up/00007.sql | 0 tests/dirstructure/migrations/up/00008.sql | 0 tests/dirstructure/migrations/up/00009.sql | 0 tests/dirstructure/migrations/up/00010.sql | 0 tests/dirstructure/migrations/up/00011.sql | 0 .../dirstructure/migrations/up/00012-dev.sql | 0 tests/dirstructure/migrations/up/00013.sql | 0 34 files changed, 141 insertions(+), 29 deletions(-) create mode 100644 src/Exception/InvalidMigrationFile.php create mode 100644 tests/MigrationTest.php create mode 100644 tests/dirstructure/base.sql create mode 100644 tests/dirstructure/migrations/down/0000.sql create mode 100644 tests/dirstructure/migrations/down/0000013.sql create mode 100644 tests/dirstructure/migrations/down/00001.sql create mode 100644 tests/dirstructure/migrations/down/00002.sql create mode 100644 tests/dirstructure/migrations/down/00003.sql create mode 100644 tests/dirstructure/migrations/down/00004.sql create mode 100644 tests/dirstructure/migrations/down/00005.sql create mode 100644 tests/dirstructure/migrations/down/00006.sql create mode 100644 tests/dirstructure/migrations/down/00007.sql create mode 100644 tests/dirstructure/migrations/down/00008.sql create mode 100644 tests/dirstructure/migrations/down/00009.sql create mode 100644 tests/dirstructure/migrations/down/00010.sql create mode 100644 tests/dirstructure/migrations/down/00011.sql create mode 100644 tests/dirstructure/migrations/down/00012-dev.sql create mode 100644 tests/dirstructure/migrations/down/00013.sql create mode 100644 tests/dirstructure/migrations/up/0000013.sql create mode 100644 tests/dirstructure/migrations/up/00001.sql create mode 100644 tests/dirstructure/migrations/up/00002.sql create mode 100644 tests/dirstructure/migrations/up/00003.sql create mode 100644 tests/dirstructure/migrations/up/00004.sql create mode 100644 tests/dirstructure/migrations/up/00005.sql create mode 100644 tests/dirstructure/migrations/up/00006.sql create mode 100644 tests/dirstructure/migrations/up/00007.sql create mode 100644 tests/dirstructure/migrations/up/00008.sql create mode 100644 tests/dirstructure/migrations/up/00009.sql create mode 100644 tests/dirstructure/migrations/up/00010.sql create mode 100644 tests/dirstructure/migrations/up/00011.sql create mode 100644 tests/dirstructure/migrations/up/00012-dev.sql create mode 100644 tests/dirstructure/migrations/up/00013.sql diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 40fcae7..dabfb03 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -23,6 +23,7 @@ and open the template in the editor. ./tests/SqliteDatabaseTest.php + ./tests/MigrationTest.php diff --git a/src/Exception/InvalidMigrationFile.php b/src/Exception/InvalidMigrationFile.php new file mode 100644 index 0000000..d90f071 --- /dev/null +++ b/src/Exception/InvalidMigrationFile.php @@ -0,0 +1,8 @@ +uri = $uri; - $this->_folder = $_folder; + $this->folder = $folder; - if (!file_exists($this->_folder . '/base.sql')) { - throw new \InvalidArgumentException("Migration script '{$this->_folder}/base.sql' not found"); + if (!file_exists($this->folder . '/base.sql')) { + throw new InvalidMigrationFile("Migration script '{$this->folder}/base.sql' not found"); } } @@ -67,11 +69,11 @@ public function getDbDriver() */ public function getDbCommand() { - if (is_null($this->_dbCommand)) { + if (is_null($this->dbCommand)) { $class = $this->getDatabaseClassName(); - $this->_dbCommand = new $class($this->getDbDriver()); + $this->dbCommand = new $class($this->getDbDriver()); } - return $this->_dbCommand; + return $this->dbCommand; } protected function getDatabaseClassName() @@ -86,7 +88,7 @@ protected function getDatabaseClassName() */ public function getBaseSql() { - return $this->_folder . "/base.sql"; + return $this->folder . "/base.sql"; } /** @@ -95,6 +97,7 @@ public function getBaseSql() * @param $version * @param $increment * @return string + * @throws \ByJG\DbMigration\Exception\InvalidMigrationFile */ public function getMigrationSql($version, $increment) { @@ -106,19 +109,18 @@ public function getMigrationSql($version, $increment) } $version = intval($version); - $filePattern = $this->_folder + $filePattern = $this->folder . "/migrations" . "/" . ($increment < 0 ? "down" : "up") - . "/*%s.sql"; + . "/*.sql"; - $result = array_merge( - glob(sprintf($filePattern, "$version")), - glob(sprintf($filePattern, "$version-dev")) - ); + $result = array_filter(glob($filePattern), function ($file) use ($version) { + return preg_match("/^0*$version(-dev)?\.sql$/", basename($file)); + }); // Valid values are 0 or 1 if (count($result) > 1) { - throw new \InvalidArgumentException("You have two files with the same version $version number"); + throw new InvalidMigrationFile("You have two files with the same version number '$version'"); } foreach ($result as $file) { @@ -130,7 +132,7 @@ public function getMigrationSql($version, $increment) } /** - * Create the database it it does not exists. Does not use this methos in a production environment; + * Create the database it it does not exists. Does not use this methos in a production environment */ public function prepareEnvironment() { @@ -140,14 +142,14 @@ public function prepareEnvironment() /** * Restore the database using the "base.sql" script and run all migration scripts - * Note: the database must exists. If dont exist run the method prepareEnvironment. + * Note: the database must exists. If dont exist run the method prepareEnvironment * * @param int $upVersion */ public function reset($upVersion = null) { - if ($this->_callableProgress) { - call_user_func_array($this->_callableProgress, ['reset', 0]); + if ($this->callableProgress) { + call_user_func_array($this->callableProgress, ['reset', 0]); } $this->getDbCommand()->dropDatabase(); $this->getDbCommand()->createDatabase(); @@ -218,8 +220,8 @@ protected function migrate($upVersion, $increment, $force) while ($this->canContinue($currentVersion, $upVersion, $increment) && file_exists($file = $this->getMigrationSql($currentVersion, $increment)) ) { - if ($this->_callableProgress) { - call_user_func_array($this->_callableProgress, ['migrate', $currentVersion]); + if ($this->callableProgress) { + call_user_func_array($this->callableProgress, ['migrate', $currentVersion]); } $this->getDbCommand()->setVersion($currentVersion, 'partial ' . ($increment>0 ? 'up' : 'down')); @@ -267,9 +269,12 @@ public function down($upVersion, $force = false) { $this->migrate($upVersion, -1, $force); } - - public function addCallbackProgress(Callable $callable) + + /** + * @param callable $callable + */ + public function addCallbackProgress(callable $callable) { - $this->_callableProgress = $callable; + $this->callableProgress = $callable; } } diff --git a/tests/MigrationTest.php b/tests/MigrationTest.php new file mode 100644 index 0000000..e825412 --- /dev/null +++ b/tests/MigrationTest.php @@ -0,0 +1,98 @@ +object = new Migration(new Uri('mysql://localhost'), __DIR__ . '/dirstructure'); + } + + public function tearDown() + { + $this->object = null; + } + + public function testGetBaseSql() + { + $base = $this->object->getBaseSql(); + $this->assertEquals(__DIR__ . '/dirstructure/base.sql', $base); + } + + /** + * @expectedException \ByJG\DbMigration\Exception\InvalidMigrationFile + */ + public function testGetBaseSqlNotFound() + { + $this->object = new Migration(new Uri('mysql://localhost'), __DIR__ . '/invalid'); + $this->object->getBaseSql(); + } + + public function testGetMigrationSql1() + { + $version = $this->object->getMigrationSql(1, 1); + $this->assertEquals(__DIR__ . '/dirstructure/migrations/up/00001.sql', $version); + } + + public function testGetMigrationSql2() + { + $version = $this->object->getMigrationSql(2, 1); + $this->assertEquals(__DIR__ . '/dirstructure/migrations/up/00002.sql', $version); + } + + public function testGetMigrationSql3() + { + $version = $this->object->getMigrationSql(12, 1); + $this->assertEquals(__DIR__ . '/dirstructure/migrations/up/00012-dev.sql', $version); + } + + /** + * @expectedException \ByJG\DbMigration\Exception\InvalidMigrationFile + * @expectedExceptionMessage version number '13' + */ + public function testGetMigrationSql4() + { + $this->object->getMigrationSql(13, 1); + } + + public function testGetMigrationSqlDown1() + { + $version = $this->object->getMigrationSql(1, -1); + $this->assertEquals(__DIR__ . '/dirstructure/migrations/down/00001.sql', $version); + } + + public function testGetMigrationSqlDown2() + { + $version = $this->object->getMigrationSql(2, -1); + $this->assertEquals(__DIR__ . '/dirstructure/migrations/down/00002.sql', $version); + } + + public function testGetMigrationSqlDown3() + { + $version = $this->object->getMigrationSql(12, -1); + $this->assertEquals(__DIR__ . '/dirstructure/migrations/down/00012-dev.sql', $version); + } + + /** + * @expectedException \ByJG\DbMigration\Exception\InvalidMigrationFile + * @expectedExceptionMessage version number '13' + */ + public function testGetMigrationSqlDown4() + { + $this->object->getMigrationSql(13, -1); + } +} diff --git a/tests/dirstructure/base.sql b/tests/dirstructure/base.sql new file mode 100644 index 0000000..e69de29 diff --git a/tests/dirstructure/migrations/down/0000.sql b/tests/dirstructure/migrations/down/0000.sql new file mode 100644 index 0000000..e69de29 diff --git a/tests/dirstructure/migrations/down/0000013.sql b/tests/dirstructure/migrations/down/0000013.sql new file mode 100644 index 0000000..e69de29 diff --git a/tests/dirstructure/migrations/down/00001.sql b/tests/dirstructure/migrations/down/00001.sql new file mode 100644 index 0000000..e69de29 diff --git a/tests/dirstructure/migrations/down/00002.sql b/tests/dirstructure/migrations/down/00002.sql new file mode 100644 index 0000000..e69de29 diff --git a/tests/dirstructure/migrations/down/00003.sql b/tests/dirstructure/migrations/down/00003.sql new file mode 100644 index 0000000..e69de29 diff --git a/tests/dirstructure/migrations/down/00004.sql b/tests/dirstructure/migrations/down/00004.sql new file mode 100644 index 0000000..e69de29 diff --git a/tests/dirstructure/migrations/down/00005.sql b/tests/dirstructure/migrations/down/00005.sql new file mode 100644 index 0000000..e69de29 diff --git a/tests/dirstructure/migrations/down/00006.sql b/tests/dirstructure/migrations/down/00006.sql new file mode 100644 index 0000000..e69de29 diff --git a/tests/dirstructure/migrations/down/00007.sql b/tests/dirstructure/migrations/down/00007.sql new file mode 100644 index 0000000..e69de29 diff --git a/tests/dirstructure/migrations/down/00008.sql b/tests/dirstructure/migrations/down/00008.sql new file mode 100644 index 0000000..e69de29 diff --git a/tests/dirstructure/migrations/down/00009.sql b/tests/dirstructure/migrations/down/00009.sql new file mode 100644 index 0000000..e69de29 diff --git a/tests/dirstructure/migrations/down/00010.sql b/tests/dirstructure/migrations/down/00010.sql new file mode 100644 index 0000000..e69de29 diff --git a/tests/dirstructure/migrations/down/00011.sql b/tests/dirstructure/migrations/down/00011.sql new file mode 100644 index 0000000..e69de29 diff --git a/tests/dirstructure/migrations/down/00012-dev.sql b/tests/dirstructure/migrations/down/00012-dev.sql new file mode 100644 index 0000000..e69de29 diff --git a/tests/dirstructure/migrations/down/00013.sql b/tests/dirstructure/migrations/down/00013.sql new file mode 100644 index 0000000..e69de29 diff --git a/tests/dirstructure/migrations/up/0000013.sql b/tests/dirstructure/migrations/up/0000013.sql new file mode 100644 index 0000000..e69de29 diff --git a/tests/dirstructure/migrations/up/00001.sql b/tests/dirstructure/migrations/up/00001.sql new file mode 100644 index 0000000..e69de29 diff --git a/tests/dirstructure/migrations/up/00002.sql b/tests/dirstructure/migrations/up/00002.sql new file mode 100644 index 0000000..e69de29 diff --git a/tests/dirstructure/migrations/up/00003.sql b/tests/dirstructure/migrations/up/00003.sql new file mode 100644 index 0000000..e69de29 diff --git a/tests/dirstructure/migrations/up/00004.sql b/tests/dirstructure/migrations/up/00004.sql new file mode 100644 index 0000000..e69de29 diff --git a/tests/dirstructure/migrations/up/00005.sql b/tests/dirstructure/migrations/up/00005.sql new file mode 100644 index 0000000..e69de29 diff --git a/tests/dirstructure/migrations/up/00006.sql b/tests/dirstructure/migrations/up/00006.sql new file mode 100644 index 0000000..e69de29 diff --git a/tests/dirstructure/migrations/up/00007.sql b/tests/dirstructure/migrations/up/00007.sql new file mode 100644 index 0000000..e69de29 diff --git a/tests/dirstructure/migrations/up/00008.sql b/tests/dirstructure/migrations/up/00008.sql new file mode 100644 index 0000000..e69de29 diff --git a/tests/dirstructure/migrations/up/00009.sql b/tests/dirstructure/migrations/up/00009.sql new file mode 100644 index 0000000..e69de29 diff --git a/tests/dirstructure/migrations/up/00010.sql b/tests/dirstructure/migrations/up/00010.sql new file mode 100644 index 0000000..e69de29 diff --git a/tests/dirstructure/migrations/up/00011.sql b/tests/dirstructure/migrations/up/00011.sql new file mode 100644 index 0000000..e69de29 diff --git a/tests/dirstructure/migrations/up/00012-dev.sql b/tests/dirstructure/migrations/up/00012-dev.sql new file mode 100644 index 0000000..e69de29 diff --git a/tests/dirstructure/migrations/up/00013.sql b/tests/dirstructure/migrations/up/00013.sql new file mode 100644 index 0000000..e69de29 From 6d3de54d2f8aa0b02f7ec042684f372dd7223105 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joao=20Gilberto=20Magalh=C3=A3es?= Date: Fri, 10 Nov 2017 14:46:21 -0200 Subject: [PATCH 2/2] Update version and README.md --- README.md | 79 +++++++++++++++++++++++++++++++++++++++++-------- scripts/migrate | 3 +- 2 files changed, 68 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 344443d..4b67b1f 100644 --- a/README.md +++ b/README.md @@ -94,23 +94,70 @@ Migration library creates the 'migrate' script. It has the follow syntax: Usage: command [options] [arguments] +Options: + -h, --help Display this help message + -q, --quiet Do not output any message + -V, --version Display this application version + --ansi Force ANSI output + --no-ansi Disable ANSI output + -n, --no-interaction Do not ask any interactive question + -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug + Available commands: create Create the directory structure FROM a pre-existing database + down Migrate down the database version. + help Displays help for a command install Install or upgrade the migrate version in a existing database - down Migrate down the database version. - reset Create a fresh new database - up Migrate Up the database version + list Lists commands + reset Create a fresh new database + up Migrate Up the database version + update Migrate Up or Down the database version based on the current database version and the migration scripts available version Get the current database version +``` + +## Commands + +### migrate create + +Create a empty directory structure with base.sql and migrations/up and migrations/down for migrations. This is +useful for create from scratch a migration scheme. + +Ex. + +```bash +migrate create /path/to/sql +``` + +### migrate install + +If you already have a database but it is not controlled by the migration system you can use this method for +install the required tables for migration. + +```bash +migrate install mysql://server/database +``` + +### migrate update -Arguments: - connection The connection string. Ex. mysql://root:password@server/database [default: false] +Will apply all necessary migrations to keep your database updated. -Example: - migrate reset mysql://root:password@server/database - migrate up mysql://root:password@server/database - migrate down mysql://root:password@server/database - migrate up --up-to=10 --path=/somepath mysql://root:password@server/database - migrate down --up-to=3 --path=/somepath mysql://root:password@server/database +```bash +migrate update mysql://server/database +``` + +Update command can choose if up or down your database depending on your current database version. + +```bash +migrate update --up-to=34 +``` + +### migrate reset + +Creates/replace a database with the "base.sql" and apply ALL migrations + +```bash +migrate reset +migrate reset --up-to=5 ``` ## Supported databases: @@ -120,7 +167,15 @@ Example: * Postgres * SqlServer -## Installing Globally +## Installing + +### At your project level + +``` +composer require 'byjg/migration=2.0.*' +``` + +### Globally ```bash composer global require 'byjg/migration=2.0.*' diff --git a/scripts/migrate b/scripts/migrate index a66ff19..55198ed 100755 --- a/scripts/migrate +++ b/scripts/migrate @@ -10,10 +10,9 @@ if (!file_exists($autoload)) { } require_once($autoload); - use Symfony\Component\Console\Application; -$application = new Application('Migrate Script by JG', '2.0.0'); +$application = new Application('Migrate Script by JG', '2.0.1'); $application->add(new \ByJG\DbMigration\Console\ResetCommand()); $application->add(new \ByJG\DbMigration\Console\UpCommand()); $application->add(new \ByJG\DbMigration\Console\DownCommand());