diff --git a/openbis/source/sql/postgresql/053/function-053.sql b/openbis/source/sql/postgresql/053/function-053.sql index 44dc8dd82f1510e929bb636db5930746b9756fcb..06c660c75c76739cb0d1d5b3c6ad184c8a26f696 100644 --- a/openbis/source/sql/postgresql/053/function-053.sql +++ b/openbis/source/sql/postgresql/053/function-053.sql @@ -71,33 +71,15 @@ CREATE TRIGGER EXTERNAL_DATA_STORAGE_FORMAT_CHECK BEFORE INSERT OR UPDATE ON EXT ------------------------------------------------------------------------------------ --- Purpose: Create trigger SAMPLE_CODE_UNIQUENESS_CHECK +-- Purpose: Create triggers for checking sample code uniqueness ------------------------------------------------------------------------------------ CREATE OR REPLACE FUNCTION SAMPLE_CODE_UNIQUENESS_CHECK() RETURNS trigger AS $$ DECLARE counter INTEGER; - unique_subcode BOOLEAN_CHAR; BEGIN LOCK TABLE samples IN EXCLUSIVE MODE; - SELECT is_subcode_unique into unique_subcode FROM sample_types WHERE id = NEW.saty_id; - - IF (unique_subcode) THEN - IF (NEW.dbin_id is not NULL) THEN - SELECT count(*) into counter FROM samples - where id != NEW.id and code = NEW.code and saty_id = NEW.saty_id and dbin_id = NEW.dbin_id; - IF (counter > 0) THEN - RAISE EXCEPTION 'Insert/Update of Sample (Code: %) failed because database instance sample of the same type with the same subcode already exists.', NEW.code; - END IF; - ELSIF (NEW.grou_id is not NULL) THEN - SELECT count(*) into counter FROM samples - where id != NEW.id and code = NEW.code and saty_id = NEW.saty_id and grou_id = NEW.grou_id; - IF (counter > 0) THEN - RAISE EXCEPTION 'Insert/Update of Sample (Code: %) failed because space sample of the same type with the same subcode already exists.', NEW.code; - END IF; - END IF; - ELSE IF (NEW.samp_id_part_of is NULL) THEN IF (NEW.dbin_id is not NULL) THEN SELECT count(*) into counter FROM samples @@ -127,7 +109,6 @@ BEGIN END IF; END IF; END IF; - END IF; RETURN NEW; END; @@ -136,6 +117,39 @@ $$ LANGUAGE 'plpgsql'; CREATE TRIGGER SAMPLE_CODE_UNIQUENESS_CHECK BEFORE INSERT OR UPDATE ON SAMPLES FOR EACH ROW EXECUTE PROCEDURE SAMPLE_CODE_UNIQUENESS_CHECK(); + +CREATE OR REPLACE FUNCTION SAMPLE_SUBCODE_UNIQUENESS_CHECK() RETURNS trigger AS $$ +DECLARE + counter INTEGER; + unique_subcode BOOLEAN_CHAR; +BEGIN + LOCK TABLE samples IN EXCLUSIVE MODE; + + SELECT is_subcode_unique into unique_subcode FROM sample_types WHERE id = NEW.saty_id; + + IF (unique_subcode) THEN + IF (NEW.dbin_id is not NULL) THEN + SELECT count(*) into counter FROM samples + where id != NEW.id and code = NEW.code and saty_id = NEW.saty_id and dbin_id = NEW.dbin_id; + IF (counter > 0) THEN + RAISE EXCEPTION 'Insert/Update of Sample (Code: %) failed because database instance sample of the same type with the same subcode already exists.', NEW.code; + END IF; + ELSIF (NEW.grou_id is not NULL) THEN + SELECT count(*) into counter FROM samples + where id != NEW.id and code = NEW.code and saty_id = NEW.saty_id and grou_id = NEW.grou_id; + IF (counter > 0) THEN + RAISE EXCEPTION 'Insert/Update of Sample (Code: %) failed because space sample of the same type with the same subcode already exists.', NEW.code; + END IF; + END IF; + END IF; + + RETURN NEW; +END; +$$ LANGUAGE 'plpgsql'; + +CREATE TRIGGER SAMPLE_SUBCODE_UNIQUENESS_CHECK BEFORE INSERT OR UPDATE ON SAMPLES + FOR EACH ROW EXECUTE PROCEDURE SAMPLE_SUBCODE_UNIQUENESS_CHECK(); + ------------------------------------------------------------------------------------ -- Purpose: Create trigger MATERIAL/SAMPLE/EXPERIMENT/DATA_SET _PROPERTY_WITH_MATERIAL_DATA_TYPE_CHECK -- It checks that if material property value is assigned to the entity, diff --git a/openbis/source/sql/postgresql/migration/migration-052-053.sql b/openbis/source/sql/postgresql/migration/migration-052-053.sql index a38df08c39816cfaac74488d8aee4b80f9208835..e8a45ae9e20cabeb559dc4145df97422c7f254c2 100644 --- a/openbis/source/sql/postgresql/migration/migration-052-053.sql +++ b/openbis/source/sql/postgresql/migration/migration-052-053.sql @@ -1,13 +1,12 @@ -- Migration from 052 to 053 -- Change code uniqueness check for samples of specific type. --- If sample_types.is_subcode_unique flag is set to 'true', codes of samples of the type will have to --- be unique no matter if they are contained in a container or not. Otherwise standard uniqueness --- check is performed (taking container connection into consideration). +-- If sample_types.is_subcode_unique flag is set to 'true', additional check is performed +-- on codes of samples of the type. Subcodes will have to be unique as well. ALTER TABLE sample_types ADD COLUMN is_subcode_unique boolean_char NOT NULL DEFAULT false; -CREATE OR REPLACE FUNCTION SAMPLE_CODE_UNIQUENESS_CHECK() RETURNS trigger AS $$ +CREATE OR REPLACE FUNCTION SAMPLE_SUBCODE_UNIQUENESS_CHECK() RETURNS trigger AS $$ DECLARE counter INTEGER; unique_subcode BOOLEAN_CHAR; @@ -30,7 +29,23 @@ BEGIN RAISE EXCEPTION 'Insert/Update of Sample (Code: %) failed because space sample of the same type with the same subcode already exists.', NEW.code; END IF; END IF; - ELSE + END IF; + + RETURN NEW; +END; +$$ LANGUAGE 'plpgsql'; + +CREATE TRIGGER SAMPLE_SUBCODE_UNIQUENESS_CHECK BEFORE INSERT OR UPDATE ON SAMPLES + FOR EACH ROW EXECUTE PROCEDURE SAMPLE_SUBCODE_UNIQUENESS_CHECK(); + +-- Fixing error messages in old trigger + +CREATE OR REPLACE FUNCTION SAMPLE_CODE_UNIQUENESS_CHECK() RETURNS trigger AS $$ +DECLARE + counter INTEGER; +BEGIN + LOCK TABLE samples IN EXCLUSIVE MODE; + IF (NEW.samp_id_part_of is NULL) THEN IF (NEW.dbin_id is not NULL) THEN SELECT count(*) into counter FROM samples @@ -60,7 +75,6 @@ BEGIN END IF; END IF; END IF; - END IF; RETURN NEW; END; diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/SampleDAOTest.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/SampleDAOTest.java index a59c03cca933fb77332510334139c35d3e1f2926..ff19599df098de0da27fda436742994f32ca8a87 100644 --- a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/SampleDAOTest.java +++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/SampleDAOTest.java @@ -161,6 +161,7 @@ public final class SampleDAOTest extends AbstractDAOTest final Session currentSession = sessionFactory.getCurrentSession(); currentSession.flush(); + // subcode uniqueness should be checked try { final SamplePE well2_1 = createContainedSample(containedType, "well1", container2); @@ -175,6 +176,32 @@ public final class SampleDAOTest extends AbstractDAOTest } } + @Test + public final void testSampleSubcodeUniquenessAcrossTypes() + { + final SampleTypePE type1 = getSampleType("DILUTION_PLATE"); + final SampleTypePE type2 = getSampleType("CELL_PLATE"); + type2.setSubcodeUnique(true); + final SamplePE sampleT1 = createSample(type1, "S_CODE"); + save(sampleT1); + + final Session currentSession = sessionFactory.getCurrentSession(); + currentSession.flush(); + + // default uniqueness should still be checked across types + try + { + final SamplePE sampleT2 = createSample(type2, "S_CODE"); + save(sampleT2); + currentSession.flush(); + fail("DataIntegrityViolationException expected"); + } catch (DataIntegrityViolationException e) + { + assertEquals("ERROR: Insert/Update of Sample (Code: S_CODE) failed because " + + "database instance sample with the same code already exists.", e.getMessage()); + } + } + @Test public void testTryToFindByPermID() { diff --git a/openbis/sourceTest/sql/postgresql/053/017=database_version_logs.tsv b/openbis/sourceTest/sql/postgresql/053/017=database_version_logs.tsv index 919f4da2237d67cbdec79508cdfee64e001ae879..43c894b29295fbc744681dad62b331e7e68d7b2f 100644 --- a/openbis/sourceTest/sql/postgresql/053/017=database_version_logs.tsv +++ b/openbis/sourceTest/sql/postgresql/053/017=database_version_logs.tsv @@ -25,5 +25,5 @@ 049 source/sql/postgresql/migration/migration-048-049.sql SUCCESS 2010-02-16 16:25:08.718 CREATE TABLE QUERIES (ID TECH_ID NOT NULL, DBIN_ID TECH_ID NOT NULL, NAME VARCHAR(200) NOT NULL, DESCRIPTION DESCRIPTION_2000,REGISTRATION_TIMESTAMP TIME_STAMP_DFL NOT NULL DEFAULT CURRENT_TIMESTAMP, PERS_ID_REGISTERER TECH_ID NOT NULL, MODIFICATION_TIMESTAMP TIME_STAMP DEFAULT CURRENT_TIMESTAMP, EXPRESSION VARCHAR(2000) NOT NULL, IS_PUBLIC BOOLEAN NOT NULL);\\012CREATE SEQUENCE QUERY_ID_SEQ;\\012ALTER TABLE QUERIES ADD CONSTRAINT QUER_PK PRIMARY KEY(ID);\\012ALTER TABLE QUERIES ADD CONSTRAINT QUER_BK_UK UNIQUE(NAME, DBIN_ID);\\012ALTER TABLE QUERIES ADD CONSTRAINT QUER_PERS_FK FOREIGN KEY (PERS_ID_REGISTERER) REFERENCES PERSONS(ID);\\012ALTER TABLE QUERIES ADD CONSTRAINT QUER_DBIN_FK FOREIGN KEY (DBIN_ID) REFERENCES DATABASE_INSTANCES(ID);\\012GRANT SELECT ON SEQUENCE query_id_seq TO GROUP OPENBIS_READONLY;\\012GRANT SELECT ON TABLE queries TO GROUP OPENBIS_READONLY;\\012 \N 050 source/sql/postgresql/migration/migration-049-050.sql SUCCESS 2010-04-09 10:38:16.886 -- Migration from 049 to 050\\012\\012-- add data set status \\012ALTER TABLE external_data ADD COLUMN status VARCHAR(100) NOT NULL DEFAULT 'ACTIVE';\\012ALTER TABLE external_data ADD CONSTRAINT exda_status_enum_ck \\012 CHECK (status IN ('LOCKED', 'ACTIVE', 'ARCHIVED', 'ACTIVATION_IN_PROGRESS', 'ARCHIVIZATION_IN_PROGRESS')); \\012-- \\012 \N 051 source/sql/postgresql/migration/migration-050-051.sql SUCCESS 2010-04-09 23:41:51.692 -- Migration from 050 to 051\\012\\012-- change archiving status names to be consistent with UI & introduce a domain \\012ALTER TABLE external_data DROP CONSTRAINT exda_status_enum_ck;\\012\\012UPDATE external_data SET status = 'AVAILABLE' WHERE status = 'ACTIVE';\\012UPDATE external_data SET status = 'UNARCHIVE_PENDING' WHERE status = 'ACTIVATION_IN_PROGRESS';\\012UPDATE external_data SET status = 'ARCHIVE_PENDING' WHERE status = 'ARCHIVIZATION_IN_PROGRESS';\\012\\012CREATE DOMAIN archiving_status AS VARCHAR(100);\\012ALTER DOMAIN archiving_status ADD CONSTRAINT archiving_status_check \\012 CHECK (VALUE IN ('LOCKED', 'AVAILABLE', 'ARCHIVED', 'ARCHIVE_PENDING', 'UNARCHIVE_PENDING'));\\012\\012ALTER TABLE external_data ALTER COLUMN status TYPE archiving_status;\\012ALTER TABLE external_data ALTER COLUMN status SET DEFAULT 'AVAILABLE';\\012\\012-- add is_archiver_configured flag to data_stores table\\012ALTER TABLE data_stores ADD COLUMN is_archiver_configured BOOLEAN_CHAR NOT NULL DEFAULT 'F';\\012 \N -052 source/sql/postgresql/migration/migration-051-052.sql SUCCESS 2010-06-05 23:44:42.462 -- Migration from 051 to 052\\012\\012-- Add QUERY_TYPE column to QUERIES\\012CREATE DOMAIN QUERY_TYPE AS VARCHAR(40) CHECK (VALUE IN ('GENERIC', 'EXPERIMENT', 'SAMPLE', 'DATA_SET', 'MATERIAL'));\\012ALTER TABLE QUERIES ADD COLUMN QUERY_TYPE QUERY_TYPE;\\012UPDATE QUERIES SET QUERY_TYPE = 'GENERIC';\\012ALTER TABLE QUERIES ALTER COLUMN QUERY_TYPE SET NOT NULL; \\012\\012-- add DB_KEY column to QUERIES\\012\\012ALTER TABLE queries ADD COLUMN db_key code NOT NULL DEFAULT '1';\\012 \N -053 source/sql/postgresql/migration/migration-052-053.sql SUCCESS 2010-06-07 11:06:56.154 -- Migration from 052 to 053\\012\\012-- Change code uniqueness check for samples of specific type.\\012-- If sample_types.is_subcode_unique flag is set to 'true', codes of samples of the type will have to\\012-- be unique no matter if they are contained in a container or not. Otherwise standard uniqueness \\012-- check is performed (taking container connection into consideration).\\012\\012ALTER TABLE sample_types ADD COLUMN is_subcode_unique boolean_char NOT NULL DEFAULT false;\\012\\012CREATE OR REPLACE FUNCTION SAMPLE_CODE_UNIQUENESS_CHECK() RETURNS trigger AS $$\\012DECLARE\\012 counter INTEGER;\\012 unique_subcode BOOLEAN_CHAR;\\012BEGIN\\012 LOCK TABLE samples IN EXCLUSIVE MODE;\\012 \\012 SELECT is_subcode_unique into unique_subcode FROM sample_types WHERE id = NEW.saty_id;\\012 \\012 IF (unique_type_code) THEN\\012 IF (NEW.dbin_id is not NULL) THEN\\012\\011\\011\\011SELECT count(*) into counter FROM samples \\012\\011\\011\\011\\011where id != NEW.id and code = NEW.code and saty_id = NEW.saty_id and dbin_id = NEW.dbin_id;\\012\\011\\011\\011IF (counter > 0) THEN\\012\\011\\011\\011\\011RAISE EXCEPTION 'Insert/Update of Sample (Code: %) failed because database instance sample of the same type with the same subcode already exists.', NEW.code;\\012\\011\\011\\011END IF;\\012\\011\\011ELSIF (NEW.grou_id is not NULL) THEN\\012\\011\\011\\011SELECT count(*) into counter FROM samples \\012\\011\\011\\011\\011where id != NEW.id and code = NEW.code and saty_id = NEW.saty_id and grou_id = NEW.grou_id;\\012\\011\\011\\011IF (counter > 0) THEN\\012\\011\\011\\011\\011RAISE EXCEPTION 'Insert/Update of Sample (Code: %) failed because space sample of the same type with the same subcode already exists.', NEW.code;\\012\\011\\011\\011END IF;\\012\\011\\011END IF;\\012 ELSE \\011\\012\\011 IF (NEW.samp_id_part_of is NULL) THEN\\012\\011\\011 IF (NEW.dbin_id is not NULL) THEN\\012\\011\\011\\011 SELECT count(*) into counter FROM samples \\012\\011\\011 where id != NEW.id and code = NEW.code and samp_id_part_of is NULL and dbin_id = NEW.dbin_id;\\012 IF (counter > 0) THEN\\012\\011\\011\\011\\011 RAISE EXCEPTION 'Insert/Update of Sample (Code: %) failed because database instance sample with the same code already exists.', NEW.code;\\012 END IF;\\012\\011\\011 ELSIF (NEW.grou_id is not NULL) THEN\\012\\011\\011\\011 SELECT count(*) into counter FROM samples \\012\\011\\011\\011\\011 where id != NEW.id and code = NEW.code and samp_id_part_of is NULL and grou_id = NEW.grou_id;\\012\\011\\011\\011 IF (counter > 0) THEN\\012\\011\\011\\011\\011 RAISE EXCEPTION 'Insert/Update of Sample (Code: %) failed because space sample with the same code already exists.', NEW.code;\\012\\011\\011\\011 END IF;\\012 END IF;\\012 ELSE\\012\\011\\011 IF (NEW.dbin_id is not NULL) THEN\\012\\011\\011\\011 SELECT count(*) into counter FROM samples \\012\\011\\011\\011\\011 where id != NEW.id and code = NEW.code and samp_id_part_of = NEW.samp_id_part_of and dbin_id = NEW.dbin_id;\\012\\011\\011\\011 IF (counter > 0) THEN\\012\\011\\011\\011\\011 RAISE EXCEPTION 'Insert/Update of Sample (Code: %) failed because database instance sample with the same code and being the part of the same container already exists.', NEW.code;\\012\\011\\011\\011 END IF;\\012\\011\\011 ELSIF (NEW.grou_id is not NULL) THEN\\012\\011\\011\\011 SELECT count(*) into counter FROM samples \\012\\011\\011\\011\\011 where id != NEW.id and code = NEW.code and samp_id_part_of = NEW.samp_id_part_of and grou_id = NEW.grou_id;\\012\\011\\011\\011 IF (counter > 0) THEN\\012\\011\\011\\011\\011 RAISE EXCEPTION 'Insert/Update of Sample (Code: %) failed because space sample with the same code and being the part of the same container already exists.', NEW.code;\\012\\011\\011\\011 END IF;\\012\\011\\011 END IF;\\012 END IF; \\012 END IF;\\012 \\012 RETURN NEW;\\012END;\\012$$ LANGUAGE 'plpgsql';\\012 \N +052 source/sql/postgresql/migration/migration-051-052.sql SUCCESS 2010-05-11 10:26:49.787 -- Migration from 051 to 052\\012\\012-- Add QUERY_TYPE column to QUERIES\\012CREATE DOMAIN QUERY_TYPE AS VARCHAR(40) CHECK (VALUE IN ('GENERIC', 'EXPERIMENT', 'SAMPLE', 'DATA_SET', 'MATERIAL'));\\012ALTER TABLE QUERIES ADD COLUMN QUERY_TYPE QUERY_TYPE;\\012UPDATE QUERIES SET QUERY_TYPE = 'GENERIC';\\012ALTER TABLE QUERIES ALTER COLUMN QUERY_TYPE SET NOT NULL; \\012\\012-- add DB_KEY column to QUERIES\\012\\012ALTER TABLE queries ADD COLUMN db_key code NOT NULL DEFAULT '1';\\012 \N +053 source/sql/postgresql/migration/migration-052-053.sql SUCCESS 2010-06-08 15:27:55.032 -- Migration from 052 to 053\\012\\012-- Change code uniqueness check for samples of specific type.\\012-- If sample_types.is_subcode_unique flag is set to 'true', additional check is performed \\012-- on codes of samples of the type. Subcodes will have to be unique as well.\\012\\012ALTER TABLE sample_types ADD COLUMN is_subcode_unique boolean_char NOT NULL DEFAULT false;\\012\\012CREATE OR REPLACE FUNCTION SAMPLE_SUBCODE_UNIQUENESS_CHECK() RETURNS trigger AS $$\\012DECLARE\\012 counter INTEGER;\\012 unique_subcode BOOLEAN_CHAR;\\012BEGIN\\012 LOCK TABLE samples IN EXCLUSIVE MODE;\\012 \\012 SELECT is_subcode_unique into unique_subcode FROM sample_types WHERE id = NEW.saty_id;\\012 \\012 IF (unique_subcode) THEN\\012 IF (NEW.dbin_id is not NULL) THEN\\012\\011\\011\\011SELECT count(*) into counter FROM samples \\012\\011\\011\\011\\011where id != NEW.id and code = NEW.code and saty_id = NEW.saty_id and dbin_id = NEW.dbin_id;\\012\\011\\011\\011IF (counter > 0) THEN\\012\\011\\011\\011\\011RAISE EXCEPTION 'Insert/Update of Sample (Code: %) failed because database instance sample of the same type with the same subcode already exists.', NEW.code;\\012\\011\\011\\011END IF;\\012\\011\\011ELSIF (NEW.grou_id is not NULL) THEN\\012\\011\\011\\011SELECT count(*) into counter FROM samples \\012\\011\\011\\011\\011where id != NEW.id and code = NEW.code and saty_id = NEW.saty_id and grou_id = NEW.grou_id;\\012\\011\\011\\011IF (counter > 0) THEN\\012\\011\\011\\011\\011RAISE EXCEPTION 'Insert/Update of Sample (Code: %) failed because space sample of the same type with the same subcode already exists.', NEW.code;\\012\\011\\011\\011END IF;\\012\\011\\011END IF;\\012 END IF;\\012 \\012 RETURN NEW;\\012END;\\012$$ LANGUAGE 'plpgsql';\\012\\012CREATE TRIGGER SAMPLE_SUBCODE_UNIQUENESS_CHECK BEFORE INSERT OR UPDATE ON SAMPLES\\012 FOR EACH ROW EXECUTE PROCEDURE SAMPLE_SUBCODE_UNIQUENESS_CHECK();\\012 \\012-- Fixing error messages in old trigger\\012\\012CREATE OR REPLACE FUNCTION SAMPLE_CODE_UNIQUENESS_CHECK() RETURNS trigger AS $$\\012DECLARE\\012 counter INTEGER;\\012BEGIN\\012 LOCK TABLE samples IN EXCLUSIVE MODE;\\012 \\012\\011 IF (NEW.samp_id_part_of is NULL) THEN\\012\\011\\011 IF (NEW.dbin_id is not NULL) THEN\\012\\011\\011\\011 SELECT count(*) into counter FROM samples \\012\\011\\011 where id != NEW.id and code = NEW.code and samp_id_part_of is NULL and dbin_id = NEW.dbin_id;\\012 IF (counter > 0) THEN\\012\\011\\011\\011\\011 RAISE EXCEPTION 'Insert/Update of Sample (Code: %) failed because database instance sample with the same code already exists.', NEW.code;\\012 END IF;\\012\\011\\011 ELSIF (NEW.grou_id is not NULL) THEN\\012\\011\\011\\011 SELECT count(*) into counter FROM samples \\012\\011\\011\\011\\011 where id != NEW.id and code = NEW.code and samp_id_part_of is NULL and grou_id = NEW.grou_id;\\012\\011\\011\\011 IF (counter > 0) THEN\\012\\011\\011\\011\\011 RAISE EXCEPTION 'Insert/Update of Sample (Code: %) failed because space sample with the same code already exists.', NEW.code;\\012\\011\\011\\011 END IF;\\012 END IF;\\012 ELSE\\012\\011\\011 IF (NEW.dbin_id is not NULL) THEN\\012\\011\\011\\011 SELECT count(*) into counter FROM samples \\012\\011\\011\\011\\011 where id != NEW.id and code = NEW.code and samp_id_part_of = NEW.samp_id_part_of and dbin_id = NEW.dbin_id;\\012\\011\\011\\011 IF (counter > 0) THEN\\012\\011\\011\\011\\011 RAISE EXCEPTION 'Insert/Update of Sample (Code: %) failed because database instance sample with the same code and being the part of the same container already exists.', NEW.code;\\012\\011\\011\\011 END IF;\\012\\011\\011 ELSIF (NEW.grou_id is not NULL) THEN\\012\\011\\011\\011 SELECT count(*) into counter FROM samples \\012\\011\\011\\011\\011 where id != NEW.id and code = NEW.code and samp_id_part_of = NEW.samp_id_part_of and grou_id = NEW.grou_id;\\012\\011\\011\\011 IF (counter > 0) THEN\\012\\011\\011\\011\\011 RAISE EXCEPTION 'Insert/Update of Sample (Code: %) failed because space sample with the same code and being the part of the same container already exists.', NEW.code;\\012\\011\\011\\011 END IF;\\012\\011\\011 END IF;\\012 END IF; \\012 \\012 RETURN NEW;\\012END;\\012$$ LANGUAGE 'plpgsql';\\012 \N diff --git a/openbis/sourceTest/sql/postgresql/053/finish-053.sql b/openbis/sourceTest/sql/postgresql/053/finish-053.sql index e8d026b2544673497af9fd37326c236cb24f25df..bd44bb2ae323162e4a7c174150263c5985f0fd9f 100644 --- a/openbis/sourceTest/sql/postgresql/053/finish-053.sql +++ b/openbis/sourceTest/sql/postgresql/053/finish-053.sql @@ -280,6 +280,10 @@ CREATE TRIGGER sample_property_with_material_data_type_check BEFORE INSERT OR UPDATE ON sample_properties FOR EACH ROW EXECUTE PROCEDURE sample_property_with_material_data_type_check(); +CREATE TRIGGER sample_subcode_uniqueness_check + BEFORE INSERT OR UPDATE ON samples + FOR EACH ROW + EXECUTE PROCEDURE sample_subcode_uniqueness_check(); ALTER TABLE ONLY authorization_groups ADD CONSTRAINT ag_dbin_fk FOREIGN KEY (dbin_id) REFERENCES database_instances(id); ALTER TABLE ONLY authorization_groups diff --git a/openbis/sourceTest/sql/postgresql/053/schema-053.sql b/openbis/sourceTest/sql/postgresql/053/schema-053.sql index bd2dd08a9b21ef37142248e1b173bb45014bf36c..7501280fc87fdc691acace6c6c63e2befbf7d754 100644 --- a/openbis/sourceTest/sql/postgresql/053/schema-053.sql +++ b/openbis/sourceTest/sql/postgresql/053/schema-053.sql @@ -203,27 +203,9 @@ CREATE FUNCTION sample_code_uniqueness_check() RETURNS trigger AS $$ DECLARE counter INTEGER; - unique_subcode BOOLEAN_CHAR; BEGIN LOCK TABLE samples IN EXCLUSIVE MODE; - SELECT is_subcode_unique into unique_subcode FROM sample_types WHERE id = NEW.saty_id; - - IF (unique_subcode) THEN - IF (NEW.dbin_id is not NULL) THEN - SELECT count(*) into counter FROM samples - where id != NEW.id and code = NEW.code and saty_id = NEW.saty_id and dbin_id = NEW.dbin_id; - IF (counter > 0) THEN - RAISE EXCEPTION 'Insert/Update of Sample (Code: %) failed because database instance sample of the same type with the same subcode already exists.', NEW.code; - END IF; - ELSIF (NEW.grou_id is not NULL) THEN - SELECT count(*) into counter FROM samples - where id != NEW.id and code = NEW.code and saty_id = NEW.saty_id and grou_id = NEW.grou_id; - IF (counter > 0) THEN - RAISE EXCEPTION 'Insert/Update of Sample (Code: %) failed because space sample of the same type with the same subcode already exists.', NEW.code; - END IF; - END IF; - ELSE IF (NEW.samp_id_part_of is NULL) THEN IF (NEW.dbin_id is not NULL) THEN SELECT count(*) into counter FROM samples @@ -253,7 +235,6 @@ BEGIN END IF; END IF; END IF; - END IF; RETURN NEW; END; @@ -285,6 +266,36 @@ BEGIN RETURN NEW; END; $$; +CREATE FUNCTION sample_subcode_uniqueness_check() RETURNS trigger + LANGUAGE plpgsql + AS $$ +DECLARE + counter INTEGER; + unique_subcode BOOLEAN_CHAR; +BEGIN + LOCK TABLE samples IN EXCLUSIVE MODE; + + SELECT is_subcode_unique into unique_subcode FROM sample_types WHERE id = NEW.saty_id; + + IF (unique_subcode) THEN + IF (NEW.dbin_id is not NULL) THEN + SELECT count(*) into counter FROM samples + where id != NEW.id and code = NEW.code and saty_id = NEW.saty_id and dbin_id = NEW.dbin_id; + IF (counter > 0) THEN + RAISE EXCEPTION 'Insert/Update of Sample (Code: %) failed because database instance sample of the same type with the same subcode already exists.', NEW.code; + END IF; + ELSIF (NEW.grou_id is not NULL) THEN + SELECT count(*) into counter FROM samples + where id != NEW.id and code = NEW.code and saty_id = NEW.saty_id and grou_id = NEW.grou_id; + IF (counter > 0) THEN + RAISE EXCEPTION 'Insert/Update of Sample (Code: %) failed because space sample of the same type with the same subcode already exists.', NEW.code; + END IF; + END IF; + END IF; + + RETURN NEW; +END; +$$; CREATE SEQUENCE attachment_content_id_seq START WITH 1 INCREMENT BY 1