We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Upgrading DB from v26->v27 can fail if user balancer LP events stored in their DB
## Problem Definition

A user who upgraded from v1.16.2 to v1.18.1 notified us that they saw a DB upgrade failure from v26->v27. Which means the app versions v1.17.2 to v1.18.0.

Turns out that for specific user DBs who have had some Balancer LP events detected and had both the balancer events and the balancer pools DB table populated the DB upgrade would fail, since the upgrade deletes the balancer pools table first, hence possibly hitting a constraint.

## Workaround

Workaround is rather easy. Download v1.17.0-v1.17.2, since that can open v26 DB, purge all uniswap and balancer data, and then open with v1.18.XX.

## Task

Fix the upgrade so that this does not occur even for this special case of users.

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `rotkehlchen/db/upgrades/v26_v27.py`
Content:
```
1 from typing import TYPE_CHECKING
2 
3 if TYPE_CHECKING:
4     from rotkehlchen.db.dbhandler import DBHandler
5 
6 
7 def upgrade_v26_to_v27(db: 'DBHandler') -> None:
8     """Upgrades the DB from v26 to v27
9 
10     - Deletes and recreates the tables that were changed after removing UnknownEthereumToken
11     """
12     cursor = db.conn.cursor()
13     cursor.execute('DROP TABLE IF EXISTS balancer_pools;')
14 
15     cursor.execute('DROP TABLE IF EXISTS balancer_events;')
16     cursor.execute("""
17 CREATE TABLE IF NOT EXISTS balancer_events (
18     tx_hash VARCHAR[42] NOT NULL,
19     log_index INTEGER NOT NULL,
20     address VARCHAR[42] NOT NULL,
21     timestamp INTEGER NOT NULL,
22     type TEXT NOT NULL,
23     pool_address_token TEXT NOT NULL,
24     lp_amount TEXT NOT NULL,
25     usd_value TEXT NOT NULL,
26     amount0 TEXT NOT NULL,
27     amount1 TEXT NOT NULL,
28     amount2 TEXT,
29     amount3 TEXT,
30     amount4 TEXT,
31     amount5 TEXT,
32     amount6 TEXT,
33     amount7 TEXT,
34     FOREIGN KEY (pool_address_token) REFERENCES assets(identifier) ON UPDATE CASCADE,
35     PRIMARY KEY (tx_hash, log_index)
36 );
37 """)
38     cursor.execute('DELETE FROM used_query_ranges WHERE name LIKE "balancer_events%";')
39 
40     cursor.execute('DROP TABLE IF EXISTS amm_swaps;')
41     cursor.execute("""
42 CREATE TABLE IF NOT EXISTS amm_swaps (
43     tx_hash VARCHAR[42] NOT NULL,
44     log_index INTEGER NOT NULL,
45     address VARCHAR[42] NOT NULL,
46     from_address VARCHAR[42] NOT NULL,
47     to_address VARCHAR[42] NOT NULL,
48     timestamp INTEGER NOT NULL,
49     location CHAR(1) NOT NULL DEFAULT('A') REFERENCES location(location),
50     token0_identifier TEXT NOT NULL,
51     token1_identifier TEXT NOT NULL,
52     amount0_in TEXT,
53     amount1_in TEXT,
54     amount0_out TEXT,
55     amount1_out TEXT,
56     FOREIGN KEY(token0_identifier) REFERENCES assets(identifier) ON UPDATE CASCADE,
57     FOREIGN KEY(token1_identifier) REFERENCES assets(identifier) ON UPDATE CASCADE,
58     PRIMARY KEY (tx_hash, log_index)
59 );""")
60     cursor.execute('DELETE FROM used_query_ranges WHERE name LIKE "balancer_trades%";')
61     cursor.execute('DELETE FROM used_query_ranges WHERE name LIKE "uniswap_trades%";')
62 
63     cursor.execute('DROP TABLE IF EXISTS uniswap_events;')
64     cursor.execute("""
65 CREATE TABLE IF NOT EXISTS uniswap_events (
66     tx_hash VARCHAR[42] NOT NULL,
67     log_index INTEGER NOT NULL,
68     address VARCHAR[42] NOT NULL,
69     timestamp INTEGER NOT NULL,
70     type TEXT NOT NULL,
71     pool_address VARCHAR[42] NOT NULL,
72     token0_identifier TEXT NOT NULL,
73     token1_identifier TEXT NOT NULL,
74     amount0 TEXT,
75     amount1 TEXT,
76     usd_price TEXT,
77     lp_amount TEXT,
78     FOREIGN KEY(token0_identifier) REFERENCES assets(identifier) ON UPDATE CASCADE,
79     FOREIGN KEY(token1_identifier) REFERENCES assets(identifier) ON UPDATE CASCADE,
80     PRIMARY KEY (tx_hash, log_index)
81 );""")
82     cursor.execute('DELETE FROM used_query_ranges WHERE name LIKE "uniswap_events%";')
83 
84     db.conn.commit()
85
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/rotkehlchen/db/upgrades/v26_v27.py b/rotkehlchen/db/upgrades/v26_v27.py
--- a/rotkehlchen/db/upgrades/v26_v27.py
+++ b/rotkehlchen/db/upgrades/v26_v27.py
@@ -10,8 +10,6 @@
     - Deletes and recreates the tables that were changed after removing UnknownEthereumToken
     """
     cursor = db.conn.cursor()
-    cursor.execute('DROP TABLE IF EXISTS balancer_pools;')
-
     cursor.execute('DROP TABLE IF EXISTS balancer_events;')
     cursor.execute("""
 CREATE TABLE IF NOT EXISTS balancer_events (
@@ -35,6 +33,7 @@
     PRIMARY KEY (tx_hash, log_index)
 );
 """)
+    cursor.execute('DROP TABLE IF EXISTS balancer_pools;')
     cursor.execute('DELETE FROM used_query_ranges WHERE name LIKE "balancer_events%";')
 
     cursor.execute('DROP TABLE IF EXISTS amm_swaps;')

{"golden_diff": "diff --git a/rotkehlchen/db/upgrades/v26_v27.py b/rotkehlchen/db/upgrades/v26_v27.py\n--- a/rotkehlchen/db/upgrades/v26_v27.py\n+++ b/rotkehlchen/db/upgrades/v26_v27.py\n@@ -10,8 +10,6 @@\n     - Deletes and recreates the tables that were changed after removing UnknownEthereumToken\n     \"\"\"\n     cursor = db.conn.cursor()\n-    cursor.execute('DROP TABLE IF EXISTS balancer_pools;')\n-\n     cursor.execute('DROP TABLE IF EXISTS balancer_events;')\n     cursor.execute(\"\"\"\n CREATE TABLE IF NOT EXISTS balancer_events (\n@@ -35,6 +33,7 @@\n     PRIMARY KEY (tx_hash, log_index)\n );\n \"\"\")\n+    cursor.execute('DROP TABLE IF EXISTS balancer_pools;')\n     cursor.execute('DELETE FROM used_query_ranges WHERE name LIKE \"balancer_events%\";')\n \n     cursor.execute('DROP TABLE IF EXISTS amm_swaps;')\n", "issue": "Upgrading DB from v26->v27 can fail if user balancer LP events stored in their DB\n## Problem Definition\r\n\r\nA user who upgraded from v1.16.2 to v1.18.1 notified us that they saw a DB upgrade failure from v26->v27. Which means the app versions v1.17.2 to v1.18.0.\r\n\r\nTurns out that for specific user DBs who have had some Balancer LP events detected and had both the balancer events and the balancer pools DB table populated the DB upgrade would fail, since the upgrade deletes the balancer pools table first, hence possibly hitting a constraint.\r\n\r\n## Workaround\r\n\r\nWorkaround is rather easy. Download v1.17.0-v1.17.2, since that can open v26 DB, purge all uniswap and balancer data, and then open with v1.18.XX.\r\n\r\n## Task\r\n\r\nFix the upgrade so that this does not occur even for this special case of users.\n", "before_files": [{"content": "from typing import TYPE_CHECKING\n\nif TYPE_CHECKING:\n    from rotkehlchen.db.dbhandler import DBHandler\n\n\ndef upgrade_v26_to_v27(db: 'DBHandler') -> None:\n    \"\"\"Upgrades the DB from v26 to v27\n\n    - Deletes and recreates the tables that were changed after removing UnknownEthereumToken\n    \"\"\"\n    cursor = db.conn.cursor()\n    cursor.execute('DROP TABLE IF EXISTS balancer_pools;')\n\n    cursor.execute('DROP TABLE IF EXISTS balancer_events;')\n    cursor.execute(\"\"\"\nCREATE TABLE IF NOT EXISTS balancer_events (\n    tx_hash VARCHAR[42] NOT NULL,\n    log_index INTEGER NOT NULL,\n    address VARCHAR[42] NOT NULL,\n    timestamp INTEGER NOT NULL,\n    type TEXT NOT NULL,\n    pool_address_token TEXT NOT NULL,\n    lp_amount TEXT NOT NULL,\n    usd_value TEXT NOT NULL,\n    amount0 TEXT NOT NULL,\n    amount1 TEXT NOT NULL,\n    amount2 TEXT,\n    amount3 TEXT,\n    amount4 TEXT,\n    amount5 TEXT,\n    amount6 TEXT,\n    amount7 TEXT,\n    FOREIGN KEY (pool_address_token) REFERENCES assets(identifier) ON UPDATE CASCADE,\n    PRIMARY KEY (tx_hash, log_index)\n);\n\"\"\")\n    cursor.execute('DELETE FROM used_query_ranges WHERE name LIKE \"balancer_events%\";')\n\n    cursor.execute('DROP TABLE IF EXISTS amm_swaps;')\n    cursor.execute(\"\"\"\nCREATE TABLE IF NOT EXISTS amm_swaps (\n    tx_hash VARCHAR[42] NOT NULL,\n    log_index INTEGER NOT NULL,\n    address VARCHAR[42] NOT NULL,\n    from_address VARCHAR[42] NOT NULL,\n    to_address VARCHAR[42] NOT NULL,\n    timestamp INTEGER NOT NULL,\n    location CHAR(1) NOT NULL DEFAULT('A') REFERENCES location(location),\n    token0_identifier TEXT NOT NULL,\n    token1_identifier TEXT NOT NULL,\n    amount0_in TEXT,\n    amount1_in TEXT,\n    amount0_out TEXT,\n    amount1_out TEXT,\n    FOREIGN KEY(token0_identifier) REFERENCES assets(identifier) ON UPDATE CASCADE,\n    FOREIGN KEY(token1_identifier) REFERENCES assets(identifier) ON UPDATE CASCADE,\n    PRIMARY KEY (tx_hash, log_index)\n);\"\"\")\n    cursor.execute('DELETE FROM used_query_ranges WHERE name LIKE \"balancer_trades%\";')\n    cursor.execute('DELETE FROM used_query_ranges WHERE name LIKE \"uniswap_trades%\";')\n\n    cursor.execute('DROP TABLE IF EXISTS uniswap_events;')\n    cursor.execute(\"\"\"\nCREATE TABLE IF NOT EXISTS uniswap_events (\n    tx_hash VARCHAR[42] NOT NULL,\n    log_index INTEGER NOT NULL,\n    address VARCHAR[42] NOT NULL,\n    timestamp INTEGER NOT NULL,\n    type TEXT NOT NULL,\n    pool_address VARCHAR[42] NOT NULL,\n    token0_identifier TEXT NOT NULL,\n    token1_identifier TEXT NOT NULL,\n    amount0 TEXT,\n    amount1 TEXT,\n    usd_price TEXT,\n    lp_amount TEXT,\n    FOREIGN KEY(token0_identifier) REFERENCES assets(identifier) ON UPDATE CASCADE,\n    FOREIGN KEY(token1_identifier) REFERENCES assets(identifier) ON UPDATE CASCADE,\n    PRIMARY KEY (tx_hash, log_index)\n);\"\"\")\n    cursor.execute('DELETE FROM used_query_ranges WHERE name LIKE \"uniswap_events%\";')\n\n    db.conn.commit()\n", "path": "rotkehlchen/db/upgrades/v26_v27.py"}], "after_files": [{"content": "from typing import TYPE_CHECKING\n\nif TYPE_CHECKING:\n    from rotkehlchen.db.dbhandler import DBHandler\n\n\ndef upgrade_v26_to_v27(db: 'DBHandler') -> None:\n    \"\"\"Upgrades the DB from v26 to v27\n\n    - Deletes and recreates the tables that were changed after removing UnknownEthereumToken\n    \"\"\"\n    cursor = db.conn.cursor()\n    cursor.execute('DROP TABLE IF EXISTS balancer_events;')\n    cursor.execute(\"\"\"\nCREATE TABLE IF NOT EXISTS balancer_events (\n    tx_hash VARCHAR[42] NOT NULL,\n    log_index INTEGER NOT NULL,\n    address VARCHAR[42] NOT NULL,\n    timestamp INTEGER NOT NULL,\n    type TEXT NOT NULL,\n    pool_address_token TEXT NOT NULL,\n    lp_amount TEXT NOT NULL,\n    usd_value TEXT NOT NULL,\n    amount0 TEXT NOT NULL,\n    amount1 TEXT NOT NULL,\n    amount2 TEXT,\n    amount3 TEXT,\n    amount4 TEXT,\n    amount5 TEXT,\n    amount6 TEXT,\n    amount7 TEXT,\n    FOREIGN KEY (pool_address_token) REFERENCES assets(identifier) ON UPDATE CASCADE,\n    PRIMARY KEY (tx_hash, log_index)\n);\n\"\"\")\n    cursor.execute('DROP TABLE IF EXISTS balancer_pools;')\n    cursor.execute('DELETE FROM used_query_ranges WHERE name LIKE \"balancer_events%\";')\n\n    cursor.execute('DROP TABLE IF EXISTS amm_swaps;')\n    cursor.execute(\"\"\"\nCREATE TABLE IF NOT EXISTS amm_swaps (\n    tx_hash VARCHAR[42] NOT NULL,\n    log_index INTEGER NOT NULL,\n    address VARCHAR[42] NOT NULL,\n    from_address VARCHAR[42] NOT NULL,\n    to_address VARCHAR[42] NOT NULL,\n    timestamp INTEGER NOT NULL,\n    location CHAR(1) NOT NULL DEFAULT('A') REFERENCES location(location),\n    token0_identifier TEXT NOT NULL,\n    token1_identifier TEXT NOT NULL,\n    amount0_in TEXT,\n    amount1_in TEXT,\n    amount0_out TEXT,\n    amount1_out TEXT,\n    FOREIGN KEY(token0_identifier) REFERENCES assets(identifier) ON UPDATE CASCADE,\n    FOREIGN KEY(token1_identifier) REFERENCES assets(identifier) ON UPDATE CASCADE,\n    PRIMARY KEY (tx_hash, log_index)\n);\"\"\")\n    cursor.execute('DELETE FROM used_query_ranges WHERE name LIKE \"balancer_trades%\";')\n    cursor.execute('DELETE FROM used_query_ranges WHERE name LIKE \"uniswap_trades%\";')\n\n    cursor.execute('DROP TABLE IF EXISTS uniswap_events;')\n    cursor.execute(\"\"\"\nCREATE TABLE IF NOT EXISTS uniswap_events (\n    tx_hash VARCHAR[42] NOT NULL,\n    log_index INTEGER NOT NULL,\n    address VARCHAR[42] NOT NULL,\n    timestamp INTEGER NOT NULL,\n    type TEXT NOT NULL,\n    pool_address VARCHAR[42] NOT NULL,\n    token0_identifier TEXT NOT NULL,\n    token1_identifier TEXT NOT NULL,\n    amount0 TEXT,\n    amount1 TEXT,\n    usd_price TEXT,\n    lp_amount TEXT,\n    FOREIGN KEY(token0_identifier) REFERENCES assets(identifier) ON UPDATE CASCADE,\n    FOREIGN KEY(token1_identifier) REFERENCES assets(identifier) ON UPDATE CASCADE,\n    PRIMARY KEY (tx_hash, log_index)\n);\"\"\")\n    cursor.execute('DELETE FROM used_query_ranges WHERE name LIKE \"uniswap_events%\";')\n\n    db.conn.commit()\n", "path": "rotkehlchen/db/upgrades/v26_v27.py"}]}