CREATE TYPE API_OPERATION AS ENUM ('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 TEXT NOT NULL
, Api_Operation API_OPERATION NOT NULL
, Api_Operation_Type TEXT NOT NULL
, Api_Operation_Id TEXT NOT NULL -- VARCHAR(32)
, Api_Operation_Property TEXT NOT NULL
);

INSERT INTO OPERATION (ID, Redo, Undo) VALUES (0, '', '');

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
);
