Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix database always trying to update #5543

Merged
merged 2 commits into from
Oct 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions medusa/databases/cache_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,6 @@ def execute(self):
self.clear_provider_tables()
self.inc_major_version()

log.info('Updated to: {}.{}', *self.connection.version)

def clear_provider_tables(self):
providers = self.connection.select(
"SELECT name FROM sqlite_master WHERE type='table' AND name NOT IN ('lastUpdate',"
Expand All @@ -197,4 +195,6 @@ def inc_major_version(self):
major_version, minor_version = self.connection.version
major_version += 1
self.connection.action('UPDATE db_version SET db_version = ?;', [major_version])
log.info('[CACHE-DB] Updated major version to: {}.{}', *self.connection.version)

return self.connection.version
7 changes: 3 additions & 4 deletions medusa/databases/failed_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def execute(self):
' status NUMERIC DEFAULT -1, quality NUMERIC DEFAULT 0, showid NUMERIC DEFAULT -1,'
' season NUMERIC DEFAULT -1, episode NUMERIC DEFAULT -1);',),
('CREATE TABLE db_version (db_version INTEGER);',),
('INSERT INTO db_version (db_version) VALUES (1);',),
('INSERT INTO db_version (db_version) VALUES (2);',),
]
for query in queries:
if len(query) == 1:
Expand Down Expand Up @@ -125,7 +125,6 @@ def execute(self):
utils.backup_database(self.connection.path, self.connection.version)

self.translate_status()
self.inc_major_version()

def translate_status(self):
"""
Expand Down Expand Up @@ -159,6 +158,8 @@ def inc_major_version(self):
major_version, minor_version = self.connection.version
major_version += 1
self.connection.action('UPDATE db_version SET db_version = ?;', [major_version])
log.info(u'[FAILED-DB] Updated major version to: {}.{}', *self.connection.version)

return self.connection.version


Expand All @@ -176,8 +177,6 @@ def execute(self):
self.update_status_unknown()
self.inc_major_version()

log.info(u'Updated to: {}.{}', *self.connection.version)

def shift_history_qualities(self):
"""
Shift all qualities << 1.
Expand Down
54 changes: 17 additions & 37 deletions medusa/databases/main_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ def execute(self):
self.connection.action(query)

else:
cur_db_version = self.checkDBVersion()
cur_db_version = self.checkMajorDBVersion()

