From b6a061f79581dc7c7ae33c76b9170bbbb5e02af0 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Mon, 26 Jun 2023 23:02:17 +0200 Subject: [PATCH] Debug v18 - warnings and pgsql migration --- htdocs/core/class/conf.class.php | 2 +- htdocs/core/lib/admin.lib.php | 33 +++++++++++-------- htdocs/core/modules/DolibarrModules.class.php | 6 ++++ htdocs/core/modules/modPrinting.class.php | 2 +- .../install/mysql/migration/7.0.0-8.0.0.sql | 4 +-- htdocs/install/pgsql/functions/functions.sql | 1 + htdocs/install/upgrade.php | 2 +- test/phpunit/PgsqlTest.php | 7 ++++ 8 files changed, 38 insertions(+), 19 deletions(-) diff --git a/htdocs/core/class/conf.class.php b/htdocs/core/class/conf.class.php index 635f19ca831..5cc8bf8b16b 100644 --- a/htdocs/core/class/conf.class.php +++ b/htdocs/core/class/conf.class.php @@ -26,10 +26,10 @@ * Config is stored into file conf.php */ - /** * Class to stock current configuration */ +#[AllowDynamicProperties] class Conf { /** diff --git a/htdocs/core/lib/admin.lib.php b/htdocs/core/lib/admin.lib.php index f03459ef08f..17b02d8b29e 100644 --- a/htdocs/core/lib/admin.lib.php +++ b/htdocs/core/lib/admin.lib.php @@ -152,20 +152,21 @@ function versiondolibarrarray() * Install process however does not use it. * Note that Sql files must have all comments at start of line. Also this function take ';' as the char to detect end of sql request * - * @param string $sqlfile Full path to sql file - * @param int $silent 1=Do not output anything, 0=Output line for update page - * @param int $entity Entity targeted for multicompany module - * @param int $usesavepoint 1=Run a savepoint before each request and a rollback to savepoint if error (this allow to have some request with errors inside global transactions). - * @param string $handler Handler targeted for menu (replace __HANDLER__ with this value) - * @param string $okerror Family of errors we accept ('default', 'none') - * @param int $linelengthlimit Limit for length of each line (Use 0 if unknown, may be faster if defined) - * @param int $nocommentremoval Do no try to remove comments (in such a case, we consider that each line is a request, so use also $linelengthlimit=0) - * @param int $offsetforchartofaccount Offset to use to load chart of account table to update sql on the fly to add offset to rowid and account_parent value - * @param int $colspan 2=Add a colspan=2 on td - * @param int $onlysqltoimportwebsite Only sql resquests used to import a website template are allowed - * @return int <=0 if KO, >0 if OK + * @param string $sqlfile Full path to sql file + * @param int $silent 1=Do not output anything, 0=Output line for update page + * @param int $entity Entity targeted for multicompany module + * @param int $usesavepoint 1=Run a savepoint before each request and a rollback to savepoint if error (this allow to have some request with errors inside global transactions). + * @param string $handler Handler targeted for menu (replace __HANDLER__ with this value) + * @param string $okerror Family of errors we accept ('default', 'none') + * @param int $linelengthlimit Limit for length of each line (Use 0 if unknown, may be faster if defined) + * @param int $nocommentremoval Do no try to remove comments (in such a case, we consider that each line is a request, so use also $linelengthlimit=0) + * @param int $offsetforchartofaccount Offset to use to load chart of account table to update sql on the fly to add offset to rowid and account_parent value + * @param int $colspan 2=Add a colspan=2 on td + * @param int $onlysqltoimportwebsite Only sql resquests used to import a website template are allowed + * @param string $database Database (replace __DATABASE__ with this value) + * @return int <=0 if KO, >0 if OK */ -function run_sql($sqlfile, $silent = 1, $entity = '', $usesavepoint = 1, $handler = '', $okerror = 'default', $linelengthlimit = 32768, $nocommentremoval = 0, $offsetforchartofaccount = 0, $colspan = 0, $onlysqltoimportwebsite = 0) +function run_sql($sqlfile, $silent = 1, $entity = 0, $usesavepoint = 1, $handler = '', $okerror = 'default', $linelengthlimit = 32768, $nocommentremoval = 0, $offsetforchartofaccount = 0, $colspan = 0, $onlysqltoimportwebsite = 0, $database = '') { global $db, $conf, $langs, $user; @@ -327,7 +328,7 @@ function run_sql($sqlfile, $silent = 1, $entity = '', $usesavepoint = 1, $handle if ($sql) { // Test if th SQL is allowed SQL if ($onlysqltoimportwebsite) { - $newsql = str_replace(array("\'"), '__BACKSLASHQUOTE__', $sql); // Replace the \' seque,ce + $newsql = str_replace(array("\'"), '__BACKSLASHQUOTE__', $sql); // Replace the \' char // Remove all strings contents including the ' so we can analyse SQL instruction only later $l = strlen($newsql); @@ -389,6 +390,10 @@ function run_sql($sqlfile, $silent = 1, $entity = '', $usesavepoint = 1, $handle $sql = preg_replace('/__HANDLER__/i', "'".$db->escape($handler)."'", $sql); } + if (!empty($database)) { + $sql = preg_replace('/__DATABASE__/i', "'".$db->escape($database)."'", $sql); + } + $newsql = preg_replace('/__ENTITY__/i', (!empty($entity) ? $entity : $conf->entity), $sql); // Add log of request diff --git a/htdocs/core/modules/DolibarrModules.class.php b/htdocs/core/modules/DolibarrModules.class.php index aa22a704700..541edd58501 100644 --- a/htdocs/core/modules/DolibarrModules.class.php +++ b/htdocs/core/modules/DolibarrModules.class.php @@ -368,6 +368,12 @@ class DolibarrModules // Can not be abstract, because we need to instantiate it */ public $warnings_activation_ext; + /** + * @var array Array of warnings to show when we disable the module + * + * array('always'='text') or array('FR'='text') + */ + public $warnings_unactivation; /** * @var array Minimum version of PHP required by module. diff --git a/htdocs/core/modules/modPrinting.class.php b/htdocs/core/modules/modPrinting.class.php index 6638d16e201..27f87ea87c4 100644 --- a/htdocs/core/modules/modPrinting.class.php +++ b/htdocs/core/modules/modPrinting.class.php @@ -100,7 +100,7 @@ class modPrinting extends DolibarrModules $this->rights[$r][4] = 'read'; // Main menu entries - $this->menus = array(); // List of menus to add + $this->menu = array(); // List of menus to add $r = 0; // This is to declare the Top Menu entry: diff --git a/htdocs/install/mysql/migration/7.0.0-8.0.0.sql b/htdocs/install/mysql/migration/7.0.0-8.0.0.sql index b4dd2d6e0d2..afea0fe18ab 100644 --- a/htdocs/install/mysql/migration/7.0.0-8.0.0.sql +++ b/htdocs/install/mysql/migration/7.0.0-8.0.0.sql @@ -180,7 +180,7 @@ ALTER TABLE llx_c_paiement DROP INDEX uk_c_paiement; ALTER TABLE llx_c_paiement ADD UNIQUE INDEX uk_c_paiement_code(entity, code); -- VMYSQL4.3 ALTER TABLE llx_c_paiement CHANGE COLUMN id id INTEGER AUTO_INCREMENT PRIMARY KEY; --- VPGSQL8.2 CREATE SEQUENCE llx_c_paiement_id_seq OWNED BY llx_c_paiement.id; +-- VPGSQL8.2 CREATE SEQUENCE __DATABASE__.llx_c_paiement_id_seq OWNED BY llx_c_paiement.id; -- VPGSQL8.2 ALTER TABLE llx_c_paiement ADD PRIMARY KEY (id); -- VPGSQL8.2 ALTER TABLE llx_c_paiement ALTER COLUMN id SET DEFAULT nextval('llx_c_paiement_id_seq'); -- VPGSQL8.2 SELECT setval('llx_c_paiement_id_seq', MAX(id)) FROM llx_c_paiement; @@ -190,7 +190,7 @@ ALTER TABLE llx_c_payment_term DROP INDEX uk_c_payment_term; ALTER TABLE llx_c_payment_term ADD UNIQUE INDEX uk_c_payment_term_code(entity, code); -- VMYSQL4.3 ALTER TABLE llx_c_payment_term CHANGE COLUMN rowid rowid INTEGER AUTO_INCREMENT PRIMARY KEY; --- VPGSQL8.2 CREATE SEQUENCE llx_c_payment_term_rowid_seq OWNED BY llx_c_payment_term.rowid; +-- VPGSQL8.2 CREATE SEQUENCE __DATABASE__.llx_c_payment_term_rowid_seq OWNED BY llx_c_payment_term.rowid; -- VPGSQL8.2 ALTER TABLE llx_c_payment_term ADD PRIMARY KEY (rowid); -- VPGSQL8.2 ALTER TABLE llx_c_payment_term ALTER COLUMN rowid SET DEFAULT nextval('llx_c_payment_term_rowid_seq'); -- VPGSQL8.2 SELECT setval('llx_c_payment_term_rowid_seq', MAX(rowid)) FROM llx_c_payment_term; diff --git a/htdocs/install/pgsql/functions/functions.sql b/htdocs/install/pgsql/functions/functions.sql index 918d17385eb..cb4e6d90c1f 100644 --- a/htdocs/install/pgsql/functions/functions.sql +++ b/htdocs/install/pgsql/functions/functions.sql @@ -52,6 +52,7 @@ CREATE OR REPLACE FUNCTION DAY(TIMESTAMP WITH TIME ZONE) RETURNS INTEGER AS $$ S CREATE OR REPLACE FUNCTION DAY(DATE) RETURNS INTEGER AS $$ SELECT EXTRACT(DAY FROM $1)::INTEGER; $$ LANGUAGE SQL IMMUTABLE; + CREATE OR REPLACE FUNCTION dol_util_rebuild_sequences() RETURNS integer as $body$ DECLARE sequencedefs RECORD; c integer ; BEGIN FOR sequencedefs IN SELECT DISTINCT constraint_column_usage.table_name as tablename, constraint_column_usage.table_name as tablename, constraint_column_usage.column_name as columnname, replace(replace(columns.column_default,'''::regclass)',''),'nextval(''','') as sequencename from information_schema.constraint_column_usage, information_schema.columns, information_schema.sequences where constraint_column_usage.table_schema ='public' AND columns.table_schema = 'public' AND columns.table_name=constraint_column_usage.table_name AND constraint_column_usage.column_name IN ('rowid','id') AND constraint_column_usage.column_name = columns.column_name AND columns.column_default is not null AND replace(replace(columns.column_default,'''::regclass)',''),'nextval(''','')=sequence_name LOOP EXECUTE 'select max('||sequencedefs.columnname||') from ' || sequencedefs.tablename INTO c; IF c is null THEN c = 0; END IF; IF c is not null THEN c = c+ 1; END IF; EXECUTE 'alter sequence ' || sequencedefs.sequencename ||' restart with ' || c; END LOOP; RETURN 1; END; $body$ LANGUAGE plpgsql; -- You can call the function with SELECT dol_util_rebuild_sequences(); diff --git a/htdocs/install/upgrade.php b/htdocs/install/upgrade.php index 4c9045c719f..a1179ac848b 100644 --- a/htdocs/install/upgrade.php +++ b/htdocs/install/upgrade.php @@ -355,7 +355,7 @@ if (!GETPOST('action', 'aZ09') || preg_match('/upgrade/i', GETPOST('action', 'aZ print ''.$langs->trans("ChoosedMigrateScript").''.$file.''."\n"; // Run sql script - $ok = run_sql($dir.$file, 0, '', 1, '', 'default', 32768, 0, 0, 2); + $ok = run_sql($dir.$file, 0, '', 1, '', 'default', 32768, 0, 0, 2, 0, $db->database_name); $listoffileprocessed[$dir.$file] = $dir.$file; diff --git a/test/phpunit/PgsqlTest.php b/test/phpunit/PgsqlTest.php index e9687868415..e80190a974f 100644 --- a/test/phpunit/PgsqlTest.php +++ b/test/phpunit/PgsqlTest.php @@ -142,6 +142,13 @@ class PgsqlTest extends PHPUnit\Framework\TestCase $langs=$this->savlangs; $db=$this->savdb; + /* + $sql = "CREATE SEQUENCE __DATABASE__.llx_c_paiement_id_seq OWNED BY llx_c_paiement.id;"; + $result=DoliDBPgsql::convertSQLFromMysql($sql); + print __METHOD__." result=".$result."\n"; + $this->assertEquals($result, "CREATE SEQUENCE __DATABASE__.llx_c_paiement_id_seq OWNED BY llx_c_paiement.id;"); + */ + $sql = "ALTER TABLE llx_bank_account MODIFY COLUMN state_id integer USING state_id::integer;"; $result=DoliDBPgsql::convertSQLFromMysql($sql); print __METHOD__." result=".$result."\n";