# Buggy code
def _upgrade_18_to_22(self):
    self.current_status = "Upgrading database from v%s to v%s..." % (18, 22)

    self.db.execute("""
DROP INDEX IF EXISTS Torrent_swift_hash_idx;

DROP VIEW IF EXISTS Friend;

ALTER TABLE Peer RENAME TO __Peer_tmp;
CREATE TABLE IF NOT EXISTS Peer (
    peer_id    integer PRIMARY KEY AUTOINCREMENT NOT NULL,
    permid     text NOT NULL,
    name       text,
    thumbnail  text
);

INSERT INTO Peer (peer_id, permid, name, thumbnail) SELECT peer_id, permid, name, thumbnail FROM __Peer_tmp;

DROP TABLE IF EXISTS __Peer_tmp;

ALTER TABLE Torrent ADD COLUMN last_tracker_check integer DEFAULT 0;
ALTER TABLE Torrent ADD COLUMN tracker_check_retries integer DEFAULT 0;
ALTER TABLE Torrent ADD COLUMN next_tracker_check integer DEFAULT 0;

CREATE TABLE IF NOT EXISTS TrackerInfo (
  tracker_id  integer PRIMARY KEY AUTOINCREMENT,
  tracker     text    UNIQUE NOT NULL,
  last_check  numeric DEFAULT 0,
  failures    integer DEFAULT 0,
  is_alive    integer DEFAULT 1
);

CREATE TABLE IF NOT EXISTS TorrentTrackerMapping (
  torrent_id  integer NOT NULL,
  tracker_id  integer NOT NULL,
  FOREIGN KEY (torrent_id) REFERENCES Torrent(torrent_id),
  FOREIGN KEY (tracker_id) REFERENCES TrackerInfo(tracker_id),
  PRIMARY KEY (torrent_id, tracker_id)
);

INSERT OR IGNORE INTO TrackerInfo (tracker) VALUES ('no-DHT');
INSERT OR IGNORE INTO TrackerInfo (tracker) VALUES ('DHT');

DROP INDEX IF EXISTS torrent_biterm_phrase_idx;
DROP TABLE IF EXISTS TorrentBiTermPhrase;
DROP INDEX IF EXISTS termfrequency_freq_idx;
DROP TABLE IF EXISTS TermFrequency;
DROP INDEX IF EXISTS Torrent_insert_idx;
DROP INDEX IF EXISTS Torrent_info_roothash_idx;

DROP TABLE IF EXISTS ClicklogSearch;
DROP INDEX IF EXISTS idx_search_term;
DROP INDEX IF EXISTS idx_search_torrent;
""")
    # update database version
    self.db.write_version(22)


# Traceback (SQLError)
root@bbq:~/tribler# nosetests Tribler/Test/Core/Upgrade/test_db_upgrader.py -v -x
If no C{methodName} argument is passed to the constructor, L{run} will ... ok
test_reimport_torrents ... ERROR
ERROR

