From a500033b104edb4fcec3d1b8c5393590cae4b1d5 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur <eldy@destailleur.fr> Date: Sun, 19 Feb 2012 03:21:10 +0100 Subject: [PATCH] New: Add postgresql function to resynchronize sequences --- htdocs/core/db/pgsql.class.php | 2 +- htdocs/core/modules/DolibarrModules.class.php | 13 ++++++------- htdocs/install/pgsql/functions/functions.sql | 3 +++ 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/htdocs/core/db/pgsql.class.php b/htdocs/core/db/pgsql.class.php index f255346e41b..138d1542619 100644 --- a/htdocs/core/db/pgsql.class.php +++ b/htdocs/core/db/pgsql.class.php @@ -563,7 +563,7 @@ class DoliDBPgsql $this->lasterror = $this->error(); $this->lasterrno = $this->errno(); } - dol_syslog(get_class($this)."::query SQL error usesavepoint = ".$usesavepoint." - ".$query." ".$this->errno(), LOG_WARNING); + dol_syslog(get_class($this)."::query SQL error usesavepoint = ".$usesavepoint." - ".$query." - ".pg_last_error($this->db)." = ".$this->errno(), LOG_WARNING); //print "\n>> ".$query."<br>\n"; //print '>> '.$this->lasterrno.' - '.$this->lasterror.' - '.$this->lastqueryerror."<br>\n"; diff --git a/htdocs/core/modules/DolibarrModules.class.php b/htdocs/core/modules/DolibarrModules.class.php index 435d26ab08a..dd430fd4fbe 100644 --- a/htdocs/core/modules/DolibarrModules.class.php +++ b/htdocs/core/modules/DolibarrModules.class.php @@ -429,22 +429,21 @@ abstract class DolibarrModules $entity = ((! empty($this->always_enabled) || ! empty($this->core_enabled)) ? 0 : $conf->entity); $sql = "DELETE FROM ".MAIN_DB_PREFIX."const"; - $sql.= " WHERE ".$this->db->decrypt('name')." = '".$this->const_name."'"; + $sql.= " WHERE name = '".$this->db->encrypt($this->const_name)."'"; $sql.= " AND entity in (0, ".$entity.")"; dol_syslog(get_class($this)."::_active sql=".$sql, LOG_DEBUG); - $this->db->query($sql); + $resql=$this->db->query($sql); + if (! $resql) $err++; $sql = "INSERT INTO ".MAIN_DB_PREFIX."const (name,value,visible,entity) VALUES"; - $sql.= " (".$this->db->encrypt($this->const_name,1); + $sql.= " ('".$this->db->encrypt($this->const_name)."'"; $sql.= ",".$this->db->encrypt('1',1); $sql.= ",0,".$entity.")"; dol_syslog(get_class($this)."::_active sql=".$sql, LOG_DEBUG); - if (!$this->db->query($sql)) - { - $err++; - } + $resql=$this->db->query($sql); + if (! $resql) $err++; return $err; } diff --git a/htdocs/install/pgsql/functions/functions.sql b/htdocs/install/pgsql/functions/functions.sql index 51b4f0290ba..ccf9aa96f30 100644 --- a/htdocs/install/pgsql/functions/functions.sql +++ b/htdocs/install/pgsql/functions/functions.sql @@ -46,3 +46,6 @@ CREATE OR REPLACE FUNCTION DAY(TIMESTAMP without TIME ZONE) RETURNS INTEGER AS $ CREATE OR REPLACE FUNCTION DAY(TIMESTAMP WITH TIME ZONE) RETURNS INTEGER AS $$ SELECT EXTRACT(DAY FROM $1)::INTEGER; $$ LANGUAGE SQL STABLE; CREATE OR REPLACE FUNCTION DAY(DATE) RETURNS INTEGER AS $$ SELECT EXTRACT(DAY FROM $1)::INTEGER; $$ LANGUAGE SQL IMMUTABLE; + + +CREATE OR REPLACE FUNCTION rebuilt_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 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 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; -- GitLab