test_sqlthread refactoring

This commit is contained in:
shportix 2023-09-18 17:23:16 +03:00
parent 88f56b50e6
commit f9ab6097bf
No known key found for this signature in database
GPG Key ID: 2594F3870220CC1E
23 changed files with 5 additions and 451 deletions

View File

@ -1,3 +0,0 @@
ALTER TABLE inventory ADD first20bytesofencryptedmessage blob DEFAULT '';
UPDATE settings SET value = 2 WHERE key = 'version';

View File

@ -1,17 +0,0 @@
-- --
-- -- Update the address colunm to unique in addressbook table
-- --
ALTER TABLE addressbook RENAME TO old_addressbook;
CREATE TABLE `addressbook` (
`label` text ,
`address` text ,
UNIQUE(address) ON CONFLICT IGNORE
) ;
INSERT INTO addressbook SELECT label, address FROM old_addressbook;
DROP TABLE old_addressbook;
UPDATE settings SET value = 11 WHERE key = 'version';

View File

@ -1,31 +0,0 @@
--
-- Let's get rid of the first20bytesofencryptedmessage field in the inventory table.
--
CREATE TEMP TABLE `inventory_backup` (
`hash` blob ,
`objecttype` text ,
`streamnumber` int ,
`payload` blob ,
`receivedtime` int ,
UNIQUE(hash) ON CONFLICT REPLACE
) ;
INSERT INTO `inventory_backup` SELECT hash, objecttype, streamnumber, payload, receivedtime FROM inventory;
DROP TABLE inventory;
CREATE TABLE `inventory` (
`hash` blob ,
`objecttype` text ,
`streamnumber` int ,
`payload` blob ,
`receivedtime` int ,
UNIQUE(hash) ON CONFLICT REPLACE
) ;
INSERT INTO inventory SELECT hash, objecttype, streamnumber, payload, receivedtime FROM inventory_backup;
DROP TABLE inventory_backup;
UPDATE settings SET value = 3 WHERE key = 'version';

View File

@ -1,7 +0,0 @@
--
-- Add a new column to the inventory table to store tags.
--
ALTER TABLE inventory ADD tag blob DEFAULT '';
UPDATE settings SET value = 4 WHERE key = 'version';

View File

@ -1,19 +0,0 @@
--
-- Add a new column to the pubkeys table to store the address version.
-- We're going to trash all of our pubkeys and let them be redownloaded.
--
DROP TABLE pubkeys;
CREATE TABLE `pubkeys` (
`hash` blob ,
`addressversion` int ,
`transmitdata` blob ,
`time` int ,
`usedpersonally` text ,
UNIQUE(hash, addressversion) ON CONFLICT REPLACE
) ;
DELETE FROM inventory WHERE objecttype = 'pubkey';
UPDATE settings SET value = 5 WHERE key = 'version';

View File

@ -1,14 +0,0 @@
--
-- Add a new table: objectprocessorqueue with which to hold objects
-- that have yet to be processed if the user shuts down Bitmessage.
--
DROP TABLE knownnodes;
CREATE TABLE `objectprocessorqueue` (
`objecttype` text,
`data` blob,
UNIQUE(objecttype, data) ON CONFLICT REPLACE
) ;
UPDATE settings SET value = 6 WHERE key = 'version';

View File

@ -1,27 +0,0 @@
--
-- changes related to protocol v3
-- In table inventory and objectprocessorqueue, objecttype is now
-- an integer (it was a human-friendly string previously)
--
DROP TABLE inventory;
CREATE TABLE `inventory` (
`hash` blob,
`objecttype` int,
`streamnumber` int,
`payload` blob,
`expirestime` integer,
`tag` blob,
UNIQUE(hash) ON CONFLICT REPLACE
) ;
DROP TABLE objectprocessorqueue;
CREATE TABLE `objectprocessorqueue` (
`objecttype` int,
`data` blob,
UNIQUE(objecttype, data) ON CONFLICT REPLACE
) ;
UPDATE settings SET value = 7 WHERE key = 'version';

View File

