used decorator in class_sqlThread module

This commit is contained in:
navjot 2021-01-15 14:57:30 +05:30
parent 1533d6c8e6
commit 74188cfbf4
No known key found for this signature in database
GPG Key ID: 9EE70AFD71357F1C

View File

@ -20,14 +20,21 @@ from debug import logger
# pylint: disable=attribute-defined-outside-init,protected-access
def execute_setting_table(func):
"""this method is used as a decorator"""
def inner(*args, **kwars):
reference = args[0]
item = '''SELECT value FROM settings WHERE key='version';'''
parameters = ''
reference.cur.execute(item, parameters)
func(reference)
upgrade_dict = {}
def db_upgrade(*args, **kwargs): # pylint: disable=unused-argument
"""upgrade the migration"""
version_dict = kwargs
def inner(func):
"""this is inner method"""
upgrade_dict.update(version_dict)
def wrapped(*args):
"""used for calling main method"""
func(*args)
return wrapped
return inner
@ -38,8 +45,10 @@ class sqlThread(threading.Thread):
threading.Thread.__init__(self, name="SQL")
def update_sent(self):
# After code refactoring, the possible status values for sent messages
# have changed.
"""
After code refactoring, the possible status values for sent messages
have changed.
"""
self.cur.execute(
'''update sent set status='doingmsgpow' where status='doingpow' ''')
self.cur.execute(
@ -50,11 +59,29 @@ class sqlThread(threading.Thread):
'''update sent set status='broadcastqueued' where status='broadcastpending' ''')
self.conn.commit()
@execute_setting_table
def versionTwo(self):
# Let's get rid of the first20bytesofencryptedmessage field in
# the inventory table.
if int(self.cur.fetchall()[0][0]) == 2:
def inventory_upgrade(self):
"""Adding a new column to the inventory table to store tags."""
logger.debug(
'In messages.dat database, adding tag field to'
' the inventory table.')
item = '''ALTER TABLE inventory ADD tag blob DEFAULT '' '''
param = ''
self.cur.execute(item, param)
item = '''update settings set value=? WHERE key='version';'''
parameters = (4,)
self.cur.execute(item, parameters)
@db_upgrade(version_one=1)
def version_one(self):
"""version_one for upgrading inventory"""
self.inventory_upgrade()
@db_upgrade(version_two=2)
def version_two(self):
"""
method for getting rid of the first20bytesofencryptedmessage field in
the inventory table.
"""
logger.debug(
'In messages.dat database, removing an obsolete field from'
' the inventory table.')
@ -78,27 +105,17 @@ class sqlThread(threading.Thread):
parameters = (3,)
self.cur.execute(item, parameters)
@execute_setting_table
def versionThree(self):
# Add a new column to the inventory table to store tags.
currentVersion = int(self.cur.fetchall()[0][0])
if currentVersion == 1 or currentVersion == 3:
logger.debug(
'In messages.dat database, adding tag field to'
' the inventory table.')
item = '''ALTER TABLE inventory ADD tag blob DEFAULT '' '''
parameters = ''
self.cur.execute(item, parameters)
item = '''update settings set value=? WHERE key='version';'''
parameters = (4,)
self.cur.execute(item, parameters)
@db_upgrade(version_three=3)
def version_three(self):
"""version_three for upgrading inventory"""
self.inventory_upgrade()
@execute_setting_table
def versionFour(self):
# 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.
currentVersion = int(self.cur.fetchall()[0][0])
if currentVersion == 4:
@db_upgrade(versio_four=4)
def versio_four(self):
"""
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.
"""
self.cur.execute('''DROP TABLE pubkeys''')
self.cur.execute(
'''CREATE TABLE pubkeys (hash blob, addressversion int, transmitdata blob, time int,'''
@ -109,12 +126,12 @@ class sqlThread(threading.Thread):
parameters = (5,)
self.cur.execute(item, parameters)
@execute_setting_table
def versionFive(self):
# Add a new table: objectprocessorqueue with which to hold objects
# that have yet to be processed if the user shuts down Bitmessage.
currentVersion = int(self.cur.fetchall()[0][0])
if currentVersion == 5:
@db_upgrade(versio_five=5)
def versio_five(self):
"""
Add a new table: objectprocessorqueue with which to hold objects
that have yet to be processed if the user shuts down Bitmessage.
"""
self.cur.execute('''DROP TABLE knownnodes''')
self.cur.execute(
'''CREATE TABLE objectprocessorqueue'''
@ -123,13 +140,13 @@ class sqlThread(threading.Thread):
parameters = (6,)
self.cur.execute(item, parameters)
@execute_setting_table
def versionSix(self):
# changes related to protocol v3
# In table inventory and objectprocessorqueue, objecttype is now
# an integer (it was a human-friendly string previously)
currentVersion = int(self.cur.fetchall()[0][0])
if currentVersion == 6:
@db_upgrade(version_six=6)
def version_six(self):
"""
changes related to protocol v3
In table inventory and objectprocessorqueue, objecttype is now
an integer (it was a human-friendly string previously)
"""
logger.debug(
'In messages.dat database, dropping and recreating'
' the inventory table.')
@ -148,13 +165,13 @@ class sqlThread(threading.Thread):
logger.debug(
'Finished dropping and recreating the inventory table.')
@execute_setting_table
def versionSeven(self):
# 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.
currentVersion = int(self.cur.fetchall()[0][0])
if currentVersion == 7:
@db_upgrade(version_seven=7)
def version_seven(self):
"""
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.
"""
logger.debug(
'In messages.dat database, clearing pubkeys table'
' because the data format has been updated.')
@ -171,29 +188,29 @@ class sqlThread(threading.Thread):
self.cur.execute(query, parameters)
logger.debug('Finished clearing currently held pubkeys.')
@execute_setting_table
def versionEight(self):
# 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.
currentVersion = int(self.cur.fetchall()[0][0])
if currentVersion == 8:
@db_upgrade(version_eight=8)
def version_eight(self):
"""
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.
"""
logger.debug(
'In messages.dat database, adding sighash field to'
' the inbox table.')
item = '''ALTER TABLE inbox ADD sighash blob DEFAULT '' '''
parameters = ''
self.cur.execute(item, parameters)
param = ''
self.cur.execute(item, param)
item = '''update settings set value=? WHERE key='version';'''
parameters = (9,)
self.cur.execute(item, parameters)
@execute_setting_table
def versionNine(self):
# We'll also need a `sleeptill` field and a `ttl` field. Also we
# can combine the pubkeyretrynumber and msgretrynumber into one.
currentVersion = int(self.cur.fetchall()[0][0])
if currentVersion == 9:
@db_upgrade(version_nine=9)
def version_nine(self):
"""
We'll also need a `sleeptill` field and a `ttl` field. Also we
can combine the pubkeyretrynumber and msgretrynumber into one.
"""
logger.info(
'In messages.dat database, making TTL-related changes:'
' combining the pubkeyretrynumber and msgretrynumber'
@ -254,6 +271,22 @@ class sqlThread(threading.Thread):
' and removing the hash field.')
self.cur.execute('''update settings set value=10 WHERE key='version';''')
@db_upgrade(version_ten=10)
def version_ten(self):
"""Update the address colunm to unique in addressbook table"""
logger.debug(
'In messages.dat database, updating address column to UNIQUE'
' in the addressbook table.')
self.cur.execute(
'''ALTER TABLE addressbook RENAME TO old_addressbook''')
self.cur.execute(
'''CREATE TABLE addressbook'''
''' (label text, address text, UNIQUE(address) ON CONFLICT IGNORE)''')
self.cur.execute(
'''INSERT INTO addressbook SELECT label, address FROM old_addressbook;''')
self.cur.execute('''DROP TABLE old_addressbook''')
self.cur.execute('''update settings set value=11 WHERE key='version';''')
def run(self): # pylint: disable=too-many-locals, too-many-branches, too-many-statements
"""Process SQL queries from `.helper_sql.sqlSubmitQueue`"""
helper_sql.sql_available = True
@ -396,21 +429,14 @@ class sqlThread(threading.Thread):
self.update_sent()
self.versionTwo()
item = '''SELECT value FROM settings WHERE key='version';'''
parameters = ''
self.cur.execute(item, parameters)
currentVersion = int(self.cur.fetchall()[0][0])
temp_dict = {val: key for key, val in upgrade_dict.items()}
self.versionThree()
self.versionFour()
self.versionFive()
self.versionSix()
self.versionSeven()
self.versionEight()
self.versionNine()
if temp_dict.get(currentVersion):
getattr(self, temp_dict.get(currentVersion))()
# Are you hoping to add a new option to the keys.dat file of existing
# Bitmessage users or modify the SQLite database? Add it right