forked from byteball/ocore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlite_migrations.js
296 lines (291 loc) · 14.6 KB
/
sqlite_migrations.js
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*jslint node: true */
"use strict";
var eventBus = require('./event_bus.js');
var constants = require("./constants.js");
var conf = require("./conf.js");
var VERSION = 29;
var async = require('async');
var bCordova = (typeof window === 'object' && window.cordova);
function migrateDb(connection, onDone){
connection.db[bCordova ? 'query' : 'all']("PRAGMA user_version", function(err, result){
if (err)
throw Error("PRAGMA user_version failed: "+err);
var rows = bCordova ? result.rows : result;
if (rows.length !== 1)
throw Error("PRAGMA user_version returned "+rows.length+" rows");
var version = rows[0].user_version;
console.log("db version "+version+", software version "+VERSION);
if (version > VERSION)
throw Error("user version "+version+" > "+VERSION+": looks like you are using a new database with an old client");
if (version === VERSION)
return onDone();
var arrQueries = [];
async.series([
function(cb){
if (version < 1){
connection.addQuery(arrQueries, "CREATE INDEX IF NOT EXISTS unitAuthorsIndexByAddressDefinitionChash ON unit_authors(address, definition_chash)");
connection.addQuery(arrQueries, "CREATE INDEX IF NOT EXISTS outputsIsSerial ON outputs(is_serial)");
connection.addQuery(arrQueries, "CREATE INDEX IF NOT EXISTS bySequence ON units(sequence)");
}
if (version < 2){
connection.addQuery(arrQueries, "CREATE UNIQUE INDEX IF NOT EXISTS hcobyAddressMci ON headers_commission_outputs(address, main_chain_index)");
connection.addQuery(arrQueries, "CREATE UNIQUE INDEX IF NOT EXISTS byWitnessAddressMci ON witnessing_outputs(address, main_chain_index)");
connection.addQuery(arrQueries, "CREATE INDEX IF NOT EXISTS inputsIndexByAddressTypeToMci ON inputs(address, type, to_main_chain_index)");
connection.addQuery(arrQueries, "DELETE FROM known_bad_joints");
}
if (version < 5){
connection.addQuery(arrQueries, "CREATE TABLE IF NOT EXISTS push_registrations (registrationId TEXT, device_address TEXT NOT NULL, PRIMARY KEY (device_address))");
}
if (version < 6){
connection.addQuery(arrQueries, "CREATE TABLE IF NOT EXISTS chat_messages ( \n\
id INTEGER PRIMARY KEY, \n\
correspondent_address CHAR(33) NOT NULL, \n\
message LONGTEXT NOT NULL, \n\
creation_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, \n\
is_incoming INTEGER(1) NOT NULL, \n\
type CHAR(15) NOT NULL DEFAULT 'text', \n\
FOREIGN KEY (correspondent_address) REFERENCES correspondent_devices(device_address) \n\
)");
connection.addQuery(arrQueries, "CREATE INDEX IF NOT EXISTS chatMessagesIndexByDeviceAddress ON chat_messages(correspondent_address, id)");
connection.addQuery(arrQueries, "ALTER TABLE correspondent_devices ADD COLUMN my_record_pref INTEGER DEFAULT 1");
connection.addQuery(arrQueries, "ALTER TABLE correspondent_devices ADD COLUMN peer_record_pref INTEGER DEFAULT 1");
connection.addQuery(arrQueries, "DELETE FROM known_bad_joints");
}
if (version < 8) {
connection.addQuery(arrQueries, "CREATE INDEX IF NOT EXISTS bySequence ON units(sequence)");
connection.addQuery(arrQueries, "DELETE FROM known_bad_joints");
}
if(version < 9){
connection.addQuery(arrQueries, "CREATE TABLE IF NOT EXISTS watched_light_units (peer VARCHAR(100) NOT NULL, unit CHAR(44) NOT NULL, creation_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (peer, unit))");
connection.addQuery(arrQueries, "CREATE INDEX IF NOT EXISTS wlabyUnit ON watched_light_units(unit)");
}
if(version < 10){
connection.addQuery(arrQueries, "BEGIN TRANSACTION");
connection.addQuery(arrQueries, "ALTER TABLE chat_messages RENAME TO chat_messages_old");
connection.addQuery(arrQueries, "CREATE TABLE IF NOT EXISTS chat_messages ( \n\
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, \n\
correspondent_address CHAR(33) NOT NULL, \n\
message LONGTEXT NOT NULL, \n\
creation_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, \n\
is_incoming INTEGER(1) NOT NULL, \n\
type CHAR(15) NOT NULL DEFAULT 'text', \n\
FOREIGN KEY (correspondent_address) REFERENCES correspondent_devices(device_address) ON DELETE CASCADE \n\
)");
connection.addQuery(arrQueries, "INSERT INTO chat_messages SELECT * FROM chat_messages_old");
connection.addQuery(arrQueries, "DROP TABLE chat_messages_old");
connection.addQuery(arrQueries, "CREATE INDEX chatMessagesIndexByDeviceAddress ON chat_messages(correspondent_address, id);");
connection.addQuery(arrQueries, "COMMIT");
connection.addQuery(arrQueries, "DELETE FROM known_bad_joints");
connection.addQuery(arrQueries, "DELETE FROM unhandled_joints");
connection.addQuery(arrQueries, "DELETE FROM dependencies");
connection.addQuery(arrQueries, "DELETE FROM hash_tree_balls");
connection.addQuery(arrQueries, "DELETE FROM catchup_chain_balls");
}
if (version < 11) {
connection.addQuery(arrQueries, "CREATE TABLE IF NOT EXISTS bots ( \n\
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, \n\
rank INTEGER NOT NULL DEFAULT 0, \n\
name VARCHAR(100) NOT NULL UNIQUE, \n\
pairing_code VARCHAR(200) NOT NULL, \n\
description LONGTEXT NOT NULL \n\
);");
}
if (version < 12)
connection.addQuery(arrQueries, "DELETE FROM known_bad_joints");
if (version < 13){
connection.addQuery(arrQueries, "ALTER TABLE unit_authors ADD COLUMN _mci INT NULL");
connection.addQuery(arrQueries, "PRAGMA user_version=13");
}
if (version < 14){
connection.addQuery(arrQueries, "UPDATE unit_authors SET _mci=(SELECT main_chain_index FROM units WHERE units.unit=unit_authors.unit)");
connection.addQuery(arrQueries, "CREATE INDEX IF NOT EXISTS unitAuthorsIndexByAddressMci ON unit_authors(address, _mci)");
}
if (version < 15){
connection.addQuery(arrQueries, "CREATE TABLE IF NOT EXISTS asset_metadata ( \n\
asset CHAR(44) NOT NULL PRIMARY KEY, \n\
metadata_unit CHAR(44) NOT NULL, \n\
registry_address CHAR(32) NULL, \n\
suffix VARCHAR(20) NULL, \n\
name VARCHAR(20) NULL, \n\
decimals TINYINT NULL, \n\
creation_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, \n\
UNIQUE (name, registry_address), \n\
FOREIGN KEY (asset) REFERENCES assets(unit), \n\
FOREIGN KEY (metadata_unit) REFERENCES units(unit) \n\
)");
}
if (version < 16){
connection.addQuery(arrQueries, "CREATE TABLE IF NOT EXISTS sent_mnemonics ( \n\
unit CHAR(44) NOT NULL, \n\
address CHAR(32) NOT NULL, \n\
mnemonic VARCHAR(107) NOT NULL, \n\
textAddress VARCHAR(120) NOT NULL, \n\
creation_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, \n\
FOREIGN KEY (unit) REFERENCES units(unit) \n\
)");
connection.addQuery(arrQueries, "CREATE INDEX IF NOT EXISTS sentByAddress ON sent_mnemonics(address)");
connection.addQuery(arrQueries, "CREATE INDEX IF NOT EXISTS sentByUnit ON sent_mnemonics(unit)");
connection.addQuery(arrQueries, "DELETE FROM known_bad_joints");
}
if (version < 17){
connection.addQuery(arrQueries, "CREATE TABLE IF NOT EXISTS private_profiles ( \n\
private_profile_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, \n\
unit CHAR(44) NOT NULL, \n\
payload_hash CHAR(44) NOT NULL, \n\
attestor_address CHAR(32) NOT NULL, \n\
address CHAR(32) NOT NULL, \n\
src_profile TEXT NOT NULL, \n\
creation_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, \n\
FOREIGN KEY (unit) REFERENCES units(unit) \n\
)");
connection.addQuery(arrQueries, "CREATE TABLE IF NOT EXISTS private_profile_fields ( \n\
private_profile_id INTEGER NOT NULL , \n\
`field` VARCHAR(50) NOT NULL, \n\
`value` VARCHAR(50) NOT NULL, \n\
blinding CHAR(16) NOT NULL, \n\
creation_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, \n\
UNIQUE (private_profile_id, `field`), \n\
FOREIGN KEY (private_profile_id) REFERENCES private_profiles(private_profile_id) \n\
)");
connection.addQuery(arrQueries, "CREATE INDEX IF NOT EXISTS ppfByField ON private_profile_fields(`field`)");
}
cb();
},
function(cb){
if (version < 18){
connection.addQuery(arrQueries, "CREATE TABLE IF NOT EXISTS attested_fields ( \n\
unit CHAR(44) NOT NULL, \n\
message_index TINYINT NOT NULL, \n\
attestor_address CHAR(32) NOT NULL, \n\
address CHAR(32) NOT NULL, \n\
`field` VARCHAR(50) NOT NULL, \n\
`value` VARCHAR(100) NOT NULL, \n\
PRIMARY KEY (unit, message_index, `field`), \n\
"+(conf.bLight ? '' : "CONSTRAINT attestationsByAttestorAddress FOREIGN KEY (attestor_address) REFERENCES addresses(address),")+" \n\
FOREIGN KEY (unit) REFERENCES units(unit) \n\
)");
connection.addQuery(arrQueries,
"CREATE INDEX IF NOT EXISTS attestedFieldsByAttestorFieldValue ON attested_fields(attestor_address, `field`, `value`)");
connection.addQuery(arrQueries, "CREATE INDEX IF NOT EXISTS attestedFieldsByAddressField ON attested_fields(address, `field`)");
connection.addQuery(arrQueries, "CREATE TABLE IF NOT EXISTS original_addresses ( \n\
unit CHAR(44) NOT NULL, \n\
address CHAR(32) NOT NULL, \n\
original_address VARCHAR(100) NOT NULL, \n\
PRIMARY KEY (unit, address), \n\
FOREIGN KEY (unit) REFERENCES units(unit) \n\
)");
connection.query(
"SELECT unit, message_index, attestor_address, address, payload FROM attestations CROSS JOIN messages USING(unit, message_index)",
function(rows){
rows.forEach(function(row){
var attestation = JSON.parse(row.payload);
if (attestation.address !== row.address)
throw Error("attestation.address !== row.address");
for (var field in attestation.profile){
var value = attestation.profile[field];
if (field.length <= constants.MAX_PROFILE_FIELD_LENGTH && typeof value === 'string' && value.length <= constants.MAX_PROFILE_VALUE_LENGTH){
connection.addQuery(arrQueries,
"INSERT "+connection.getIgnore()+" INTO attested_fields \n\
(unit, message_index, attestor_address, address, field, value) VALUES(?,?, ?,?, ?,?)",
[row.unit, row.message_index, row.attestor_address, row.address, field, value]);
}
}
});
cb();
}
);
}
else
cb();
},
function(cb){
if (version < 19)
connection.addQuery(arrQueries, "CREATE INDEX IF NOT EXISTS outputsIsSerial ON outputs(is_serial)");
if (version < 20)
connection.addQuery(arrQueries, "DELETE FROM known_bad_joints");
if (version < 21)
connection.addQuery(arrQueries, "ALTER TABLE push_registrations ADD COLUMN platform TEXT NOT NULL DEFAULT 'android'");
if (version < 22)
connection.addQuery(arrQueries, "CREATE INDEX IF NOT EXISTS sharedAddressSigningPathsByDeviceAddress ON shared_address_signing_paths(device_address);");
if (version < 23){
connection.addQuery(arrQueries, "CREATE TABLE IF NOT EXISTS peer_addresses ( \n\
address CHAR(32) NOT NULL, \n\
signing_paths VARCHAR(255) NULL, \n\
device_address CHAR(33) NOT NULL, \n\
definition TEXT NULL, \n\
creation_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, \n\
PRIMARY KEY (address), \n\
FOREIGN KEY (device_address) REFERENCES correspondent_devices(device_address) \n\
)");
connection.addQuery(arrQueries, "CREATE TABLE IF NOT EXISTS prosaic_contracts ( \n\
hash CHAR(44) NOT NULL PRIMARY KEY, \n\
peer_address CHAR(32) NOT NULL, \n\
peer_device_address CHAR(33) NOT NULL, \n\
my_address CHAR(32) NOT NULL, \n\
is_incoming TINYINT NOT NULL, \n\
creation_date TIMESTAMP NOT NULL, \n\
ttl INT NOT NULL DEFAULT 168, -- 168 hours = 24 * 7 = 1 week \n\
status TEXT CHECK (status IN('pending', 'revoked', 'accepted', 'declined')) NOT NULL DEFAULT 'active', \n\
title VARCHAR(1000) NOT NULL, \n\
`text` TEXT NOT NULL, \n\
shared_address CHAR(32), \n\
unit CHAR(44), \n\
cosigners VARCHAR(1500), \n\
FOREIGN KEY (my_address) REFERENCES my_addresses(address) \n\
)");
}
if (version < 24){
connection.addQuery(arrQueries, "BEGIN TRANSACTION");
connection.addQuery(arrQueries, "CREATE TABLE asset_attestors_new ( \n\
unit CHAR(44) NOT NULL, \n\
message_index TINYINT NOT NULL, \n\
asset CHAR(44) NOT NULL, -- in the initial attestor list: same as unit \n\
attestor_address CHAR(32) NOT NULL, \n\
PRIMARY KEY (unit, message_index, attestor_address), \n\
UNIQUE (asset, attestor_address, unit), \n\
FOREIGN KEY (unit) REFERENCES units(unit), \n\
CONSTRAINT assetAttestorsByAsset FOREIGN KEY (asset) REFERENCES assets(unit) \n\
)");
connection.addQuery(arrQueries, "INSERT INTO asset_attestors_new SELECT * FROM asset_attestors");
connection.addQuery(arrQueries, "DROP TABLE asset_attestors");
connection.addQuery(arrQueries, "ALTER TABLE asset_attestors_new RENAME TO asset_attestors");
connection.addQuery(arrQueries, "COMMIT");
}
if (version < 25)
connection.addQuery(arrQueries, "ALTER TABLE correspondent_devices ADD COLUMN is_blackhole TINYINT NOT NULL DEFAULT 0");
if (version < 26){
connection.addQuery(arrQueries, "ALTER TABLE correspondent_devices ADD COLUMN push_enabled TINYINT NOT NULL DEFAULT 1");
connection.addQuery(arrQueries, "CREATE TABLE IF NOT EXISTS correspondent_settings ( \n\
device_address CHAR(33) NOT NULL, \n\
correspondent_address CHAR(33) NOT NULL, \n\
push_enabled TINYINT NOT NULL, \n\
creation_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, \n\
PRIMARY KEY (device_address, correspondent_address) \n\
)");
connection.addQuery(arrQueries, "PRAGMA user_version=26");
}
if (version < 27){
connection.addQuery(arrQueries, "CREATE UNIQUE INDEX IF NOT EXISTS unqPayloadHash ON private_profiles(payload_hash)");
}
if (version < 28){
connection.addQuery(arrQueries, "ALTER TABLE units ADD COLUMN timestamp INT NOT NULL DEFAULT 0");
}
if (version < 29)
connection.addQuery(arrQueries, "DELETE FROM known_bad_joints");
cb();
}
], function(){
connection.addQuery(arrQueries, "PRAGMA user_version="+VERSION);
eventBus.emit('started_db_upgrade');
if (typeof window === 'undefined')
console.error("=== will upgrade the database, it can take some time");
async.series(arrQueries, function(){
eventBus.emit('finished_db_upgrade');
if (typeof window === 'undefined')
console.error("=== db upgrade finished");
onDone();
});
});
});
}
exports.migrateDb = migrateDb;