diff --git a/src/sql/init_version_1.sql b/src/sql/init_version_1.sql new file mode 100644 index 00000000..a6e024f7 --- /dev/null +++ b/src/sql/init_version_1.sql @@ -0,0 +1,3 @@ +ALTER TABLE inventory ADD first20bytesofencryptedmessage blob DEFAULT ''; + +UPDATE settings SET value = 2 WHERE key = 'version'; \ No newline at end of file diff --git a/src/sql/init_version_10.sql b/src/sql/init_version_10.sql index 8bd8b0b3..062fa8fd 100644 --- a/src/sql/init_version_10.sql +++ b/src/sql/init_version_10.sql @@ -13,3 +13,5 @@ CREATE TABLE `addressbook` ( INSERT INTO addressbook SELECT label, address FROM old_addressbook; DROP TABLE old_addressbook; + +UPDATE settings SET value = 11 WHERE key = 'version'; diff --git a/src/sql/init_version_2.sql b/src/sql/init_version_2.sql index ea42df4c..0c02b681 100644 --- a/src/sql/init_version_2.sql +++ b/src/sql/init_version_2.sql @@ -27,3 +27,5 @@ CREATE TABLE `inventory` ( INSERT INTO inventory SELECT hash, objecttype, streamnumber, payload, receivedtime FROM inventory_backup; DROP TABLE inventory_backup; + +UPDATE settings SET value = 3 WHERE key = 'version'; diff --git a/src/sql/init_version_3.sql b/src/sql/init_version_3.sql index 9de784a5..64ae3898 100644 --- a/src/sql/init_version_3.sql +++ b/src/sql/init_version_3.sql @@ -3,3 +3,5 @@ -- ALTER TABLE inventory ADD tag blob DEFAULT ''; + +UPDATE settings SET value = 4 WHERE key = 'version'; diff --git a/src/sql/init_version_4.sql b/src/sql/init_version_4.sql index d2fd393d..8bf57071 100644 --- a/src/sql/init_version_4.sql +++ b/src/sql/init_version_4.sql @@ -15,3 +15,5 @@ CREATE TABLE `pubkeys` ( ) ; DELETE FROM inventory WHERE objecttype = 'pubkey'; + +UPDATE settings SET value = 5 WHERE key = 'version'; diff --git a/src/sql/init_version_5.sql b/src/sql/init_version_5.sql index a13fa8cf..d10c7d0d 100644 --- a/src/sql/init_version_5.sql +++ b/src/sql/init_version_5.sql @@ -10,3 +10,5 @@ CREATE TABLE `objectprocessorqueue` ( `data` blob, UNIQUE(objecttype, data) ON CONFLICT REPLACE ) ; + +UPDATE settings SET value = 6 WHERE key = 'version'; \ No newline at end of file diff --git a/src/sql/init_version_6.sql b/src/sql/init_version_6.sql index b9a03669..67be8cdb 100644 --- a/src/sql/init_version_6.sql +++ b/src/sql/init_version_6.sql @@ -23,3 +23,5 @@ CREATE TABLE `objectprocessorqueue` ( `data` blob, UNIQUE(objecttype, data) ON CONFLICT REPLACE ) ; + +UPDATE settings SET value = 7 WHERE key = 'version'; diff --git a/src/sql/init_version_7.sql b/src/sql/init_version_7.sql index a2f6f6e3..7bdf874d 100644 --- a/src/sql/init_version_7.sql +++ b/src/sql/init_version_7.sql @@ -9,3 +9,5 @@ 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'; \ No newline at end of file diff --git a/src/sql/init_version_8.sql b/src/sql/init_version_8.sql index 0c1813d3..95aa47e1 100644 --- a/src/sql/init_version_8.sql +++ b/src/sql/init_version_8.sql @@ -5,3 +5,5 @@ -- ALTER TABLE inbox ADD sighash blob DEFAULT ''; + +UPDATE settings SET value = 9 WHERE key = 'version'; \ No newline at end of file diff --git a/src/sql/init_version_9.sql b/src/sql/init_version_9.sql index bc8296b9..d9876ae2 100644 --- a/src/sql/init_version_9.sql +++ b/src/sql/init_version_9.sql @@ -72,3 +72,5 @@ CREATE TABLE `pubkeys` ( INSERT INTO pubkeys SELECT address, addressversion, transmitdata, `time`, usedpersonally FROM pubkeys_backup; DROP TABLE pubkeys_backup; + +UPDATE settings SET value = 10 WHERE key = 'version'; diff --git a/src/sql/initialize_schema_v1.sql b/src/sql/initialize_schema_v1.sql new file mode 100644 index 00000000..d79ffb49 --- /dev/null +++ b/src/sql/initialize_schema_v1.sql @@ -0,0 +1,92 @@ +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( '', 'pubkey', 1, '', 1); + diff --git a/src/tests/sql/init_version_1.sql b/src/tests/sql/init_version_1.sql new file mode 100644 index 00000000..a6e024f7 --- /dev/null +++ b/src/tests/sql/init_version_1.sql @@ -0,0 +1,3 @@ +ALTER TABLE inventory ADD first20bytesofencryptedmessage blob DEFAULT ''; + +UPDATE settings SET value = 2 WHERE key = 'version'; \ No newline at end of file diff --git a/src/tests/sql/init_version_10.sql b/src/tests/sql/init_version_10.sql index b1764e76..062fa8fd 100644 --- a/src/tests/sql/init_version_10.sql +++ b/src/tests/sql/init_version_10.sql @@ -1 +1,17 @@ -INSERT INTO `addressbook` VALUES ('test', "BM-2cWzMnxjJ7yRP3nLEWUV5LisTZyREWSxYz"), ('testone', "BM-2cWzMnxjJ7yRP3nLEWUV5LisTZyREWSxYz"); +-- -- +-- -- 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'; diff --git a/src/tests/sql/init_version_2.sql b/src/tests/sql/init_version_2.sql index 133284ec..0c02b681 100644 --- a/src/tests/sql/init_version_2.sql +++ b/src/tests/sql/init_version_2.sql @@ -1 +1,31 @@ -INSERT INTO `inventory` VALUES ('hash', 1, 1,1, 1,'test'); +-- +-- 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'; diff --git a/src/tests/sql/init_version_3.sql b/src/tests/sql/init_version_3.sql index 875d859d..64ae3898 100644 --- a/src/tests/sql/init_version_3.sql +++ b/src/tests/sql/init_version_3.sql @@ -1 +1,7 @@ -INSERT INTO `settings` VALUES ('version','3'); +-- +-- 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'; diff --git a/src/tests/sql/init_version_4.sql b/src/tests/sql/init_version_4.sql index ea3f1768..8bf57071 100644 --- a/src/tests/sql/init_version_4.sql +++ b/src/tests/sql/init_version_4.sql @@ -1 +1,19 @@ -INSERT INTO `pubkeys` VALUES ('hash', 1, 1, 1,'test'); + -- + -- 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'; diff --git a/src/tests/sql/init_version_5.sql b/src/tests/sql/init_version_5.sql index b894c038..d10c7d0d 100644 --- a/src/tests/sql/init_version_5.sql +++ b/src/tests/sql/init_version_5.sql @@ -1 +1,14 @@ -INSERT INTO `objectprocessorqueue` VALUES ('hash', 1); + -- + -- 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'; \ No newline at end of file diff --git a/src/tests/sql/init_version_6.sql b/src/tests/sql/init_version_6.sql index 7cd30571..67be8cdb 100644 --- a/src/tests/sql/init_version_6.sql +++ b/src/tests/sql/init_version_6.sql @@ -1 +1,27 @@ -INSERT INTO `inventory` VALUES ('hash', 1, 1, 1,'test','test'); +-- +-- 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'; diff --git a/src/tests/sql/init_version_7.sql b/src/tests/sql/init_version_7.sql index bd87f8d8..7bdf874d 100644 --- a/src/tests/sql/init_version_7.sql +++ b/src/tests/sql/init_version_7.sql @@ -1,3 +1,13 @@ -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); +-- +-- 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'; \ No newline at end of file diff --git a/src/tests/sql/init_version_8.sql b/src/tests/sql/init_version_8.sql index 9d9b6f3a..95aa47e1 100644 --- a/src/tests/sql/init_version_8.sql +++ b/src/tests/sql/init_version_8.sql @@ -1 +1,9 @@ -INSERT INTO `inbox` VALUES (1, "poland", "malasia", "test", "yes", "test message", "folder", 1, 1, 1); +-- +-- 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'; \ No newline at end of file diff --git a/src/tests/sql/init_version_9.sql b/src/tests/sql/init_version_9.sql index 764634d2..d9876ae2 100644 --- a/src/tests/sql/init_version_9.sql +++ b/src/tests/sql/init_version_9.sql @@ -1,2 +1,76 @@ -INSERT INTO `sent` VALUES -(1,'BM-2cWzMnxjJ7yRP3nLEWUV5LisTZyREWSxYz',1,'BM-2cWzSnwjJ7yRP3nLEWUV5LisTZyREWSzUK','Test1 subject','message test 1','ackdata',1638176409,1638176409,1638176423,'msgqueued',1,'testfolder',1,2); +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'; diff --git a/src/tests/sql/initialize_schema.sql b/src/tests/sql/initialize_schema.sql new file mode 100644 index 00000000..8413aa0a --- /dev/null +++ b/src/tests/sql/initialize_schema.sql @@ -0,0 +1,100 @@ +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 +) ; diff --git a/src/tests/sql/initialize_schema_v1.sql b/src/tests/sql/initialize_schema_v1.sql new file mode 100644 index 00000000..3da18946 --- /dev/null +++ b/src/tests/sql/initialize_schema_v1.sql @@ -0,0 +1,92 @@ +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); + diff --git a/src/tests/sql/old/init_version_10.sql b/src/tests/sql/old/init_version_10.sql new file mode 100644 index 00000000..b1764e76 --- /dev/null +++ b/src/tests/sql/old/init_version_10.sql @@ -0,0 +1 @@ +INSERT INTO `addressbook` VALUES ('test', "BM-2cWzMnxjJ7yRP3nLEWUV5LisTZyREWSxYz"), ('testone', "BM-2cWzMnxjJ7yRP3nLEWUV5LisTZyREWSxYz"); diff --git a/src/tests/sql/old/init_version_2.sql b/src/tests/sql/old/init_version_2.sql new file mode 100644 index 00000000..133284ec --- /dev/null +++ b/src/tests/sql/old/init_version_2.sql @@ -0,0 +1 @@ +INSERT INTO `inventory` VALUES ('hash', 1, 1,1, 1,'test'); diff --git a/src/tests/sql/old/init_version_3.sql b/src/tests/sql/old/init_version_3.sql new file mode 100644 index 00000000..e479c3b7 --- /dev/null +++ b/src/tests/sql/old/init_version_3.sql @@ -0,0 +1 @@ +INSERT INTO `settings` VALUES ('version',3); diff --git a/src/tests/sql/old/init_version_4.sql b/src/tests/sql/old/init_version_4.sql new file mode 100644 index 00000000..fd496f42 --- /dev/null +++ b/src/tests/sql/old/init_version_4.sql @@ -0,0 +1,3 @@ +INSERT INTO `pubkeys` VALUES ('hash', 1, 1, 1,'test'); + +UPDATE settings SET value=4 WHERE key='version'; diff --git a/src/tests/sql/old/init_version_5.sql b/src/tests/sql/old/init_version_5.sql new file mode 100644 index 00000000..b894c038 --- /dev/null +++ b/src/tests/sql/old/init_version_5.sql @@ -0,0 +1 @@ +INSERT INTO `objectprocessorqueue` VALUES ('hash', 1); diff --git a/src/tests/sql/old/init_version_6.sql b/src/tests/sql/old/init_version_6.sql new file mode 100644 index 00000000..7cd30571 --- /dev/null +++ b/src/tests/sql/old/init_version_6.sql @@ -0,0 +1 @@ +INSERT INTO `inventory` VALUES ('hash', 1, 1, 1,'test','test'); diff --git a/src/tests/sql/old/init_version_7.sql b/src/tests/sql/old/init_version_7.sql new file mode 100644 index 00000000..bd87f8d8 --- /dev/null +++ b/src/tests/sql/old/init_version_7.sql @@ -0,0 +1,3 @@ +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); diff --git a/src/tests/sql/old/init_version_8.sql b/src/tests/sql/old/init_version_8.sql new file mode 100644 index 00000000..9d9b6f3a --- /dev/null +++ b/src/tests/sql/old/init_version_8.sql @@ -0,0 +1 @@ +INSERT INTO `inbox` VALUES (1, "poland", "malasia", "test", "yes", "test message", "folder", 1, 1, 1); diff --git a/src/tests/sql/old/init_version_9.sql b/src/tests/sql/old/init_version_9.sql new file mode 100644 index 00000000..764634d2 --- /dev/null +++ b/src/tests/sql/old/init_version_9.sql @@ -0,0 +1,2 @@ +INSERT INTO `sent` VALUES +(1,'BM-2cWzMnxjJ7yRP3nLEWUV5LisTZyREWSxYz',1,'BM-2cWzSnwjJ7yRP3nLEWUV5LisTZyREWSzUK','Test1 subject','message test 1','ackdata',1638176409,1638176409,1638176423,'msgqueued',1,'testfolder',1,2); diff --git a/src/tests/test_sqlthread.py b/src/tests/test_sqlthread.py index 5b061335..4d5cb7e8 100644 --- a/src/tests/test_sqlthread.py +++ b/src/tests/test_sqlthread.py @@ -214,8 +214,10 @@ class TestUpgradeBitmessageDB(TestSqlBase, unittest.TestCase): # pylint: disabl Setup DB schema before start. And applying default schema for version test. """ - self._setup_db() - self.test_db.cur.execute('''INSERT INTO settings VALUES('version','2')''') + self.test_db = TestDB() + self.test_db.create_sql_function() + self.test_db.initialize_sql("initialize_schema_v1") + self.test_db.conn.commit() def version(self): """ @@ -229,119 +231,274 @@ class TestUpgradeBitmessageDB(TestSqlBase, unittest.TestCase): # pylint: disabl self = args[0] func_name = func.__name__ version = func_name.rsplit('_', 1)[-1] - self.test_db._upgrade_one_level_sql_statement(int(version)) # pylint: disable= W0212, protected-access - - # Update versions DB mocking - self.initialise_database(self.test_db, "init_version_{}".format(version)) - + 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 ('', '')''') + self.test_db.conn.commit() + self.test_db._upgrade_one_level_sql_statement(i) # pylint: disable= W0212, protected-access return func(*args) # <-- use (self, ...) func = self return wrapper + @version + def test_bm_db_version_1(self): + """ + Test update from version 1 to 2 + """ + # check version update in settings table + version = self.test_db.sql_schema_version + self.assertEqual(version, 2, "Settings version value not updated") + + # check adding first20bytesofencryptedmessage column to inventory table + res = self.get_table_schema('inventory') + check = ['first20bytesofencryptedmessage', 'blob'] + answ = (check in res) + self.assertEqual(answ, True, "No first20bytesofencryptedmessage in inventory table in second version") + @version def test_bm_db_version_2(self): """ - Test with version 2 + Test update from version 2 to 3 """ - res = self.test_db.cur.execute(''' SELECT count(name) FROM sqlite_master + # check version update in settings table + version = self.test_db.sql_schema_version + self.assertEqual(version, 3, "Settings version value not updated") + inventory_schema = self.get_table_schema('inventory') + check_column = ['first20bytesofencryptedmessage', 'blob'] + answer = (check_column in inventory_schema) + + # check deleting first20bytesofencryptedmessage column to inventory table + self.assertNotEqual(answer, True, "Column first20bytesofencryptedmessage in table inventory not "\ + "deleted in version 3") + + # check deleting inventory_backup table + self.test_db.cur.execute(''' SELECT count(name) FROM sqlite_master WHERE type='table' AND name='inventory_backup' ''') - self.assertNotEqual(res, 1, "Table inventory_backup not deleted in versioning 2") + res = self.test_db.cur.fetchall()[0][0] + self.assertNotEqual(res, 1, "Table inventory_backup not deleted in versioning 3") @version def test_bm_db_version_3(self): """ - Test with version 1 - Version 1 and 3 are same so will skip 3 + Test update from version 3 to 4 """ - res = self.test_db.cur.execute('''PRAGMA table_info('inventory');''') - result = list(filter_table_column(res, "tag")) - self.assertEqual(result, ['tag'], "Data not migrated for version 3") + # check version update in settings table + version = self.test_db.sql_schema_version + self.assertEqual(version, 4, "Settings version value not updated") + + # check adding tag column to inventory table + inventory_schema = self.get_table_schema('inventory') + check_column = ['tag', 'blob'] + answer = (check_column in inventory_schema) + self.assertEqual(answer, True, "No column tag in table inventory in version 4") @version def test_bm_db_version_4(self): """ - Test with version 4 + Test update from version 4 to 5 """ - self.test_db.cur.execute("select * from pubkeys where addressversion = '1';") - res = self.test_db.cur.fetchall() - self.assertEqual(len(res), 1, "Table inventory not deleted in versioning 4") + # check version update in settings table + version = self.test_db.sql_schema_version + self.assertEqual(version, 5, "Settings version value not updated") + + # check changing column addressversion type to int in table pubkeys + pubkeys_schema = self.get_table_schema("pubkeys") + check_column = ["addressversion", "int"] + answer = check_column in pubkeys_schema + self.assertEqual(answer, True, "Column addressversion not changed to int in table pubkeys") + + # check deleting pubkey objects from inventory table + self.test_db.cur.execute(''' SELECT COUNT(hash) FROM inventory WHERE objecttype = 'pubkey' ''') + res = self.test_db.cur.fetchall()[0][0] + self.assertEqual(res, 0, "Pubkey objects not deleted from inventory table in versioning 5") @version def test_bm_db_version_5(self): """ - Test with version 5 + Test update from version 5 to 6 """ - self.test_db.cur.execute(''' SELECT count(name) FROM sqlite_master WHERE type='table' AND name='knownnodes' ''') # noqa - res = self.test_db.cur.fetchall() - self.assertNotEqual(res[0][0], 1, "Table knownnodes not deleted in versioning 5") + # check version update in settings table + version = self.test_db.sql_schema_version + self.assertEqual(version, 6, "Settings version value not updated") + + # check deleting knownnodes table + self.test_db.cur.execute(''' SELECT count(name) FROM sqlite_master WHERE type='table' AND name='knownnodes' ''') + res = self.test_db.cur.fetchall()[0][0] + self.assertNotEqual(res, 1, "Table knownnodes not deleted in versioning 6") + + # check creating objectprocessorqueue table self.test_db.cur.execute(''' SELECT count(name) FROM sqlite_master - WHERE type='table' AND name='objectprocessorqueue'; ''') - res = self.test_db.cur.fetchall() - self.assertNotEqual(len(res), 0, "Table objectprocessorqueue not created in versioning 5") - self.test_db.cur.execute(''' SELECT * FROM objectprocessorqueue where objecttype='hash' ; ''') - res = self.test_db.cur.fetchall() - self.assertNotEqual(len(res), 0, "Table objectprocessorqueue not created in versioning 5") + WHERE type='table' AND name='objectprocessorqueue' ''') + res = self.test_db.cur.fetchall()[0][0] + self.assertNotEqual(res, 0, "Table objectprocessorqueue not created in versioning 6") + + # check objectprocessorqueue table schema + objectprocessorqueue_schema = self.get_table_schema("objectprocessorqueue") + check = [['objecttype', 'text'], + ['data', 'blob']] + self.assertEqual(objectprocessorqueue_schema, check, "objectprocessorqueue table is not valid") @version def test_bm_db_version_6(self): """ - Test with version 6 + Test update from version 6 to 7 """ - self.test_db.cur.execute('''PRAGMA table_info('inventory');''') - inventory = self.test_db.cur.fetchall() - inventory = list(filter_table_column(inventory, "expirestime")) - self.assertEqual(inventory, ['expirestime'], "Data not migrated for version 6") + inventory_schema = self.get_table_schema("inventory") + objectprocessorqueue_schema = self.get_table_schema("objectprocessorqueue") - self.test_db.cur.execute('''PRAGMA table_info('objectprocessorqueue');''') - objectprocessorqueue = self.test_db.cur.fetchall() - objectprocessorqueue = list(filter_table_column(objectprocessorqueue, "objecttype")) - self.assertEqual(objectprocessorqueue, ['objecttype'], "Data not migrated for version 6") + # check version update in settings table + version = self.test_db.sql_schema_version + self.assertEqual(version, 7, "Settings version value not updated") + + # check changing objecttype column type to int in table objectprocessorqueue + check = ["objecttype", "int"] + answ = check in objectprocessorqueue_schema + self.assertEqual(answ, True, "Type of objecttype column in table objectprocessorqueue not changed to int") + + # check changing objecttype column type to int in table inventory + check = ["objecttype", "int"] + answ = check in inventory_schema + self.assertEqual(answ, True, "Type of objecttype column in table inventory not changed to int") + + # check adding expirestime column in table inventory + check = ["expirestime", "integer"] + answ = check in inventory_schema + self.assertEqual(answ, True, "expirestime column not added to table inventory") + + # check deleting receivedtime column from table inventory + check = ["receivedtime", "integer"] + answ = check in inventory_schema + self.assertNotEqual(answ, True, "receivedtime column not deleted from table inventory") @version def test_bm_db_version_7(self): """ - Test with version 7 + Test update from version 7 to 8 """ + # check version update in settings table + version = self.test_db.sql_schema_version + self.assertEqual(version, 8, "Settings version value not updated") + + # check clearing pubkeys table self.test_db.cur.execute('''SELECT * FROM pubkeys ''') pubkeys = self.test_db.cur.fetchall() - self.assertEqual(pubkeys, [], "Data not migrated for version 7") + self.assertEqual(pubkeys, [], "pubkeys table is not clear") - self.test_db.cur.execute('''SELECT * FROM inventory ''') + # check deleting pubkeys from table inventory + self.test_db.cur.execute('''SELECT * FROM inventory WHERE objecttype = 1''') inventory = self.test_db.cur.fetchall() - self.assertEqual(inventory, [], "Data not migrated for version 7") + self.assertEqual(inventory, [], "pubkeys not deleted from inventory table") + # check updating statuses in sent table self.test_db.cur.execute('''SELECT status FROM sent ''') sent = self.test_db.cur.fetchall() - self.assertEqual(sent, [('msgqueued',), ('msgqueued',)], "Data not migrated for version 7") + self.assertEqual(sent, [('msgqueued',), ('msgqueued',)], "Statuses in sent table not updated") @version def test_bm_db_version_8(self): """ - Test with version 8 + Test update from version 8 to 9 """ - self.test_db.cur.execute('''PRAGMA table_info('inbox');''') - res = self.test_db.cur.fetchall() - result = list(filter_table_column(res, "sighash")) - self.assertEqual(result, ['sighash'], "Data not migrated for version 8") + # check version update in settings table + version = self.test_db.sql_schema_version + self.assertEqual(version, 9, "Settings version value not updated") + + # check adding sighash column to inbox table + inbox_schema = self.get_table_schema("inbox") + check = ['sighash', 'blob'] + answ = check in inbox_schema + self.assertEqual(answ, True, "sighash column not added to inbox table") @version def test_bm_db_version_9(self): """ - Test with version 9 + Test update from version 9 to 10 """ + # check version update in settings table + version = self.test_db.sql_schema_version + self.assertEqual(version, 10, "Settings version value not updated") + + sent_schema = self.get_table_schema('sent') + pubkeys_schema = self.get_table_schema('pubkeys') + + # check pubkeys table schema updating + check = ['hash', 'blob'] + answ = check in pubkeys_schema + self.assertNotEqual(answ, True, "Column hash not deleted from pubkeys table") + + check = ['address', 'text'] + answ = check in pubkeys_schema + self.assertEqual(answ, True, "Column address not added to pubkeys table") + + # check sent table schema updating + check = ['pubkeyretrynumber', 'integer'] + answ = check in sent_schema + self.assertNotEqual(answ, True, "Column pubkeyretrynumber not deleted from sent table") + + check = ['msgretrynumber', 'integer'] + answ = check in sent_schema + self.assertNotEqual(answ, True, "Column msgretrynumber not deleted from sent table") + + check = ['senttime', 'integer'] + answ = check in sent_schema + self.assertEqual(answ, True, "Column senttime not added to sent table") + + check = ['sleeptill', 'int'] + answ = check in sent_schema + self.assertEqual(answ, True, "Column sleeptill not added to sent table") + + check = ['retrynumber', 'integer'] + answ = check in sent_schema + self.assertEqual(answ, True, "Column retrynumber not added to sent table") + + check = ['ttl', 'int'] + answ = check in sent_schema + self.assertEqual(answ, True, "Column ttl not added to sent table") + + # check pubkeys_backup table deleting self.test_db.cur.execute("SELECT count(name) FROM sqlite_master WHERE type='table' AND name='pubkeys_backup'") # noqa res = self.test_db.cur.fetchall() self.assertNotEqual(res[0][0], 1, "Table pubkeys_backup not deleted") + # check data migration + check_pubkey = [('BM-2D77qGjcBfFmqn3EGs85ojKJtCh7b3tutK', 3, '', 1, '')] + self.test_db.cur.execute('''SELECT * FROM pubkeys''') + res = self.test_db.cur.fetchall() + self.assertEqual(res, check_pubkey, "Migration pubkeys table data failed") + + self.test_db.cur.execute('''SELECT * FROM sent''') + res = self.test_db.cur.fetchall() + check_sent = [('', '', '', '', '', '', '', 1, 1, 0, 'msgqueued', 0, '', 1, 216000), + ('', '', '', '', '', '', '', 1, 1, 0, 'msgqueued', 0, '', 1, 216000)] + self.assertEqual(res, check_sent, "Migration sent table data failed") + @version def test_bm_db_version_10(self): """ - Test with version 10 + Test update from version 10 to 11 """ - label = "test" - self.test_db.cur.execute("SELECT * FROM addressbook WHERE label='test' ") # noqa + # check version update in settings table + version = self.test_db.sql_schema_version + self.assertEqual(version, 11, "Settings version value not updated") + + # check data migration in addressbook table + self.test_db.cur.execute('''SELECT * FROM addressbook''') res = self.test_db.cur.fetchall() - self.assertEqual(res[0][0], label, "Data not migrated for version 10") + self.assertEqual(res, [('', '')], "Migration addressbook table data failed") def test_bm_db_version_type(self): """