From 257d7871e9d0d6a4f2cea3785e52f6cd640d05b0 Mon Sep 17 00:00:00 2001 From: songchenxuan Date: Thu, 8 Jun 2023 10:53:40 +0800 Subject: [PATCH] * Convert database all date and datetime field 0000-00-00 to null and add default null. --- module/upgrade/model.php | 41 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/module/upgrade/model.php b/module/upgrade/model.php index d905fac7f6..fe0d2efa93 100644 --- a/module/upgrade/model.php +++ b/module/upgrade/model.php @@ -634,6 +634,8 @@ class upgradeModel extends model if($this->config->edition != 'open') $this->processDataset(); $this->setURSwitchStatus($fromVersion); break; + case '18_4_beta1': + $this->updateDateFields(); } $this->deletePatch(); @@ -9221,6 +9223,45 @@ class upgradeModel extends model return $fieldSettings; } + /** + * Update database all tables date and datetime fields 0000-00-00 0000-00-00 00:00:00 to null. + * + * @access public + * @return bool + */ + public function updateDateFields() + { + $suffixSQL = "FROM information_schema.columns "; + $suffixSQL .= "WHERE TABLE_SCHEMA = '{$this->config->db->name}' "; + $suffixSQL .= "AND ((DATA_TYPE = 'date') OR (DATA_TYPE = 'datetime')) "; + $suffixSQL .= "AND TABLE_NAME NOT IN (SELECT TABLE_NAME FROM information_schema.VIEWS WHERE TABLE_SCHEMA = '{$this->config->db->name}') "; + $suffixSQL .= "ORDER BY table_name, ordinal_position;"; + + $updateSQL = "SELECT CONCAT('UPDATE `', table_name, '` SET `', column_name, '` = NULL WHERE `', column_name, '` = \'0000-00-00\' OR `', column_name, '` = \'0000-00-00 00:00:00\';') AS statement "; + $updateSQL .= $suffixSQL; + $alterSQL = "SELECT CONCAT('ALTER TABLE `', table_name, '` MODIFY `', column_name, '` ', column_type, ' NULL;') AS statement "; + $alterSQL .= $suffixSQL; + + $updateResult = $this->dbh->query($updateSQL)->fetchAll(); + $alterResult = $this->dbh->query($alterSQL)->fetchAll(); + + $execUpdate = ''; + $execAlter = ''; + foreach($updateResult as $update) + { + $execUpdate .= $update->statement; + } + foreach($alterResult as $alter) + { + /* zt_burn use PRIMARY KEY (`execution`, `date`, `task`) and date field NOT NULL, continue. */ + if(strpos($alter->statement, '_burn') !== false) continue; + $execAlter .= $alter->statement; + } + + $this->dbh->exec($execUpdate); + $this->dbh->exec($execAlter); + } + /** * Convert doc collect. *