Advertisement
Guest User

Migrate Kavita v0.8.3 -> v0.8.4 People Metadata

a guest
Nov 27th, 2024
598
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.45 KB | Fixit | 0 0
  1. ###
  2. ### Python 3.8 requirement
  3. ### python migrate_data.py
  4. ### Use old.db from Kavita v0.8.3 (from the backup in config/backups)
  5. ### Point new.db to config/kavita.db
  6.  
  7.  
  8. import sqlite3
  9.  
  10. # Function to migrate data
  11. def migrate_data():
  12.     old_db = "old.db"  # Source database
  13.     new_db = "new.db"  # Target database
  14.  
  15.     try:
  16.         # Connect to the databases
  17.         old_conn = sqlite3.connect(old_db)
  18.         new_conn = sqlite3.connect(new_db)
  19.  
  20.         # Fetch data from the old database
  21.         cursor_old = old_conn.cursor()
  22.         old_data_query = """
  23.            SELECT
  24.              p.Name AS PersonName,
  25.              p.NormalizedName AS PersonNormalizedName,
  26.              s.ID AS SeriesId,
  27.              p.Role
  28.            FROM Person AS p
  29.            JOIN PersonSeriesMetadata AS psm
  30.              ON p.Id = psm.PeopleId
  31.            JOIN Series AS s
  32.              ON psm.SeriesMetadatasId = s.ID;
  33.        """
  34.         old_data = cursor_old.execute(old_data_query).fetchall()
  35.  
  36.         # Insert data into the new database
  37.         cursor_new = new_conn.cursor()
  38.         new_conn.execute("BEGIN TRANSACTION")
  39.         insert_person_query = """
  40.            INSERT OR IGNORE INTO Person (Name, NormalizedName)
  41.            VALUES (?, ?);
  42.        """
  43.         select_person_id_query = """
  44.            SELECT Id FROM Person WHERE Name = ? AND NormalizedName = ?;
  45.        """
  46.         insert_series_metadata_people_query = """
  47.            INSERT INTO SeriesMetadataPeople (SeriesMetadataId, PersonId, Role)
  48.            VALUES (?, ?, ?);
  49.        """
  50.  
  51.         for row in old_data:
  52.             person_name, person_normalized_name, series_id, role = row
  53.             cursor_new.execute(select_person_id_query, (person_name, person_normalized_name))
  54.             person = cursor_new.fetchone()
  55.  
  56.             if person:
  57.                 person_id = person[0]
  58.             else:
  59.                 cursor_new.execute(insert_person_query, (person_name, person_normalized_name))
  60.                 person_id = cursor_new.lastrowid
  61.  
  62.             cursor_new.execute(insert_series_metadata_people_query, (series_id, person_id, role))
  63.  
  64.         new_conn.commit()
  65.         print("Data migration successful!")
  66.  
  67.     except Exception as e:
  68.         new_conn.rollback()
  69.         print("Error migrating data:", e)
  70.     finally:
  71.         # Close the connections
  72.         old_conn.close()
  73.         new_conn.close()
  74.  
  75. # Run the migration
  76. if __name__ == "__main__":
  77.     migrate_data()
  78.  
Tags: kavita
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement