Project

General

Profile

« Previous | Next » 

Revision aec5c9ae

Added by koszko about 1 year ago

automatically change repo url to point to api v2 endpoint

View differences:

common/indexeddb.js
53 53
);
54 54

  
55 55
/* Update when changes are made to database schema. Must have 3 elements */
56
const db_version = [1, 0, 0];
56
const db_version = [1, 0, 1];
57 57

  
58 58
const nr_reductor = ([i, s], num) => [i - 1, s + num * 1024 ** i];
59 59
const version_nr = ver => ver.slice(0, 3).reduce(nr_reductor, [2, 0])[1];
60 60

  
61
let db_version_nr = version_nr(db_version);
62

  
61 63
const stores = 	[
62 64
    ["file",      {keyPath: "sha256"}],
63 65
    ["file_uses", {keyPath: "sha256"}],
......
100 102
}
101 103

  
102 104
async function perform_upgrade(event) {
105
    if (event.oldVersion > db_version_nr)
106
	throw "bad db version: " + event.oldVersion;
107

  
103 108
    const opened_db = event.target.result;
109
    const transaction = event.target.transaction;
104 110

  
105
    /* When we move to a new database schema, we will add upgrade logic here. */
106
    if (event.oldVersion > 0)
107
	throw "bad db version: " + event.oldVersion;
111
    if (event.oldVersion == 0) {
112
	for (const [store_name, key_mode] of stores)
113
	    opened_db.createObjectStore(store_name, key_mode);
114
    }
108 115

  
109
    let store;
110
    for (const [store_name, key_mode] of stores)
111
	store = opened_db.createObjectStore(store_name, key_mode);
116
    if (event.oldVersion > 0 && event.oldVersion < db_version_nr) {
117
	const v1_url = "https://hydrilla.koszko.org/api_v1/";
118
	const v1_entry = await idb_get(transaction, "repo", v1_url);
112 119

  
113
    const ctx = make_context(store.transaction, initial_data.file);
114
    await _save_items(initial_data.resource, initial_data.mapping,
115
		      initial_data.repo || [], ctx);
120
	if (v1_entry) {
121
	    const v2_url = "https://hydrilla.koszko.org/api_v2/";
122

  
123
	    await idb_del(transaction, "repo", v1_url);
124
	    await idb_put(transaction, "repo", {url: v2_url});
125
	}
126
    }
127

  
128
    if (event.oldVersion == 0) {
129
	const ctx = make_context(transaction, initial_data.file);
130
	await _save_items(initial_data.resource, initial_data.mapping,
131
			  initial_data.repo || [], ctx);
132
    } else {
133
	await new Promise(
134
	    (...cbs) => [transaction.onsuccess, transaction.onerror] = cbs
135
	);
136
    }
116 137

  
117 138
    return opened_db;
118 139
}
......
125 146
    let resolve, reject;
126 147
    const waiter = new Promise((...cbs) => [resolve, reject] = cbs);
127 148

  
128
    const request = indexedDB.open("haketilo", version_nr(db_version));
149
    const request = indexedDB.open("haketilo", db_version_nr);
129 150
    request.onsuccess       = ev => resolve(ev.target.result);
130 151
    request.onerror         = ev => reject("db error: " + ev.target.errorCode);
131 152
    request.onupgradeneeded = ev => perform_upgrade(ev).then(resolve, reject);

Also available in: Unified diff