@@ -475,6 +475,42 @@ def download(table):
475475 df .to_csv (csv_path , index = False )
476476 return send_from_directory (UPLOAD_FOLDER , f"{ table } .csv" , as_attachment = True )
477477
478+ @app .route ('/migrate_csv_to_db' )
479+ def migrate_csv_to_db ():
480+ if not session .get ('admin' ):
481+ return "❌ Admin login required" , 403
482+
483+ import os
484+ csv_path = '/data/drive_music.csv' if os .path .exists ('/data/drive_music.csv' ) else 'drive_music.csv'
485+
486+ try :
487+ with sqlite3 .connect (DB_NAME ) as conn :
488+ c = conn .cursor ()
489+
490+ # Step 1: Clear existing table
491+ c .execute ("DELETE FROM music_clips" )
492+
493+ # Step 2: Re-read CSV and insert each entry
494+ with open (csv_path , encoding = 'utf-8' ) as f :
495+ reader = csv .DictReader (f )
496+ for row in reader :
497+ title = row .get ('title' , '' ).strip ()
498+ description = row .get ('description' , '' ).strip ()
499+ preview = row .get ('preview_url' , '' ).strip ()
500+ download = row .get ('download_url' , '' ).strip ()
501+ if preview and download :
502+ db_value = f"{ preview } ||{ download } "
503+ c .execute (
504+ "INSERT INTO music_clips (filename, title, description) VALUES (?, ?, ?)" ,
505+ (db_value , title , description )
506+ )
507+
508+ conn .commit ()
509+ return "✅ Database refreshed from CSV!"
510+
511+ except Exception as e :
512+ return f"❌ Error during migration: { e } "
513+
478514# SEARCH ROUTE
479515@app .route ('/search' )
480516def search ():
0 commit comments