| 1322 | | |
| | 1322 | |
| | 1323 | $new_schema_version = 60; |
| | 1324 | if ($schema_version < $new_schema_version && $new_schema_version <= $targetSchema) { |
| | 1325 | printUpdateVersion($new_schema_version); |
| | 1326 | $sql .= "\n\nUPDATE schema_info SET value='$new_schema_version' WHERE tag='schema_version';\n"; |
| | 1327 | $sql .= "CREATE TABLE token_templates \n"; |
| | 1328 | $sql .= "( \n"; |
| | 1329 | $sql .= "token_template_id text PRIMARY KEY, \n"; |
| | 1330 | $sql .= "token_template_network text REFERENCES networks (network_id) ON UPDATE CASCADE ON DELETE CASCADE NOT NULL, -- (Note: Server-wide tokens aren't supported, but the code will look up the tokens of networks you peer with) \n"; |
| | 1331 | $sql .= "token_template_creation_date timestamp NOT NULL DEFAULT now(),\n"; |
| | 1332 | $sql .= "token_max_incoming_data integer, -- Ex: Allows capping bandwidth \n"; |
| | 1333 | $sql .= "token_max_outgoing_data integer, -- Ex: Allows capping bandwidth \n"; |
| | 1334 | $sql .= "token_max_total_data integer, -- Ex: Allows capping bandwidth \n"; |
| | 1335 | $sql .= "token_max_connection_duration interval, -- Ex: Allows limiting the length of a single connection \n"; |
| | 1336 | $sql .= "token_max_usage_duration interval, -- Ex: Allows selling access by the hour (counting only when in use) \n"; |
| | 1337 | $sql .= "token_max_wall_clock_duration interval, -- Ex: Allows selling daily, weekly or monthly passes (starting the count as soon as the token is first used) \n"; |
| | 1338 | $sql .= "token_max_age interval, -- Ex: Allow setting a maximum time before expiration (starting the count as soon as the token is issued) \n"; |
| | 1339 | $sql .= "token_is_reusable boolean DEFAULT true -- Can a user connect again using this token? (normally, yes) \n"; |
| | 1340 | |
| | 1341 | $sql .= ");\n\n"; |
| | 1342 | |
| | 1343 | $sql .= "CREATE TABLE tokens_template_valid_nodes -- (Unfortunately, for hotels selling 24h access to their clients, we have to consider that their network may consist of more than one node. If the token has no entry in this table, it's considered valid everywhere on the Network (and it's peers)) \n"; |
| | 1344 | $sql .= "( \n"; |
| | 1345 | $sql .= "token_template_id text REFERENCES token_templates (token_template_id) ON UPDATE CASCADE ON DELETE CASCADE NOT NULL, \n"; |
| | 1346 | $sql .= "token_valid_at_node text REFERENCES nodes (node_id) ON UPDATE CASCADE ON DELETE CASCADE NOT NULL, \n"; |
| | 1347 | $sql .= "PRIMARY KEY (token_template_id, token_valid_at_node) \n"; |
| | 1348 | $sql .= ");\n\n"; |
| | 1349 | |
| | 1350 | $sql .= "CREATE TABLE token_lots \n"; |
| | 1351 | $sql .= "( \n"; |
| | 1352 | $sql .= "token_lot_id text PRIMARY KEY, \n"; |
| | 1353 | $sql .= "token_lot_comment text, -- A free-form comment about the lot text \n"; |
| | 1354 | $sql .= "token_lot_creation_date timestamp NOT NULL DEFAULT now()\n"; |
| | 1355 | $sql .= ");\n\n"; |
| | 1356 | |
| | 1357 | $sql .= "CREATE TABLE tokens \n"; |
| | 1358 | $sql .= "( \n"; |
| | 1359 | $sql .= "token_id text PRIMARY KEY, \n"; |
| | 1360 | $sql .= "token_template_id text REFERENCES token_templates (token_template_id) ON UPDATE CASCADE ON DELETE CASCADE, \n"; |
| | 1361 | $sql .= "token_status text REFERENCES token_status (token_status) ON UPDATE CASCADE ON DELETE RESTRICT, \n"; |
| | 1362 | $sql .= "token_lot_id text REFERENCES token_lots (token_lot_id) ON UPDATE CASCADE ON DELETE CASCADE, \n"; |
| | 1363 | $sql .= "token_creation_date timestamp NOT NULL DEFAULT now(), -- (not the same as connection start time) \n"; |
| | 1364 | $sql .= "token_issuer text REFERENCES users (user_id) ON UPDATE CASCADE ON DELETE CASCADE NOT NULL, -- A user in the system. User responsible for the creation of the token (not necessarily the same as the one using it), \n"; |
| | 1365 | $sql .= "token_owner text REFERENCES users (user_id) ON UPDATE CASCADE ON DELETE CASCADE -- The user that can USE the token, anyone if empty.\n"; |
| | 1366 | $sql .= ");\n\n"; |
| | 1367 | |
| | 1368 | $sql .= "INSERT INTO tokens (token_id, token_status, token_creation_date, token_issuer, token_owner) SELECT token AS token_id, token_status, timestamp_in AS token_creation_date, user_id AS token_issuer, user_id AS token_owner FROM connections; \n"; |
| | 1369 | $sql .= "CREATE INDEX idx_token_status ON tokens (token_status);\n"; |
| | 1370 | $sql .= "ALTER TABLE connections ADD CONSTRAINT fk_tokens FOREIGN KEY (token) REFERENCES tokens (token_id) ON UPDATE CASCADE ON DELETE RESTRICT; \n"; |
| | 1371 | |
| | 1372 | $sql .= "ALTER TABLE connections DROP column token_status; \n"; |
| | 1373 | $sql .= "ALTER TABLE connections ADD COLUMN max_total_bytes integer;\n"; |
| | 1374 | $sql .= "ALTER TABLE connections ALTER COLUMN max_total_bytes SET DEFAULT NULL;\n"; |
| | 1375 | $sql .= "ALTER TABLE connections ADD COLUMN max_incoming_bytes integer;\n"; |
| | 1376 | $sql .= "ALTER TABLE connections ALTER COLUMN max_incoming_bytes SET DEFAULT NULL;\n"; |
| | 1377 | $sql .= "ALTER TABLE connections ADD COLUMN max_outgoing_bytes integer;\n"; |
| | 1378 | $sql .= "ALTER TABLE connections ALTER COLUMN max_outgoing_bytes SET DEFAULT NULL;\n"; |
| | 1379 | $sql .= "ALTER TABLE connections ADD COLUMN expiration_date timestamp;\n"; |
| | 1380 | $sql .= "ALTER TABLE connections ALTER COLUMN expiration_date SET DEFAULT NULL;\n"; |
| | 1381 | $sql .= "ALTER TABLE connections ADD COLUMN logout_reason integer;\n"; |
| | 1382 | $sql .= "ALTER TABLE connections ALTER COLUMN logout_reason SET DEFAULT NULL;\n"; |
| | 1383 | $sql .= "ALTER TABLE connections RENAME COLUMN token TO token_id;\n"; |
| | 1384 | } |
| | 1385 | /* |
| | 1386 | |
| | 1387 | |
| | 1388 | */ |