Skip to content

Commit

Permalink
Merge pull request #92 from geekwright/issue_85
Browse files Browse the repository at this point in the history
Handle new table properly in Migrate:getCurrentSchema()
  • Loading branch information
geekwright authored Apr 13, 2022
2 parents 13c51b1 + 8aa4af3 commit 8987315
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pr_tests.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: CI

on: [push]
on: [push, pull_request]

jobs:
phpunit:
Expand Down
21 changes: 17 additions & 4 deletions src/Database/Migrate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
Expand Down
39 changes: 33 additions & 6 deletions src/Database/Tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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'],
Expand Down

0 comments on commit 8987315

Please sign in to comment.