Fix constraint and loading on module activation.

This commit is contained in:
Laurent Destailleur 2021-12-07 14:14:13 +01:00
parent b238419020
commit 0e7d23e169
5 changed files with 49 additions and 13 deletions

View File

@ -1049,15 +1049,19 @@ class DolibarrModules // Can not be abstract, because we need to instantiate it
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps,PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
/**
* Create tables and keys required by module.
* Files module.sql and module.key.sql with create table and create keys
* commands must be stored in directory reldir='/module/sql/'
* This function is called by this->init
* Create tables and keys required by module:
* - Files module.sql files with create table instructions
* - Then files modules.key.sql with create keys instructions
* - Then data_xxx.sql (usualy provided by external modules only)
* - Then update_xxx.sql (usualy provided by external modules only)
* Files must be stored in directory defined by reldir (Example: '/install/mysql/tables' or '/module/sql/')
* This function is usually called by the this->init of module descriptors.
*
* @param string $reldir Relative directory where to scan files
* @return int <=0 if KO, >0 if OK
* @param string $reldir Relative directory where to scan files. Example: '/install/mysql/tables' or '/module/sql/'
* @param string $onlywithsuffix Only with the defined suffix
* @return int <=0 if KO, >0 if OK
*/
protected function _load_tables($reldir)
protected function _load_tables($reldir, $onlywithsuffix = '')
{
// phpcs:enable
global $conf;
@ -1088,6 +1092,14 @@ class DolibarrModules // Can not be abstract, because we need to instantiate it
}
sort($files);
foreach ($files as $file) {
if ($onlywithsuffix) {
if (!preg_match('/\-'.preg_quote($onlywithsuffix, '/').'\./i', $file)) {
//print 'File '.$file.' does not match suffix '.$onlywithsuffix.' so it is discarded<br>'."\n";
continue;
} else {
//print 'File '.$file.' match suffix '.$onlywithsuffix.' so we keep it<br>'."\n";
}
}
if (preg_match('/\.sql$/i', $file) && !preg_match('/\.key\.sql$/i', $file) && substr($file, 0, 4) == 'llx_' && substr($file, 0, 4) != 'data') {
$result = run_sql($dir.$file, empty($conf->global->MAIN_DISPLAY_SQL_INSTALL_LOG) ? 1 : 0, '', 1);
if ($result <= 0) {
@ -1105,6 +1117,14 @@ class DolibarrModules // Can not be abstract, because we need to instantiate it
}
sort($files);
foreach ($files as $file) {
if ($onlywithsuffix) {
if (!preg_match('/\-'.preg_quote($onlywithsuffix, '/').'\./i', $file)) {
//print 'File '.$file.' does not match suffix '.$onlywithsuffix.' so it is discarded<br>'."\n";
continue;
} else {
//print 'File '.$file.' match suffix '.$onlywithsuffix.' so we keep it<br>'."\n";
}
}
if (preg_match('/\.key\.sql$/i', $file) && substr($file, 0, 4) == 'llx_' && substr($file, 0, 4) != 'data') {
$result = run_sql($dir.$file, empty($conf->global->MAIN_DISPLAY_SQL_INSTALL_LOG) ? 1 : 0, '', 1);
if ($result <= 0) {
@ -1122,6 +1142,14 @@ class DolibarrModules // Can not be abstract, because we need to instantiate it
}
sort($files);
foreach ($files as $file) {
if ($onlywithsuffix) {
if (!preg_match('/\-'.preg_quote($onlywithsuffix, '/').'\./i', $file)) {
//print 'File '.$file.' does not match suffix '.$onlywithsuffix.' so it is discarded<br>'."\n";
continue;
} else {
//print 'File '.$file.' match suffix '.$onlywithsuffix.' so we keep it<br>'."\n";
}
}
if (preg_match('/\.sql$/i', $file) && !preg_match('/\.key\.sql$/i', $file) && substr($file, 0, 4) == 'data') {
$result = run_sql($dir.$file, empty($conf->global->MAIN_DISPLAY_SQL_INSTALL_LOG) ? 1 : 0, '', 1);
if ($result <= 0) {
@ -1139,6 +1167,14 @@ class DolibarrModules // Can not be abstract, because we need to instantiate it
}
sort($files);
foreach ($files as $file) {
if ($onlywithsuffix) {
if (!preg_match('/\-'.preg_quote($onlywithsuffix, '/').'\./i', $file)) {
//print 'File '.$file.' does not match suffix '.$onlywithsuffix.' so it is discarded<br>'."\n";
continue;
} else {
//print 'File '.$file.' match suffix '.$onlywithsuffix.' so we keep it<br>'."\n";
}
}
if (preg_match('/\.sql$/i', $file) && !preg_match('/\.key\.sql$/i', $file) && substr($file, 0, 6) == 'update') {
$result = run_sql($dir.$file, empty($conf->global->MAIN_DISPLAY_SQL_INSTALL_LOG) ? 1 : 0, '', 1);
if ($result <= 0) {

View File

@ -56,7 +56,7 @@ class modHRM extends DolibarrModules
// Module description, used if translation string 'ModuleXXXDesc' not found (where XXX is value of numeric property 'numero' of module)
$this->description = "HRM";
// Possible values for version are: 'development', 'experimental', 'dolibarr' or version
$this->version = 'development';
$this->version = 'experimental';
// Key used in llx_const table to save module status enabled/disabled (where MYMODULE is value of property name of module in uppercase)
$this->const_name = 'MAIN_MODULE_'.strtoupper($this->name);
// Name of image file used for this module.
@ -268,10 +268,10 @@ class modHRM extends DolibarrModules
// Permissions
$this->remove($options);
/*$result = $this->_load_tables('/hrm/sql/');
$result = $this->_load_tables('/install/mysql/tables/', 'hrm');
if ($result < 0) {
return -1; // Do not activate module if error 'not allowed' returned when loading module SQL queries (the _load_table run sql with run_sql with the error allowed parameter set to 'default')
}*/
}
$sql = array();

View File

@ -25,5 +25,5 @@ ALTER TABLE llx_hrm_evaluationdet ADD INDEX idx_hrm_evaluationdet_fk_evaluation
--ALTER TABLE llx_hrm_evaluationdet ADD UNIQUE INDEX uk_hrm_evaluationdet_fieldxy(fieldx, fieldy);
--ALTER TABLE llx_hrm_evaluationdet ADD CONSTRAINT llx_hrm_evaluationdet_fk_field FOREIGN KEY (fk_field) REFERENCES llx_hrm_myotherobject(rowid);
ALTER TABLE llx_hrm_evaluationdet ADD CONSTRAINT llx_hrm_evaluationdet_fk_evaluation FOREIGN KEY (fk_evaluation) REFERENCES llx_hrm_evaluation(rowid);

View File

@ -23,5 +23,5 @@ ALTER TABLE llx_hrm_skilldet ADD CONSTRAINT llx_hrm_skilldet_fk_user_creat FOREI
--ALTER TABLE llx_hrm_skilldet ADD UNIQUE INDEX uk_hrm_skilldet_fieldxy(fieldx, fieldy);
--ALTER TABLE llx_hrm_skilldet ADD CONSTRAINT llx_hrm_skilldet_fk_field FOREIGN KEY (fk_field) REFERENCES llx_hrm_myotherobject(rowid);
ALTER TABLE llx_hrm_skilldet ADD CONSTRAINT llx_hrm_skilldet_fk_skill FOREIGN KEY (fk_skill) REFERENCES llx_hrm_skill(rowid);

View File

@ -23,5 +23,5 @@ ALTER TABLE llx_hrm_skillrank ADD CONSTRAINT llx_hrm_skillrank_fk_user_creat FOR
--ALTER TABLE llx_hrm_skillrank ADD UNIQUE INDEX uk_hrm_skillrank_fieldxy(fieldx, fieldy);
--ALTER TABLE llx_hrm_skillrank ADD CONSTRAINT llx_hrm_skillrank_fk_field FOREIGN KEY (fk_field) REFERENCES llx_hrm_myotherobject(rowid);
ALTER TABLE llx_hrm_skillrank ADD CONSTRAINT llx_hrm_skillrank_fk_skill FOREIGN KEY (fk_skill) REFERENCES llx_hrm_skill(rowid);