Fix similar_artists profile_id column being dropped on every startup
This commit is contained in:
parent
c2af4152ab
commit
3dcf07807c
1 changed files with 53 additions and 47 deletions
|
|
@ -825,55 +825,61 @@ class MusicDatabase:
|
||||||
logger.info("Successfully migrated discovery_recent_albums table for iTunes support")
|
logger.info("Successfully migrated discovery_recent_albums table for iTunes support")
|
||||||
|
|
||||||
# Migration: Add UNIQUE constraint to similar_artists table
|
# Migration: Add UNIQUE constraint to similar_artists table
|
||||||
# Test if ON CONFLICT works by trying a dummy operation
|
# Skip if table already has profile-scoped UNIQUE constraint (from v3 migration)
|
||||||
needs_similar_migration = False
|
cursor.execute("SELECT sql FROM sqlite_master WHERE type='table' AND name='similar_artists'")
|
||||||
try:
|
sa_create_sql = cursor.fetchone()
|
||||||
cursor.execute("""
|
has_profile_unique = sa_create_sql and 'UNIQUE(profile_id' in (sa_create_sql[0] or '')
|
||||||
INSERT INTO similar_artists
|
|
||||||
(source_artist_id, similar_artist_name, similarity_rank, occurrence_count, last_updated)
|
|
||||||
VALUES ('__migration_test__', '__migration_test__', 1, 1, CURRENT_TIMESTAMP)
|
|
||||||
ON CONFLICT(source_artist_id, similar_artist_name)
|
|
||||||
DO UPDATE SET occurrence_count = occurrence_count
|
|
||||||
""")
|
|
||||||
# Clean up test row
|
|
||||||
cursor.execute("DELETE FROM similar_artists WHERE source_artist_id = '__migration_test__'")
|
|
||||||
logger.info("similar_artists table has correct UNIQUE constraint")
|
|
||||||
except Exception as constraint_error:
|
|
||||||
logger.info(f"similar_artists needs migration (constraint test failed: {constraint_error})")
|
|
||||||
needs_similar_migration = True
|
|
||||||
|
|
||||||
if needs_similar_migration:
|
if not has_profile_unique:
|
||||||
logger.info("Migrating similar_artists to add UNIQUE constraint...")
|
# Test if ON CONFLICT works by trying a dummy operation
|
||||||
# Get a fresh connection for the migration
|
needs_similar_migration = False
|
||||||
with self._get_connection() as migration_conn:
|
try:
|
||||||
migration_cursor = migration_conn.cursor()
|
cursor.execute("""
|
||||||
# SQLite doesn't support adding constraints, so recreate table
|
INSERT INTO similar_artists
|
||||||
migration_cursor.execute("DROP TABLE IF EXISTS similar_artists_new")
|
(source_artist_id, similar_artist_name, similarity_rank, occurrence_count, last_updated)
|
||||||
migration_cursor.execute("""
|
VALUES ('__migration_test__', '__migration_test__', 1, 1, CURRENT_TIMESTAMP)
|
||||||
CREATE TABLE similar_artists_new (
|
ON CONFLICT(source_artist_id, similar_artist_name)
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
DO UPDATE SET occurrence_count = occurrence_count
|
||||||
source_artist_id TEXT NOT NULL,
|
|
||||||
similar_artist_spotify_id TEXT,
|
|
||||||
similar_artist_itunes_id TEXT,
|
|
||||||
similar_artist_name TEXT NOT NULL,
|
|
||||||
similarity_rank INTEGER DEFAULT 1,
|
|
||||||
occurrence_count INTEGER DEFAULT 1,
|
|
||||||
last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
||||||
UNIQUE(source_artist_id, similar_artist_name)
|
|
||||||
)
|
|
||||||
""")
|
""")
|
||||||
migration_cursor.execute("""
|
# Clean up test row
|
||||||
INSERT OR IGNORE INTO similar_artists_new
|
cursor.execute("DELETE FROM similar_artists WHERE source_artist_id = '__migration_test__'")
|
||||||
(source_artist_id, similar_artist_spotify_id, similar_artist_itunes_id,
|
logger.info("similar_artists table has correct UNIQUE constraint")
|
||||||
similar_artist_name, similarity_rank, occurrence_count, last_updated)
|
except Exception as constraint_error:
|
||||||
SELECT source_artist_id, similar_artist_spotify_id, similar_artist_itunes_id,
|
logger.info(f"similar_artists needs migration (constraint test failed: {constraint_error})")
|
||||||
similar_artist_name, similarity_rank, occurrence_count, last_updated
|
needs_similar_migration = True
|
||||||
FROM similar_artists
|
|
||||||
""")
|
if needs_similar_migration:
|
||||||
migration_cursor.execute("DROP TABLE similar_artists")
|
logger.info("Migrating similar_artists to add UNIQUE constraint...")
|
||||||
migration_cursor.execute("ALTER TABLE similar_artists_new RENAME TO similar_artists")
|
# Get a fresh connection for the migration
|
||||||
migration_conn.commit()
|
with self._get_connection() as migration_conn:
|
||||||
logger.info("Successfully migrated similar_artists table with UNIQUE constraint")
|
migration_cursor = migration_conn.cursor()
|
||||||
|
# SQLite doesn't support adding constraints, so recreate table
|
||||||
|
migration_cursor.execute("DROP TABLE IF EXISTS similar_artists_new")
|
||||||
|
migration_cursor.execute("""
|
||||||
|
CREATE TABLE similar_artists_new (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
source_artist_id TEXT NOT NULL,
|
||||||
|
similar_artist_spotify_id TEXT,
|
||||||
|
similar_artist_itunes_id TEXT,
|
||||||
|
similar_artist_name TEXT NOT NULL,
|
||||||
|
similarity_rank INTEGER DEFAULT 1,
|
||||||
|
occurrence_count INTEGER DEFAULT 1,
|
||||||
|
last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
UNIQUE(source_artist_id, similar_artist_name)
|
||||||
|
)
|
||||||
|
""")
|
||||||
|
migration_cursor.execute("""
|
||||||
|
INSERT OR IGNORE INTO similar_artists_new
|
||||||
|
(source_artist_id, similar_artist_spotify_id, similar_artist_itunes_id,
|
||||||
|
similar_artist_name, similarity_rank, occurrence_count, last_updated)
|
||||||
|
SELECT source_artist_id, similar_artist_spotify_id, similar_artist_itunes_id,
|
||||||
|
similar_artist_name, similarity_rank, occurrence_count, last_updated
|
||||||
|
FROM similar_artists
|
||||||
|
""")
|
||||||
|
migration_cursor.execute("DROP TABLE similar_artists")
|
||||||
|
migration_cursor.execute("ALTER TABLE similar_artists_new RENAME TO similar_artists")
|
||||||
|
migration_conn.commit()
|
||||||
|
logger.info("Successfully migrated similar_artists table with UNIQUE constraint")
|
||||||
|
|
||||||
# ============== INDEXES (after migrations to ensure columns exist) ==============
|
# ============== INDEXES (after migrations to ensure columns exist) ==============
|
||||||
cursor.execute("CREATE INDEX IF NOT EXISTS idx_similar_artists_source ON similar_artists (source_artist_id)")
|
cursor.execute("CREATE INDEX IF NOT EXISTS idx_similar_artists_source ON similar_artists (source_artist_id)")
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue