Skip to content
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

Fix the upgrader logic if the tags db file was not created yet #6956

Merged
merged 1 commit into from
Jul 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/tribler/core/upgrade/tests/test_upgrader.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,26 @@ def test_upgrade_pony13to14(upgrader: TriblerUpgrader, state_dir, channels_dir,
assert mds.get_value('db_version') == '14'


def test_upgrade_pony13to14_no_tags(upgrader: TriblerUpgrader, state_dir, channels_dir, trustchain_keypair, mds_path):
tags_path = state_dir / 'sqlite/tags.db'

_copy(source_name='pony_v13.db', target=mds_path)

upgrader.upgrade_pony_db_13to14() # No exception if the tags database file is missing before the upgrade
assert not tags_path.exists() # Tags' database file is still missing after upgrade if it has not existed before

# TagsComponent specifies create_tables=True option when it creates TagDatabase.
# That means that the empty tags' database will be automatically created if it was not already present
tags = TagDatabase(str(tags_path), create_tables=True, check_tables=False)
mds = MetadataStore(mds_path, channels_dir, trustchain_keypair, check_tables=False)

with db_session:
# The end result is the same as in the previous test
assert upgrader.column_exists_in_table(mds._db, 'ChannelNode', 'tag_processor_version')
assert upgrader.column_exists_in_table(tags.instance, 'TorrentTagOp', 'auto_generated')
assert mds.get_value('db_version') == '14'


def test_upgrade_pony12to13(upgrader, channels_dir, mds_path, trustchain_keypair): # pylint: disable=W0621
_copy('pony_v12.db', mds_path)

Expand Down
8 changes: 5 additions & 3 deletions src/tribler/core/upgrade/upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,12 @@ def add_column(db, table_name, column_name, column_type):

self._logger.info(f'{version.current}->{version.next}')

add_column(db=mds._db, table_name='ChannelNode', column_name='tag_processor_version', column_type='INT')
add_column(db=tags.instance, table_name='TorrentTagOp', column_name='auto_generated', column_type='BOOLEAN')
if tags is not None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT:
Maybe (for simplifying the logic of future upgraders) we should create tags DB in the case it does not exist.

Otherwise, in every future upgrader we should use the construction that you have used above:

if tags is non None:
    ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the suggestion! I think it is better to have this check repeated in future upgrades.

Right now, the tags database does not have a version, and we rely on the metadata store version. If we create the tags database during the upgrade, it will already have the latest schema, and the subsequent upgrades should have the logic to skip the upgrade. So, creating the database in the upgrade will not reduce the number of checks.

add_column(db=tags.instance, table_name='TorrentTagOp', column_name='auto_generated',
column_type='BOOLEAN')
tags.instance.commit()

tags.instance.commit()
add_column(db=mds._db, table_name='ChannelNode', column_name='tag_processor_version', column_type='INT')
mds._db.commit()
mds.set_value(key='db_version', value=version.next)

Expand Down