CREATE TYPE API_OPERATION AS ENUM ('init', 'add', 'delete', 'update');

CREATE TABLE OPERATION
(
  ID SERIAL PRIMARY KEY
, Redo TEXT NOT NULL
, Undo TEXT NOT NULL
, Parent INTEGER REFERENCES OPERATION(ID)
, Redo_Child INTEGER REFERENCES OPERATION(ID)
, Api_Id TEXT NOT NULL
, Api_Op API_OPERATION NOT NULL
, Api_Object_Type TEXT NOT NULL
, Api_Object_Id TEXT NOT NULL -- VARCHAR(32)
, Api_Object_Properties TEXT[]
);

INSERT INTO OPERATION (ID, Redo, Undo, Api_Id, Api_Op, Api_Object_Type, Api_Object_Id) VALUES (0, '', '', '', 'init', '', '');

CREATE TABLE CURRENT_OPERATION
(
  ID INTEGER PRIMARY KEY REFERENCES OPERATION(ID)
);

INSERT INTO CURRENT_OPERATION (ID) VALUES (0);

CREATE TABLE SNAPSHOT_OPERATION
(
  ID INTEGER PRIMARY KEY REFERENCES OPERATION(ID)
, Tag TEXT NOT NULL UNIQUE
);

CREATE TABLE TRANSACTION_OPERATION
(
  ID INTEGER PRIMARY KEY REFERENCES OPERATION(ID)
, STRICT BOOLEAN NOT NULL DEFAULT FALSE
);
