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