Skip to content

Commit

Permalink
Fix the upgrade logic if the tags database was not created yet
Browse files Browse the repository at this point in the history
  • Loading branch information
kozlovsky committed Jun 29, 2022
1 parent ec023e6 commit 6e8104b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
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
7 changes: 4 additions & 3 deletions src/tribler/core/upgrade/upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,11 @@ 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:
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

0 comments on commit 6e8104b

Please sign in to comment.