Repository: rotki/rotki

Issue:
rotki failed to start
## Problem Definition

<!-- Provide a description of what is the current problem and why you are raising this issue.
If it's a bug please describe what was the unexpected thing that occured and what was the
expected behaviour. -->

I uninstall rotki from my mac approximately 1 year ago. and now I re-install it. when I launch it, I get Error in the first page: rotki failed to start with logs below. Seems the database structure changed. So I searched how to delete it and did not find.

fyi I installed it with brew --cask

## Logs

<!-- Before being able to provide logs you need to enable debug logging on rotki, which is off by default.
For information on how to enable it check here: https://rotki.readthedocs.io/en/latest/contribute.html#id2

Attach the relevant part of the logs to the issue, making sure to omit any sensitive/identifiable information. Or join [our discord](https://discord.com/invite/aGCxHG7) and DM it to somebody from our team.

-->
[rotki-core] [rotki-core] Failed to start rotkehlchen backend:
Traceback (most recent call last):
  File "rotkehlchen/globaldb/upgrades/manager.py", line 65, in maybe_upgrade_globaldb
  File "rotkehlchen/globaldb/upgrades/v2_v3.py", line 369, in migrate_to_v3
  File "rotkehlchen/globaldb/upgrades/v2_v3.py", line 316, in translate_binance_pairs
  File "rotkehlchen/db/drivers/gevent.py", line 89, in execute
sqlite3.OperationalError: no such table: binance_pairs

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "rotkehlchen/__main__.py", line 20, in main
  File "rotkehlchen/server.py", line 32, in __init__
  File "rotkehlchen/rotkehlchen.py", line 141, in __init__
  File "rotkehlchen/globaldb/handler.py", line 135, in __new__
  File "rotkehlchen/globaldb/handler.py", line 95, in _initialize_global_db_directory
  File "rotkehlchen/globaldb/handler.py", line 74, in initialize_globaldb
  File "rotkehlchen/globaldb/upgrades/manager.py", line 74, in maybe_upgrade_globaldb
ValueError: Failed at global DB upgrade from version 2 to 3: no such table: binance_pairs



### System Description


Operating system: macOS Ventura 13.0
Rotki version: 1.26.0



Discussion:
Hey @aslimacc thank you very much. The easiest way for you to probably proceed would be to go to `~/Library/Application Support/rotki/data/global` and delete the `global.db` since if you have not used rotki for a year you can have it recreated.

And I guess your user DB is probably also too old so it would probably make sense to restart it.

Other than that your exception shows a potential error in the edge case of someone not using rotki for over a year which we will have to address.

Resolving patch:
diff --git a/rotkehlchen/globaldb/upgrades/v2_v3.py b/rotkehlchen/globaldb/upgrades/v2_v3.py
--- a/rotkehlchen/globaldb/upgrades/v2_v3.py
+++ b/rotkehlchen/globaldb/upgrades/v2_v3.py
@@ -313,6 +313,23 @@ def translate_owned_assets(cursor: 'DBCursor') -> List[Tuple[str]]:
 
 def translate_binance_pairs(cursor: 'DBCursor') -> List[Tuple[str, str, str, str]]:
     """Collect and update assets in the binance_pairs tables to use the new id format"""
+    table_exists = cursor.execute(
+        'SELECT COUNT(*) FROM sqlite_master WHERE type="table" AND name="binance_pairs"',
+    ).fetchone()[0]
+    if table_exists == 0:  # handle binance_pairs not having been created
+        cursor.execute(  # fix https://github.com/rotki/rotki/issues/5073
+            """CREATE TABLE IF NOT EXISTS binance_pairs (
+            pair TEXT NOT NULL,
+            base_asset TEXT NOT NULL,
+            quote_asset TEXT NOT NULL,
+            location TEXT NOT NULL,
+            FOREIGN KEY(base_asset) REFERENCES assets(identifier) ON UPDATE CASCADE ON DELETE CASCADE,
+            FOREIGN KEY(quote_asset) REFERENCES assets(identifier) ON UPDATE CASCADE ON DELETE CASCADE,
+            PRIMARY KEY(pair, location)
+            );""",  # noqa: E501
+        )
+        return []
+
     cursor.execute('SELECT pair, base_asset, quote_asset, location from binance_pairs;')
     binance_pairs = []
     for entry in cursor:


Test patch: