forked from phalcon/phalcon-devtools
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request phalcon#1117 from sergeysviridenko/3.2.table_prefix
Added table prefix to generate/run migration
- Loading branch information
Showing
16 changed files
with
626 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
scripts/Phalcon/Mvc/Model/Migration/TableAware/ListTablesDb.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
scripts/Phalcon/Mvc/Model/Migration/TableAware/ListTablesInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
60 changes: 60 additions & 0 deletions
60
scripts/Phalcon/Mvc/Model/Migration/TableAware/ListTablesIterator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.