Refactoring of database upgrade mechanism
This commit is contained in:
parent
583e5910fe
commit
bb38c158fe
|
@ -23,25 +23,53 @@ from debug import logger
|
||||||
class UpgradeDB():
|
class UpgradeDB():
|
||||||
"""Upgrade Db with respect to versions"""
|
"""Upgrade Db with respect to versions"""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self, cur):
|
||||||
""" Apply switcher to call methods accordingly """
|
""" init variables globally """
|
||||||
self.cur = None
|
|
||||||
self.parameters = ""
|
|
||||||
|
|
||||||
def switcher(self, cur, version):
|
|
||||||
""" Apply switcher to call methods accordingly """
|
|
||||||
self.cur = cur
|
self.cur = cur
|
||||||
self.parameters = ""
|
self.parameters = ""
|
||||||
|
self.current_level = self.get_current_level()
|
||||||
|
self.max_level = 11
|
||||||
|
|
||||||
# Switch methods with respect to versions
|
def get_current_level(self):
|
||||||
if version in range(1, 11):
|
# Upgrade Db with respect to their versions
|
||||||
method_name = 'version_' + str(version)
|
item = '''SELECT value FROM settings WHERE key='version';'''
|
||||||
method = getattr(self, method_name, lambda: "Invalid version")
|
parameters = ''
|
||||||
return method()
|
self.cur.execute(item, parameters)
|
||||||
# return None when its getting wrong input
|
return int(self.cur.fetchall()[0][0])
|
||||||
return None
|
|
||||||
|
|
||||||
def version_1(self):
|
def upgrade_one_level(self, level):
|
||||||
|
""" Apply switcher to call methods accordingly """
|
||||||
|
|
||||||
|
if level != self.get_current_level():
|
||||||
|
return None
|
||||||
|
|
||||||
|
# Migrate Db with level
|
||||||
|
method_name = 'upgrade_schema_data_' + str(level)
|
||||||
|
method = getattr(self, method_name, lambda: "Invalid version")
|
||||||
|
return method()
|
||||||
|
|
||||||
|
def upgrade_to_latest(self, cur):
|
||||||
|
"""
|
||||||
|
Initialise upgrade level
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Declare variables
|
||||||
|
self.cur = cur
|
||||||
|
self.current_level = self.get_current_level()
|
||||||
|
self.max_level = 11
|
||||||
|
|
||||||
|
# call upgrading level in loop
|
||||||
|
for l in range(self.current_level, self.max_level):
|
||||||
|
self.upgrade_one_level(l)
|
||||||
|
self.upgrade_schema_data_level(l)
|
||||||
|
|
||||||
|
def upgrade_schema_data_level(self, level):
|
||||||
|
print "------------- Call upgrade level ", level
|
||||||
|
item = '''update settings set value=? WHERE key='version';'''
|
||||||
|
parameters = (level + 1,)
|
||||||
|
self.cur.execute(item, parameters)
|
||||||
|
|
||||||
|
def upgrade_schema_data_1(self):
|
||||||
"""
|
"""
|
||||||
For version 1 and 3
|
For version 1 and 3
|
||||||
Add a new column to the inventory table to store tags.
|
Add a new column to the inventory table to store tags.
|
||||||
|
@ -53,11 +81,8 @@ class UpgradeDB():
|
||||||
item = '''ALTER TABLE inventory ADD tag blob DEFAULT '' '''
|
item = '''ALTER TABLE inventory ADD tag blob DEFAULT '' '''
|
||||||
parameters = ''
|
parameters = ''
|
||||||
self.cur.execute(item, parameters)
|
self.cur.execute(item, parameters)
|
||||||
item = '''update settings set value=? WHERE key='version';'''
|
|
||||||
parameters = (4,)
|
|
||||||
self.cur.execute(item, parameters)
|
|
||||||
|
|
||||||
def version_2(self):
|
def upgrade_schema_data_2(self):
|
||||||
"""
|
"""
|
||||||
For version 2
|
For version 2
|
||||||
Let's get rid of the first20bytesofencryptedmessage field in the inventory table.
|
Let's get rid of the first20bytesofencryptedmessage field in the inventory table.
|
||||||
|
@ -82,18 +107,16 @@ class UpgradeDB():
|
||||||
'''INSERT INTO inventory SELECT hash, objecttype, streamnumber, payload, receivedtime'''
|
'''INSERT INTO inventory SELECT hash, objecttype, streamnumber, payload, receivedtime'''
|
||||||
''' FROM inventory_backup;''')
|
''' FROM inventory_backup;''')
|
||||||
self.cur.execute('''DROP TABLE inventory_backup;''')
|
self.cur.execute('''DROP TABLE inventory_backup;''')
|
||||||
item = '''update settings set value=? WHERE key='version';'''
|
|
||||||
parameters = (3,)
|
|
||||||
self.cur.execute(item, parameters)
|
|
||||||
|
|
||||||
def version_3(self):
|
def upgrade_schema_data_3(self):
|
||||||
"""
|
"""
|
||||||
For version 3
|
For version 3
|
||||||
Call method for version 1
|
Call method for version 1
|
||||||
"""
|
"""
|
||||||
self.version_1()
|
|
||||||
|
|
||||||
def version_4(self):
|
self.upgrade_schema_data_1()
|
||||||
|
|
||||||
|
def upgrade_schema_data_4(self):
|
||||||
"""
|
"""
|
||||||
For version 4
|
For version 4
|
||||||
Add a new column to the pubkeys table to store the address version.
|
Add a new column to the pubkeys table to store the address version.
|
||||||
|
@ -106,11 +129,8 @@ class UpgradeDB():
|
||||||
'''usedpersonally text, UNIQUE(hash, addressversion) ON CONFLICT REPLACE)''')
|
'''usedpersonally text, UNIQUE(hash, addressversion) ON CONFLICT REPLACE)''')
|
||||||
self.cur.execute(
|
self.cur.execute(
|
||||||
'''delete from inventory where objecttype = 'pubkey';''')
|
'''delete from inventory where objecttype = 'pubkey';''')
|
||||||
item = '''update settings set value=? WHERE key='version';'''
|
|
||||||
parameters = (5,)
|
|
||||||
self.cur.execute(item, parameters)
|
|
||||||
|
|
||||||
def version_5(self):
|
def upgrade_schema_data_5(self):
|
||||||
"""
|
"""
|
||||||
For version 5
|
For version 5
|
||||||
Add a new table: objectprocessorqueue with which to hold objects
|
Add a new table: objectprocessorqueue with which to hold objects
|
||||||
|
@ -121,11 +141,8 @@ class UpgradeDB():
|
||||||
self.cur.execute(
|
self.cur.execute(
|
||||||
'''CREATE TABLE objectprocessorqueue'''
|
'''CREATE TABLE objectprocessorqueue'''
|
||||||
''' (objecttype text, data blob, UNIQUE(objecttype, data) ON CONFLICT REPLACE)''')
|
''' (objecttype text, data blob, UNIQUE(objecttype, data) ON CONFLICT REPLACE)''')
|
||||||
item = '''update settings set value=? WHERE key='version';'''
|
|
||||||
parameters = (6,)
|
|
||||||
self.cur.execute(item, parameters)
|
|
||||||
|
|
||||||
def version_6(self):
|
def upgrade_schema_data_6(self):
|
||||||
"""
|
"""
|
||||||
For version 6
|
For version 6
|
||||||
Changes related to protocol v3
|
Changes related to protocol v3
|
||||||
|
@ -145,13 +162,8 @@ class UpgradeDB():
|
||||||
self.cur.execute(
|
self.cur.execute(
|
||||||
'''CREATE TABLE objectprocessorqueue'''
|
'''CREATE TABLE objectprocessorqueue'''
|
||||||
''' (objecttype int, data blob, UNIQUE(objecttype, data) ON CONFLICT REPLACE)''')
|
''' (objecttype int, data blob, UNIQUE(objecttype, data) ON CONFLICT REPLACE)''')
|
||||||
item = '''update settings set value=? WHERE key='version';'''
|
|
||||||
parameters = (7,)
|
|
||||||
self.cur.execute(item, parameters)
|
|
||||||
logger.debug(
|
|
||||||
'Finished dropping and recreating the inventory table.')
|
|
||||||
|
|
||||||
def version_7(self):
|
def upgrade_schema_data_7(self):
|
||||||
"""
|
"""
|
||||||
For version 7
|
For version 7
|
||||||
The format of data stored in the pubkeys table has changed. Let's
|
The format of data stored in the pubkeys table has changed. Let's
|
||||||
|
@ -166,33 +178,25 @@ class UpgradeDB():
|
||||||
'''delete from inventory where objecttype = 1;''')
|
'''delete from inventory where objecttype = 1;''')
|
||||||
self.cur.execute(
|
self.cur.execute(
|
||||||
'''delete from pubkeys;''')
|
'''delete from pubkeys;''')
|
||||||
# Any sending messages for which we *thought* that we had
|
|
||||||
# the pubkey must be rechecked.
|
|
||||||
self.cur.execute(
|
self.cur.execute(
|
||||||
'''UPDATE sent SET status='msgqueued' WHERE status='doingmsgpow' or status='badkey';''')
|
'''UPDATE sent SET status='msgqueued' WHERE status='doingmsgpow' or status='badkey';''')
|
||||||
query = '''update settings set value=? WHERE key='version';'''
|
|
||||||
parameters = (8,)
|
|
||||||
self.cur.execute(query, parameters)
|
|
||||||
logger.debug('Finished clearing currently held pubkeys.')
|
|
||||||
|
|
||||||
def version_8(self):
|
def upgrade_schema_data_8(self):
|
||||||
"""
|
"""
|
||||||
For version 8
|
For version 8
|
||||||
Add a new column to the inbox table to store the hash of
|
Add a new column to the inbox table to store the hash of
|
||||||
the message signature. We'll use this as temporary message UUID
|
the message signature. We'll use this as temporary message UUID
|
||||||
in order to detect duplicates.
|
in order to detect duplicates.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
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 = ''
|
parameters = ''
|
||||||
self.cur.execute(item, parameters)
|
self.cur.execute(item, parameters)
|
||||||
item = '''update settings set value=? WHERE key='version';'''
|
|
||||||
parameters = (9,)
|
|
||||||
self.cur.execute(item, parameters)
|
|
||||||
|
|
||||||
def version_9(self):
|
def upgrade_schema_data_9(self):
|
||||||
"""
|
"""
|
||||||
For version 9
|
For version 9
|
||||||
We'll also need a `sleeptill` field and a `ttl` field. Also we
|
We'll also need a `sleeptill` field and a `ttl` field. Also we
|
||||||
|
@ -204,6 +208,7 @@ class UpgradeDB():
|
||||||
' combining the pubkeyretrynumber and msgretrynumber'
|
' combining the pubkeyretrynumber and msgretrynumber'
|
||||||
' fields into the retrynumber field and adding the'
|
' fields into the retrynumber field and adding the'
|
||||||
' sleeptill and ttl fields...')
|
' sleeptill and ttl fields...')
|
||||||
|
|
||||||
self.cur.execute(
|
self.cur.execute(
|
||||||
'''CREATE TEMPORARY TABLE sent_backup'''
|
'''CREATE TEMPORARY TABLE sent_backup'''
|
||||||
''' (msgid blob, toaddress text, toripe blob, fromaddress text, subject text, message text,'''
|
''' (msgid blob, toaddress text, toripe blob, fromaddress text, subject text, message text,'''
|
||||||
|
@ -227,7 +232,7 @@ class UpgradeDB():
|
||||||
logger.debug('In messages.dat database, adding address field to the pubkeys table.')
|
logger.debug('In messages.dat database, adding address field to the pubkeys table.')
|
||||||
# We're going to have to calculate the address for each row in the pubkeys
|
# We're going to have to calculate the address for each row in the pubkeys
|
||||||
# table. Then we can take out the hash field.
|
# table. Then we can take out the hash field.
|
||||||
self.cur.execute('''ALTER TABLE pubkeys ADD address text DEFAULT '' ''')
|
self.cur.execute('''ALTER TABLE pubkeys ADD address text DEFAULT ''')
|
||||||
self.cur.execute('''SELECT hash, addressversion FROM pubkeys''')
|
self.cur.execute('''SELECT hash, addressversion FROM pubkeys''')
|
||||||
queryResult = self.cur.fetchall()
|
queryResult = self.cur.fetchall()
|
||||||
from addresses import encodeAddress
|
from addresses import encodeAddress
|
||||||
|
@ -257,9 +262,9 @@ class UpgradeDB():
|
||||||
logger.debug(
|
logger.debug(
|
||||||
'In messages.dat database, done adding address field to the pubkeys table'
|
'In messages.dat database, done adding address field to the pubkeys table'
|
||||||
' 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';''')
|
||||||
|
|
||||||
def version_10(self):
|
def upgrade_schema_data_10(self):
|
||||||
"""
|
"""
|
||||||
For version 10
|
For version 10
|
||||||
Update the address colunm to unique in addressbook table
|
Update the address colunm to unique in addressbook table
|
||||||
|
@ -276,7 +281,7 @@ class UpgradeDB():
|
||||||
self.cur.execute(
|
self.cur.execute(
|
||||||
'''INSERT INTO addressbook SELECT label, address FROM old_addressbook;''')
|
'''INSERT INTO addressbook SELECT label, address FROM old_addressbook;''')
|
||||||
self.cur.execute('''DROP TABLE old_addressbook''')
|
self.cur.execute('''DROP TABLE old_addressbook''')
|
||||||
self.cur.execute('''update settings set value=11 WHERE key='version';''')
|
# self.cur.execute('''update settings set value=11 WHERE key='version';''')
|
||||||
|
|
||||||
|
|
||||||
class sqlThread(threading.Thread, UpgradeDB):
|
class sqlThread(threading.Thread, UpgradeDB):
|
||||||
|
@ -438,12 +443,7 @@ class sqlThread(threading.Thread, UpgradeDB):
|
||||||
self.conn.commit()
|
self.conn.commit()
|
||||||
|
|
||||||
# Upgrade Db with respect to their versions
|
# Upgrade Db with respect to their versions
|
||||||
item = '''SELECT value FROM settings WHERE key='version';'''
|
self.upgrade_to_latest(self.cur)
|
||||||
parameters = ''
|
|
||||||
self.cur.execute(item, parameters)
|
|
||||||
currentVersion = int(self.cur.fetchall()[0][0])
|
|
||||||
# Call upgrade class for upgrade DB
|
|
||||||
self.switcher(self.cur, currentVersion)
|
|
||||||
|
|
||||||
# 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
|
||||||
|
|
Loading…
Reference in New Issue
Block a user