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);
default_settings.json
74 74
	    "f1d9dc9f2f4edaaebd9167eb8b79a6da6434313df47921aa4ea8ae278f7739e3": "/**\n * Haketilo demo script.\n *\n * Copyright (C) 2021 Wojtek Kosior\n * Available under the terms of Creative Commons Zero\n * <https://creativecommons.org/publicdomain/zero/1.0/legalcode>\n */\n\n{\n    const banner = document.createElement(\"h2\");\n    \n    banner.textContent = \"Hoooray! Haketilo works :D\";\n    \n    banner.setAttribute(\"style\", `\\\n    margin: 1em; \\\n    border-radius: 1em 0px; \\\n    background-color: #474; \\\n    padding: 10px 20px; \\\n    color: #eee; \\\n    box-shadow: 0 6px 8px 0 rgba(0,0,0,0.24), 0 17px 50px 0 rgba(0,0,0,0.19); \\\n    display: inline-block;\\\n    `);\n    \n    document.body.prepend(banner);\n}\n"
75 75
	}
76 76
    },
77
    "repo": ["https://hydrilla.koszko.org/api_v1/"]
77
    "repo": ["https://hydrilla.koszko.org/api_v2/"]
78 78
}
test/haketilo_test/test_integration.py
48 48

  
49 49
    driver.find_element_by_id('repos_head').click()
50 50
    lst = driver.find_element_by_css_selector('.active_tab .text_entries')
51
    assert 'https://hydrilla.koszko.org/api_v1' in lst.text
51
    assert 'https://hydrilla.koszko.org/api_v2' in lst.text
52 52

  
53 53
    # TODO: do some more tests, including popup interaction and repository
54 54
    # querying
test/haketilo_test/unit/test_indexeddb.py
50 50
        'identifier': 'helloapple'
51 51
    }
52 52

  
53
@pytest.mark.get_page('https://gotmyowndoma.in')
54
def test_haketilodb_update_1_0_1(driver, execute_in_page):
55
    """
56
    indexeddb.js modifies data when updating to database version 1.0.1. Verify
57
    the update procedure works properly.
58
    """
59
    execute_in_page(load_script('common/indexeddb.js'))
60
    execute_in_page('db_version_nr = version_nr([1, 0, 0]);')
61
    mock_broadcast(execute_in_page)
62

  
63
    # Start with no database.
64
    clear_indexeddb(execute_in_page)
65

  
66
    v1_url = 'https://hydrilla.koszko.org/api_v1/'
67
    v2_url = 'https://hydrilla.koszko.org/api_v2/'
68
    urls_v1 = {'https://sample.url/abc/', v1_url}
69
    urls_v2 = {'https://sample.url/abc/', v2_url}
70

  
71
    for url in urls_v1:
72
        execute_in_page('returnval(set_repo(arguments[0]));', url)
73

  
74
    assert urls_v1 == set(execute_in_page('returnval(get_repos());'))
75

  
76
    # Verify that url gets updated to v2 upon database update.
77
    driver.get(driver.execute_script('return window.location.href;'))
78
    execute_in_page(load_script('common/indexeddb.js'))
79
    mock_broadcast(execute_in_page)
80

  
81
    database_contents = get_db_contents(execute_in_page)
82

  
83
    assert set(execute_in_page('returnval(get_repos());')) == urls_v2
84

  
85
    # Verify that url does not get updated when there is no database update.
86
    execute_in_page('returnval(del_repo(arguments[0]));', v2_url)
87
    execute_in_page('returnval(set_repo(arguments[0]));', v1_url)
88

  
89
    driver.get(driver.execute_script('return window.location.href;'))
90
    execute_in_page(load_script('common/indexeddb.js'))
91
    mock_broadcast(execute_in_page)
92

  
93
    database_contents = get_db_contents(execute_in_page)
94

  
95
    assert set(execute_in_page('returnval(get_repos());')) == urls_v1
96

  
53 97
@pytest.mark.get_page('https://gotmyowndoma.in')
54 98
def test_haketilodb_item_modifications(driver, execute_in_page):
55 99
    """

Also available in: Unified diff