# Buggy code
def _upgrade_26_to_27(self):
    self.status_update_func("Upgrading database from v%s to v%s..." % (26, 27))

    # replace status_id and category_id in Torrent table with status and category
    self.status_update_func("Updating Torrent table and removing unused tables...")
    self.db.execute("""
CREATE TABLE _tmp_Torrent (
  torrent_id       integer PRIMARY KEY AUTOINCREMENT NOT NULL,
  infohash		   text NOT NULL,
  name             text,
  length           integer,
  creation_date    integer,
  num_files        integer,
  insert_time      numeric,
  secret           integer,
  relevance        numeric DEFAULT 0,
  category         text,
  status           text DEFAULT 'unknown',
  num_seeders      integer,
  num_leechers     integer,
  comment          text,
  dispersy_id      integer,
  is_collected     integer DEFAULT 0,
  last_tracker_check    integer DEFAULT 0,
  tracker_check_retries integer DEFAULT 0,
  next_tracker_check    integer DEFAULT 0
);

INSERT INTO _tmp_Torrent
SELECT torrent_id, infohash, T.name, length, creation_date, num_files, insert_time, secret, relevance, C.name, TS.name,
num_seeders, num_leechers, comment, dispersy_id, is_collected, last_tracker_check, tracker_check_retries,
next_tracker_check
FROM Torrent AS T
LEFT JOIN Category AS C ON T.category_id == C.category_id
LEFT JOIN TorrentStatus AS TS ON T.status_id == TS.status_id;

DROP TABLE Torrent;
ALTER TABLE _tmp_Torrent RENAME TO Torrent;

DROP TABLE Category;
DROP TABLE TorrentStatus;
""")

    # update database version
    self.db.write_version(27)


# 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_26_to_27(self):
    self.status_update_func("Upgrading database from v%s to v%s..." % (26, 27))

    # replace status_id and category_id in Torrent table with status and category
    self.status_update_func("Updating Torrent table and removing unused tables...")
    self.db.execute("""
CREATE TABLE _tmp_Torrent (
  torrent_id       integer PRIMARY KEY AUTOINCREMENT NOT NULL,
  infohash		   text NOT NULL,
  name             text,
  length           integer,
  creation_date    integer,
  num_files        integer,
  insert_time      numeric,
  secret           integer,
  relevance        numeric DEFAULT 0,
  category         text,
  status           text DEFAULT 'unknown',
  num_seeders      integer,
  num_leechers     integer,
  comment          text,
  dispersy_id      integer,
  is_collected     integer DEFAULT 0,
  last_tracker_check    integer DEFAULT 0,
  tracker_check_retries integer DEFAULT 0,
  next_tracker_check    integer DEFAULT 0
);

INSERT INTO _tmp_Torrent
SELECT torrent_id, infohash, T.name, length, creation_date, num_files, insert_time, secret, relevance, C.name, TS.name,
num_seeders, num_leechers, comment, dispersy_id, is_collected, last_tracker_check, tracker_check_retries,
next_tracker_check
FROM Torrent AS T
LEFT JOIN Category AS C ON T.category_id == C.category_id
LEFT JOIN TorrentStatus AS TS ON T.status_id == TS.status_id;

DROP VIEW IF EXISTS CollectedTorrent;
DROP TABLE Torrent;
ALTER TABLE _tmp_Torrent RENAME TO Torrent;
CREATE VIEW CollectedTorrent AS SELECT * FROM Torrent WHERE is_collected == 1;

DROP TABLE Category;
DROP TABLE TorrentStatus;
""")

    # update database version
    self.db.write_version(27)