2015-06-09 11:13:00 +00:00
|
|
|
from __future__ import print_function
|
2014-12-09 12:30:00 +00:00
|
|
|
import sys
|
|
|
|
import os.path
|
|
|
|
import glob
|
|
|
|
import unittest
|
|
|
|
import test_lib as test
|
|
|
|
import sickbeard
|
|
|
|
from time import sleep
|
2015-06-05 09:44:43 +00:00
|
|
|
from sickbeard import db, logger
|
|
|
|
from sickbeard.databases.mainDB import MIN_DB_VERSION, MAX_DB_VERSION
|
2014-12-09 12:30:00 +00:00
|
|
|
|
2015-06-04 13:36:38 +00:00
|
|
|
sys.path.insert(1, os.path.abspath('..'))
|
|
|
|
sys.path.insert(1, os.path.abspath('../lib'))
|
2014-12-09 12:30:00 +00:00
|
|
|
|
|
|
|
sickbeard.SYS_ENCODING = 'UTF-8'
|
|
|
|
|
|
|
|
|
|
|
|
class MigrationBasicTests(test.SickbeardTestDBCase):
|
|
|
|
def setUp(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_migrations(self):
|
2015-06-05 09:44:43 +00:00
|
|
|
schema = {0: OldInitialSchema, # sickbeard.mainDB.InitialSchema,
|
|
|
|
31: sickbeard.mainDB.AddAnimeTVShow,
|
|
|
|
32: sickbeard.mainDB.AddAbsoluteNumbering,
|
|
|
|
33: sickbeard.mainDB.AddSceneAbsoluteNumbering,
|
|
|
|
34: sickbeard.mainDB.AddAnimeBlacklistWhitelist,
|
|
|
|
35: sickbeard.mainDB.AddSceneAbsoluteNumbering2,
|
|
|
|
36: sickbeard.mainDB.AddXemRefresh,
|
|
|
|
37: sickbeard.mainDB.AddSceneToTvShows,
|
|
|
|
38: sickbeard.mainDB.AddIndexerMapping,
|
|
|
|
39: sickbeard.mainDB.AddVersionToTvEpisodes,
|
|
|
|
41: AddDefaultEpStatusToTvShows
|
|
|
|
}
|
2014-12-09 12:30:00 +00:00
|
|
|
|
|
|
|
count = 1
|
|
|
|
while count < len(schema.keys()):
|
2015-06-05 09:44:43 +00:00
|
|
|
my_db = db.DBConnection()
|
2014-12-09 12:30:00 +00:00
|
|
|
|
|
|
|
for version in sorted(schema.keys())[:count]:
|
2015-06-05 09:44:43 +00:00
|
|
|
update = schema[version](my_db)
|
2014-12-09 12:30:00 +00:00
|
|
|
update.execute()
|
|
|
|
sleep(0.1)
|
|
|
|
|
2015-06-05 09:44:43 +00:00
|
|
|
db.MigrationCode(my_db)
|
|
|
|
my_db.close()
|
|
|
|
for filename in glob.glob(os.path.join(test.TESTDIR, test.TESTDBNAME) + '*'):
|
2014-12-09 12:30:00 +00:00
|
|
|
os.remove(filename)
|
|
|
|
|
|
|
|
sleep(0.1)
|
|
|
|
count += 1
|
|
|
|
|
|
|
|
|
2015-06-05 09:44:43 +00:00
|
|
|
# 0 -> 31
|
|
|
|
class OldInitialSchema(db.SchemaUpgrade):
|
|
|
|
def execute(self):
|
|
|
|
db.backup_database('sickbeard.db', self.checkDBVersion())
|
|
|
|
|
|
|
|
if not self.hasTable('tv_shows') and not self.hasTable('db_version'):
|
|
|
|
queries = [
|
|
|
|
'CREATE TABLE db_version (db_version INTEGER);',
|
|
|
|
'CREATE TABLE history (action NUMERIC, date NUMERIC, showid NUMERIC, season NUMERIC, episode NUMERIC, quality NUMERIC, resource TEXT, provider TEXT)',
|
|
|
|
'CREATE TABLE imdb_info (indexer_id INTEGER PRIMARY KEY, imdb_id TEXT, title TEXT, year NUMERIC, akas TEXT, runtimes NUMERIC, genres TEXT, countries TEXT, country_codes TEXT, certificates TEXT, rating TEXT, votes INTEGER, last_update NUMERIC)',
|
|
|
|
'CREATE TABLE info (last_backlog NUMERIC, last_indexer NUMERIC, last_proper_search NUMERIC)',
|
|
|
|
'CREATE TABLE scene_numbering(indexer TEXT, indexer_id INTEGER, season INTEGER, episode INTEGER,scene_season INTEGER, scene_episode INTEGER, PRIMARY KEY(indexer_id, season, episode))',
|
|
|
|
'CREATE TABLE tv_shows (show_id INTEGER PRIMARY KEY, indexer_id NUMERIC, indexer NUMERIC, show_name TEXT, location TEXT, network TEXT, genre TEXT, classification TEXT, runtime NUMERIC, quality NUMERIC, airs TEXT, status TEXT, flatten_folders NUMERIC, paused NUMERIC, startyear NUMERIC, air_by_date NUMERIC, lang TEXT, subtitles NUMERIC, notify_list TEXT, imdb_id TEXT, last_update_indexer NUMERIC, dvdorder NUMERIC, archive_firstmatch NUMERIC, rls_require_words TEXT, rls_ignore_words TEXT, sports NUMERIC);',
|
|
|
|
'CREATE TABLE tv_episodes (episode_id INTEGER PRIMARY KEY, showid NUMERIC, indexerid NUMERIC, indexer NUMERIC, name TEXT, season NUMERIC, episode NUMERIC, description TEXT, airdate NUMERIC, hasnfo NUMERIC, hastbn NUMERIC, status NUMERIC, location TEXT, file_size NUMERIC, release_name TEXT, subtitles TEXT, subtitles_searchcount NUMERIC, subtitles_lastsearch TIMESTAMP, is_proper NUMERIC, scene_season NUMERIC, scene_episode NUMERIC);',
|
|
|
|
'CREATE UNIQUE INDEX idx_indexer_id ON tv_shows (indexer_id)',
|
|
|
|
'CREATE INDEX idx_showid ON tv_episodes (showid);',
|
|
|
|
'CREATE INDEX idx_sta_epi_air ON tv_episodes (status,episode, airdate);',
|
|
|
|
'CREATE INDEX idx_sta_epi_sta_air ON tv_episodes (season,episode, status, airdate);',
|
|
|
|
'CREATE INDEX idx_status ON tv_episodes (status,season,episode,airdate);',
|
|
|
|
'CREATE INDEX idx_tv_episodes_showid_airdate ON tv_episodes(showid,airdate)',
|
|
|
|
'INSERT INTO db_version (db_version) VALUES (31);'
|
|
|
|
]
|
|
|
|
for query in queries:
|
|
|
|
self.connection.action(query)
|
|
|
|
|
|
|
|
else:
|
|
|
|
cur_db_version = self.checkDBVersion()
|
|
|
|
|
|
|
|
if cur_db_version < MIN_DB_VERSION:
|
|
|
|
logger.log_error_and_exit(u'Your database version ('
|
|
|
|
+ str(cur_db_version)
|
|
|
|
+ ') is too old to migrate from what this version of SickGear supports ('
|
2015-08-11 11:51:26 +00:00
|
|
|
+ str(MIN_DB_VERSION) + ').' + '\n'
|
2015-06-05 09:44:43 +00:00
|
|
|
+ 'Upgrade using a previous version (tag) build 496 to build 501 of SickGear first or remove database file to begin fresh.'
|
|
|
|
)
|
|
|
|
|
|
|
|
if cur_db_version > MAX_DB_VERSION:
|
|
|
|
logger.log_error_and_exit(u'Your database version ('
|
|
|
|
+ str(cur_db_version)
|
|
|
|
+ ') has been incremented past what this version of SickGear supports ('
|
2015-08-11 11:51:26 +00:00
|
|
|
+ str(MAX_DB_VERSION) + ').' + '\n'
|
2015-06-05 09:44:43 +00:00
|
|
|
+ 'If you have used other forks of SickGear, your database may be unusable due to their modifications.'
|
|
|
|
)
|
|
|
|
|
|
|
|
return self.checkDBVersion()
|
|
|
|
|
|
|
|
|
2014-12-09 12:30:00 +00:00
|
|
|
class AddDefaultEpStatusToTvShows(db.SchemaUpgrade):
|
|
|
|
def execute(self):
|
2015-06-05 09:44:43 +00:00
|
|
|
self.addColumn('tv_shows', 'default_ep_status', 'TEXT', '')
|
2014-12-09 12:30:00 +00:00
|
|
|
self.setDBVersion(41)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2015-06-09 11:13:00 +00:00
|
|
|
print('==================')
|
|
|
|
print('Starting - Migration Tests')
|
|
|
|
print('==================')
|
|
|
|
print('######################################################################')
|
2014-12-09 12:30:00 +00:00
|
|
|
suite = unittest.TestLoader().loadTestsFromTestCase(MigrationBasicTests)
|
|
|
|
unittest.TextTestRunner(verbosity=2).run(suite)
|