Skip to content

Commit

Permalink
Merge pull request phalcon#1117 from sergeysviridenko/3.2.table_prefix
Browse files Browse the repository at this point in the history
Added table prefix to generate/run migration
  • Loading branch information
sergeyklay authored Oct 6, 2017
2 parents eb02443 + 8d4d05f commit a3c5a12
Show file tree
Hide file tree
Showing 16 changed files with 626 additions and 15 deletions.
2 changes: 1 addition & 1 deletion scripts/Phalcon/Commands/Builtin/Migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function getPossibleParams()
'config=s' => 'Configuration file',
'migrations=s' => 'Migrations directory',
'directory=s' => 'Directory where the project was created',
'table=s' => 'Table to migrate. Default: all',
'table=s' => 'Table to migrate. Table name or table prefix with asterisk. Default: all',
'version=s' => 'Version to migrate',
'descr=s' => 'Migration description (used for timestamp based migration)',
'data=s' => 'Export data [always|oncreate] (Import data when run migration)',
Expand Down
17 changes: 11 additions & 6 deletions scripts/Phalcon/Console/OptionParserTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,19 @@
trait OptionParserTrait
{
/**
* Indicates whether the script was a particular option.
* Get prefix from the option
*
* @param string $key
* @param array $option
* @return boolean
* @param string $prefix
* @param mixed $prefixEnd
*
* @return mixed
*/
public function isReceivedOption($key, array $options)
public function getPrefixOption($prefix, $prefixEnd = '*')
{
return isset($options[$key]);
if (substr($prefix, -1) != $prefixEnd) {
return '';
}

return substr($prefix, 0, -1);
}
}
15 changes: 15 additions & 0 deletions scripts/Phalcon/Console/OptionStack.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

namespace Phalcon\Console;

use Phalcon\Console\OptionParserTrait;

/**
* Phalcon\Console\OptionStack
*
Expand All @@ -30,6 +32,8 @@
*/
class OptionStack
{
use OptionParserTrait;

/**
* Parameters received by the script.
* @var array
Expand Down Expand Up @@ -128,4 +132,15 @@ public function countOptions()
{
return count($this->options);
}

/**
* Indicates whether the script was a particular option.
*
* @param string $key
* @return boolean
*/
public function isReceivedOption($key)
{
return isset($this->options[$key]);
}
}
35 changes: 30 additions & 5 deletions scripts/Phalcon/Migrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
use Phalcon\Version\IncrementalItem as IncrementalVersion;
use Phalcon\Version\ItemCollection as VersionCollection;
use Phalcon\Console\OptionStack;
use Phalcon\Mvc\Model\Migration\TableAware\ListTablesIterator;
use Phalcon\Mvc\Model\Migration\TableAware\ListTablesDb;

/**
* Migrations Class
Expand Down Expand Up @@ -76,6 +78,8 @@ public static function isConsole()
public static function generate(array $options)
{
$optionStack = new OptionStack();
$modelMigration = new ModelMigration();
$listTables = new ListTablesDb();
$optionStack->setOptions($options);
$optionStack->setDefaultOption('version', null);
$optionStack->setDefaultOption('descr', null);
Expand Down Expand Up @@ -126,6 +130,16 @@ public static function generate(array $options)
}
}
} else {
$prefix = $optionStack->getPrefixOption($optionStack->getOption('tableName'));
if (!empty($prefix)) {
$optionStack->setOption('tableName', $listTables->listTablesForPrefix($prefix));
}

if ($optionStack->getOption('tableName') == '') {
print Color::info('No one table is created. You should create tables first.') . PHP_EOL;
return;
}

$tables = explode(',', $optionStack->getOption('tableName'));
foreach ($tables as $table) {
$migration = ModelMigration::generate($versionItem, $table, $optionStack->getOption('exportData'));
Expand Down Expand Up @@ -159,6 +173,7 @@ public static function generate(array $options)
public static function run(array $options)
{
$optionStack = new OptionStack();
$listTables = new ListTablesIterator();
$optionStack->setOptions($options);
$optionStack->setDefaultOption('verbose', false);

Expand Down Expand Up @@ -235,6 +250,7 @@ public static function run(array $options)

// Run migration
$versionsBetween = VersionCollection::between($initialVersion, $finalVersion, $versionItems);
$prefix = $optionStack->getPrefixOption($optionStack->getOption('tableName'));
foreach ($versionsBetween as $versionItem) {

// If we are rolling back, we skip migrating when initialVersion is the same as current
Expand All @@ -256,19 +272,28 @@ public static function run(array $options)
}

$migrationStartTime = date("Y-m-d H:i:s");

// Directory depends on Forward or Back Migration
$iterator = new DirectoryIterator(
$migrationsDir . DIRECTORY_SEPARATOR . (ModelMigration::DIRECTION_BACK === $direction ? $initialVersion->getVersion() : $versionItem->getVersion())
);

if ($optionStack->getOption('tableName') === '@') {
// Directory depends on Forward or Back Migration
$iterator = new DirectoryIterator(
$migrationsDir . DIRECTORY_SEPARATOR . (ModelMigration::DIRECTION_BACK === $direction ? $initialVersion->getVersion() : $versionItem->getVersion())
);
foreach ($iterator as $fileInfo) {
if (!$fileInfo->isFile() || 0 !== strcasecmp($fileInfo->getExtension(), 'php')) {
continue;
}
ModelMigration::migrate($initialVersion, $versionItem, $fileInfo->getBasename('.php'));
}
} else {
ModelMigration::migrate($initialVersion, $versionItem, $optionStack->getOption('tableName'));
if (!empty($prefix)) {
$optionStack->setOption('tableName', $listTables->listTablesForPrefix($prefix, $iterator));
}

$tables = explode(',', $optionStack->getOption('tableName'));
foreach ($tables as $tableName) {
ModelMigration::migrate($initialVersion, $versionItem, $tableName);
}
}

if (ModelMigration::DIRECTION_FORWARD == $direction) {
Expand Down
10 changes: 10 additions & 0 deletions scripts/Phalcon/Mvc/Model/Migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -894,4 +894,14 @@ function ($value) {
fclose($batchHandler);
self::$_connection->commit();
}

/**
* Get db connection
*
* @return \Phalcon\Db\AdapterInterface
*/
public function getConnection()
{
return self::$_connection;
}
}
68 changes: 68 additions & 0 deletions scripts/Phalcon/Mvc/Model/Migration/TableAware/ListTablesDb.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

