~larry-e-works/uci-engine/write-exitcode-to-file

« back to all changes in this revision

Viewing changes to nf-stats-service/db_patches/000-initial_schema.sql

  • Committer: Thomi Richards
  • Date: 2014-06-27 20:02:44 UTC
  • mto: (629.2.9 nfss)
  • mto: This revision was merged to the branch mainline in revision 636.
  • Revision ID: thomi.richards@canonical.com-20140627200244-zi7dwxnyw38ypr2f
Initial version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
-- Non Functional Test Metrics Database Schema.
 
2
 
 
3
DROP TABLE IF EXISTS data;
 
4
DROP TABLE IF EXISTS test;
 
5
DROP TABLE IF EXISTS project;
 
6
 
 
7
CREATE SEQUENCE project_id_seq;
 
8
CREATE TABLE project (
 
9
    id      INTEGER PRIMARY KEY default nextval('project_id_seq'),
 
10
    name    varchar(128) NOT NULL,
 
11
    UNIQUE (name)    
 
12
);
 
13
ALTER SEQUENCE project_id_seq owned by project.id;
 
14
CREATE INDEX project_name_index ON project(name);
 
15
 
 
16
CREATE SEQUENCE test_id_seq;
 
17
CREATE TABLE test (
 
18
    id          INTEGER PRIMARY KEY default nextval('test_id_seq'),
 
19
    name        varchar(128) NOT NULL,
 
20
    project_id  integer,
 
21
    FOREIGN KEY (project_id) REFERENCES project(id),
 
22
    UNIQUE (project_id, name)
 
23
);
 
24
ALTER SEQUENCE test_id_seq owned by test.id;
 
25
CREATE INDEX test_name_index ON test(name);
 
26
 
 
27
-- The client table stores secret resource owner keys, as well as public
 
28
-- client and resource owner keys. We also store some information about
 
29
-- the client.
 
30
-- The 'valid' column defaults to true. We only accept connections from 
 
31
-- clients where this is set to 'true' and we set it to 'false' when 
 
32
-- invalidating client keys. We do this rather than deleting the key
 
33
-- because we still want to retain historical information about which
 
34
-- data points were inserted by which client.
 
35
CREATE SEQUENCE client_id_seq;
 
36
CREATE TABLE client (
 
37
    id                      INTEGER PRIMARY KEY default nextval('client_id_seq'),
 
38
    access_key              VARCHAR(30) NOT NULL,
 
39
    name                    TEXT,
 
40
    description             TEXT,
 
41
    point_of_contact        TEXT,
 
42
    resource_owner_key      VARCHAR(30) NOT NULL,
 
43
    resource_owner_secret   VARCHAR(30) NOT NULL,
 
44
    valid                   BOOLEAN default true,
 
45
    UNIQUE (access_key)
 
46
);
 
47
ALTER SEQUENCE client_id_seq owned by client.id;
 
48
-- we'll often want to look up a specific client access key, so let's index that:
 
49
CREATE INDEX client_access_key_index ON client(access_key);
 
50
-- We need to have a dummy client row added so the auth process is constant time:
 
51
-- Note that the resource owner key and secret need to be valid values, but can 
 
52
-- never be authenticated against.
 
53
INSERT INTO client (access_key, name, resource_owner_key, resource_owner_secret, valid) VALUES (
 
54
    'dummy-client-access-token', 
 
55
    'Dummy client', 
 
56
    'O9MFI1GUCrzBU6Uv1vshLX5wvhjDji', 
 
57
    '{wtgn,V>$#bn1fwqbe&,}/l1STF<@Y',
 
58
    false
 
59
);
 
60
-- We also add a client that's invalid (can never be authenticated) that's used 
 
61
-- for all the pre-populated data in the system.
 
62
INSERT INTO client (access_key, name, description, resource_owner_key, resource_owner_secret, valid) VALUES (
 
63
    'sys-prepopulated-data-client', 
 
64
    'System Client', 
 
65
    'This is not a real client - it is used to link to pre-populated data.',
 
66
    'Bad resource owner key', 
 
67
    'Bad resource owner secret',
 
68
    false
 
69
);
 
70
 
 
71
CREATE SEQUENCE data_id_seq;
 
72
CREATE TABLE data (
 
73
    id              INTEGER PRIMARY KEY default nextval('data_id_seq'),
 
74
    date_entered    timestamp with time zone NOT NULL,
 
75
    data            json NOT NULL,
 
76
    test_id         integer,
 
77
    client_id       integer,
 
78
    FOREIGN KEY (test_id) REFERENCES test(id),
 
79
    FOREIGN KEY (client_id) REFERENCES client(id)
 
80
);
 
81
ALTER SEQUENCE data_id_seq owned by data.id;
 
82
-- We will frequently be looking for data between two date ranges. Index the
 
83
-- date_entered column so this is efficient:
 
84
CREATE INDEX data_date_entered_index ON data(date_entered);
 
85
 
 
86
 
 
87
-- We need to store nonce values in the database. The nonce and timestamp are
 
88
-- stored with the access key and the resource owner key.
 
89
CREATE TABLE nonce (
 
90
    access_key          VARCHAR(30) NOT NULL,
 
91
    timestamp           timestamp without time zone NOT NULL,
 
92
    nonce               TEXT NOT NULL,
 
93
    resource_owner_key  TEXT
 
94
);
 
95
-- we'll always be searching for entries in all columns, so index that:
 
96
CREATE UNIQUE INDEX nonce_all_index ON nonce(access_key, timestamp, nonce, resource_owner_key);
 
97
-- We'll also be cleaning this table out periodically by sorting on the timestamp column:
 
98
CREATE INDEX nonce_timestamp_index ON nonce(timestamp);