-
Notifications
You must be signed in to change notification settings - Fork 576
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor sqlthread #1794
base: v0.6
Are you sure you want to change the base?
Refactor sqlthread #1794
Conversation
src/class_sqlThread.py
Outdated
Upgrade Db with respect to versions | ||
""" | ||
|
||
conn, cur = connection_build() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here allow to call it optionally with a parameter for in memory database
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pass arguments with DB file name
src/class_sqlThread.py
Outdated
self.conn = sqlite3.connect(state.appdata + 'messages.dat') | ||
self.conn.text_factory = str | ||
self.cur = self.conn.cursor() | ||
self.conn, self.cur = connection_build() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be in parent class
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inherit connection vars from UpgradeDB class
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved in to __init__
src/class_sqlThread.py
Outdated
self.conn.commit() | ||
|
||
settingsversion = 4 | ||
settingsversion = self.earlier_setting_version(settingsversion) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be in parent class
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
src/class_sqlThread.py
Outdated
@@ -465,10 +349,14 @@ def run(self): # pylint: disable=too-many-locals, too-many-branches, too-many-s | |||
else: | |||
logger.error(err) | |||
|
|||
# Let us check to see the last time we vaccumed the messages.dat file. | |||
# If it has been more than a month let's do it now. | |||
def check_vaccumed(self): # pylint: disable=too-many-locals, too-many-branches, too-many-statements, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move to parent
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
vacuum
not vaccum
src/class_sqlThread.py
Outdated
@@ -635,8 +523,45 @@ def run(self): # pylint: disable=too-many-locals, too-many-branches, too-many-s | |||
helper_sql.sqlReturnQueue.put((self.cur.fetchall(), rowcount)) | |||
# helper_sql.sqlSubmitQueue.task_done() | |||
|
|||
def earlier_setting_version(self, settingsversion): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move to parent
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
different name, to show it's related to BMConfigParser
version rather than sql version
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change name to upgrade_config_parser_setting_version
src/class_sqlThread.py
Outdated
|
||
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, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
only this should be in this class, the other functionality should be refactored into methods and put into parent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
src/class_sqlThread.py
Outdated
self.cur.execute(item, parameters) | ||
|
||
|
||
class sqlThread(threading.Thread, UpgradeDB): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe an UpgradeDB
object would be an attribute of the sqlThread
rarther than sqlThread
inheriting it. That way we isolate the DB stuff and can test it without threading, and the thread would need to use the same interfaces as the test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changes done,
Refactored code and kept in Upgrade DB,
0f93137
to
8f544a2
Compare
44654df
to
f0044d3
Compare
src/class_sqlThread.py
Outdated
def __init__(self): | ||
self._current_level = None | ||
self.max_level = 11 | ||
# self.conn, self.cur = connection_build('messages.dat') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
make another class which overrides this to point to in-memory database, this can then be used for tests. You can call it something like class MockDB(UpgradeDB)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
src/class_sqlThread.py
Outdated
|
||
def __init__(self): | ||
threading.Thread.__init__(self, name="SQL") | ||
def connection_build(file_name): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Put this inside UpgradeDB
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
src/class_sqlThread.py
Outdated
|
||
def __get_current_settings_version(self): | ||
""" | ||
Upgrade Db with respect to their versions |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
update docstring
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
src/class_sqlThread.py
Outdated
|
||
def _upgrade_one_level_sql_statement(self, file_name): | ||
""" | ||
Execute SQL files and queries |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
update docstring
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
src/class_sqlThread.py
Outdated
|
||
helper_startup.updateConfig() | ||
def embaded_version(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be called something like init_database
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changes are done for that and, but for some query I applied same query script.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's call it upgrade_schema_if_old_version
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed def name to upgrade_schema_if_old_version
src/class_sqlThread.py
Outdated
@@ -635,8 +523,45 @@ def run(self): # pylint: disable=too-many-locals, too-many-branches, too-many-s | |||
helper_sql.sqlReturnQueue.put((self.cur.fetchall(), rowcount)) | |||
# helper_sql.sqlSubmitQueue.task_done() | |||
|
|||
def earlier_setting_version(self, settingsversion): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
different name, to show it's related to BMConfigParser
version rather than sql version
src/class_sqlThread.py
Outdated
# If the settings version is equal to 2 or 3 then the | ||
# sqlThread will modify the pubkeys table and change | ||
# the settings version to 4. | ||
settingsversion = BMConfigParser().getint( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
write getter + setter for this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Applied setter and getter
src/class_sqlThread.py
Outdated
# call create_function for encode address | ||
self.create_function(self.conn) | ||
|
||
self.initiate_database(self.cur, self.conn) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something like this to skip init when running tests:
if not self.instanceof(MockDB):
self.initiate_database()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
managed in earlier logic used in memory for test mocking
Your |
src/tests/sql/init_version_5.sql
Outdated
@@ -0,0 +1,10 @@ | |||
DROP TABLE objectprocessorqueue; | |||
|
|||
CREATE TABLE IF NOT EXISTS `knownnodes` ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool (; I haven't seen this used
$ sqlite3 .config/PyBitmessage/messages.dat
SQLite version 3.34.1 2021-01-20 14:10:07
Enter ".help" for usage hints.
sqlite> select * from knownnodes;
Error: no such table: knownnodes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the table was deleted in a later version.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like some weird leftover of deleted code. Since the current upgrade code deletes it, we should keep it, otherwise we'd have to test against ancient versions.
src/tests/sql/init_version_5.sql
Outdated
@@ -0,0 +1,10 @@ | |||
DROP TABLE objectprocessorqueue; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why drop here, if you are going to create it later?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you're right, if it's removed and there is an error, it means there is a bug somewhere
src/tests/sql/init_version_7.sql
Outdated
`payload` blob DEFAULT NULL, | ||
`integer` integer NOT NULL, | ||
UNIQUE(hash) ON CONFLICT REPLACE | ||
) ; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see a difference from the previous version - in the init_version_6.sql
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't reviewed the SQL files yet, I'm focusing on getting the structure of classes and tests right, it's already at like 3rd iteration.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I looked through the tests and instructed to redesign how they perform setup.
@g1itch This PR should among other things separate the SQL execution from threads, so that you can run the tests without launching the SQL thread. There is a lot of work and that's why it's taking long. |
I see. I have no problem with the speed yet. I'm glad to see many line of code deleted in this PR. But I'd prefer to report the strange things, I can see in the code before it got merged. Because I believe we can broke it harder than before. More tests will help to avoid such case. |
By the way, the core test suite reports an interesting issue here: https://buildbot.bitmessage.org/#builders/25/builds/1515 |
src/class_sqlThread.py
Outdated
self.max_level = 11 | ||
self.conn, self.cur = self.connection_build('messages.dat') | ||
|
||
def connection_build(self, file_name=None): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def connection_build(self, file_name='messages.dat'):
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
applied changes method name to __connection_build_internal
and pass file_name
src/class_sqlThread.py
Outdated
return self.__get_current_settings_version() | ||
|
||
@sql_schema_version.setter | ||
def sql_schema_version(self, current_level): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead of current_level
use ignore
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed current_level
src/class_sqlThread.py
Outdated
item = '''update settings set value=? WHERE key='version';''' | ||
parameters = (current_level + 1,) | ||
self.cur.execute(item, parameters) | ||
self._current_level = self.__get_current_settings_version() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
variable self.current_level
shouldn't be needed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now its
def sql_schema_version(self):
"""
Update version with one level
"""
item = '''update settings set value=value+1 WHERE key='version';'''
self.cur.execute(item)
self._current_level = self.__get_current_settings_version()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Its changed into
self.cur.execute(item, self.__get_current_settings_version() + 1)
Because its not taking value+1
src/class_sqlThread.py
Outdated
self.cur.execute(item, parameters) | ||
self.conn.commit() | ||
|
||
settingsversion = 3 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
separate getter setter for settingsversion
src/class_sqlThread.py
Outdated
# Redefinition-of-parameters-type-from-tuple-to-str, R0204, line-too-long, E501 | ||
"""Process SQL queries from `.helper_sql.sqlSubmitQueue`""" | ||
helper_sql.sql_available = True | ||
self.conn, self.cur = UpgradeDB().conn, UpgradeDB().cur |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
insetad just create one object and save it, like
self.db = UpgradeDB()
and then just reference
self.db.cur.execute(blabla)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
passed while initialise
src/class_sqlThread.py
Outdated
self.cur.execute('PRAGMA secure_delete = true') | ||
|
||
# call create_function for encode address | ||
self.create_function(self.conn) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self.db.create_function(blabla)
src/class_sqlThread.py
Outdated
# call create_function for encode address | ||
self.create_function(self.conn) | ||
|
||
self.initiate_database(self.cur, self.conn) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self.db.initialize_schema()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed into initialize_schema
src/class_sqlThread.py
Outdated
# version we are on can stay embedded in the messages.dat file. Let us | ||
# check to see if the settings table exists yet. | ||
|
||
self.embaded_version() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed to upgrade_schema_if_old_version
79ebee5
to
7240658
Compare
d5296dd
to
a55c086
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Barely readable ); The tests/sql
seems duplicating sql
setup.py
Outdated
@@ -137,7 +137,7 @@ def run(self): | |||
package_data={'': [ | |||
'bitmessageqt/*.ui', 'bitmsghash/*.cl', 'sslkeys/*.pem', | |||
'translations/*.ts', 'translations/*.qm', | |||
'images/*.png', 'images/*.ico', 'images/*.icns' | |||
'images/*.png', 'images/*.ico', 'images/*.icns', 'sql/*.py', 'sql/*.sql' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand the purpose of this empty sql/__init__.py
and especially listing it in the package_data.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The file was removed, but the reference in setup.py
still persists. @cis-muzahid please delete this reference, and also delete the file src/tests/sql/__init__.py
, that is also unnecessary.
src/class_sqlThread.py
Outdated
@@ -8,6 +8,7 @@ | |||
import sys | |||
import threading | |||
import time | |||
import logging |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isort
src/class_sqlThread.py
Outdated
class BitmessageDB(object): | ||
""" | ||
Upgrade Db with respect to versions | ||
""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
strange formatting
src/class_sqlThread.py
Outdated
""" | ||
Get current setting Version | ||
""" | ||
item = '''SELECT value FROM settings WHERE key='version';''' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for semicolon
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem, we found out what it is, will be fixed in next commit.
src/class_sqlThread.py
Outdated
@property | ||
def sql_config_settings_version(self): | ||
""" | ||
Getter for BmConfigparser |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BMConfigParser
(obj)
"MainWindow", | ||
'Alert: Your disk or data storage volume is full. Bitmessage will now exit.'), | ||
True))) | ||
os._exit(0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Weird formatting
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree
' Here is the actual error message thrown by the sqlThread: %s', | ||
str(item), | ||
str(repr(parameters)), | ||
str(err)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These casts seem redundant
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tend to agree. Something like traceback.format_exc
would be better.
src/tests/core.py
Outdated
queryreturn = sqlExecute("INSERT INTO testtbl VALUES(101);") | ||
self.assertEqual(queryreturn, 1) | ||
|
||
queryreturn = sqlQuery(''' SELECT * FROM testtbl ''') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"SELECT * FROM testtbl"
src/tests/core.py
Outdated
@@ -409,6 +409,22 @@ def test_adding_two_same_case_sensitive_addresses(self): | |||
self.delete_address_from_addressbook(address1) | |||
self.delete_address_from_addressbook(address2) | |||
|
|||
def test_sqlscripts(self): | |||
""" Testing sql statements""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What it tests?
a55c086
to
b3d6dc4
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see no point in another review since my previous comments are mostly ignored.
src/class_sqlThread.py
Outdated
parameters = helper_sql.sqlSubmitQueue.get() | ||
rowcount = 0 | ||
try: | ||
self.db.cur.execute(item, parameters) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
item
is a bad name. Use query
.
dd64925
to
bb88554
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are still a couple of unresolved conversations.
setup.py
Outdated
@@ -137,7 +137,7 @@ def run(self): | |||
package_data={'': [ | |||
'bitmessageqt/*.ui', 'bitmsghash/*.cl', 'sslkeys/*.pem', | |||
'translations/*.ts', 'translations/*.qm', | |||
'images/*.png', 'images/*.ico', 'images/*.icns' | |||
'images/*.png', 'images/*.ico', 'images/*.icns', 'sql/*.py', 'sql/*.sql' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The file was removed, but the reference in setup.py
still persists. @cis-muzahid please delete this reference, and also delete the file src/tests/sql/__init__.py
, that is also unnecessary.
"MainWindow", | ||
'Alert: Your disk or data storage volume is full. Bitmessage will now exit.'), | ||
True))) | ||
os._exit(0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree
c531e62
to
533df72
Compare
533df72
to
899d28f
Compare
2b89a05
to
d6acbc3
Compare
c953c10
to
de89853
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The PR shouldn't remove the *.sql
files. It should ignore them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unstage the sql files
676025a
to
f61b1c1
Compare
4267c44
to
a13a37d
Compare
38f7628
to
3720c3f
Compare
Refactor sqlthread and add test cases for setting versions and SQL internal function called create_function