From ba5a18dd82a8579b52cacb58a8d491e6fd3ef10a Mon Sep 17 00:00:00 2001 From: geekwright Date: Wed, 13 Apr 2022 00:07:22 -0500 Subject: [PATCH 1/3] Handle new table properly in Migrate:getCurrentSchema() If table was identified in $modversion['tables'] in xoops_version.php, but did not exist in the database, schema was not applied to the new table. This is related to the errors mentioned in #85 Also includes fixes for: - schema errors for column default by MySQL function (i.e. CURRENT_TIMESTAMP) - tweaks to generated schema file names for new module versioning - minor docblock cleanups --- src/Database/Migrate.php | 21 +++++++++++++++++---- src/Database/Tables.php | 39 +++++++++++++++++++++++++++++++++------ 2 files changed, 50 insertions(+), 10 deletions(-) diff --git a/src/Database/Migrate.php b/src/Database/Migrate.php index 08dc3e5..b775383 100644 --- a/src/Database/Migrate.php +++ b/src/Database/Migrate.php @@ -27,8 +27,8 @@ * @category Xmf\Database\Migrate * @package Xmf * @author Richard Griffith - * @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(); diff --git a/src/Database/Tables.php b/src/Database/Tables.php index 639ae65..2dbd579 100644 --- a/src/Database/Tables.php +++ b/src/Database/Tables.php @@ -27,8 +27,8 @@ * @category Xmf\Database\Tables * @package Xmf * @author Richard Griffith - * @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'], From ef52d24f180321a7618aaa0dfbb4d45ee4310ce7 Mon Sep 17 00:00:00 2001 From: geekwright Date: Wed, 13 Apr 2022 00:26:55 -0500 Subject: [PATCH 2/3] add on pull_request --- .github/workflows/pr_tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pr_tests.yml b/.github/workflows/pr_tests.yml index 6020a66..706d683 100644 --- a/.github/workflows/pr_tests.yml +++ b/.github/workflows/pr_tests.yml @@ -1,6 +1,6 @@ name: CI -on: [push] +on: [push pull_request] jobs: phpunit: From 8aa4af3b8cc487efa4caf003197612083f69bad2 Mon Sep 17 00:00:00 2001 From: geekwright Date: Wed, 13 Apr 2022 00:41:39 -0500 Subject: [PATCH 3/3] add on pull_request --- .github/workflows/pr_tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pr_tests.yml b/.github/workflows/pr_tests.yml index 706d683..cb58324 100644 --- a/.github/workflows/pr_tests.yml +++ b/.github/workflows/pr_tests.yml @@ -1,6 +1,6 @@ name: CI -on: [push pull_request] +on: [push, pull_request] jobs: phpunit: