-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate.js
More file actions
37 lines (33 loc) · 1.15 KB
/
Copy pathmigrate.js
File metadata and controls
37 lines (33 loc) · 1.15 KB
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
import initSqlJs from 'sql.js';
import { readFileSync, writeFileSync } from 'fs';
(async () => {
const SQL = await initSqlJs();
const file = readFileSync('server/exchange.db');
const db = new SQL.Database(file);
let count = 0;
const blocks = db.exec('SELECT * FROM trade_blocks');
if (blocks.length > 0) {
const rows = blocks[0].values;
for (const r of rows) {
const data = JSON.parse(r[3]); // trade_data
if (data.type === 'announcement') {
const index = r[0];
// delete from trade
db.run('DELETE FROM trade_blocks WHERE block_index = ?', [index]);
// insert into registry
try {
db.run(
'INSERT INTO registry_blocks (block_index, previous_hash, timestamp, registry_data, matcher_pubkey, signature, hash) VALUES (?, ?, ?, ?, ?, ?, ?)',
[index, r[1], r[2], r[3], r[4], r[5], r[6]]
);
count++;
} catch (e) {
// might fail if it already exists
}
}
}
}
const outData = db.export();
writeFileSync('server/exchange.db', Buffer.from(outData));
console.log('Migrated ' + count + ' announcement blocks');
})();