/*
+------------------------------------------------------------------------+
| Phalcon Developer Tools |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2017 Phalcon Team (https://www.phalconphp.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file LICENSE.txt. |
| |
| If you did not receive a copy of the license and are unable to |
| obtain it through the world-wide-web, please send an email |
| to [email protected] so we can send you a copy immediately. |
+------------------------------------------------------------------------+
| Authors: Sergii Svyrydenko <[email protected]> |
+------------------------------------------------------------------------+
*/

namespace Phalcon\Mvc\Model\Migration\TableAware;

use Phalcon\Mvc\Model\Migration\TableAware\ListTablesInterface;
use InvalidArgumentException;
use Phalcon\Mvc\Model\Migration as ModelMigration;
use Phalcon\Db\Exception as DbException;

/**
* Phalcon\Mvc\Model\Migration\TableAware\ListTablesDb
*
* @package Phalcon\Mvc\Model\Migration\TableAware
*/
class ListTablesDb implements ListTablesInterface
{
/**
* Get table names with prefix for running migration
*
* @param string $tablePrefix
* @param \DirectoryIterator $iterator
* @return string
*/
public function listTablesForPrefix($tablePrefix, \DirectoryIterator $iterator = null)
{
if (empty($tablePrefix)) {
throw new InvalidArgumentException("Parameters weren't defined in " . __METHOD__);
}

$modelMigration = new ModelMigration();
$connection = $modelMigration->getConnection();

$tablesList = $connection->listTables();
if (empty($tablesList)) {
return '';
}

$strlen = strlen($tablePrefix);
foreach ($tablesList as $key => $value) {
if (substr($value, 0, $strlen) != $tablePrefix) {
unset($tablesList[$key]);
}
}

if (empty($tablesList)) {
throw new DbException("Specified table prefix doesn't match with any table name");
}

return implode(',', $tablesList);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/*
+------------------------------------------------------------------------+
| Phalcon Developer Tools |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2017 Phalcon Team (https://www.phalconphp.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file LICENSE.txt. |
| |
| If you did not receive a copy of the license and are unable to |
| obtain it through the world-wide-web, please send an email |
| to [email protected] so we can send you a copy immediately. |
+------------------------------------------------------------------------+
| Authors: Sergii Svyrydenko <[email protected]> |
+------------------------------------------------------------------------+
*/

namespace Phalcon\Mvc\Model\Migration\TableAware;

/**
* Phalcon\Mvc\Model\Migration\TableAware\ListTablesInterface
*
* @package Phalcon\Mvc\Model\Migration\TableAware
*/
interface ListTablesInterface
{
/**
* Get list table from prefix
*
* @param string $tablePrefix Table prefix
* @param \DirectoryIterator $iterator
* @return string
*/
public function listTablesForPrefix($tablePrefix, \DirectoryIterator $iterator = null);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/*
+------------------------------------------------------------------------+
| Phalcon Developer Tools |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2017 Phalcon Team (https://www.phalconphp.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file LICENSE.txt. |
| |
| If you did not receive a copy of the license and are unable to |
| obtain it through the world-wide-web, please send an email |
| to [email protected] so we can send you a copy immediately. |
+------------------------------------------------------------------------+
| Authors: Sergii Svyrydenko <[email protected]> |
+------------------------------------------------------------------------+
*/

namespace Phalcon\Mvc\Model\Migration\TableAware;

use Phalcon\Mvc\Model\Migration\TableAware\ListTablesInterface;
use DirectoryIterator;
use InvalidArgumentException;

/**
* Phalcon\Mvc\Model\Migration\TableAware\ListTablesIterator
*
* @package Phalcon\Mvc\Model\Migration\TableAware
*/
class ListTablesIterator implements ListTablesInterface
{
/**
* Get table names with prefix for running migration
*
* @param string $tablePrefix
* @param DirectoryIterator $iterator
* @return string
*/
public function listTablesForPrefix($tablePrefix, DirectoryIterator $iterator = null)
{
if (empty($tablePrefix) || empty($iterator)) {
throw new InvalidArgumentException("Parameters weren't defined in " . __METHOD__);
}

$strlen = strlen($tablePrefix);
$fileNames = [];
foreach ($iterator as $fileInfo) {
if (substr($fileInfo->getFilename(), 0, $strlen) == $tablePrefix) {
$file = explode('.', $fileInfo->getFilename());
$fileNames[] = $file[0];
}
}

$fileNames = array_unique($fileNames);
// natsort($fileNames);

return implode(',', $fileNames);
}
}
Loading

0 comments on commit a3c5a12

Please sign in to comment.