@ -1,13 +0,0 @@
--
-- The format of data stored in the pubkeys table has changed. Let's
-- clear it, and the pubkeys from inventory, so that they'll
-- be re-downloaded.
--
DELETE FROM inventory WHERE objecttype = 1;
DELETE FROM pubkeys;
UPDATE sent SET status='msgqueued' WHERE status='doingmsgpow' or status='badkey';
UPDATE settings SET value = 8 WHERE key = 'version';

View File

@ -1,9 +0,0 @@
--
-- Add a new column to the inbox table to store the hash of
-- the message signature. We'll use this as temporary message UUID
-- in order to detect duplicates.
--
ALTER TABLE inbox ADD sighash blob DEFAULT '';
UPDATE settings SET value = 9 WHERE key = 'version';

View File

@ -1,76 +0,0 @@
CREATE TEMPORARY TABLE `sent_backup` (
`msgid` blob,
`toaddress` text,
`toripe` blob,
`fromaddress` text,
`subject` text,
`message` text,
`ackdata` blob,
`lastactiontime` integer,
`status` text,
`retrynumber` integer,
`folder` text,
`encodingtype` int
) ;
INSERT INTO sent_backup SELECT msgid, toaddress, toripe, fromaddress, subject, message, ackdata, lastactiontime, status, 0, folder, encodingtype FROM sent;
DROP TABLE sent;
CREATE TABLE `sent` (
`msgid` blob,
`toaddress` text,
`toripe` blob,
`fromaddress` text,
`subject` text,
`message` text,
`ackdata` blob,
`senttime` integer,
`lastactiontime` integer,
`sleeptill` int,
`status` text,
`retrynumber` integer,
`folder` text,
`encodingtype` int,
`ttl` int
) ;
INSERT INTO sent SELECT msgid, toaddress, toripe, fromaddress, subject, message, ackdata, lastactiontime, lastactiontime, 0, status, 0, folder, encodingtype, 216000 FROM sent_backup;
DROP TABLE sent_backup;
ALTER TABLE pubkeys ADD address text DEFAULT '' ;
--
-- replica for loop to update hashed address
--
UPDATE pubkeys SET address=(enaddr(pubkeys.addressversion, 1, hash));
CREATE TEMPORARY TABLE `pubkeys_backup` (
`address` text,
`addressversion` int,
`transmitdata` blob,
`time` int,
`usedpersonally` text,
UNIQUE(address) ON CONFLICT REPLACE
) ;
INSERT INTO pubkeys_backup SELECT address, addressversion, transmitdata, `time`, usedpersonally FROM pubkeys;
DROP TABLE pubkeys;
CREATE TABLE `pubkeys` (
`address` text,
`addressversion` int,
`transmitdata` blob,
`time` int,
`usedpersonally` text,
UNIQUE(address) ON CONFLICT REPLACE
) ;
INSERT INTO pubkeys SELECT address, addressversion, transmitdata, `time`, usedpersonally FROM pubkeys_backup;
DROP TABLE pubkeys_backup;
UPDATE settings SET value = 10 WHERE key = 'version';

View File

@ -1,100 +0,0 @@
CREATE TABLE `inbox` (
`msgid` blob,
`toaddress` text,
`fromaddress` text,
`subject` text,
`received` text,
`message` text,
`folder` text,
`encodingtype` int,
`read` bool,
`sighash` blob,
UNIQUE(msgid) ON CONFLICT REPLACE
) ;
CREATE TABLE `sent` (
`msgid` blob,
`toaddress` text,
`toripe` blob,
`fromaddress` text,
`subject` text,
`message` text,
`ackdata` blob,
`senttime` integer,
`lastactiontime` integer,
`sleeptill` integer,
`status` text,
`retrynumber` integer,
`folder` text,
`encodingtype` int,
`ttl` int
) ;
CREATE TABLE `subscriptions` (
`label` text,
`address` text,
`enabled` bool
) ;
CREATE TABLE `addressbook` (
`label` text,
`address` text,
UNIQUE(address) ON CONFLICT IGNORE
) ;
CREATE TABLE `blacklist` (
`label` text,
`address` text,
`enabled` bool
) ;
CREATE TABLE `whitelist` (
`label` text,
`address` text,
`enabled` bool
) ;
CREATE TABLE `pubkeys` (
`address` text,
`addressversion` int,
`transmitdata` blob,
`time` int,
`usedpersonally` text,
UNIQUE(address) ON CONFLICT REPLACE
) ;
CREATE TABLE `inventory` (
`hash` blob,
`objecttype` int,
`streamnumber` int,
`payload` blob,
`expirestime` integer,
`tag` blob,
UNIQUE(hash) ON CONFLICT REPLACE
) ;
INSERT INTO subscriptions VALUES ('Bitmessage new releases/announcements', 'BM-GtovgYdgs7qXPkoYaRgrLFuFKz1SFpsw', 1);
CREATE TABLE `settings` (
`key` blob,
`value` blob,
UNIQUE(key) ON CONFLICT REPLACE
) ;
INSERT INTO settings VALUES('version','11');
INSERT INTO settings VALUES('lastvacuumtime', CAST(strftime('%s', 'now') AS STR) );
CREATE TABLE `objectprocessorqueue` (
`objecttype` int,
`data` blob,
UNIQUE(objecttype, data) ON CONFLICT REPLACE
) ;

