From bb943408b20ee135056d8203e8b6d07b7b3f6478 Mon Sep 17 00:00:00 2001 From: zcq <64899484@qq.com> Date: Sat, 27 Jun 2020 11:34:57 +0800 Subject: [PATCH 1/7] featrue: support dry run --- src/Console/Commands/Migration.php | 1 + src/Migrations.php | 6 ++- src/Mvc/Model/Migration.php | 14 +++--- src/Proxy.php | 68 ++++++++++++++++++++++++++++++ 4 files changed, 83 insertions(+), 6 deletions(-) create mode 100644 src/Proxy.php diff --git a/src/Console/Commands/Migration.php b/src/Console/Commands/Migration.php index 1f82bfe..29f886d 100644 --- a/src/Console/Commands/Migration.php +++ b/src/Console/Commands/Migration.php @@ -154,6 +154,7 @@ public function run(): void 'version' => $this->parser->get('version'), 'migrationsInDb' => $migrationsInDb, 'verbose' => $this->parser->has('verbose'), + 'dry' => $this->parser->has('dry'), ]); break; case 'list': diff --git a/src/Migrations.php b/src/Migrations.php index 96a912d..5a66438 100644 --- a/src/Migrations.php +++ b/src/Migrations.php @@ -197,6 +197,7 @@ public static function run(array $options) $listTables = new ListTablesIterator(); $optionStack->setOptions($options); $optionStack->setDefaultOption('verbose', false); + $optionStack->setDefaultOption('dry', false); // Define versioning type to be used if (!empty($options['tsBased']) || $optionStack->getOption('tsBased')) { @@ -265,7 +266,7 @@ public static function run(array $options) $finalVersion = VersionCollection::maximum($versionItems); } - ModelMigration::setup($optionStack->getOption('config')->database, $optionStack->getOption('verbose')); + ModelMigration::setup($optionStack->getOption('config')->database, $optionStack->getOption('verbose'), $optionStack->getOption('dry')); self::connectionSetup($optionStack->getOptions()); /** @@ -372,6 +373,9 @@ public static function run(array $options) } } + if ($optionStack->getOption('dry')){ + return; + } if (ModelMigration::DIRECTION_FORWARD == $direction) { self::addCurrentVersion($optionStack->getOptions(), (string)$versionItem, $migrationStartTime); print Color::success('Version ' . $versionItem . ' was successfully migrated'); diff --git a/src/Mvc/Model/Migration.php b/src/Mvc/Model/Migration.php index 93a20f1..8ec40ba 100644 --- a/src/Mvc/Model/Migration.php +++ b/src/Mvc/Model/Migration.php @@ -31,6 +31,7 @@ use Phalcon\Migrations\Listeners\DbProfilerListener; use Phalcon\Migrations\Migration\Action\Generate as GenerateAction; use Phalcon\Migrations\Migrations; +use Phalcon\Migrations\Proxy; use Phalcon\Migrations\Utils; use Phalcon\Migrations\Utils\Nullify; use Phalcon\Migrations\Version\ItemCollection as VersionCollection; @@ -92,10 +93,11 @@ class Migration * * @param Config $database Database config * @param bool $verbose array with settings + * @param bool $dryRun array with settings * @throws DbException * @since 3.2.1 Using Postgresql::describeReferences and DialectPostgresql dialect class */ - public static function setup(Config $database, bool $verbose = false): void + public static function setup(Config $database, bool $verbose = false, bool $dryRun = false): void { if (!isset($database->adapter)) { throw new DbException('Unspecified database Adapter in your configuration!'); @@ -120,19 +122,21 @@ public static function setup(Config $database, bool $verbose = false): void $configArray = $database->toArray(); unset($configArray['adapter']); - self::$connection = new $adapter($configArray); + $connection = new $adapter($configArray); self::$databaseConfig = $database; // Connection custom dialect Dialect/DialectMysql if ($database->adapter == 'Mysql') { - self::$connection->setDialect(new DialectMysql()); + $connection->setDialect(new DialectMysql()); } // Connection custom dialect Dialect/DialectPostgresql if ($database->adapter == 'Postgresql') { - self::$connection->setDialect(new DialectPostgresql()); + $connection->setDialect(new DialectPostgresql()); } + self::$connection = new Proxy($connection, $dryRun); + if (!Migrations::isConsole() || !$verbose) { return; } @@ -772,6 +776,6 @@ function ($value) { */ public function getConnection() { - return self::$connection; + return self::$connection->getTarget(); } } diff --git a/src/Proxy.php b/src/Proxy.php new file mode 100644 index 0000000..3f751cb --- /dev/null +++ b/src/Proxy.php @@ -0,0 +1,68 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Phalcon\Migrations; + +use InvalidArgumentException; +use Phalcon\Config; + +class Proxy +{ + private $target; + private $dry; + + public function __construct($target, $dry = false) + { + $this->target = $target; + $this->dry = $dry; + } + + protected function log($line) + { + echo "[" . $line . "]\r\n"; + } + protected function getTarget() + { + return $this->target; + } + public function __set($name, $value) + { + $this->target->$name = $value; + } + + public function __get($name) + { + return $this->target->$name; + } + + public function __isset($name) + { + return isset($this->target->$name); + } + + public function __call($name, $arguments) + { + if ($this->dry && in_array($name,[ + 'addColumn','addForeignKey','addIndex','addPrimaryKey', + 'createTable','createView','dropColumn','dropForeignKey', + 'dropIndex','dropPrimaryKey','dropTable','dropView','modifyColumn' + ])) { + $dialect = $this->target->getDialect(); + if (method_exists($dialect, $name)) { + $this->log(call_user_func_array(array($dialect, $name), $arguments)); + return true; + } + } + return call_user_func_array(array($this->target, $name), $arguments); + } +} From 95c8239fd05952c312fa38b0b5742e75a46ab31a Mon Sep 17 00:00:00 2001 From: zcq <64899484@qq.com> Date: Sat, 27 Jun 2020 12:18:35 +0800 Subject: [PATCH 2/7] style: CodeSniffer --- src/Migrations.php | 8 ++++++-- src/Proxy.php | 8 ++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/Migrations.php b/src/Migrations.php index 5a66438..75121a2 100644 --- a/src/Migrations.php +++ b/src/Migrations.php @@ -266,7 +266,11 @@ public static function run(array $options) $finalVersion = VersionCollection::maximum($versionItems); } - ModelMigration::setup($optionStack->getOption('config')->database, $optionStack->getOption('verbose'), $optionStack->getOption('dry')); + ModelMigration::setup( + $optionStack->getOption('config')->database, + $optionStack->getOption('verbose'), + $optionStack->getOption('dry') + ); self::connectionSetup($optionStack->getOptions()); /** @@ -373,7 +377,7 @@ public static function run(array $options) } } - if ($optionStack->getOption('dry')){ + if ($optionStack->getOption('dry')) { return; } if (ModelMigration::DIRECTION_FORWARD == $direction) { diff --git a/src/Proxy.php b/src/Proxy.php index 3f751cb..a3ff758 100644 --- a/src/Proxy.php +++ b/src/Proxy.php @@ -52,10 +52,10 @@ public function __isset($name) public function __call($name, $arguments) { - if ($this->dry && in_array($name,[ - 'addColumn','addForeignKey','addIndex','addPrimaryKey', - 'createTable','createView','dropColumn','dropForeignKey', - 'dropIndex','dropPrimaryKey','dropTable','dropView','modifyColumn' + if ($this->dry && in_array($name, [ + 'addColumn', 'addForeignKey', 'addIndex', 'addPrimaryKey', + 'createTable', 'createView', 'dropColumn', 'dropForeignKey', + 'dropIndex', 'dropPrimaryKey', 'dropTable', 'dropView', 'modifyColumn' ])) { $dialect = $this->target->getDialect(); if (method_exists($dialect, $name)) { From c3df2c3ab3d46ac3877d24accbd0833a12d1d790 Mon Sep 17 00:00:00 2001 From: zcq <64899484@qq.com> Date: Sat, 27 Jun 2020 12:29:58 +0800 Subject: [PATCH 3/7] style: CodeSniffer --- src/Migrations.php | 4 ++-- src/Proxy.php | 13 ++++++++----- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/Migrations.php b/src/Migrations.php index 75121a2..f9ee508 100644 --- a/src/Migrations.php +++ b/src/Migrations.php @@ -267,8 +267,8 @@ public static function run(array $options) } ModelMigration::setup( - $optionStack->getOption('config')->database, - $optionStack->getOption('verbose'), + $optionStack->getOption('config')->database, + $optionStack->getOption('verbose'), $optionStack->getOption('dry') ); self::connectionSetup($optionStack->getOptions()); diff --git a/src/Proxy.php b/src/Proxy.php index a3ff758..1af3d9b 100644 --- a/src/Proxy.php +++ b/src/Proxy.php @@ -31,10 +31,12 @@ protected function log($line) { echo "[" . $line . "]\r\n"; } + protected function getTarget() { return $this->target; } + public function __set($name, $value) { $this->target->$name = $value; @@ -52,11 +54,12 @@ public function __isset($name) public function __call($name, $arguments) { - if ($this->dry && in_array($name, [ - 'addColumn', 'addForeignKey', 'addIndex', 'addPrimaryKey', - 'createTable', 'createView', 'dropColumn', 'dropForeignKey', - 'dropIndex', 'dropPrimaryKey', 'dropTable', 'dropView', 'modifyColumn' - ])) { + $dialectArray = [ + 'addColumn', 'addForeignKey', 'addIndex', 'addPrimaryKey', + 'createTable', 'createView', 'dropColumn', 'dropForeignKey', + 'dropIndex', 'dropPrimaryKey', 'dropTable', 'dropView', 'modifyColumn' + ]; + if ($this->dry && in_array($name, $dialectArray)) { $dialect = $this->target->getDialect(); if (method_exists($dialect, $name)) { $this->log(call_user_func_array(array($dialect, $name), $arguments)); From 246f6e98e59d263e21d6426c73f3efebb94a153f Mon Sep 17 00:00:00 2001 From: zcq <64899484@qq.com> Date: Sat, 27 Jun 2020 12:39:12 +0800 Subject: [PATCH 4/7] fix: test error --- src/Mvc/Model/Migration.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Mvc/Model/Migration.php b/src/Mvc/Model/Migration.php index 8ec40ba..d75b724 100644 --- a/src/Mvc/Model/Migration.php +++ b/src/Mvc/Model/Migration.php @@ -776,6 +776,9 @@ function ($value) { */ public function getConnection() { + if (self::$connection instanceof AbstractAdapter) { + return self::$connection; + } return self::$connection->getTarget(); } } From 5b605ec40671d872ea9f28c85b7822409e1f42a7 Mon Sep 17 00:00:00 2001 From: zcq <64899484@qq.com> Date: Sat, 27 Jun 2020 13:13:37 +0800 Subject: [PATCH 5/7] fix: test error --- src/Proxy.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Proxy.php b/src/Proxy.php index 1af3d9b..3ef7615 100644 --- a/src/Proxy.php +++ b/src/Proxy.php @@ -66,6 +66,9 @@ public function __call($name, $arguments) return true; } } + if (method_exists($this, $name)) { + return call_user_func_array(array($this, $name), $arguments); + } return call_user_func_array(array($this->target, $name), $arguments); } } From 34ea1f9381b4d73ff4db752c96cd9422c9c06bf9 Mon Sep 17 00:00:00 2001 From: zcq <64899484@qq.com> Date: Sat, 27 Jun 2020 15:34:12 +0800 Subject: [PATCH 6/7] style: CodeSniffer --- src/Mvc/Model/Migration.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Mvc/Model/Migration.php b/src/Mvc/Model/Migration.php index d75b724..aabfc2e 100644 --- a/src/Mvc/Model/Migration.php +++ b/src/Mvc/Model/Migration.php @@ -56,7 +56,7 @@ class Migration /** * Migration database connection * - * @var AbstractAdapter + * @var AbstractAdapter|Proxy */ protected static $connection; @@ -144,7 +144,7 @@ public static function setup(Config $database, bool $verbose = false, bool $dryR $eventsManager = new EventsManager(); $eventsManager->attach('db', new DbProfilerListener()); - self::$connection->setEventsManager($eventsManager); + $connection->setEventsManager($eventsManager); } /** From aa1f4e4060086a76841eb546c9809fa0fbee9b16 Mon Sep 17 00:00:00 2001 From: zcq <64899484@qq.com> Date: Sat, 27 Jun 2020 15:42:18 +0800 Subject: [PATCH 7/7] fix: test error --- src/Proxy.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Proxy.php b/src/Proxy.php index 3ef7615..1a164b2 100644 --- a/src/Proxy.php +++ b/src/Proxy.php @@ -32,7 +32,7 @@ protected function log($line) echo "[" . $line . "]\r\n"; } - protected function getTarget() + public function getTarget() { return $this->target; }