Repository: rotki/rotki

Issue:
For the next database upgrade
In the next database upgrade:

- Change name to the uniswap events table as requested here https://github.com/rotki/rotki/pull/3259#discussion_r679013085


Discussion:

Resolving patch:
diff --git a/rotkehlchen/db/dbhandler.py b/rotkehlchen/db/dbhandler.py
--- a/rotkehlchen/db/dbhandler.py
+++ b/rotkehlchen/db/dbhandler.py
@@ -147,7 +147,7 @@
     ('asset_movements', 'asset', 'fee_asset'),
     ('ledger_actions', 'asset', 'rate_asset'),
     ('amm_swaps', 'token0_identifier', 'token1_identifier'),
-    ('uniswap_events', 'token0_identifier', 'token1_identifier'),
+    ('amm_events', 'token0_identifier', 'token1_identifier'),
     ('adex_events', 'token'),
     ('balancer_events', 'pool_address_token'),
     ('timed_balances', 'currency'),
@@ -1076,7 +1076,7 @@ def delete_eth2_daily_stats(self) -> None:
     def add_amm_events(self, events: Sequence[LiquidityPoolEvent]) -> None:
         query = (
             """
-            INSERT INTO uniswap_events (
+            INSERT INTO amm_events (
                 tx_hash,
                 log_index,
                 address,
@@ -1120,7 +1120,7 @@ def get_amm_events(
         """
         cursor = self.conn.cursor()
         events_sql_str = ", ".join([f'"{EventType.serialize_for_db(event)}"' for event in events])
-        query = f'SELECT * FROM uniswap_events WHERE uniswap_events.type IN ({events_sql_str}) '
+        query = f'SELECT * FROM amm_events WHERE amm_events.type IN ({events_sql_str}) '
 
         # Timestamp filters are omitted, done via `form_query_to_filter_timestamps`
         if address is not None:
@@ -1214,7 +1214,7 @@ def delete_uniswap_events_data(self) -> None:
         burn_uniswap = EventType.serialize_for_db(EventType.BURN_UNISWAP)
         uniswap_types = f'"{mint_uniswap}", "{burn_uniswap}"'
         cursor.execute(
-            f'DELETE FROM uniswap_events WHERE uniswap_events.type IN ({uniswap_types});',
+            f'DELETE FROM amm_events WHERE amm_events.type IN ({uniswap_types});',
         )
         cursor.execute(
             f'DELETE FROM used_query_ranges WHERE name LIKE "{UNISWAP_EVENTS_PREFIX}%";',
@@ -1241,7 +1241,7 @@ def delete_sushiswap_events_data(self) -> None:
         burn_sushiswap = EventType.serialize_for_db(EventType.BURN_SUSHISWAP)
         sushiswap_types = f'"{mint_sushiswap}", "{burn_sushiswap}"'
         cursor.execute(
-            f'DELETE FROM uniswap_events WHERE uniswap_events.type IN ({sushiswap_types});',
+            f'DELETE FROM amm_events WHERE amm_events.type IN ({sushiswap_types});',
         )
         cursor.execute(
             f'DELETE FROM used_query_ranges WHERE name LIKE "{SUSHISWAP_EVENTS_PREFIX}%";',
@@ -2410,7 +2410,7 @@ def delete_data_for_ethereum_address(self, address: ChecksumEthAddress) -> None:
         cursor.execute('DELETE FROM aave_events WHERE address = ?', (address,))
         cursor.execute('DELETE FROM adex_events WHERE address = ?', (address,))
         cursor.execute('DELETE FROM balancer_events WHERE address=?;', (address,))
-        cursor.execute('DELETE FROM uniswap_events WHERE address=?;', (address,))
+        cursor.execute('DELETE FROM amm_events WHERE address=?;', (address,))
         cursor.execute(
             'DELETE FROM multisettings WHERE name LIKE "queried_address_%" AND value = ?',
             (address,),
diff --git a/rotkehlchen/db/schema.py b/rotkehlchen/db/schema.py
--- a/rotkehlchen/db/schema.py
+++ b/rotkehlchen/db/schema.py
@@ -529,7 +529,7 @@
 """
 
 DB_CREATE_AMM_EVENTS = """
-CREATE TABLE IF NOT EXISTS uniswap_events (
+CREATE TABLE IF NOT EXISTS amm_events (
     tx_hash VARCHAR[42] NOT NULL,
     log_index INTEGER NOT NULL,
     address VARCHAR[42] NOT NULL,
diff --git a/rotkehlchen/db/upgrades/v28_v29.py b/rotkehlchen/db/upgrades/v28_v29.py
--- a/rotkehlchen/db/upgrades/v28_v29.py
+++ b/rotkehlchen/db/upgrades/v28_v29.py
@@ -117,4 +117,9 @@ def upgrade_v28_to_v29(db: 'DBHandler') -> None:
     )
 
     _create_new_tables(db.conn)
+
+    # Rename uniswap_events table. Drop amm_events first if it was created at initialization
+    # of the db handler
+    cursor.execute('DROP TABLE IF EXISTS amm_events;')
+    cursor.execute('ALTER TABLE uniswap_events RENAME TO amm_events;')
     db.conn.commit()


Test patch:
diff --git a/rotkehlchen/tests/db/test_db.py b/rotkehlchen/tests/db/test_db.py
--- a/rotkehlchen/tests/db/test_db.py
+++ b/rotkehlchen/tests/db/test_db.py
@@ -110,7 +110,7 @@
     'xpubs',
     'xpub_mappings',
     'amm_swaps',
-    'uniswap_events',
+    'amm_events',
     'eth2_deposits',
     'eth2_daily_staking_details',
     'adex_events',
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
@@ -2308,6 +2308,14 @@ def test_upgrade_db_28_to_29(user_data_dir):  # pylint: disable=unused-argument
         ))
     assert transactions_after == expected_transactions_after
 
+    # check that uniswap_events table was renamed
+    cursor.execute(
+        'SELECT COUNT(*) FROM sqlite_master WHERE type="table" AND name="uniswap_events";',
+    )
+    assert cursor.fetchone()[0] == 0
+    cursor.execute('SELECT COUNT(*) FROM sqlite_master WHERE type="table" AND name="amm_events";')
+    assert cursor.fetchone()[0] == 1
+
     # Finally also make sure that we have updated to the target version
     assert db.get_version() == 29