Fix overwrite Tautulli database importing

This commit is contained in:
JonnyWong16 2020-05-02 21:56:39 -07:00
parent 95bdc000ca
commit e999000102
No known key found for this signature in database
GPG key ID: B1F1F9807184697A

View file

@ -78,12 +78,11 @@ def import_tautulli_db(database=None, method=None, backup=True):
# Get the current number of used ids in the session_history table
session_history_seq = db.select_single('SELECT seq FROM sqlite_sequence WHERE name = "session_history"')
if method == 'merge':
# Create a temporary table so we can reindex session_history.reference_id after merging
logger.info("Tautulli Database :: Creating temporary database table to re-index grouped session history.")
db.action('CREATE TABLE IF NOT EXISTS temp (id INTEGER PRIMARY KEY, old_id, new_id)')
db.action('INSERT INTO temp (old_id) SELECT id FROM import_db.session_history')
db.action('UPDATE temp SET new_id = id + ?', [session_history_seq['seq']])
# Create a temporary table so we can reindex session_history.reference_id
logger.info("Tautulli Database :: Creating temporary database table to re-index grouped session history.")
db.action('CREATE TABLE IF NOT EXISTS temp (id INTEGER PRIMARY KEY, old_id, new_id)')
db.action('INSERT INTO temp (old_id) SELECT id FROM import_db.session_history')
db.action('UPDATE temp SET new_id = id + ?', [session_history_seq['seq']])
# Keep track of all table columns so that duplicates can be removed after importing
session_history_tables = ('session_history', 'session_history_metadata', 'session_history_media_info')
@ -120,12 +119,12 @@ def import_tautulli_db(database=None, method=None, backup=True):
db.connection.execute('DETACH import_db')
if method == 'merge':
# Reindex session_history.reference_id
logger.info("Tautulli Database :: Re-indexing grouped session history.")
db.action('UPDATE session_history SET reference_id = (SELECT new_id FROM temp WHERE old_id = reference_id)'
'WHERE id > ?', [session_history_seq['seq']])
# Reindex session_history.reference_id
logger.info("Tautulli Database :: Re-indexing grouped session history.")
db.action('UPDATE session_history SET reference_id = (SELECT new_id FROM temp WHERE old_id = reference_id)'
'WHERE id > ?', [session_history_seq['seq']])
if method == 'merge':
if session_history_columns:
# Remove reference_id column from the list of session_history table columns to get unique rows
columns = ', '.join([c for c in session_history_columns if c != 'reference_id'])
@ -142,8 +141,8 @@ def import_tautulli_db(database=None, method=None, backup=True):
db.action('DELETE FROM {table} WHERE id NOT IN '
'(SELECT MIN(id) FROM {table} GROUP BY {columns})'.format(table=table, columns=columns))
logger.info("Tautulli Database :: Deleting temporary database table.")
db.action('DROP TABLE temp')
logger.info("Tautulli Database :: Deleting temporary database table.")
db.action('DROP TABLE temp')
db.action('VACUUM')