Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# Copyright 2019 PrivateStorage.io, LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import (
unicode_literals,
)
"""
This module defines the database schema used by the model interface.
"""
def get_schema_version(cursor):
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS [version] AS SELECT 0 AS [version]
"""
)
cursor.execute(
"""
SELECT [version] FROM [version]
"""
)
[(actual_version,)] = cursor.fetchall()
return actual_version
def get_schema_upgrades(from_version):
"""
Generate unicode strings containing SQL expressions to alter a schema from
``from_version`` to the latest version.
:param int from_version: The version of the schema which may require
upgrade.
"""
while from_version in _UPGRADES:
for upgrade in _UPGRADES[from_version]:
yield upgrade
yield _INCREMENT_VERSION
from_version += 1
def run_schema_upgrades(upgrades, cursor):
"""
Apply the given upgrades using the given cursor.
:param list[unicode] upgrades: The SQL statements to apply for the
upgrade.
:param cursor: A DB-API cursor to use to run the SQL.
"""
for upgrade in upgrades:
cursor.execute(upgrade)
_INCREMENT_VERSION = (
"""
UPDATE [version]
SET [version] = [version] + 1
"""
)
# A mapping from old schema versions to lists of unicode strings of SQL to
# execute against that version of the schema to create the successor schema.
_UPGRADES = {
0: [
"""
CREATE TABLE [vouchers] (
[number] text,
[created] text, -- An ISO8601 date+time string.
[state] text DEFAULT "pending", -- pending, double-spend, redeemed
[finished] text DEFAULT NULL, -- ISO8601 date+time string when
-- the current terminal state was entered.
[token-count] num DEFAULT NULL, -- Set in the redeemed state to the number
-- of tokens received on this voucher's
-- redemption.
PRIMARY KEY([number])
)
""",
"""
CREATE TABLE [tokens] (
[text] text, -- The random string that defines the token.
[voucher] text, -- Reference to the voucher these tokens go with.
PRIMARY KEY([text])
FOREIGN KEY([voucher]) REFERENCES [vouchers]([number])
)
""",
"""
CREATE TABLE [unblinded-tokens] (
[token] text, -- The base64 encoded unblinded token.
PRIMARY KEY([token])
)
""",
"""
CREATE TABLE [lease-maintenance-spending] (
[id] integer, -- A unique identifier for a group of activity.
[started] text, -- ISO8601 date+time string when the activity began.
[finished] text, -- ISO8601 date+time string when the activity completed (or null).
-- The number of passes that would be required to renew all
-- shares encountered during this activity. Note that because
-- leases on different shares don't necessarily expire at the
-- same time this is not necessarily the number of passes
-- **actually** used during this activity. Some shares may
-- not have required lease renewal. Also note that while the
-- activity is ongoing this value may change.
[count] integer,
PRIMARY KEY([id])
)
""",
],
}
def _check_consistency():
if _UPGRADES.keys() != range(len(_UPGRADES)):
raise TypeError("Inconsistent schema versions in schema upgraders.")
_check_consistency()