Skip to content

Commit

Permalink
Added Database Updater Frame
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Uhl committed Aug 8, 2016
1 parent 9bf2011 commit e45160c
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 0 deletions.
91 changes: 91 additions & 0 deletions administrator/components/com_admin/script.php
Expand Up @@ -39,6 +39,7 @@ public function update($installer)
$this->updateAssets();
$this->clearStatsCache();
$this->convertTablesToUtf8mb4(true);
$this->convertTablesToNewNullDate();
$this->cleanJoomlaCache();

// VERY IMPORTANT! THIS METHOD SHOULD BE CALLED LAST, SINCE IT COULD
Expand Down Expand Up @@ -1683,6 +1684,96 @@ public function flushSessions()
return true;
}


/**
* Converts the site's database tables to the new null Date of mySQL 5.7. Can also be used as reverter
*
* @param string $oldNull the old null date string
* @param string $newNull the new null date string
*
* @return void
*
* @since 12.2
*/
public function convertTablesToNewNullDate($newNull=null,$oldNull=null)
{
$db = JFactory::getDbo();

//Possible Convert Back
if($db->serverUsesNewNullTime())
{
if(is_null($newNull))
$newNull='1000-01-01 00:00:00';

if(is_null($oldNull))
$oldNull='0000-00-00 00:00:00';
} else
{
if(is_null($oldNull))
$oldNull='1000-01-01 00:00:00';

if(is_null($newNull))
$newNull='0000-00-00 00:00:00';
}

// This is only required for MySQL databases
$serverType = $db->getServerType();

if ($serverType != 'mysql')
{
return;
}

// Check conversion status in database
$db->setQuery('SELECT ' . $db->quoteName('converted')
. ' FROM ' . $db->quoteName('#__nullDate_conversion')
);

$convertedDB = '0000-00-00 00:00:00';

try
{
$convertedDB = $db->loadResult();
}
catch (Exception $e)
{
// Render the error message from the Exception object
JFactory::getApplication()->enqueueMessage($e->getMessage(), 'error');
return;
}

if($convertedDB == $newNull)
{
//Already updated - nothing to do
return;
}

$fileName = JPATH_ROOT . "/administrator/components/com_admin/sql/others/mysql/nullDate-conversion.sql";

if (is_file($fileName))
{
$fileContents = @file_get_contents($fileName);
$queries = $db->splitSql($fileContents);

if (!empty($queries))
{
foreach ($queries as $query)
{
$query = str_replace(array('#O#','#T#'), array($oldNull,$newNull), $query);

try
{
$db->setQuery($query)->execute();
}
catch (Exception $e)
{
JFactory::getApplication()->enqueueMessage($e->getMessage(), 'error');
}
}
}
}
}

/**
* Converts the site's database tables to support UTF-8 Multibyte.
*
Expand Down
@@ -0,0 +1,2 @@
ALTER TABLE `#__nullDate_conversion` CHANGE `converted` `converted` datetime NOT NULL DEFAULT #T#;
UPDATE `#__nullDate_conversion` SET converted = #T#;
@@ -0,0 +1,13 @@
--
-- Table structure for table `#__nullDate_conversion`
--

CREATE TABLE IF NOT EXISTS `#__nullDate_conversion` (
`converted` datetime NOT NULL DEFAULT '0000-00-00 00:00:00'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 DEFAULT COLLATE=utf8mb4_unicode_ci;

--
-- Dumping data for table `#__nullDate_conversion`
--

INSERT INTO `#__nullDate_conversion` (`converted`) VALUES ('0000-00-00 00:00:00');
15 changes: 15 additions & 0 deletions installation/sql/mysql/joomla.sql
Expand Up @@ -1969,6 +1969,21 @@ CREATE TABLE IF NOT EXISTS `#__utf8_conversion` (

INSERT INTO `#__utf8_conversion` (`converted`) VALUES (0);

--
-- Table structure for table `#__nullDate_conversion`
--

CREATE TABLE IF NOT EXISTS `#__nullDate_conversion` (
`converted` datetime NOT NULL DEFAULT '0000-00-00 00:00:00'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 DEFAULT COLLATE=utf8mb4_unicode_ci;

--
-- Dumping data for table `#__nullDate_conversion`
--

INSERT INTO `#__nullDate_conversion` (`converted`) VALUES ('0000-00-00 00:00:00');


--
-- Table structure for table `#__viewlevels`
--
Expand Down

0 comments on commit e45160c

Please sign in to comment.