CREATE TABLE identity (
    id TEXT PRIMARY KEY,
    name TEXT NOT NULL
);

CREATE TABLE service_token_role_membership (
   role_name TEXT NOT NULL,
   token_id TEXT NOT NULL
       REFERENCES service_token(id)
           ON DELETE CASCADE
           ON UPDATE CASCADE,
   PRIMARY KEY (role_name, token_id)
);

CREATE TABLE service_token (
    id TEXT PRIMARY KEY,
    key TEXT NOT NULL,
    expiry INTEGER,
    parent_id TEXT NULL REFERENCES service_token(id)
        ON DELETE NO ACTION
        ON UPDATE CASCADE,
    identity_id TEXT NULL REFERENCES identity(id)
        ON DELETE CASCADE
        ON UPDATE CASCADE
);
