Skip to content

Instantly share code, notes, and snippets.

@esimonetti
Last active October 30, 2019 08:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esimonetti/0db5816ade99843736366045da20ba97 to your computer and use it in GitHub Desktop.
Save esimonetti/0db5816ade99843736366045da20ba97 to your computer and use it in GitHub Desktop.
Create missing tables - USE TOOTHPASTE INSTEAD! https://github.com/esimonetti/toothpaste
<?php
// Enrico Simonetti
// enricosimonetti.com
//
// 2019-09-24 Sugar 9.0.0
// create missing tables
function usage($error = '') {
if (!empty($error)) print(PHP_EOL . 'Error: ' . $error . PHP_EOL);
print(' php ' . __FILE__ . ' --instance /full/path' . PHP_EOL);
exit(1);
}
// only allow CLI
$sapi_type = php_sapi_name();
if (substr($sapi_type, 0, 3) != 'cli') {
die(__FILE__ . ' is CLI only.');
}
// get command line params
$o = getopt('', array('instance:'));
if (!$o) usage();
// find directory
if (!empty($o['instance']) && is_dir($o['instance'])) {
print('Debug: Entering directory ' . $o['instance'] . PHP_EOL);
chdir($o['instance']);
} else {
chdir(dirname(__FILE__));
}
if (!file_exists('config.php') || !file_exists('sugar_version.php')) {
usage('The provided directory is not a Sugar system');
}
// sugar basic setup
define('sugarEntry', true);
require_once('include/entryPoint.php');
// retrieve all dictionaries for later use
$dictionaries = glob('metadata/*.php');
$retrievedDictionary = [];
if (!empty($dictionaries)) {
foreach ($dictionaries as $dictionaryFile) {
require($dictionaryFile);
$retrievedDictionary = array_merge($retrievedDictionary, $dictionary);
}
}
require('modules/OutboundEmail/vardefs.php');
$retrievedDictionary = array_merge($retrievedDictionary, $dictionary);
if (extension_loaded('xdebug')) {
echo 'Xdebug is enabled on this system. It is highly recommended to disable Xdebug on PHP CLI before running this script. Xdebug will cause unwanted slowness.'.PHP_EOL;
}
if (empty($current_language)) {
$current_language = $sugar_config['default_language'];
}
$app_list_strings = return_app_list_strings_language($current_language);
$app_strings = return_application_language($current_language);
$mod_strings = return_module_language($current_language, 'Administration');
$start_time = microtime(true);
$db = DBManagerFactory::getInstance();
if (!empty($retrievedDictionary)) {
foreach ($retrievedDictionary as $key => $dictionaryContent) {
if (!empty($dictionaryContent['table']) && !$db->tableExists($dictionaryContent['table']) && !empty($dictionaryContent['fields']) && !empty($dictionaryContent['indices'])) {
echo 'Repairing ' . $dictionaryContent['table'] . PHP_EOL;
$db->repairTableParams($dictionaryContent['table'], $dictionaryContent['fields'], $dictionaryContent['indices']);
}
}
}
// now all the modules
global $beanList;
$fullModuleList = array_merge($beanList, $app_list_strings['moduleList']);
asort($fullModuleList);
foreach ($fullModuleList as $module => $label) {
$bean = BeanFactory::newBean($module);
if ($bean instanceof SugarBean) {
$table = $bean->getTableName();
if (!empty($table) && !$db->tableExists($table)) {
// execute creation
echo 'Detected missing table. Creating ' . $table . PHP_EOL;
$db->createTable($bean);
}
}
}
global $current_user, $dictionary;
$current_user = BeanFactory::newBean('Users');
$current_user->getSystemUser();
include 'include/modules.php';
$rac = new RepairAndClear();
$rac->execute = true;
$rac->clearVardefs();
$rac->rebuildExtensions();
$rac->clearExternalAPICache();
$rac->setStatementObserver(function (?string $statement) : void {
echo 'Running sql: ' . $statement . PHP_EOL;
});
$rac->repairDatabase();
$_REQUEST['silent'] = true;
include('modules/Administration/RebuildRelationship.php');
// repair
$rac->repairAndClearAll(array('clearAll'), array($mod_strings['LBL_ALL_MODULES']), true, false, '');
// when the other register shutdown functionalities complete, exit this script
register_shutdown_function(
function ($start) {
print('Script completed in ' . (int)(microtime(true) - $start) . ' seconds.' . PHP_EOL);
},
$start_time
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment