Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Console/Commands/Migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down
10 changes: 9 additions & 1 deletion src/Migrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Expand Down Expand Up @@ -265,7 +266,11 @@ 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());

/**
Expand Down Expand Up @@ -372,6 +377,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');
Expand Down
21 changes: 14 additions & 7 deletions src/Mvc/Model/Migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -55,7 +56,7 @@ class Migration
/**
* Migration database connection
*
* @var AbstractAdapter
* @var AbstractAdapter|Proxy
*/
protected static $connection;

Expand Down Expand Up @@ -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!');
Expand All @@ -120,27 +122,29 @@ 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;
}

$eventsManager = new EventsManager();
$eventsManager->attach('db', new DbProfilerListener());

self::$connection->setEventsManager($eventsManager);
$connection->setEventsManager($eventsManager);
}

/**
Expand Down Expand Up @@ -772,6 +776,9 @@ function ($value) {
*/
public function getConnection()
{
return self::$connection;
if (self::$connection instanceof AbstractAdapter) {
return self::$connection;
}
return self::$connection->getTarget();
}
}
74 changes: 74 additions & 0 deletions src/Proxy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

/**
* This file is part of the Phalcon Migrations.
*
* (c) Phalcon Team <[email protected]>
*
* 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";
}

public 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)
{
$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));
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);
}
}