IDEMPIERE-6166 PostgreSQL DUAL table with more than one record (#2387)

* IDEMPIERE-6166 PostgreSQL DUAL table with more than one record

- create unique index on dual.dummy for postgresql

* - implement suggestion from hengsin - make dual a view instead of a table
- fix similar issue with the table dbreplicasyncverifier - it must ensure to contain just one table - running again a COPY command must fail
This commit is contained in:
Carlos Ruiz 2024-06-10 13:11:42 +02:00
parent 9042f8319a
commit 0f02545dbb
4 changed files with 66 additions and 9 deletions

View File

@ -0,0 +1,29 @@
/*
CREATE TABLE dbreplicasyncverifier (lastupdate date)
;
INSERT INTO dbreplicasyncverifier values (to_date('1900-01-01 00:00:00', 'yyyy-mm-dd HH24:MI:SS'))
;
*/
CREATE OR REPLACE FUNCTION forbid_multiple_rows_in_dbreplicasyncverifier()
RETURNS TRIGGER AS $$
BEGIN
-- Check if the table already contains a row
IF (SELECT COUNT(*) FROM dbreplicasyncverifier) > 0 THEN
RAISE EXCEPTION 'Table can only contain one row.';
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql
;
CREATE TRIGGER single_row_only_trigger_dbreplicasyncverifier
BEFORE INSERT ON dbreplicasyncverifier
FOR EACH ROW
EXECUTE FUNCTION forbid_multiple_rows_in_dbreplicasyncverifier()
;
CREATE OR REPLACE RULE delete_dbreplicasyncverifier AS ON DELETE TO dbreplicasyncverifier DO INSTEAD NOTHING
;

View File

@ -41,13 +41,6 @@ END;
$BODY$
LANGUAGE plpgsql;
CREATE TABLE dual ( dummy char );
INSERT INTO dual values ( 'X' );
CREATE OR REPLACE RULE insert_dual AS ON INSERT TO dual DO INSTEAD NOTHING;
CREATE OR REPLACE RULE update_dual AS ON UPDATE TO dual DO INSTEAD NOTHING;
CREATE OR REPLACE RULE delete_dual AS ON DELETE TO dual DO INSTEAD NOTHING;
CREATE VIEW dual AS SELECT 'X'::varchar AS dummy
;

View File

@ -0,0 +1,5 @@
-- IDEMPIERE-6166 PostgreSQL DUAL table with more than one record
SELECT register_migration_script('202406072107_IDEMPIERE-6166.sql') FROM dual;
-- placeholder, this is just required for postgresql

View File

@ -0,0 +1,30 @@
-- IDEMPIERE-6166 PostgreSQL DUAL table with more than one record
SELECT register_migration_script('202406072107_IDEMPIERE-6166.sql') FROM dual;
DROP TABLE IF EXISTS dual
;
CREATE VIEW dual AS SELECT 'X'::varchar AS dummy
;
DROP RULE insert_dbreplicasyncverifier ON dbreplicasyncverifier
;
CREATE OR REPLACE FUNCTION forbid_multiple_rows_in_dbreplicasyncverifier()
RETURNS TRIGGER AS $$
BEGIN
-- Check if the table already contains a row
IF (SELECT COUNT(*) FROM dbreplicasyncverifier) > 0 THEN
RAISE EXCEPTION 'Table dbreplicasyncverifier can only contain one row.';
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql
;
CREATE TRIGGER single_row_only_trigger_dbreplicasyncverifier
BEFORE INSERT ON dbreplicasyncverifier
FOR EACH ROW
EXECUTE FUNCTION forbid_multiple_rows_in_dbreplicasyncverifier()
;