@@ -3781,6 +3781,168 @@ def migration_036_add_episodecount_column(conn, db_type: str):
37813781 cursor .close ()
37823782
37833783
3784+ @register_migration ("037" , "fix_shared_episodes_schema" , "Add missing SharedBy and SharedWith columns to SharedEpisodes table" , requires = ["009" ])
3785+ def migration_037_fix_shared_episodes_schema (conn , db_type : str ):
3786+ """Add missing SharedBy and SharedWith columns to SharedEpisodes table
3787+
3788+ Old schema had: EpisodeID, UrlKey, ExpirationDate
3789+ New schema needs: EpisodeID, SharedBy, SharedWith, ShareCode, ExpirationDate
3790+ """
3791+ cursor = conn .cursor ()
3792+
3793+ try :
3794+ logger .info ("Starting SharedEpisodes schema fix migration" )
3795+
3796+ if db_type == "postgresql" :
3797+ # Check if sharedby column exists
3798+ cursor .execute ("""
3799+ SELECT column_name FROM information_schema.columns
3800+ WHERE table_name = 'SharedEpisodes'
3801+ AND column_name = 'sharedby'
3802+ """ )
3803+ sharedby_exists = len (cursor .fetchall ()) > 0
3804+
3805+ if not sharedby_exists :
3806+ logger .info ("Adding sharedby column to SharedEpisodes table (PostgreSQL)" )
3807+ cursor .execute ("""
3808+ ALTER TABLE "SharedEpisodes"
3809+ ADD COLUMN sharedby INTEGER NOT NULL DEFAULT 1
3810+ """ )
3811+ conn .commit ()
3812+
3813+ # Check if sharedwith column exists
3814+ cursor .execute ("""
3815+ SELECT column_name FROM information_schema.columns
3816+ WHERE table_name = 'SharedEpisodes'
3817+ AND column_name = 'sharedwith'
3818+ """ )
3819+ sharedwith_exists = len (cursor .fetchall ()) > 0
3820+
3821+ if not sharedwith_exists :
3822+ logger .info ("Adding sharedwith column to SharedEpisodes table (PostgreSQL)" )
3823+ cursor .execute ("""
3824+ ALTER TABLE "SharedEpisodes"
3825+ ADD COLUMN sharedwith INTEGER
3826+ """ )
3827+ conn .commit ()
3828+
3829+ # Check if sharecode column exists (might have been UrlKey)
3830+ cursor .execute ("""
3831+ SELECT column_name FROM information_schema.columns
3832+ WHERE table_name = 'SharedEpisodes'
3833+ AND column_name = 'sharecode'
3834+ """ )
3835+ sharecode_exists = len (cursor .fetchall ()) > 0
3836+
3837+ if not sharecode_exists :
3838+ # Check if UrlKey exists
3839+ cursor .execute ("""
3840+ SELECT column_name FROM information_schema.columns
3841+ WHERE table_name = 'SharedEpisodes'
3842+ AND column_name IN ('UrlKey', 'urlkey')
3843+ """ )
3844+ urlkey_result = cursor .fetchall ()
3845+
3846+ if urlkey_result :
3847+ urlkey_name = urlkey_result [0 ][0 ]
3848+ logger .info (f"Renaming { urlkey_name } to sharecode (PostgreSQL)" )
3849+ cursor .execute (f"""
3850+ ALTER TABLE "SharedEpisodes"
3851+ RENAME COLUMN "{ urlkey_name } " TO sharecode
3852+ """ )
3853+ else :
3854+ logger .info ("Adding sharecode column to SharedEpisodes table (PostgreSQL)" )
3855+ cursor .execute ("""
3856+ ALTER TABLE "SharedEpisodes"
3857+ ADD COLUMN sharecode TEXT UNIQUE
3858+ """ )
3859+ conn .commit ()
3860+
3861+ logger .info ("SharedEpisodes schema fix completed (PostgreSQL)" )
3862+
3863+ else : # MySQL/MariaDB
3864+ # Check if SharedBy column exists
3865+ cursor .execute ("""
3866+ SELECT COUNT(*)
3867+ FROM INFORMATION_SCHEMA.COLUMNS
3868+ WHERE TABLE_NAME = 'SharedEpisodes'
3869+ AND COLUMN_NAME = 'SharedBy'
3870+ AND TABLE_SCHEMA = DATABASE()
3871+ """ )
3872+ sharedby_exists = cursor .fetchone ()[0 ] > 0
3873+
3874+ if not sharedby_exists :
3875+ logger .info ("Adding SharedBy column to SharedEpisodes table (MySQL)" )
3876+ cursor .execute ("""
3877+ ALTER TABLE SharedEpisodes
3878+ ADD COLUMN SharedBy INT NOT NULL DEFAULT 1
3879+ """ )
3880+ conn .commit ()
3881+
3882+ # Check if SharedWith column exists
3883+ cursor .execute ("""
3884+ SELECT COUNT(*)
3885+ FROM INFORMATION_SCHEMA.COLUMNS
3886+ WHERE TABLE_NAME = 'SharedEpisodes'
3887+ AND COLUMN_NAME = 'SharedWith'
3888+ AND TABLE_SCHEMA = DATABASE()
3889+ """ )
3890+ sharedwith_exists = cursor .fetchone ()[0 ] > 0
3891+
3892+ if not sharedwith_exists :
3893+ logger .info ("Adding SharedWith column to SharedEpisodes table (MySQL)" )
3894+ cursor .execute ("""
3895+ ALTER TABLE SharedEpisodes
3896+ ADD COLUMN SharedWith INT
3897+ """ )
3898+ conn .commit ()
3899+
3900+ # Check if ShareCode column exists (might have been UrlKey)
3901+ cursor .execute ("""
3902+ SELECT COUNT(*)
3903+ FROM INFORMATION_SCHEMA.COLUMNS
3904+ WHERE TABLE_NAME = 'SharedEpisodes'
3905+ AND COLUMN_NAME = 'ShareCode'
3906+ AND TABLE_SCHEMA = DATABASE()
3907+ """ )
3908+ sharecode_exists = cursor .fetchone ()[0 ] > 0
3909+
3910+ if not sharecode_exists :
3911+ # Check if UrlKey exists
3912+ cursor .execute ("""
3913+ SELECT COUNT(*)
3914+ FROM INFORMATION_SCHEMA.COLUMNS
3915+ WHERE TABLE_NAME = 'SharedEpisodes'
3916+ AND COLUMN_NAME = 'UrlKey'
3917+ AND TABLE_SCHEMA = DATABASE()
3918+ """ )
3919+ urlkey_exists = cursor .fetchone ()[0 ] > 0
3920+
3921+ if urlkey_exists :
3922+ logger .info ("Renaming UrlKey to ShareCode (MySQL)" )
3923+ cursor .execute ("""
3924+ ALTER TABLE SharedEpisodes
3925+ CHANGE COLUMN UrlKey ShareCode TEXT
3926+ """ )
3927+ else :
3928+ logger .info ("Adding ShareCode column to SharedEpisodes table (MySQL)" )
3929+ cursor .execute ("""
3930+ ALTER TABLE SharedEpisodes
3931+ ADD COLUMN ShareCode TEXT
3932+ """ )
3933+ conn .commit ()
3934+
3935+ logger .info ("SharedEpisodes schema fix completed (MySQL)" )
3936+
3937+ logger .info ("SharedEpisodes schema fix migration completed successfully" )
3938+
3939+ except Exception as e :
3940+ logger .error (f"Error in migration 037: { e } " )
3941+ raise
3942+ finally :
3943+ cursor .close ()
3944+
3945+
37843946if __name__ == "__main__" :
37853947 # Register all migrations and run them
37863948 register_all_migrations ()
0 commit comments