-
Notifications
You must be signed in to change notification settings - Fork 6
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 #92 from geekwright/issue_85
Handle new table properly in Migrate:getCurrentSchema()
- Loading branch information
Showing
3 changed files
with
51 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
name: CI | ||
|
||
on: [push] | ||
on: [push, pull_request] | ||
|
||
jobs: | ||
phpunit: | ||
|
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 |
---|---|---|
|
@@ -27,8 +27,8 @@ | |
* @category Xmf\Database\Migrate | ||
* @package Xmf | ||
* @author Richard Griffith <[email protected]> | ||
* @copyright 2018 XOOPS Project (https://xoops.org) | ||
* @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html) | ||
* @copyright 2018-2022 XOOPS Project (https://xoops.org) | ||
* @license GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html) | ||
* @link https://xoops.org | ||
*/ | ||
class Migrate | ||
|
@@ -68,7 +68,18 @@ public function __construct($dirname) | |
if (empty($this->moduleTables)) { | ||
throw new \RuntimeException("No tables established in module"); | ||
} | ||
$version = $module->getInfo('version'); | ||
|
||
$version = preg_replace_callback( | ||
'/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/', | ||
function ($match) { | ||
$semver = $match[1] . '_' . $match[2] . '_' .$match[3]; | ||
if (!empty($match[4])) { | ||
$semver .= '_' . substr($match[4], 0, 8); | ||
} | ||
return $semver; | ||
}, | ||
$module->getInfo('version')); | ||
|
||
$this->tableDefinitionFile = $this->helper->path("sql/{$dirname}_{$version}_migrate.yml"); | ||
$this->tableHandler = new Tables(); | ||
} | ||
|
@@ -103,7 +114,9 @@ public function saveCurrentSchema() | |
public function getCurrentSchema() | ||
{ | ||
foreach ($this->moduleTables as $tableName) { | ||
$this->tableHandler->useTable($tableName); | ||
if (false === $this->tableHandler->useTable($tableName)) { | ||
$this->tableHandler->addTable($tableName); | ||
} | ||
} | ||
|
||
return $this->tableHandler->dumpTables(); | ||
|
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 |
---|---|---|
|
@@ -27,8 +27,8 @@ | |
* @category Xmf\Database\Tables | ||
* @package Xmf | ||
* @author Richard Griffith <[email protected]> | ||
* @copyright 2011-2018 XOOPS Project (https://xoops.org) | ||
* @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html) | ||
* @copyright 2011-2022 XOOPS Project (https://xoops.org) | ||
* @license GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html) | ||
* @link https://xoops.org | ||
*/ | ||
class Tables | ||
|
@@ -281,7 +281,7 @@ public function useTable($table) | |
* @param string $table table containing the column | ||
* @param string $column column to alter | ||
* | ||
* @return string|bool attribute string, or false if error encountered | ||
* @return string|false attribute string, or false if error encountered | ||
*/ | ||
public function getColumnAttributes($table, $column) | ||
{ | ||
|
@@ -304,7 +304,7 @@ public function getColumnAttributes($table, $column) | |
* | ||
* @param string $table get indexes for this named table | ||
* | ||
* @return array|bool array of indexes, or false if error encountered | ||
* @return array|false array of indexes, or false if error encountered | ||
*/ | ||
public function getTableIndexes($table) | ||
{ | ||
|
@@ -804,6 +804,32 @@ protected function fetch($result) | |
return $this->db->fetchArray($result); | ||
} | ||
|
||
/** | ||
* create default value clause for DDL | ||
* | ||
* @param string|null $default the default value to be quoted | ||
* | ||
* @return string the correctly quoted default value | ||
*/ | ||
protected function quoteDefaultClause($default) | ||
{ | ||
// . (($column['COLUMN_DEFAULT'] === null) ? '' : " DEFAULT '" . $column['COLUMN_DEFAULT'] . "' ") | ||
// no default specified | ||
if (null===$default) { | ||
return ''; | ||
} | ||
|
||
// functions should not be quoted | ||
// this section will need expanded when XOOPS minimum is no longer a mysql 5 version | ||
// Until mysql 8, only allowed function is CURRENT_TIMESTAMP | ||
if ($default === 'CURRENT_TIMESTAMP') { | ||
return ' DEFAULT CURRENT_TIMESTAMP '; | ||
} | ||
|
||
// surround default with quotes | ||
return " DEFAULT '{$default}' "; | ||
} | ||
|
||
/** | ||
* get table definition from INFORMATION_SCHEMA | ||
* | ||
|
@@ -846,8 +872,9 @@ protected function getTable($table) | |
while ($column = $this->fetch($result)) { | ||
$attributes = ' ' . $column['COLUMN_TYPE'] . ' ' | ||
. (($column['IS_NULLABLE'] === 'NO') ? ' NOT NULL ' : '') | ||
. (($column['COLUMN_DEFAULT'] === null) ? '' : " DEFAULT '" . $column['COLUMN_DEFAULT'] . "' ") | ||
. $column['EXTRA']; | ||
. $this->quoteDefaultClause($column['COLUMN_DEFAULT']) | ||
//. $column['EXTRA']; | ||
. str_replace('DEFAULT_GENERATED ', '', $column['EXTRA']); | ||
|
||
$columnDef = array( | ||
'name' => $column['COLUMN_NAME'], | ||
|