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

    # remove UserEventLog, TorrentSource, and TorrentCollecting tables
    self.status_update_func("Removing unused tables...")
    self.db.execute("""
DROP TABLE IF EXISTS UserEventLog;
DROP TABLE IF EXISTS TorrentSource;
DROP TABLE IF EXISTS TorrentCollecting;
""")

    # remove click_position, reranking_strategy, and progress from MyPreference
    self.status_update_func("Updating MyPreference table...")
    self.db.execute("""
CREATE TABLE _tmp_MyPreference (
  torrent_id     integer PRIMARY KEY NOT NULL,
  destination_path text NOT NULL,
  creation_time  integer NOT NULL
);

INSERT INTO _tmp_MyPreference SELECT torrent_id, destination_path, creation_time FROM MyPreference;

DROP TABLE MyPreference;
ALTER TABLE _tmp_MyPreference RENAME TO MyPreference;
""")

    # remove source_id and thumbnail columns from Torrent table
    # replace torrent_file_name column with is_collected column
    # change CollectedTorrent view
    self.status_update_func("Updating Torrent table...")
    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_id      integer,
  status_id        integer DEFAULT 0,
  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
);

UPDATE Torrent SET torrent_file_name = '1' WHERE torrent_file_name IS NOT NULL;
UPDATE Torrent SET torrent_file_name = '0' WHERE torrent_file_name IS NULL;

INSERT INTO _tmp_Torrent
SELECT torrent_id, infohash, name, length, creation_date, num_files, insert_time, secret, relevance, category_id,
status_id, num_seeders, num_leechers, comment, dispersy_id, CAST(torrent_file_name AS INTEGER),
last_tracker_check, tracker_check_retries, next_tracker_check FROM Torrent;

DROP TABLE Torrent;
ALTER TABLE _tmp_Torrent RENAME TO Torrent;

DROP VIEW IF EXISTS CollectedTorrent;
CREATE VIEW CollectedTorrent AS SELECT * FROM Torrent WHERE is_collected == 1;
""")

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


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

    # remove UserEventLog, TorrentSource, and TorrentCollecting tables
    self.status_update_func("Removing unused tables...")
    self.db.execute("""
DROP TABLE IF EXISTS UserEventLog;
DROP TABLE IF EXISTS TorrentSource;
DROP TABLE IF EXISTS TorrentCollecting;
""")

    # remove click_position, reranking_strategy, and progress from MyPreference
    self.status_update_func("Updating MyPreference table...")
    self.db.execute("""
CREATE TABLE _tmp_MyPreference (
  torrent_id     integer PRIMARY KEY NOT NULL,
  destination_path text NOT NULL,
  creation_time  integer NOT NULL
);

INSERT INTO _tmp_MyPreference SELECT torrent_id, destination_path, creation_time FROM MyPreference;

DROP TABLE MyPreference;
ALTER TABLE _tmp_MyPreference RENAME TO MyPreference;
""")

    # remove source_id and thumbnail columns from Torrent table
    # replace torrent_file_name column with is_collected column
    # change CollectedTorrent view
    self.status_update_func("Updating Torrent table...")
    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_id      integer,
  status_id        integer DEFAULT 0,
  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
);

UPDATE Torrent SET torrent_file_name = '1' WHERE torrent_file_name IS NOT NULL;
UPDATE Torrent SET torrent_file_name = '0' WHERE torrent_file_name IS NULL;

INSERT INTO _tmp_Torrent
SELECT torrent_id, infohash, name, length, creation_date, num_files, insert_time, secret, relevance, category_id,
status_id, num_seeders, num_leechers, comment, dispersy_id, CAST(torrent_file_name AS INTEGER),
last_tracker_check, tracker_check_retries, next_tracker_check FROM Torrent;

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;
""")

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