Skip to content

Commit

Permalink
Bug #152494 fix: If there is some error in the migration script then …
Browse files Browse the repository at this point in the history
…the script is executed multiplke times
  • Loading branch information
ankush-maherwal committed Oct 31, 2019
1 parent 54b5ae6 commit f151e0e
Showing 1 changed file with 49 additions and 32 deletions.
81 changes: 49 additions & 32 deletions administrator/houseKeeping/1.5.0/fieldDefaultValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,48 +32,65 @@ class TjHouseKeepingFieldDefaultValue extends TjModelHouseKeeping
*/
public function migrate()
{
$db = JFactory::getDbo();
JLoader::import('components.com_tjfields.tables.option', JPATH_ADMINISTRATOR);
$optionTable = Table::getInstance('Option', 'TjfieldsTable', array('dbo', $db));
$optionTableColumns = $optionTable->getFields();
$result = array();

// No migration needed if the default_option column is already removed
if (!array_key_exists('default_option', $optionTableColumns))
try
{
return true;
}
$db = JFactory::getDbo();
JLoader::import('components.com_tjfields.tables.option', JPATH_ADMINISTRATOR);
$optionTable = Table::getInstance('Option', 'TjfieldsTable', array('dbo', $db));
$optionTableColumns = $optionTable->getFields();

$query = $db->getQuery(true);
$query->select('*');
$query->from($db->quoteName('#__tjfields_options'));
$query->where($db->quoteName('default_option') . '=1');
$db->setQuery($query);
$fieldOptions = $db->loadObjectList();
// No migration needed if the default_option column is already removed
if (!array_key_exists('default_option', $optionTableColumns))
{
$result['status'] = true;
$result['message'] = "Migration successful";

if (!empty($fieldOptions))
{
JLoader::import('components.com_tjfields.tables.field', JPATH_ADMINISTRATOR);
$fieldTable = JTable::getInstance('Field', 'TjfieldsTable', array('dbo', $db));
return $result;
}

$query = $db->getQuery(true);
$query->select('*');
$query->from($db->quoteName('#__tjfields_options'));
$query->where($db->quoteName('default_option') . '=1');
$db->setQuery($query);
$fieldOptions = $db->loadObjectList();

foreach ($fieldOptions as $fieldOption)
if (!empty($fieldOptions))
{
if (!empty($fieldOption->default_option))
JLoader::import('components.com_tjfields.tables.field', JPATH_ADMINISTRATOR);
$fieldTable = JTable::getInstance('Field', 'TjfieldsTable', array('dbo', $db));

foreach ($fieldOptions as $fieldOption)
{
$fieldTable->load($fieldOption->field_id);
$fieldParams = json_decode($fieldTable->params);
$fieldParams->default = $fieldOption->value;
$fieldTable->params = json_encode($fieldParams);
$fieldTable->store();
if (!empty($fieldOption->default_option))
{
$fieldTable->load($fieldOption->field_id);
$fieldParams = json_decode($fieldTable->params);
$fieldParams->default = $fieldOption->value;
$fieldTable->params = json_encode($fieldParams);
$fieldTable->store();
}
}
}
}

// Drop 'default_option' column from options table
$query = $db->getQuery(true);
$query = "ALTER TABLE `#__tjfields_options` DROP `default_option`";
$db->setQuery($query);
$db->execute();
// Drop 'default_option' column from options table
$query = $db->getQuery(true);
$query = "ALTER TABLE `#__tjfields_options` DROP `default_option`";
$db->setQuery($query);
$db->execute();

$result['status'] = true;
$result['message'] = "Migration successful";
}
catch (Exception $e)
{
$result['err_code'] = '';
$result['status'] = false;
$result['message'] = $e->getMessage();
}

return true;
return $result;
}
}

0 comments on commit f151e0e

Please sign in to comment.