Repository: rotki/rotki

Issue:
Rename setting name used for selected binance markets
The setting used to store the selected pairs from binance to be synced has a bad name `PAIRS` that should be updated to something more descriptive. Currently is defined as a constant here https://github.com/rotki/rotki/blob/develop/rotkehlchen/db/constants.py#L3

The task to close this issue is:

1. Rename this setting name with something better
2. if this setting is present already in the `user_credentials_mappings` table do an update on that table and use the new name


Discussion:

Resolving patch:
diff --git a/rotkehlchen/db/constants.py b/rotkehlchen/db/constants.py
--- a/rotkehlchen/db/constants.py
+++ b/rotkehlchen/db/constants.py
@@ -1,6 +1,6 @@
 KRAKEN_ACCOUNT_TYPE_KEY = 'kraken_account_type'
 FTX_SUBACCOUNT_NAME_KEY = 'ftx_subaccount'
-BINANCE_MARKETS_KEY = 'PAIRS'
+BINANCE_MARKETS_KEY = 'binance_selected_trade_pairs'
 USER_CREDENTIAL_MAPPING_KEYS = (
     KRAKEN_ACCOUNT_TYPE_KEY,
     FTX_SUBACCOUNT_NAME_KEY,
diff --git a/rotkehlchen/db/upgrades/v31_v32.py b/rotkehlchen/db/upgrades/v31_v32.py
--- a/rotkehlchen/db/upgrades/v31_v32.py
+++ b/rotkehlchen/db/upgrades/v31_v32.py
@@ -1,6 +1,8 @@
 from collections import defaultdict
 from typing import TYPE_CHECKING, Dict, Set
 
+from rotkehlchen.db.constants import BINANCE_MARKETS_KEY
+
 if TYPE_CHECKING:
     from sqlite3 import Cursor
 
@@ -162,6 +164,12 @@ def _update_history_entries_from_kraken(cursor: 'Cursor') -> None:
         )
 
 
+def _update_settings_name_for_selected_binance_markets(cursor: 'Cursor') -> None:
+    cursor.execute("""
+    UPDATE user_credentials_mappings SET setting_name = ? WHERE setting_name = "PAIRS"
+    """, (BINANCE_MARKETS_KEY,))
+
+
 def upgrade_v31_to_v32(db: 'DBHandler') -> None:
     """Upgrades the DB from v31 to v32
     - use new identifiers for the history_events table. The id will be generated by sqlite
@@ -182,4 +190,5 @@ def upgrade_v31_to_v32(db: 'DBHandler') -> None:
     _add_new_tables(cursor)
     _refactor_manual_balance_id(cursor)
     _update_fee_for_existing_trades(cursor)
+    _update_settings_name_for_selected_binance_markets(cursor)
     db.conn.commit()
diff --git a/rotkehlchen/exchanges/binance.py b/rotkehlchen/exchanges/binance.py
--- a/rotkehlchen/exchanges/binance.py
+++ b/rotkehlchen/exchanges/binance.py
@@ -191,7 +191,7 @@ def __init__(
             database: 'DBHandler',
             msg_aggregator: MessagesAggregator,
             uri: str = BINANCE_BASE_URL,
-            PAIRS: Optional[List[str]] = None,  # noqa: N803
+            binance_selected_trade_pairs: Optional[List[str]] = None,  # noqa: N803
     ):
         exchange_location = Location.BINANCE
         if uri == BINANCEUS_BASE_URL:
@@ -211,7 +211,7 @@ def __init__(
         })
         self.msg_aggregator = msg_aggregator
         self.offset_ms = 0
-        self.selected_pairs = PAIRS
+        self.selected_pairs = binance_selected_trade_pairs
 
     def first_connection(self) -> None:
         if self.first_connection_made:


Test patch:
diff --git a/rotkehlchen/tests/data/v31_rotkehlchen.db b/rotkehlchen/tests/data/v31_rotkehlchen.db
Binary files a/rotkehlchen/tests/data/v31_rotkehlchen.db and b/rotkehlchen/tests/data/v31_rotkehlchen.db differ
diff --git a/rotkehlchen/tests/db/test_db_upgrades.py b/rotkehlchen/tests/db/test_db_upgrades.py
--- a/rotkehlchen/tests/db/test_db_upgrades.py
+++ b/rotkehlchen/tests/db/test_db_upgrades.py
@@ -2483,6 +2483,13 @@ def test_upgrade_db_31_to_32(user_data_dir):  # pylint: disable=unused-argument
     assert len([row[2] for row in result]) == 5
     assert len({row[2] for row in result}) == 3
 
+    # check that user_credential_mappings with setting_name=PAIRS are present
+    selected_binance_markets_before = cursor.execute('SELECT * from user_credentials_mappings WHERE setting_name="PAIRS"').fetchall()  # noqa: E501
+    assert selected_binance_markets_before == [
+        ('binance', 'E', 'PAIRS', 'pro'),
+        ('binanceus', 'S', 'PAIRS', 'abc'),
+    ]
+
     db_v31.logout()
     # Execute upgrade
     db = _init_db_with_target_version(
@@ -2557,6 +2564,12 @@ def test_upgrade_db_31_to_32(user_data_dir):  # pylint: disable=unused-argument
     data_in_db = cursor.execute('SELECT address, ens_name, last_update FROM ens_mappings').fetchone()  # noqa: E501
     assert data_in_db[:2] == ens_names_test_data
     assert data_in_db[2] is not None
+    # Check that selected binance markets settings_name changed to the updated one.
+    selected_binance_markets_after = cursor.execute('SELECT * from user_credentials_mappings WHERE setting_name="binance_selected_trade_pairs"').fetchall()  # noqa: E501
+    assert selected_binance_markets_after == [
+        ('binance', 'E', 'binance_selected_trade_pairs', 'pro'),
+        ('binanceus', 'S', 'binance_selected_trade_pairs', 'abc'),
+    ]
 
 
 def test_latest_upgrade_adds_remove_tables(user_data_dir):
diff --git a/rotkehlchen/tests/exchanges/test_binance.py b/rotkehlchen/tests/exchanges/test_binance.py
--- a/rotkehlchen/tests/exchanges/test_binance.py
+++ b/rotkehlchen/tests/exchanges/test_binance.py
@@ -873,7 +873,12 @@ def test_binance_query_trade_history_custom_markets(function_scope_binance):
     binance = function_scope_binance
 
     markets = ['ETHBTC', 'BNBBTC', 'BTCUSDC']
-    binance.edit_exchange(name=None, api_key=None, api_secret=None, PAIRS=markets)
+    binance.edit_exchange(
+        name=None,
+        api_key=None,
+        api_secret=None,
+        binance_selected_trade_pairs=markets,
+    )
     count = 0
     p = re.compile(r'symbol=[A-Z]*')
     seen = set()