-- Create EXPERIMENT_ATTACHMENT_CONTENTS.\012CREATE TABLE EXPERIMENT_ATTACHMENT_CONTENTS (ID TECH_ID NOT NULL,VALUE FILE NOT NULL);\012CREATE SEQUENCE EXPERIMENT_ATTACHMENT_CONTENT_ID_SEQ;\012ALTER TABLE EXPERIMENT_ATTACHMENT_CONTENTS ADD CONSTRAINT EXAC_PK PRIMARY KEY(ID);\012\012-- Alter EXPERIMENT_ATTACHMENTS by adding the new column which has a NULL constraint.\012-- The NOT NULL constraint is added later on.\012ALTER TABLE EXPERIMENT_ATTACHMENTS ADD COLUMN EXAC_ID TECH_ID;\012\012-- Does the VALUE migration between EXPERIMENT_ATTACHMENTS to EXPERIMENT_ATTACHMENT_CONTENTS.\012CREATE OR REPLACE FUNCTION move_exp_att_content() RETURNS integer AS $$\012DECLARE\012    rec RECORD;\012    seq BIGINT;\012BEGIN\012  FOR rec IN SELECT id, value FROM EXPERIMENT_ATTACHMENTS LOOP\012  \011seq := nextval('EXPERIMENT_ATTACHMENT_CONTENT_ID_SEQ');\012    INSERT INTO EXPERIMENT_ATTACHMENT_CONTENTS (id, value) VALUES (seq, rec.value);\012    UPDATE EXPERIMENT_ATTACHMENTS SET exac_id = seq WHERE id = rec.id;\012  END LOOP;\012  RETURN 1;\012END;\012$$ LANGUAGE 'plpgsql';\012\012-- Call the function.\012SELECT move_exp_att_content();\012DROP FUNCTION move_exp_att_content();\012\012-- Finish EXPERIMENT_ATTACHMENTS changes.\012ALTER TABLE EXPERIMENT_ATTACHMENTS ADD CONSTRAINT EXAT_CONT_FK FOREIGN KEY (EXAC_ID) REFERENCES EXPERIMENT_ATTACHMENT_CONTENTS(ID);\012ALTER TABLE EXPERIMENT_ATTACHMENTS DROP COLUMN VALUE;\012ALTER TABLE EXPERIMENT_ATTACHMENTS ALTER COLUMN EXAC_ID SET NOT NULL;\012CREATE INDEX EXAT_EXAC_FK_I ON EXPERIMENT_ATTACHMENTS (EXAC_ID);\012\012-- allow longer user names\012ALTER TABLE PERSONS ALTER COLUMN USER_ID TYPE VARCHAR(50);\012DROP DOMAIN user_id;\012CREATE DOMAIN USER_ID AS VARCHAR(50);\012ALTER TABLE PERSONS ALTER COLUMN USER_ID TYPE USER_ID;\012