Skip to content

Commit

Permalink
Reset dirty flag when changes to episodes are made (#7102)
Browse files Browse the repository at this point in the history
* Reset dirty when changes to episodes are made

* Reset dirty when exception occurs, minor logging changes

* Update CHANGELOG.md
  • Loading branch information
medariox authored Aug 31, 2019
1 parent 50c3ce4 commit 9335bb5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Improved API v2 exception reporting on Python 2 ([#6931](https://github.com/pymedusa/Medusa/pull/6931))
- Added support for qbittorrent api v2. Required from qbittorrent version > 3.2.0. ([#7040](https://github.com/pymedusa/Medusa/pull/7040))
- Show Header: Improved visibility of local and global configured required and ignored words. ([#7085](https://github.com/pymedusa/Medusa/pull/7085))
- Reduced frequency of file system access when not strictly required ([#7102](https://github.com/pymedusa/Medusa/pull/7102))

#### Fixes
- Fixed hdtorrent provider parse the publishing date with the day first ([#6847](https://github.com/pymedusa/Medusa/pull/6847))
Expand Down
36 changes: 27 additions & 9 deletions medusa/tv/episode.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,7 @@ def load_from_db(self, season, episode):
"""
if self.loaded:
return True

main_db_con = db.DBConnection()
sql_results = main_db_con.select(
'SELECT '
Expand Down Expand Up @@ -653,7 +654,7 @@ def load_from_db(self, season, episode):

# don't overwrite my location
if sql_results[0]['location']:
self.location = os.path.normpath(sql_results[0]['location'])
self._location = os.path.normpath(sql_results[0]['location'])
if sql_results[0]['file_size']:
self.file_size = int(sql_results[0]['file_size'])
else:
Expand Down Expand Up @@ -961,6 +962,8 @@ def load_from_indexer(self, season=None, episode=None, tvapi=None, cached_season
)
self.status = UNSET

self.save_to_db()

def __load_from_nfo(self, location):

if not self.series.is_location_valid():
Expand Down Expand Up @@ -1044,6 +1047,8 @@ def __load_from_nfo(self, location):

self.hastbn = bool(os.path.isfile(replace_extension(nfo_file, 'tbn')))

self.save_to_db()

def __str__(self):
"""Represent a string.
Expand Down Expand Up @@ -1182,12 +1187,12 @@ def delete_episode(self):

def get_sql(self):
"""Create SQL queue for this episode if any of its data has been changed since the last save."""
try:
if not self.dirty:
log.debug('{id}: Not creating SQL queue - record is not dirty',
{'id': self.series.series_id})
return
if not self.dirty:
log.debug('{id}: Not creating SQL query - record is not dirty',
{'id': self.series.series_id})
return

try:
main_db_con = db.DBConnection()
rows = main_db_con.select(
'SELECT '
Expand All @@ -1210,7 +1215,7 @@ def get_sql(self):
# use a custom update method to get the data into the DB for existing records.
# Multi or added subtitle or removed subtitles
if app.SUBTITLES_MULTI or not rows[0]['subtitles'] or not self.subtitles:
return [
sql_query = [
'UPDATE '
' tv_episodes '
'SET '
Expand Down Expand Up @@ -1248,7 +1253,7 @@ def get_sql(self):
else:
# Don't update the subtitle language when the srt file doesn't contain the
# alpha2 code, keep value from subliminal
return [
sql_query = [
'UPDATE '
' tv_episodes '
'SET '
Expand Down Expand Up @@ -1284,7 +1289,7 @@ def get_sql(self):
self.version, self.release_group, self.manually_searched, self.watched, ep_id]]
else:
# use a custom insert method to get the data into the DB.
return [
sql_query = [
'INSERT OR IGNORE INTO '
' tv_episodes '
' (episode_id, '
Expand Down Expand Up @@ -1323,12 +1328,24 @@ def get_sql(self):
except Exception as error:
log.error('{id}: Error while updating database: {error_msg!r}',
{'id': self.series.series_id, 'error_msg': error})
self.reset_dirty()
return

self.loaded = False
self.reset_dirty()

return sql_query

def save_to_db(self):
"""Save this episode to the database if any of its data has been changed since the last save."""
if not self.dirty:
return

log.debug('{id}: Saving episode to database: {show} {ep}',
{'id': self.series.series_id,
'show': self.series.name,
'ep': episode_num(self.season, self.episode)})

new_value_dict = {
'indexerid': self.indexerid,
'name': self.name,
Expand Down Expand Up @@ -1362,6 +1379,7 @@ def save_to_db(self):
# use a custom update/insert method to get the data into the DB
main_db_con = db.DBConnection()
main_db_con.upsert('tv_episodes', new_value_dict, control_value_dict)

self.loaded = False
self.reset_dirty()

Expand Down

0 comments on commit 9335bb5

Please sign in to comment.