~hadware/magicicada-server/trusty-support

« back to all changes in this revision

Viewing changes to src/backends/db/schemas/fsync_main/__init__.py

  • Committer: Facundo Batista
  • Date: 2015-08-05 13:10:02 UTC
  • Revision ID: facundo@taniquetil.com.ar-20150805131002-he7b7k704d8o7js6
First released version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2008-2015 Canonical
 
2
#
 
3
# This program is free software: you can redistribute it and/or modify
 
4
# it under the terms of the GNU Affero General Public License as
 
5
# published by the Free Software Foundation, either version 3 of the
 
6
# License, or (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU Affero General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU Affero General Public License
 
14
# along with this program. If not, see <http://www.gnu.org/licenses/>.
 
15
#
 
16
# For further info, check  http://launchpad.net/filesync-server
 
17
 
 
18
"""Initializing schema.
 
19
 
 
20
It has all the create, delete and drop instructions to build the
 
21
database.
 
22
"""
 
23
 
 
24
from backends.db.tools.schema import Schema
 
25
 
 
26
__all__ = ["create_schema"]
 
27
 
 
28
 
 
29
def create_schema():
 
30
    '''Creates the Schema with all the instructions.'''
 
31
    # We pass this very package to schema, so we need to reimport ourselves
 
32
    from backends.db.schemas import fsync_main as patch_package
 
33
    return Schema(CREATE, DROP, DELETE, patch_package, 'patch_main')
 
