|
| 1 | +CREATE TABLE Users ( |
| 2 | +username VARCHAR(50) PRIMARY KEY, |
| 3 | +email VARCHAR(255) NOT NULL, |
| 4 | +password VARCHAR(255) NOT NULL, |
| 5 | +firstName VARCHAR(20) NOT NULL, |
| 6 | +lastName VARCHAR(20) NOT NULL, |
| 7 | +birthDate DATE NOT NULL, |
| 8 | +status VARCHAR(8) NOT NULL CHECK (status IN ('pending', 'active', 'disabled', 'blocked')) |
| 9 | +); |
| 10 | + |
| 11 | +CREATE TABLE Policy ( |
| 12 | +name VARCHAR(8) PRIMARY KEY CHECK (name IN ('trial', 'silver', 'gold', 'platinum')), |
| 13 | +maxAccess INT, |
| 14 | +threshold INT |
| 15 | +); |
| 16 | + |
| 17 | +CREATE TABLE Operation ( |
| 18 | +name VARCHAR(50) PRIMARY KEY, |
| 19 | +target VARCHAR(8) CHECK (target IN ('class', 'relation', 'subgraph')), |
| 20 | +description VARCHAR(100) NOT NULL |
| 21 | +); |
| 22 | + |
| 23 | +CREATE TABLE Category ( |
| 24 | +name VARCHAR(13) PRIMARY KEY CHECK (name IN ('add', 'fix', 'reification', 'explain', |
| 25 | +'openGPTDialog', 'generate')) |
| 26 | +); |
| 27 | + |
| 28 | +CREATE TABLE UserSubscribesPolicy ( |
| 29 | +username VARCHAR(50) REFERENCES Users(username) ON DELETE CASCADE, |
| 30 | +startDate TIMESTAMP, |
| 31 | +endDate TIMESTAMP, |
| 32 | +requestDate TIMESTAMP, |
| 33 | +status VARCHAR(8) NOT NULL CHECK (status IN ('pending', 'active', 'rejected', 'expired')), |
| 34 | +policyName VARCHAR(8) NOT NULL CHECK (policyName IN ('trial', 'silver', 'gold', 'platinum')) |
| 35 | +REFERENCES Policy(name) ON DELETE RESTRICT, |
| 36 | +PRIMARY KEY (username, requestDate) |
| 37 | +); |
| 38 | + |
| 39 | +CREATE TABLE UserMadeOperation ( |
| 40 | +username VARCHAR(50) REFERENCES Users(username) ON DELETE CASCADE, |
| 41 | +date TIMESTAMP, |
| 42 | +operationName VARCHAR(50) NOT NULL REFERENCES Operation(name) ON DELETE RESTRICT, |
| 43 | +PRIMARY KEY (username, date) |
| 44 | +); |
| 45 | + |
| 46 | +CREATE TABLE OperationIsCategory ( |
| 47 | +operationName VARCHAR(50) REFERENCES Operation(name) ON DELETE CASCADE, |
| 48 | +categoryName VARCHAR(13) REFERENCES Category(name) ON DELETE RESTRICT CHECK |
| 49 | +(categoryName IN ('add', 'fix', 'reification', 'explain', 'openGPTDialog', 'generate')), |
| 50 | +PRIMARY KEY (operationName, categoryName) |
| 51 | +); |
| 52 | + |
| 53 | +CREATE TABLE PolicyAllowsCategory ( |
| 54 | +policyName VARCHAR(8) CHECK (policyName IN ('trial', 'silver', 'gold', 'platinum')) REFERENCES |
| 55 | +Policy(name) ON DELETE CASCADE, |
| 56 | +categoryName VARCHAR(13) REFERENCES Category(name) ON DELETE RESTRICT CHECK |
| 57 | +(categoryName IN ('add', 'fix', 'reification', 'explain', 'openGPTDialog', 'generate')), |
| 58 | +PRIMARY KEY (policyName, categoryName) |
| 59 | +); |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | +INSERT INTO Policy (name, maxAccess, threshold) |
| 65 | +VALUES |
| 66 | +('trial', 10, 3), |
| 67 | +('silver', 50, 10), |
| 68 | +('gold', 100, 15), |
| 69 | +('platinum', null, null); |
| 70 | + |
| 71 | +INSERT INTO Operation (name, target, description) |
| 72 | +VALUES |
| 73 | +('AddClassSimilarToClass', 'class', 'add a new class semantically similar to another class'), |
| 74 | +('AddClassAssociatedToClass', 'class', 'add a new class in relation with another class'), |
| 75 | +('AddAttributeToRelationship', 'relation', 'add relevant attributes to a relationship'), |
| 76 | +('AddClassesSimilarToEntities', 'subgraph', 'add one or more new classes that semantically fit the context |
| 77 | +defined by subgraph'), |
| 78 | +('ReifyClass', 'class', 'create a class for representing the instance of a class'), |
| 79 | +('ExplainClass', 'class', 'explain in human-friendly terms the role of class in the schema'), |
| 80 | +('ExplainEntities', 'subgraph', 'explain in human-friendly terms the role of a portion of the schema in the |
| 81 | +schema'), |
| 82 | +('FixClassName', 'class', 'rename a class'), |
| 83 | +('FixClassOntology', 'class', 'enhance the relevant ontologies for a class'), |
| 84 | +('FixRelationshipCardinality', 'relation', 'fix cardinality for a relationship'), |
| 85 | +('OpenGPTDialog', NULL, 'openGPTDialog'), |
| 86 | +('Generate', NULL, 'generate'); |
| 87 | + |
| 88 | +INSERT INTO Category (name) |
| 89 | +VALUES |
| 90 | +('add'), |
| 91 | +('fix'), |
| 92 | +('reification'), |
| 93 | +('explain'), |
| 94 | +('openGPTDialog'), |
| 95 | +('generate'); |
| 96 | + |
| 97 | +INSERT INTO OperationIsCategory (operationName, categoryName) |
| 98 | +VALUES |
| 99 | +('AddClassSimilarToClass', 'add'), |
| 100 | +('AddClassAssociatedToClass', 'add'), |
| 101 | +('AddAttributeToRelationship', 'add'), |
| 102 | +('AddClassesSimilarToEntities', 'add'), |
| 103 | +('ReifyClass', 'reification'), |
| 104 | +('ExplainClass', 'explain'), |
| 105 | +('ExplainEntities', 'explain'), |
| 106 | +('FixClassName', 'fix'), |
| 107 | +('FixClassOntology', 'fix'), |
| 108 | +('FixRelationshipCardinality', 'fix'), |
| 109 | +('OpenGPTDialog', 'openGPTDialog'), |
| 110 | +('Generate', 'generate'); |
| 111 | + |
| 112 | +INSERT INTO PolicyAllowsCategory (policyName, categoryName) |
| 113 | +VALUES |
| 114 | +('trial', 'add'), |
| 115 | +('trial', 'fix'), |
| 116 | +('trial', 'reification'), |
| 117 | +('trial', 'explain'), |
| 118 | +('trial', 'openGPTDialog'), |
| 119 | +('trial', 'generate'), |
| 120 | +('silver', 'add'), |
| 121 | +('silver', 'fix'), |
| 122 | +('silver', 'reification'), |
| 123 | +('silver', 'explain'), |
| 124 | +('silver', 'openGPTDialog'), |
| 125 | +('silver', 'generate'), |
| 126 | +('gold', 'add'), |
| 127 | +('gold', 'fix'), |
| 128 | +('gold', 'reification'), |
| 129 | +('gold', 'explain'), |
| 130 | +('gold', 'openGPTDialog'), |
| 131 | +('gold', 'generate'), |
| 132 | +('platinum', 'add'), |
| 133 | +('platinum', 'fix'), |
| 134 | +('platinum', 'reification'), |
| 135 | +('platinum', 'explain'), |
| 136 | +('platinum', 'openGPTDialog'), |
| 137 | +('platinum', 'generate'); |
| 138 | + |
| 139 | + |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | +CREATE OR REPLACE FUNCTION notify_user_status() RETURNS trigger AS $notify_user_status$ |
| 144 | +DECLARE |
| 145 | + to_email TEXT; |
| 146 | + user_name TEXT; |
| 147 | +BEGIN |
| 148 | + IF OLD.status IS DISTINCT FROM NEW.status THEN |
| 149 | + to_email := NEW.email; |
| 150 | + user_name := NEW.username; |
| 151 | + PERFORM pg_notify('user_status', to_email || ',' || user_name || ',' || NEW.status::TEXT); |
| 152 | + END IF; |
| 153 | + RETURN NEW; |
| 154 | +END; |
| 155 | +$notify_user_status$ LANGUAGE plpgsql; |
| 156 | + |
| 157 | +CREATE TRIGGER send_status_email |
| 158 | +AFTER UPDATE OF status ON Users |
| 159 | +FOR EACH ROW |
| 160 | +WHEN (OLD.status IS DISTINCT FROM NEW.status) |
| 161 | +EXECUTE FUNCTION notify_user_status() ; |
| 162 | + |
| 163 | + |
| 164 | + |
| 165 | + |
| 166 | + |
| 167 | +CREATE OR REPLACE FUNCTION notify_policy_status() RETURNS trigger AS $notify_policy_status$ |
| 168 | +DECLARE |
| 169 | + to_email TEXT; |
| 170 | + user_name TEXT; |
| 171 | +BEGIN |
| 172 | + IF OLD.status IS DISTINCT FROM NEW.status THEN |
| 173 | + SELECT email INTO to_email FROM Users WHERE username = NEW.username; |
| 174 | + user_name := NEW.username; |
| 175 | + PERFORM pg_notify('policy_status', to_email || ',' || user_name || ',' || NEW.status::TEXT); |
| 176 | + END IF; |
| 177 | + RETURN NEW; |
| 178 | +END; |
| 179 | +$notify_policy_status$ LANGUAGE plpgsql; |
| 180 | + |
| 181 | +CREATE TRIGGER send_policy_status_email |
| 182 | +AFTER UPDATE OF status ON UserSubscribesPolicy |
| 183 | +FOR EACH ROW |
| 184 | +WHEN (OLD.status IS DISTINCT FROM NEW.status) |
| 185 | +EXECUTE FUNCTION notify_policy_status() ; |
| 186 | + |
0 commit comments