======================================================================
ERROR: test_reimport_torrents
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/twisted/internet/defer.py", line 150, in maybeDeferred
result = f(*args, **kw)
File "/usr/local/lib/python2.7/dist-packages/twisted/internet/utils.py", line 201, in runWithWarningsSuppressed
reraise(exc_info[1], exc_info[2])
File "/usr/local/lib/python2.7/dist-packages/twisted/internet/utils.py", line 197, in runWithWarningsSuppressed
result = f(*a, **kw)
File "/root/tribler/Tribler/Test/test_as_server.py", line 56, in check
result = fun(*argv, **kwargs)
File "/root/tribler/Tribler/Test/Core/Upgrade/test_db_upgrader.py", line 55, in test_reimport_torrents
db_migrator.start_migrate()
File "/root/tribler/Tribler/Core/Upgrade/db_upgrader.py", line 59, in start_migrate
self._upgrade_22_to_23()
File "/root/tribler/Tribler/Core/Upgrade/db_upgrader.py", line 310, in _upgrade_22_to_23
""")
File "/root/tribler/Tribler/pyipv8/ipv8/util.py", line 49, in helper
return blockingCallFromThread(reactor, func, *args, **kargs)
File "/root/tribler/Tribler/pyipv8/ipv8/util.py", line 61, in blockingCallFromThread
return f(*args, **kwargs)
File "/root/tribler/Tribler/Core/CacheDB/sqlitecachedb.py", line 295, in execute
raise msg
SQLError: SQLError: error in view SuperPeer: no such table: main.__Peer_tmp
-------------------- >> begin captured logging << --------------------
Tribler.Core.Utilities.network_utils: DEBUG: Got a working random port 25506
TriblerConfig: DEBUG: Get random port 25506 for [libtorrent~port]
Tribler.Core.Utilities.network_utils: DEBUG: Got a working random port 6688
TriblerConfig: DEBUG: Get random port 6688 for [dispersy~port]
Tribler.Core.Utilities.network_utils: DEBUG: Got a working random port 27429
TriblerConfig: DEBUG: Get random port 27429 for [mainline_dht~port]
Tribler.Core.Utilities.network_utils: DEBUG: Got a working random port 50148
TriblerConfig: DEBUG: Get random port 50148 for [video_server~port]
Tribler.Core.Utilities.network_utils: DEBUG: Got a working random port 14895
TriblerConfig: DEBUG: Get random port 14895 for [libtorrent~anon_listen_port]
Tribler.Core.Utilities.network_utils: DEBUG: Got a working random port 25372
TriblerConfig: DEBUG: Get random port 25372 for [tunnel_community~socks5_listen_ports~0]
Tribler.Core.Utilities.network_utils: DEBUG: Got a working random port 8203
TriblerConfig: DEBUG: Get random port 8203 for [tunnel_community~socks5_listen_ports~1]
Tribler.Core.Utilities.network_utils: DEBUG: Got a working random port 44513
TriblerConfig: DEBUG: Get random port 44513 for [tunnel_community~socks5_listen_ports~2]
Tribler.Core.Utilities.network_utils: DEBUG: Got a working random port 37178
TriblerConfig: DEBUG: Get random port 37178 for [tunnel_community~socks5_listen_ports~3]
Tribler.Core.Utilities.network_utils: DEBUG: Got a working random port 51695
TriblerConfig: DEBUG: Get random port 51695 for [tunnel_community~socks5_listen_ports~4]
SQLiteCacheDB: INFO: begin page_size upgrade...
SQLiteCacheDB: INFO: ...end page_size upgrade
SQLiteCacheDB: INFO: Reading database version...
SQLiteCacheDB: INFO: Current database version is 17
SQLiteCacheDB: INFO: Beginning the first transaction...
SQLiteCacheDB: INFO: Start committing...
SQLiteCacheDB: INFO: Beginning another transaction...
SQLiteCacheDB: INFO: Start committing...
SQLiteCacheDB: INFO: Beginning another transaction...
SQLiteCacheDB: ERROR: cachedb: ===MainThread===
SQL Type: <type 'unicode'>
-----

DROP TABLE IF EXISTS Torrent;
ALTER TABLE _tmp_Torrent RENAME TO Torrent;

-----
None
======
Traceback (most recent call last):
File "/root/tribler/Tribler/Core/CacheDB/sqlitecachedb.py", line 282, in execute
return cur.execute(sql)
File "src/cursor.c", line 236, in resetcursor
SQLError: SQLError: error in view SuperPeer: no such table: main.__Peer_tmp
--------------------- >> end captured logging << ---------------------

# Fixed code
def _upgrade_18_to_22(self):
    self.current_status = "Upgrading database from v%s to v%s..." % (18, 22)

    self.db.execute("""
DROP INDEX IF EXISTS Torrent_swift_hash_idx;

DROP VIEW IF EXISTS Friend;
DROP VIEW IF EXISTS SuperPeer;

ALTER TABLE Peer RENAME TO __Peer_tmp;
CREATE TABLE IF NOT EXISTS Peer (
    peer_id    integer PRIMARY KEY AUTOINCREMENT NOT NULL,
    permid     text NOT NULL,
    name       text,
    thumbnail  text
);

INSERT INTO Peer (peer_id, permid, name, thumbnail) SELECT peer_id, permid, name, thumbnail FROM __Peer_tmp;

DROP TABLE IF EXISTS __Peer_tmp;

ALTER TABLE Torrent ADD COLUMN last_tracker_check integer DEFAULT 0;
ALTER TABLE Torrent ADD COLUMN tracker_check_retries integer DEFAULT 0;
ALTER TABLE Torrent ADD COLUMN next_tracker_check integer DEFAULT 0;

CREATE TABLE IF NOT EXISTS TrackerInfo (
  tracker_id  integer PRIMARY KEY AUTOINCREMENT,
  tracker     text    UNIQUE NOT NULL,
  last_check  numeric DEFAULT 0,
  failures    integer DEFAULT 0,
  is_alive    integer DEFAULT 1
);

CREATE TABLE IF NOT EXISTS TorrentTrackerMapping (
  torrent_id  integer NOT NULL,
  tracker_id  integer NOT NULL,
  FOREIGN KEY (torrent_id) REFERENCES Torrent(torrent_id),
  FOREIGN KEY (tracker_id) REFERENCES TrackerInfo(tracker_id),
  PRIMARY KEY (torrent_id, tracker_id)
);

INSERT OR IGNORE INTO TrackerInfo (tracker) VALUES ('no-DHT');
INSERT OR IGNORE INTO TrackerInfo (tracker) VALUES ('DHT');

DROP INDEX IF EXISTS torrent_biterm_phrase_idx;
DROP TABLE IF EXISTS TorrentBiTermPhrase;
DROP INDEX IF EXISTS termfrequency_freq_idx;
DROP TABLE IF EXISTS TermFrequency;
DROP INDEX IF EXISTS Torrent_insert_idx;
DROP INDEX IF EXISTS Torrent_info_roothash_idx;

DROP TABLE IF EXISTS ClicklogSearch;
DROP INDEX IF EXISTS idx_search_term;
DROP INDEX IF EXISTS idx_search_torrent;
""")
    # update database version
    self.db.write_version(22)