if cur_db_version < MIN_DB_VERSION:
log.error(
Expand All @@ -283,38 +283,38 @@ def execute(self):

class AddVersionToTvEpisodes(InitialSchema):
def test(self):
return self.checkDBVersion() >= 40
return self.checkMajorDBVersion() >= 40

def execute(self):
utils.backup_database(self.connection.path, self.checkDBVersion())
utils.backup_database(self.connection.path, self.checkMajorDBVersion())

log.info(u'Adding column version to tv_episodes and history')
self.addColumn('tv_episodes', 'version', 'NUMERIC', '-1')
self.addColumn('tv_episodes', 'release_group', 'TEXT', '')
self.addColumn('history', 'version', 'NUMERIC', '-1')

self.incDBVersion()
self.incMajorDBVersion()


class AddDefaultEpStatusToTvShows(AddVersionToTvEpisodes):
def test(self):
return self.checkDBVersion() >= 41
return self.checkMajorDBVersion() >= 41

def execute(self):
utils.backup_database(self.connection.path, self.checkDBVersion())
utils.backup_database(self.connection.path, self.checkMajorDBVersion())

log.info(u'Adding column default_ep_status to tv_shows')
self.addColumn('tv_shows', 'default_ep_status', 'NUMERIC', '-1')

self.incDBVersion()
self.incMajorDBVersion()


class AlterTVShowsFieldTypes(AddDefaultEpStatusToTvShows):
def test(self):
return self.checkDBVersion() >= 42
return self.checkMajorDBVersion() >= 42

def execute(self):
utils.backup_database(self.connection.path, self.checkDBVersion())
utils.backup_database(self.connection.path, self.checkMajorDBVersion())

log.info(u'Converting column indexer and default_ep_status field types to numeric')
self.connection.action('DROP TABLE IF EXISTS tmp_tv_shows')
Expand All @@ -330,14 +330,14 @@ def execute(self):
self.connection.action('INSERT INTO tv_shows SELECT * FROM tmp_tv_shows')
self.connection.action('DROP TABLE tmp_tv_shows')

self.incDBVersion()
self.incMajorDBVersion()


class AddMinorVersion(AlterTVShowsFieldTypes):
def test(self):
return self.checkDBVersion() >= 43 and self.hasColumn('db_version', 'db_minor_version')
return self.checkMajorDBVersion() >= 43 and self.hasColumn('db_version', 'db_minor_version')

def incDBVersion(self):
def incMajorDBVersion(self):
warnings.warn('Deprecated: Use inc_major_version or inc_minor_version instead', DeprecationWarning)

def inc_major_version(self):
Expand All @@ -346,26 +346,28 @@ def inc_major_version(self):
minor_version = 0
self.connection.action('UPDATE db_version SET db_version = ?, db_minor_version = ?;',
[major_version, minor_version])
log.info(u'[MAIN-DB] Updated major version to: {}.{}', *self.connection.version)

return self.connection.version

def inc_minor_version(self):
major_version, minor_version = self.connection.version
minor_version += 1
self.connection.action('UPDATE db_version SET db_version = ?, db_minor_version = ?;',
[major_version, minor_version])
log.info(u'[MAIN-DB] Updated minor version to: {}.{}', *self.connection.version)

return self.connection.version

def execute(self):
utils.backup_database(self.connection.path, self.checkDBVersion())
utils.backup_database(self.connection.path, self.checkMajorDBVersion())
Copy link
Contributor

@sharkykh sharkykh Oct 29, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think these (the backup calls) should be changed.
Don't you need the tuple so that the database backup contains the minor version too?
image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's okay, because these backups are going to be of very old versions (< 42) and won't have a minor version anyway. The new versions are using self.connection.version which contains the major and minor version.


log.info(u'Add minor version numbers to database')
self.addColumn('db_version', 'db_minor_version')

self.inc_major_version()
self.inc_minor_version()

log.info(u'Updated to: {}.{}', *self.connection.version)


class TestIncreaseMajorVersion(AddMinorVersion):
"""
Expand All @@ -390,8 +392,6 @@ def execute(self):
self.inc_major_version()
self.inc_minor_version()

log.info(u'Updated to: {}.{}', *self.connection.version)


class AddProperTags(TestIncreaseMajorVersion):
"""Adds column proper_tags to history table."""
Expand All @@ -416,8 +416,6 @@ def execute(self):
MainSanityCheck(self.connection).update_old_propers()
self.inc_minor_version()

log.info(u'Updated to: {}.{}', *self.connection.version)


class AddManualSearched(AddProperTags):
"""Adds columns manually_searched to history and tv_episodes table."""
Expand Down Expand Up @@ -445,8 +443,6 @@ def execute(self):
MainSanityCheck(self.connection).update_old_propers()
self.inc_minor_version()

log.info(u'Updated to: {}.{}', *self.connection.version)


class AddInfoHash(AddManualSearched):
"""Adds column info_hash to history table."""
Expand All @@ -466,8 +462,6 @@ def execute(self):

self.inc_minor_version()

log.info(u'Updated to: {}.{}', *self.connection.version)


class AddPlot(AddInfoHash):
"""Adds column plot to imdb_info table."""
Expand All @@ -491,8 +485,6 @@ def execute(self):

self.inc_minor_version()

log.info(u'Updated to: {}.{}', *self.connection.version)


class AddResourceSize(AddPlot):
"""Adds column size to history table."""
Expand All @@ -512,8 +504,6 @@ def execute(self):

self.inc_minor_version()

log.info(u'Updated to: {}.{}', *self.connection.version)


class AddPKIndexerMapping(AddResourceSize):
"""Add PK to mindexer column in indexer_mapping table."""
Expand All @@ -537,8 +527,6 @@ def execute(self):

self.inc_minor_version()

log.info(u'Updated to: {}.{}', *self.connection.version)


class AddIndexerInteger(AddPKIndexerMapping):
"""Make indexer as INTEGER in tv_episodes table."""
Expand Down Expand Up @@ -567,8 +555,6 @@ def execute(self):

self.inc_minor_version()

log.info(u'Updated to: {}.{}', *self.connection.version)


class AddIndexerIds(AddIndexerInteger):
"""
Expand Down Expand Up @@ -681,8 +667,6 @@ def create_series_dict():

self.inc_minor_version()

log.info(u'Updated to: {}.{}', *self.connection.version)

# Flag the image migration.
from medusa import app
app.MIGRATE_IMAGES = True
Expand Down Expand Up @@ -753,8 +737,6 @@ def execute(self):

self.inc_minor_version()

log.info(u'Updated to: {}.{}', *self.connection.version)


class ShiftQualities(AddSeparatedStatusQualityFields):
"""Shift all qualities one place to the left."""
Expand All @@ -771,8 +753,6 @@ def execute(self):
self.shift_history_qualities()
self.inc_minor_version()

log.info(u'Updated to: {}.{}', *self.connection.version)

def shift_tv_qualities(self):
"""
Shift all qualities << 1.
Expand Down
9 changes: 5 additions & 4 deletions medusa/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ def checkDBVersion(self):
db_minor_version = self.check_db_minor_version()
if db_minor_version is None:
db_minor_version = 0

return self.check_db_major_version(), db_minor_version

def check_db_major_version(self):
Expand Down Expand Up @@ -490,10 +491,10 @@ def addColumn(self, table, column, column_type='NUMERIC', default=0):
self.connection.action('ALTER TABLE [%s] ADD %s %s' % (table, column, column_type))
self.connection.action('UPDATE [%s] SET %s = ?' % (table, column), (default,))

def checkDBVersion(self):
return self.connection.checkDBVersion()
def checkMajorDBVersion(self):
return self.connection.checkDBVersion()[0]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.connection.checkDBVersion() returns a tuple like: (41, 11)

(41, 11) >= 42 -> True


def incDBVersion(self):
new_version = self.checkDBVersion() + 1
def incMajorDBVersion(self):
new_version = self.checkMajorDBVersion() + 1
self.connection.action('UPDATE db_version SET db_version = ?', [new_version])
return new_version