View File

@ -1,92 +0,0 @@
CREATE TABLE `inbox` (
`msgid` blob,
`toaddress` text,
`fromaddress` text,
`subject` text,
`received` text,
`message` text,
`folder` text,
`encodingtype` int,
`read` bool,
UNIQUE(msgid) ON CONFLICT REPLACE
);
CREATE TABLE `sent` (
`msgid` blob,
`toaddress` text,
`toripe` blob,
`fromaddress` text,
`subject` text,
`message` text,
`ackdata` blob,
`lastactiontime` integer,
`status` text,
`pubkeyretrynumber` integer,
`msgretrynumber` integer,
`folder` text,
`encodingtype` int
);
CREATE TABLE `subscriptions` (
`label` text,
`address` text,
`enabled` bool
);
CREATE TABLE `addressbook` (
`label` text,
`address` text
);
CREATE TABLE `blacklist` (
`label` text,
`address` text,
`enabled` bool
);
CREATE TABLE `whitelist` (
`label` text,
`address` text,
`enabled` bool
);
CREATE TABLE `pubkeys` (
`hash` blob,
`transmitdata` blob,
`time` int,
`usedpersonally` text,
UNIQUE(hash) ON CONFLICT REPLACE
);
CREATE TABLE `inventory` (
`hash` blob,
`objecttype` text,
`streamnumber` int,
`payload` blob,
`receivedtime` integer,
UNIQUE(hash) ON CONFLICT REPLACE
);
CREATE TABLE `knownnodes` (
`timelastseen` int,
`stream` int,
`services` blob,
`host` blob,
`port` blob,
UNIQUE(host, stream, port) ON CONFLICT REPLACE
);
CREATE TABLE `settings` (
`key` blob,
`value` blob,
UNIQUE(key) ON CONFLICT REPLACE
);
INSERT INTO subscriptions VALUES ('Bitmessage new releases/announcements', 'BM-GtovgYdgs7qXPkoYaRgrLFuFKz1SFpsw', 1);
INSERT INTO settings VALUES('version', 1);
INSERT INTO settings VALUES('lastvacuumtime', CAST(strftime('%s', 'now') AS STR) );
INSERT INTO inventory VALUES( '1', 'pubkey', 1, '1', CAST(strftime('%s', 'now') AS integer);

View File

@ -1 +0,0 @@
INSERT INTO `addressbook` VALUES ('test', "BM-2cWzMnxjJ7yRP3nLEWUV5LisTZyREWSxYz"), ('testone', "BM-2cWzMnxjJ7yRP3nLEWUV5LisTZyREWSxYz");

View File

@ -1 +0,0 @@
INSERT INTO `inventory` VALUES ('hash', 1, 1,1, 1,'test');

View File

@ -1 +0,0 @@
INSERT INTO `settings` VALUES ('version',3);

View File

@ -1,3 +0,0 @@
INSERT INTO `pubkeys` VALUES ('hash', 1, 1, 1,'test');
UPDATE settings SET value=4 WHERE key='version';

View File

@ -1 +0,0 @@
INSERT INTO `objectprocessorqueue` VALUES ('hash', 1);

View File

@ -1 +0,0 @@
INSERT INTO `inventory` VALUES ('hash', 1, 1, 1,'test','test');

View File

@ -1,3 +0,0 @@
INSERT INTO `sent` VALUES
(1,'BM-2cWzMnxjJ7yRP3nLEWUV5LisTZyREWSxYz',1,'BM-2cWzSnwjJ7yRP3nLEWUV5LisTZyREWSzUK','Test1 subject','message test 1','ackdata',1638176409,1638176409,1638176423,'msgqueued',1,'testfolder',1,2),
(2,'BM-2cWzMnxjJ7yRP3nLEWUV5LisTZyREWSxYz',1,'BM-2cWzSnwjJ7yRP3nLEWUV5LisTZyREWSzUK','Test2 subject','message test 2','ackdata',1638176423,1638176423,1638176423,'msgqueued',1,'testfolder',1,2);

View File

@ -1 +0,0 @@
INSERT INTO `inbox` VALUES (1, "poland", "malasia", "test", "yes", "test message", "folder", 1, 1, 1);

View File

@ -1,2 +0,0 @@
INSERT INTO `sent` VALUES
(1,'BM-2cWzMnxjJ7yRP3nLEWUV5LisTZyREWSxYz',1,'BM-2cWzSnwjJ7yRP3nLEWUV5LisTZyREWSzUK','Test1 subject','message test 1','ackdata',1638176409,1638176409,1638176423,'msgqueued',1,'testfolder',1,2);

View File

@ -10,16 +10,6 @@ from pybitmessage.class_sqlThread import TestDB # noqa:E402
from pybitmessage.addresses import encodeAddress # noqa:E402
def filter_table_column(schema, column):
"""
Filter column from schema
"""
for x in schema:
for y in x:
if y == column:
yield y
class TestSqlBase(object): # pylint: disable=E1101, too-few-public-methods, E1004, W0232
""" Base for test case """
@ -42,12 +32,12 @@ class TestSqlBase(object): # pylint: disable=E1101, too-few-public-methods, E10
res = [[x[1], x[2]] for x in res]
return res
def initialise_database(self, test_db_cur, file): # pylint: disable=W0622, redefined-builtin
def execute_test_script(self, test_db_cur, file_name): # pylint: disable=W0622, redefined-builtin
"""
Initialise DB
Executing sql script from file
"""
with open(os.path.join(self.root_path, "tests/sql/{}.sql".format(file)), 'r') as sql_as_string:
with open(os.path.join(self.root_path, "tests/sql/{}.sql".format(file_name)), 'r') as sql_as_string:
sql_as_string = sql_as_string.read()
test_db_cur.cur.executescript(sql_as_string)
@ -232,22 +222,8 @@ class TestUpgradeBitmessageDB(TestSqlBase, unittest.TestCase): # pylint: disabl
func_name = func.__name__
version = func_name.rsplit('_', 1)[-1]
for i in range(1, int(version) + 1):
if i == 7:
self.test_db.cur.execute('''INSERT INTO inventory VALUES( '', 1, 1, '', 1, '') ''')
self.test_db.cur.execute('''INSERT INTO pubkeys VALUES( '', 1, '', 1, '') ''')
self.test_db.cur.execute('''INSERT INTO sent
VALUES( '', '', '', '', '', '', '', 1, 'doingmsgpow', 1, 1, '', 1) ''')
self.test_db.cur.execute('''INSERT INTO sent
VALUES( '', '', '', '', '', '', '', 1, 'badkey', 1, 1, '', 1) ''')
self.test_db.conn.commit()
elif i == 9:
bytes_value = b'\x00\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01'
self.test_db.cur.execute('''INSERT INTO pubkeys VALUES( ?, ?, ?, ?, ?) ''',
(bytes_value, 3, '', 1, ''))
self.test_db.conn.commit()
elif i == 10:
self.test_db.cur.execute('''INSERT INTO addressbook VALUES ('', '')''')
self.test_db.cur.execute('''INSERT INTO addressbook VALUES ('', '')''')
if i == 7 or i == 9 or i == 10:
self.execute_test_script(self.test_db, 'insert_test_values_version_{}'.format(i))
self.test_db.conn.commit()
self.test_db._upgrade_one_level_sql_statement(i) # pylint: disable= W0212, protected-access
return func(*args) # <-- use (self, ...)