34
 
 
35
 
 
36
CREATE = [
 
37
    """
 
38
    CREATE TYPE lifecycle_status as enum('Live', 'Dead');
 
39
    """,
 
40
    """
 
41
    CREATE TABLE StorageUser (
 
42
        id integer NOT NULL PRIMARY KEY,
 
43
        max_average_download_bytes_per_day bigint,
 
44
        status lifecycle_status
 
45
            DEFAULT 'Live'::lifecycle_status NOT NULL,
 
46
        visible_name character varying(256),
 
47
        username character varying(256) UNIQUE,
 
48
        subscription_status lifecycle_status
 
49
            DEFAULT 'Live'::lifecycle_status NOT NULL,
 
50
        shard_id Text DEFAULT 'storage',
 
51
        root_volume_id UUID
 
52
    );
 
53
    """,
 
54
    """
 
55
    COMMENT ON TABLE StorageUser IS 'StorageUsers that the \
 
56
    storage system is aware of.';
 
57
    """,
 
58
    """
 
59
    COMMENT ON COLUMN StorageUser.id IS 'A unique identifier for this record.';
 
60
    """,
 
61
    """
 
62
    COMMENT ON COLUMN StorageUser.status IS
 
63
        'Whether the record represents a live, valid StorageUser.';
 
64
    """,
 
65
    """
 
66
    COMMENT ON COLUMN storageuser.visible_name IS
 
67
    'The visible name of the user, the more human friendly of his/her \
 
68
    denominations.';
 
69
    """,
 
70
    """
 
71
    COMMENT ON COLUMN storageuser.username IS
 
72
        'The username of the user, its human friendly unique id.';
 
73
    """,
 
74
    """
 
75
    COMMENT ON COLUMN storageuser.subscription_status IS
 
76
        'The status of the user subscription.';
 
77
    """,
 
78
    """
 
79
    COMMENT ON COLUMN StorageUser.shard_id IS
 
80
        'The internal identifier for the database shard the user''s data is on'
 
81
    """,
 
82
    """
 
83
    COMMENT ON COLUMN StorageUser.root_volume_id IS
 
84
        'The root UserVolume id for this user';
 
85
    """,
 
86
    """
 
87
    CREATE TYPE access_level AS ENUM('View', 'Modify');
 
88
    """,
 
89
    """
 
90
    CREATE TABLE Share (
 
91
        shared_by INT NOT NULL REFERENCES StorageUser (id),
 
92
        subtree UUID NOT NULL,
 
93
        shared_to INT REFERENCES StorageUser (id),
 
94
        name text NOT NULL,
 
95
        accepted boolean NOT NULL,
 
96
        access access_level NOT NULL,
 
97
        when_shared timestamp without time zone
 
98
            DEFAULT timezone('UTC'::text, now()) NOT NULL,
 
99
        when_last_changed timestamp without time zone
 
100
            DEFAULT timezone('UTC'::text, now()) NOT NULL,
 
101
        status lifecycle_status
 
102
            DEFAULT 'Live'::lifecycle_status NOT NULL,
 
103
        id uuid NOT NULL PRIMARY KEY,
 
104
        email text
 
105
    );
 
106
    """,
 
107
    """
 
108
    COMMENT ON TABLE share IS 'Shares encompass both access control and \
 
109
    inter-user_id visbility of objects.  An object from a StorageUser''s \
 
110
    volume is visible to another StorageUser iff there is a Share entry \
 
111
    for it or for its ancestors.  Access granted is in terms of the highest p\
 
112
    ermission granted the StorageUser on the object or any of its \
 
113
    ancestors.  Owners always have full access to their own objects.  Note \
 
114
    that, given visibility, people can copy objects and do whatever they \
 
115
    like with the copies.'
 
116
    """,
 
117
    """
 
118
    COMMENT ON COLUMN share.id IS 'A unique identifier for the share';
 
119
    """,
 
120
    """
 
121
    COMMENT ON COLUMN share.shared_by IS 'The StorageUser who initiated the \
 
122
    share (can be different from the owner, though the owner must explicitly \
 
123
    approve all shares of subtrees she owns).';
 
124
    """,
 
125
    """
 
126
    COMMENT ON COLUMN Share.subtree IS
 
127
        'The root of the subtree being offered.';
 
128
    """,
 
129
    """
 
130
    COMMENT ON COLUMN Share.shared_to IS
 
131
        'The StorageUser to whom the share is being offered.';
 
132
    """,
 
133
    """
 
134
    COMMENT ON COLUMN share.name IS 'The name the StorageUser sees the share \
 
135
    as in his list of shares, once it has been accepted.';
 
136
    """,
 
137
    """
 
138
    COMMENT ON COLUMN Share.accepted IS
 
139
        'Whether the offered share has been accepted yet or not.';
 
140
    """,
 
141
    """
 
142
    COMMENT ON COLUMN Share.access IS
 
143
        'The access level granted by this share.';
 
144
    """,
 
145
    """
 
146
    COMMENT ON COLUMN Share.when_shared IS
 
147
        'Timestamp when the share was originally offered.';
 
148
    """,
 
149
    """
 
150
    COMMENT ON COLUMN share.when_last_changed IS 'Timestamp when details \
 
151
    of the share were changed (e.g. if it was accepted, or the granted \
 
152
    access was changed).';
 
153
    """,
 
154
    """
 
155
    COMMENT ON COLUMN Share.status IS
 
156
        'Whether this share is active/alive.';
 
157
    """,
 
158
    """
 
159
    COMMENT ON COLUMN share.email IS 'Email address that \
 
160
             this share was offered to.';
 
161
    """,
 
162
    """
 
163
    CREATE INDEX share_subtree_idx ON share USING btree (subtree);
 
164
    """,
 
165
    """
 
166
    CREATE INDEX share_shared_by_fkey ON Share(shared_by)
 
167
    """,
 
168
    """
 
169
    CREATE UNIQUE INDEX share_shared_to_key
 
170
        ON Share(shared_to, name) WHERE status='Live'
 
171
    """,
 
172
    """
 
173
    CREATE UNIQUE INDEX share_shared_to_shared_by_subtree
 
174
        ON Share(shared_to, shared_by, subtree)
 
175
        WHERE status='Live' AND shared_to IS NOT NULL
 
176
    """,
 
177
    """
 
178
    CREATE TABLE PublicFile (
 
179
        id SERIAL PRIMARY KEY,
 
180
        owner_id integer NOT NULL REFERENCES StorageUser (id),
 
181
        node_id uuid NOT NULL UNIQUE,
 
182
        public_uuid UUID UNIQUE);
 
183
    """,
 
184
    """
 
185
    COMMENT ON TABLE PublicFile IS
 
186
        'A mapping of public file IDs to storage objects.';
 
187
    """,
 
188
    """
 
189
    COMMENT ON COLUMN PublicFile.id IS
 
190
        'Unique identifier for this public file.';
 
191
    """,
 
192
    """
 
193
    COMMENT ON COLUMN PublicFile.owner_id IS 'The owner of this file.';
 
194
    """,
 
195
    """
 
196
    COMMENT ON COLUMN PublicFile.node_id IS 'The file to be made public.';
 
197
    """,
 
198
    """
 
199
    GRANT SELECT,INSERT,DELETE,UPDATE ON TABLE share TO storage, webapp;
 
200
    """,
 
201
    """
 
202
    GRANT SELECT,INSERT,DELETE,UPDATE ON TABLE storageuser TO storage, webapp;
 
203
    """,
 
204
    """
 
205
    GRANT SELECT,INSERT,DELETE,UPDATE ON TABLE PublicFile to storage, webapp;
 
206
    """,
 
207
    """
 
208
    GRANT SELECT,USAGE ON TABLE publicfile_id_seq to storage, webapp;
 
209
    """,
 
210
]
 
211
 
 
212
 
 
213
DROP = []
 
214
DELETE = [
 
215
    "DELETE FROM Share",
 
216
    "DELETE FROM PublicFile",
 
217
    "DELETE FROM StorageUser",
 
218
    "ALTER SEQUENCE PublicFile_id_seq RESTART WITH 1",
 
219
]