CREATE SEQUENCE OBJECTSCRIPS_seq;
CREATE TABLE ObjectScrips (
        id              NUMBER(11,0)
                 CONSTRAINT ObjectScrips_Key PRIMARY KEY,
        Scrip       NUMBER(11,0)  NOT NULL,
        Stage           VARCHAR2(32) DEFAULT 'TransactionCreate' NOT NULL,
        ObjectId              NUMBER(11,0)  NOT NULL,
        SortOrder       NUMBER(11,0) DEFAULT 0 NOT NULL,
        Creator         NUMBER(11,0) DEFAULT 0 NOT NULL,
        Created         DATE,
        LastUpdatedBy   NUMBER(11,0) DEFAULT 0 NOT NULL,
        LastUpdated     DATE
);
ALTER TABLE Scrips ADD Disabled NUMBER(11,0) DEFAULT 0 NOT NULL;

INSERT INTO ObjectScrips(
    id, Scrip, Stage, ObjectId,
    Creator, Created, LastUpdatedBy, LastUpdated
)
(SELECT OBJECTSCRIPS_seq.nextval, id, Stage, Queue, Creator, Created, LastUpdatedBy, LastUpdated
FROM Scrips)
;

UPDATE Scrips SET Disabled = 1 WHERE Stage = 'Disabled';
UPDATE ObjectScrips SET Stage = 'TransactionCreate' WHERE Stage = 'Disabled';

CREATE UNIQUE INDEX ObjectScrips1 ON ObjectScrips (ObjectId, Scrip);

ALTER TABLE Scrips DROP( Stage, Queue );
