diff --git a/.idea/runConfigurations/Test_Postgres.xml b/.idea/runConfigurations/Test_Postgres.xml index c10103a..5342ac9 100644 --- a/.idea/runConfigurations/Test_Postgres.xml +++ b/.idea/runConfigurations/Test_Postgres.xml @@ -1,6 +1,26 @@ - - + + + + + + + \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index 7fd0961..5263eb3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,13 +1,38 @@ language: php + +addons: + hosts: + - mysql-container + - postgres-container + - dblib-container + php: - "7.2" - "7.1" - "7.0" - "5.6" +services: + - docker + +before_install: + - sudo service mysql stop || echo "mysql not stopped" + - sudo service postgresql stop || echo "postgresql not stopped" + - npm i @usdocker/usdocker @usdocker/postgres @usdocker/mysql @usdocker/mssql + - node_modules/.bin/usdocker --refresh +# - node_modules/.bin/usdocker mssql up + - node_modules/.bin/usdocker postgres up + - node_modules/.bin/usdocker mysql up + install: - composer install + - node_modules/.bin/usdocker mssql status + - node_modules/.bin/usdocker postgres status + - node_modules/.bin/usdocker mysql status script: - - phpunit + - vendor/bin/phpunit +# - vendor/bin/phpunit tests/SqlServerDatabaseTest.php + - vendor/bin/phpunit tests/PostgresDatabaseTest.php + - vendor/bin/phpunit tests/MysqlDatabaseTest.php diff --git a/README.md b/README.md index 1542618..45a9186 100644 --- a/README.md +++ b/README.md @@ -8,29 +8,26 @@ Simple library in PHP for database version control. Supports Sqlite, MySql, Sql Database Migration is a set of commands for upgrade or downgrade a database. This library uses only SQL commands. -## Introduction - -The basic usage is +## Installing -- Create a connection a ConnectionManagement object. For more information see the "byjg/anydataset" component -- Create a Migration object with this connection and the folder where the scripts sql are located. -- Use the proper command for "reset", "up" or "down" the migrations scripts. +``` +composer require 'byjg/migration=2.0.*' +``` -See an example: +## Supported databases: -```php -$connectionUri = new \ByJG\Util\Uri('mysql://migrateuser:migratepwd@localhost/migratedatabase'); -$migration = new Migration($connectionUri, '.'); + * Sqlite + * Mysql / MariaDB + * Postgres + * SqlServer -// Restore the database using the "base.sql" script and run ALL existing scripts for up the database version -// and run the up() method to maintain the database updated; -$migration->reset(); +## How It Works? -// Run ALL existing scripts for up the database version from the current version to the last version; -$migration->up(); -``` +The Database Migration uses PURE SQL to manage the database versioning. +In order to get working you need to: -The Migration object controls the database version. + - Create the SQL Scripts + - Manage using Command Line or the API. ### The SQL Scripts @@ -59,14 +56,14 @@ The directory scripts is : +-- 00001.sql ``` -- "base.sql" is the base script -- "up" folder contains the scripts for migrate up the version. -For example: 00002.sql is the script for move the database from version '1' to '2'. -- "down" folder contains the scripts for migrate down the version. -For example: 00001.sql is the script for move the database from version '2' to '1'. -The "down" folder is optional. + - "base.sql" is the base script + - "up" folder contains the scripts for migrate up the version. + For example: 00002.sql is the script for move the database from version '1' to '2'. + - "down" folder contains the scripts for migrate down the version. + For example: 00001.sql is the script for move the database from version '2' to '1'. + The "down" folder is optional. -### Multi Development environment +**Multi Development environment** If you work with multiple developers and multiple branches it is to difficult to determine what is the next number. @@ -74,8 +71,8 @@ In that case you have the suffix "-dev" after the version number. See the scenario: -- Developer 1 create a branch and the most recent version in e.g. 42. -- Developer 2 create a branch at the same time and have the same database version number. + - Developer 1 create a branch and the most recent version in e.g. 42. + - Developer 2 create a branch at the same time and have the same database version number. In both case the developers will create a file called 43-dev.sql. Both developers will migrate UP and DOWN with no problem and your local version will be 43. @@ -86,7 +83,7 @@ If he is try to migrate UP or DOWN the migration script will down and alert him there a TWO versions 43. In that case, developer 2 will have to update your file do 44-dev.sql and continue to work until merge your changes and generate a final version. -## Running in the command line +### Running in the command line Migration library creates the 'migrate' script. It has the follow syntax: @@ -115,9 +112,9 @@ Available commands: version Get the current database version ``` -## Commands +#### Commands -### Basic Usage +##### Basic Usage The basic usage is: @@ -140,7 +137,7 @@ You can omit the uri parameter if you define it in the export MIGRATE_CONNECTION=sqlite:///path/to/my.db ``` -### Command: create +##### Command: 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. @@ -151,7 +148,7 @@ Ex. migrate create /path/to/sql ``` -### Command: install +##### Command: 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. @@ -160,7 +157,7 @@ install the required tables for migration. migrate install mysql://server/database ``` -### Command: update +##### Command: update Will apply all necessary migrations to keep your database updated. @@ -175,7 +172,7 @@ You can also specify a version: migrate update --up-to=34 ``` -### Command: reset +##### Command: reset Creates/replace a database with the "base.sql" and apply ALL migrations @@ -193,28 +190,42 @@ migrate reset --yes # reset the database without ask anything. Be careful!! export MIGRATE_DISABLE_RESET=true ``` -## Supported databases: +### Using the API + +The basic usage is -* Sqlite -* Mysql / MariaDB -* Postgres -* SqlServer +- Create a connection a ConnectionManagement object. For more information see the "byjg/anydataset" component +- Create a Migration object with this connection and the folder where the scripts sql are located. +- Use the proper command for "reset", "up" or "down" the migrations scripts. -## Installing +See an example: -### At your project level +```php +registerDatabase('mysql', \ByJG\DbMigration\Database\MySqlDatabase::class); +$migration->registerDatabase('maria', \ByJG\DbMigration\Database\MySqlDatabase::class); -```bash -composer global require 'byjg/migration=2.0.*' -sudo ln -s $HOME/.composer/vendor/bin/migrate /usr/local/bin +// Restore the database using the "base.sql" script +// and run ALL existing scripts for up the database version to the latest version +$migration->reset(); + +// Run ALL existing scripts for up the database version +// from the current version to the last version; +$migration->up(); ``` +The Migration object controls the database version. + + + ## Unit Tests This library has integrated tests and need to be setup for each database you want to test. diff --git a/composer.json b/composer.json index 93229b1..1777ab7 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ "symfony/console": "^3.1" }, "require-dev": { - "phpunit/phpunit": "^5.7|^6.5" + "phpunit/phpunit": ">=5.7" }, "autoload": { "psr-4": { diff --git a/example/mysql/test_mysql.php b/example/mysql/test_mysql.php index 1c2bcc5..5e60abe 100644 --- a/example/mysql/test_mysql.php +++ b/example/mysql/test_mysql.php @@ -11,6 +11,7 @@ $uri = new \ByJG\Util\Uri('mysql://root:password@mysql-container/migratedatabase'); $migration = new \ByJG\DbMigration\Migration($uri, __DIR__); +$migration->registerDatabase('mysql', \ByJG\DbMigration\Database\MySqlDatabase::class); $migration->prepareEnvironment(); diff --git a/example/postgres/test_postgres.php b/example/postgres/test_postgres.php index 81b0c62..3689773 100644 --- a/example/postgres/test_postgres.php +++ b/example/postgres/test_postgres.php @@ -11,6 +11,7 @@ $uri = new \ByJG\Util\Uri('pgsql://postgres:password@postgres-container/migratedatabase'); $migration = new \ByJG\DbMigration\Migration($uri, __DIR__); +$migration->registerDatabase('pgsql', \ByJG\DbMigration\Database\PgsqlDatabase::class); $migration->prepareEnvironment(); diff --git a/example/sql_server/test_sqlserver.php b/example/sql_server/test_sqlserver.php index ad14684..ef53161 100644 --- a/example/sql_server/test_sqlserver.php +++ b/example/sql_server/test_sqlserver.php @@ -11,6 +11,7 @@ $uri = new \ByJG\Util\Uri('dblib://sa:Pa$$word!@mssql-container/migratedatabase'); $migration = new \ByJG\DbMigration\Migration($uri, __DIR__); +$migration->registerDatabase('dblib', \ByJG\DbMigration\Database\DblibDatabase::class); $migration->prepareEnvironment(); diff --git a/example/sqlite/test_sqlite.php b/example/sqlite/test_sqlite.php index a8bdf9a..c67f7b8 100644 --- a/example/sqlite/test_sqlite.php +++ b/example/sqlite/test_sqlite.php @@ -5,6 +5,7 @@ $uri = new \ByJG\Util\Uri('sqlite:///tmp/teste.sqlite'); $migration = new \ByJG\DbMigration\Migration($uri, __DIR__); +$migration->registerDatabase('sqlite', \ByJG\DbMigration\Database\SqliteDatabase::class); $migration->reset(); diff --git a/scripts/migrate b/scripts/migrate index 55198ed..e8b7660 100755 --- a/scripts/migrate +++ b/scripts/migrate @@ -12,7 +12,7 @@ require_once($autoload); use Symfony\Component\Console\Application; -$application = new Application('Migrate Script by JG', '2.0.1'); +$application = new Application('Migrate Script by JG', '2.0.4'); $application->add(new \ByJG\DbMigration\Console\ResetCommand()); $application->add(new \ByJG\DbMigration\Console\UpCommand()); $application->add(new \ByJG\DbMigration\Console\DownCommand()); diff --git a/src/Console/ConsoleCommand.php b/src/Console/ConsoleCommand.php index abae9ec..2f3b0ce 100644 --- a/src/Console/ConsoleCommand.php +++ b/src/Console/ConsoleCommand.php @@ -2,6 +2,10 @@ namespace ByJG\DbMigration\Console; +use ByJG\DbMigration\Database\DblibDatabase; +use ByJG\DbMigration\Database\MySqlDatabase; +use ByJG\DbMigration\Database\PgsqlDatabase; +use ByJG\DbMigration\Database\SqliteDatabase; use ByJG\DbMigration\Migration; use ByJG\Util\Uri; use Symfony\Component\Console\Command\Command; @@ -34,6 +38,12 @@ protected function configure() InputOption::VALUE_OPTIONAL, 'Run up to the specified version' ) + ->addOption( + 'no-base', + null, + InputOption::VALUE_NONE, + 'Remove the check for base.sql file' + ) ->addUsage('') ->addUsage('Example: ') ->addUsage(' migrate reset mysql://root:password@server/database') @@ -55,6 +65,11 @@ protected function configure() protected $path; + /** + * @param \Symfony\Component\Console\Input\InputInterface $input + * @param \Symfony\Component\Console\Output\OutputInterface $output + * @throws \ByJG\DbMigration\Exception\InvalidMigrationFile + */ protected function initialize(InputInterface $input, OutputInterface $output) { $this->connection = $input->getArgument('connection'); @@ -72,8 +87,15 @@ protected function initialize(InputInterface $input, OutputInterface $output) $this->upTo = $input->getOption('up-to'); + $requiredBase = !$input->getOption('no-base'); + $uri = new Uri($this->connection); - $this->migration = new Migration($uri, $this->path); + $this->migration = new Migration($uri, $this->path, $requiredBase); + $this->migration + ->registerDatabase('sqlite', SqliteDatabase::class) + ->registerDatabase('mysql', MySqlDatabase::class) + ->registerDatabase('pgsql', PgsqlDatabase::class) + ->registerDatabase('dblib', DblibDatabase::class); } protected function execute(InputInterface $input, OutputInterface $output) @@ -102,6 +124,4 @@ protected function handleError($exception, OutputInterface $output) $output->writeln($exception->getMessage()); } } - - } diff --git a/src/Console/DatabaseVersionCommand.php b/src/Console/DatabaseVersionCommand.php index 74c14c9..51afa92 100644 --- a/src/Console/DatabaseVersionCommand.php +++ b/src/Console/DatabaseVersionCommand.php @@ -1,10 +1,4 @@ setName('version') ->setDescription('Get the current database version'); diff --git a/src/Console/DownCommand.php b/src/Console/DownCommand.php index 0dbbead..aaa5ae3 100644 --- a/src/Console/DownCommand.php +++ b/src/Console/DownCommand.php @@ -1,10 +1,4 @@ setName('down') ->setDescription('Migrate down the database version.'); @@ -47,5 +41,4 @@ protected function execute(InputInterface $input, OutputInterface $output) $this->handleError($ex, $output); } } - } diff --git a/src/Console/InstallCommand.php b/src/Console/InstallCommand.php index be9159d..cfffec5 100644 --- a/src/Console/InstallCommand.php +++ b/src/Console/InstallCommand.php @@ -1,10 +1,4 @@ setName('install') ->setDescription('Install or upgrade the migrate version in a existing database'); } + /** + * @param \Symfony\Component\Console\Input\InputInterface $input + * @param \Symfony\Component\Console\Output\OutputInterface $output + * @return int|null|void + */ protected function execute(InputInterface $input, OutputInterface $output) { try { diff --git a/src/Console/ResetCommand.php b/src/Console/ResetCommand.php index 9b18541..d446c32 100644 --- a/src/Console/ResetCommand.php +++ b/src/Console/ResetCommand.php @@ -1,10 +1,4 @@ addOption('yes', null, null, 'Answer yes to any interactive question'); } + /** + * @param \Symfony\Component\Console\Input\InputInterface $input + * @param \Symfony\Component\Console\Output\OutputInterface $output + * @return int|null|void + * @throws \ByJG\DbMigration\Exception\ResetDisabledException + */ protected function execute(InputInterface $input, OutputInterface $output) { if (getenv('MIGRATE_DISABLE_RESET') === "true") { diff --git a/src/Console/UpCommand.php b/src/Console/UpCommand.php index 7a8a0a4..e7ad6bf 100644 --- a/src/Console/UpCommand.php +++ b/src/Console/UpCommand.php @@ -1,10 +1,4 @@ setName('up') ->setDescription('Migrate Up the database version'); diff --git a/src/Console/UpdateCommand.php b/src/Console/UpdateCommand.php index a6c8f8c..c0636bf 100644 --- a/src/Console/UpdateCommand.php +++ b/src/Console/UpdateCommand.php @@ -1,10 +1,4 @@ setName('update') ->setDescription('Migrate Up or Down the database version based on the current database version and the ' . diff --git a/src/Database/AbstractDatabase.php b/src/Database/AbstractDatabase.php index 5f8d415..f068d2a 100644 --- a/src/Database/AbstractDatabase.php +++ b/src/Database/AbstractDatabase.php @@ -3,8 +3,10 @@ namespace ByJG\DbMigration\Database; use ByJG\AnyDataset\DbDriverInterface; +use ByJG\AnyDataset\Factory; use ByJG\DbMigration\Exception\DatabaseNotVersionedException; use ByJG\DbMigration\Exception\OldVersionSchemaException; +use Psr\Http\Message\UriInterface; abstract class AbstractDatabase implements DatabaseInterface { @@ -13,14 +15,19 @@ abstract class AbstractDatabase implements DatabaseInterface */ private $dbDriver; + /** + * @var \Psr\Http\Message\UriInterface + */ + private $uri; + /** * Command constructor. * - * @param DbDriverInterface $dbDriver + * @param UriInterface $uri */ - public function __construct(DbDriverInterface $dbDriver) + public function __construct(UriInterface $uri) { - $this->dbDriver = $dbDriver; + $this->uri = $uri; } /** @@ -28,9 +35,17 @@ public function __construct(DbDriverInterface $dbDriver) */ public function getDbDriver() { + if (is_null($this->dbDriver)) { + $this->dbDriver = Factory::getDbRelationalInstance($this->uri->__toString()); + } return $this->dbDriver; } + /** + * @return array + * @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException + * @throws \ByJG\DbMigration\Exception\OldVersionSchemaException + */ public function getVersion() { $result = []; @@ -49,6 +64,10 @@ public function getVersion() return $result; } + /** + * @param $version + * @param $status + */ public function setVersion($version, $status) { $this->getDbDriver()->execute( @@ -60,6 +79,10 @@ public function setVersion($version, $status) ); } + /** + * @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException + * @throws \ByJG\DbMigration\Exception\OldVersionSchemaException + */ protected function checkExistsVersion() { // Get the version to check if exists @@ -69,11 +92,14 @@ protected function checkExistsVersion() } } + /** + * + */ public function updateVersionTable() { $currentVersion = $this->getDbDriver()->getScalar('select version from migration_version'); $this->getDbDriver()->execute('drop table migration_version'); $this->createVersion(); $this->setVersion($currentVersion, 'unknow'); - } + } } diff --git a/src/Database/DatabaseInterface.php b/src/Database/DatabaseInterface.php index 1b6503b..caedcba 100644 --- a/src/Database/DatabaseInterface.php +++ b/src/Database/DatabaseInterface.php @@ -2,16 +2,21 @@ namespace ByJG\DbMigration\Database; -use ByJG\Util\Uri; +use Psr\Http\Message\UriInterface; interface DatabaseInterface { - public static function prepareEnvironment(Uri $dbDriver); + public static function prepareEnvironment(UriInterface $dbDriver); public function createDatabase(); public function dropDatabase(); - + + /** + * @return array + * @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException + * @throws \ByJG\DbMigration\Exception\OldVersionSchemaException + */ public function getVersion(); public function updateVersionTable(); @@ -21,4 +26,6 @@ public function executeSql($sql); public function setVersion($version, $status); public function createVersion(); + + public function getDbDriver(); } diff --git a/src/Database/DblibDatabase.php b/src/Database/DblibDatabase.php index 573c1cf..3985612 100644 --- a/src/Database/DblibDatabase.php +++ b/src/Database/DblibDatabase.php @@ -4,11 +4,12 @@ use ByJG\AnyDataset\Factory; use ByJG\Util\Uri; +use Psr\Http\Message\UriInterface; class DblibDatabase extends AbstractDatabase { - public static function prepareEnvironment(Uri $uri) + public static function prepareEnvironment(UriInterface $uri) { $database = preg_replace('~^/~', '', $uri->getPath()); @@ -49,6 +50,10 @@ protected function createTableIfNotExists($database, $table, $createTable) $this->getDbDriver()->execute($sql); } + /** + * @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException + * @throws \ByJG\DbMigration\Exception\OldVersionSchemaException + */ public function createVersion() { $database = preg_replace('~^/~', '', $this->getDbDriver()->getUri()->getPath()); diff --git a/src/Database/MysqlDatabase.php b/src/Database/MySqlDatabase.php similarity index 83% rename from src/Database/MysqlDatabase.php rename to src/Database/MySqlDatabase.php index d5c06b8..874910f 100644 --- a/src/Database/MysqlDatabase.php +++ b/src/Database/MySqlDatabase.php @@ -4,11 +4,12 @@ use ByJG\AnyDataset\Factory; use ByJG\Util\Uri; +use Psr\Http\Message\UriInterface; class MySqlDatabase extends AbstractDatabase { - public static function prepareEnvironment(Uri $uri) + public static function prepareEnvironment(UriInterface $uri) { $database = preg_replace('~^/~', '', $uri->getPath()); @@ -33,6 +34,10 @@ public function dropDatabase() $this->getDbDriver()->execute("drop database `$database`"); } + /** + * @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException + * @throws \ByJG\DbMigration\Exception\OldVersionSchemaException + */ public function createVersion() { $this->getDbDriver()->execute('CREATE TABLE IF NOT EXISTS migration_version (version int, status varchar(20))'); diff --git a/src/Database/PgsqlDatabase.php b/src/Database/PgsqlDatabase.php index ef372b9..66f5c7a 100644 --- a/src/Database/PgsqlDatabase.php +++ b/src/Database/PgsqlDatabase.php @@ -4,18 +4,19 @@ use ByJG\AnyDataset\Factory; use ByJG\Util\Uri; +use Psr\Http\Message\UriInterface; class PgsqlDatabase extends AbstractDatabase { - public static function prepareEnvironment(Uri $uri) + public static function prepareEnvironment(UriInterface $uri) { $database = preg_replace('~^/~', '', $uri->getPath()); $dbDriver = self::getDbDriverWithoutDatabase($uri); self::createDatabaseIfNotExists($dbDriver, $database); } - protected static function getDbDriverWithoutDatabase(Uri $uri) + protected static function getDbDriverWithoutDatabase(UriInterface $uri) { $customUri = new Uri($uri->__toString()); return Factory::getDbRelationalInstance($customUri->withPath('/')->__toString()); @@ -33,7 +34,7 @@ protected static function createDatabaseIfNotExists($dbDriver, $database) ); if (empty($currentDbName)) { - $dbDriver->execute("CREATE DATABASE $database WITH encoding=UTF8;"); + $dbDriver->execute("CREATE DATABASE $database WITH encoding=\"UTF8\";"); } } @@ -55,6 +56,10 @@ public function dropDatabase() } } + /** + * @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException + * @throws \ByJG\DbMigration\Exception\OldVersionSchemaException + */ public function createVersion() { $this->getDbDriver()->execute('CREATE TABLE IF NOT EXISTS migration_version (version int, status varchar(20))'); diff --git a/src/Database/SqliteDatabase.php b/src/Database/SqliteDatabase.php index 84ff6c7..a6571b4 100644 --- a/src/Database/SqliteDatabase.php +++ b/src/Database/SqliteDatabase.php @@ -2,12 +2,12 @@ namespace ByJG\DbMigration\Database; -use ByJG\Util\Uri; +use Psr\Http\Message\UriInterface; class SqliteDatabase extends AbstractDatabase { - public static function prepareEnvironment(Uri $uri) + public static function prepareEnvironment(UriInterface $uri) { } @@ -30,6 +30,10 @@ public function dropDatabase() } } + /** + * @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException + * @throws \ByJG\DbMigration\Exception\OldVersionSchemaException + */ public function createVersion() { $this->getDbDriver()->execute('CREATE TABLE IF NOT EXISTS migration_version (version int, status varchar(20))'); diff --git a/src/Exception/DatabaseDoesNotRegistered.php b/src/Exception/DatabaseDoesNotRegistered.php new file mode 100644 index 0000000..e593995 --- /dev/null +++ b/src/Exception/DatabaseDoesNotRegistered.php @@ -0,0 +1,8 @@ +uri = $uri; $this->folder = $folder; - - if (!file_exists($this->folder . '/base.sql')) { + if ($requiredBase && !file_exists($this->folder . '/base.sql')) { throw new InvalidMigrationFile("Migration script '{$this->folder}/base.sql' not found"); } } + /** + * @param $scheme + * @param $className + * @return $this + */ + public function registerDatabase($scheme, $className) + { + $this->databases[$scheme] = $className; + return $this; + } + /** * @return DbDriverInterface + * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered */ public function getDbDriver() { - if (is_null($this->dbDriver)) { - $this->dbDriver = Factory::getDbRelationalInstance($this->uri->__toString()); - } - return $this->dbDriver; + return $this->getDbCommand()->getDbDriver(); } /** * @return DatabaseInterface + * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered */ public function getDbCommand() { if (is_null($this->dbCommand)) { $class = $this->getDatabaseClassName(); - $this->dbCommand = new $class($this->getDbDriver()); + $this->dbCommand = new $class($this->uri); } return $this->dbCommand; } + /** + * @return mixed + * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered + */ protected function getDatabaseClassName() { - return "\\ByJG\\DbMigration\\Database\\" . ucfirst($this->uri->getScheme()) . "Database"; + if (isset($this->databases[$this->uri->getScheme()])) { + return $this->databases[$this->uri->getScheme()]; + } + throw new DatabaseDoesNotRegistered( + 'Scheme "' . $this->uri->getScheme() . '" does not found. Did you registered it?' + ); } /** @@ -133,18 +157,25 @@ public function getMigrationSql($version, $increment) /** * Create the database it it does not exists. Does not use this methos in a production environment + * + * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered */ public function prepareEnvironment() { $class = $this->getDatabaseClassName(); $class::prepareEnvironment($this->uri); } - + /** * 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 * * @param int $upVersion + * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered + * @throws \ByJG\DbMigration\Exception\DatabaseIsIncompleteException + * @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException + * @throws \ByJG\DbMigration\Exception\InvalidMigrationFile + * @throws \ByJG\DbMigration\Exception\OldVersionSchemaException */ public function reset($upVersion = null) { @@ -154,16 +185,26 @@ public function reset($upVersion = null) $this->getDbCommand()->dropDatabase(); $this->getDbCommand()->createDatabase(); $this->getDbCommand()->createVersion(); - $this->getDbCommand()->executeSql(file_get_contents($this->getBaseSql())); + + if (file_exists($this->getBaseSql())) { + $this->getDbCommand()->executeSql(file_get_contents($this->getBaseSql())); + } + $this->getDbCommand()->setVersion(0, 'complete'); $this->up($upVersion); } + /** + * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered + */ public function createVersion() { $this->getDbCommand()->createVersion(); } + /** + * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered + */ public function updateTableVersion() { $this->getDbCommand()->updateVersionTable(); @@ -172,7 +213,10 @@ public function updateTableVersion() /** * Get the current database version * - * @return int + * @return string[] The current 'version' and 'status' as an associative array + * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered + * @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException + * @throws \ByJG\DbMigration\Exception\OldVersionSchemaException */ public function getCurrentVersion() { @@ -206,7 +250,11 @@ protected function canContinue($currentVersion, $upVersion, $increment) * @param int $upVersion * @param int $increment Can accept 1 for UP or -1 for down * @param bool $force + * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered * @throws \ByJG\DbMigration\Exception\DatabaseIsIncompleteException + * @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException + * @throws \ByJG\DbMigration\Exception\InvalidMigrationFile + * @throws \ByJG\DbMigration\Exception\OldVersionSchemaException */ protected function migrate($upVersion, $increment, $force) { @@ -236,6 +284,11 @@ protected function migrate($upVersion, $increment, $force) * * @param int $upVersion * @param bool $force + * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered + * @throws \ByJG\DbMigration\Exception\DatabaseIsIncompleteException + * @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException + * @throws \ByJG\DbMigration\Exception\InvalidMigrationFile + * @throws \ByJG\DbMigration\Exception\OldVersionSchemaException */ public function up($upVersion = null, $force = false) { @@ -247,6 +300,11 @@ public function up($upVersion = null, $force = false) * * @param int $upVersion * @param bool $force + * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered + * @throws \ByJG\DbMigration\Exception\DatabaseIsIncompleteException + * @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException + * @throws \ByJG\DbMigration\Exception\InvalidMigrationFile + * @throws \ByJG\DbMigration\Exception\OldVersionSchemaException */ public function update($upVersion = null, $force = false) { @@ -264,6 +322,11 @@ public function update($upVersion = null, $force = false) * * @param int $upVersion * @param bool $force + * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered + * @throws \ByJG\DbMigration\Exception\DatabaseIsIncompleteException + * @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException + * @throws \ByJG\DbMigration\Exception\InvalidMigrationFile + * @throws \ByJG\DbMigration\Exception\OldVersionSchemaException */ public function down($upVersion, $force = false) { diff --git a/tests/BaseDatabase.php b/tests/BaseDatabase.php index 63136ec..d66b9ab 100644 --- a/tests/BaseDatabase.php +++ b/tests/BaseDatabase.php @@ -1,10 +1,5 @@ object->getBaseSql(); } + public function testGetBaseSqlNotFoundAndNotRequired() + { + $this->object = new Migration(new Uri('mysql://localhost'), __DIR__ . '/invalid', false); + $this->object->getBaseSql(); + $this->assertTrue(true); + } + public function testGetMigrationSql1() { $version = $this->object->getMigrationSql(1, 1); diff --git a/tests/MysqlDatabaseTest.php b/tests/MysqlDatabaseTest.php index 2b11247..d1e25e0 100644 --- a/tests/MysqlDatabaseTest.php +++ b/tests/MysqlDatabaseTest.php @@ -2,6 +2,9 @@ require_once 'BaseDatabase.php'; +/** + * @requires extension pdo_mysql + */ class MysqlDatabaseTest extends BaseDatabase { protected $uri = 'mysql://root:password@mysql-container/migratedatabase'; @@ -14,6 +17,7 @@ class MysqlDatabaseTest extends BaseDatabase public function setUp() { $this->migrate = new \ByJG\DbMigration\Migration(new \ByJG\Util\Uri($this->uri), __DIR__ . '/../example/mysql'); + $this->migrate->registerDatabase("mysql", \ByJG\DbMigration\Database\MySqlDatabase::class); parent::setUp(); } } diff --git a/tests/PostgresDatabaseTest.php b/tests/PostgresDatabaseTest.php index 464c0c3..54b3a87 100644 --- a/tests/PostgresDatabaseTest.php +++ b/tests/PostgresDatabaseTest.php @@ -2,6 +2,10 @@ require_once 'BaseDatabase.php'; + +/** + * @requires extension pdo_pgsql + */ class PostgresDatabaseTest extends BaseDatabase { protected $uri = 'pgsql://postgres:password@postgres-container/migratedatabase'; @@ -14,6 +18,7 @@ class PostgresDatabaseTest extends BaseDatabase public function setUp() { $this->migrate = new \ByJG\DbMigration\Migration(new \ByJG\Util\Uri($this->uri), __DIR__ . '/../example/postgres'); + $this->migrate->registerDatabase("pgsql", \ByJG\DbMigration\Database\PgsqlDatabase::class); parent::setUp(); } } diff --git a/tests/SqlServerDatabaseTest.php b/tests/SqlServerDatabaseTest.php index dbbde24..11e666a 100644 --- a/tests/SqlServerDatabaseTest.php +++ b/tests/SqlServerDatabaseTest.php @@ -2,6 +2,9 @@ require_once 'BaseDatabase.php'; +/** + * @requires extension pdo_dblib + */ class SqlServerDatabaseTest extends BaseDatabase { protected $uri = 'dblib://sa:Pa$$word!@mssql-container/migratedatabase'; @@ -22,6 +25,7 @@ public function getExpectedUsersVersion1() public function setUp() { $this->migrate = new \ByJG\DbMigration\Migration(new \ByJG\Util\Uri($this->uri), __DIR__ . '/../example/sql_server'); + $this->migrate->registerDatabase("dblib", \ByJG\DbMigration\Database\DblibDatabase::class); parent::setUp(); } } diff --git a/tests/SqliteDatabaseTest.php b/tests/SqliteDatabaseTest.php index caa7a16..9cf8ed5 100644 --- a/tests/SqliteDatabaseTest.php +++ b/tests/SqliteDatabaseTest.php @@ -2,6 +2,9 @@ require_once 'BaseDatabase.php'; +/** + * @requires extension pdo_sqlite + */ class SqliteDatabaseTest extends BaseDatabase { protected $path = __DIR__ . '/../example/sqlite/test.sqlite'; @@ -18,6 +21,7 @@ public function setUp() $uri = new \ByJG\Util\Uri("sqlite://{$this->path}"); $this->migrate = new \ByJG\DbMigration\Migration($uri, __DIR__ . '/../example/sqlite'); + $this->migrate->registerDatabase("sqlite", \ByJG\DbMigration\Database\SqliteDatabase::class); parent::setUp(); } }