From: Chris Fulljames Date: Wed, 19 Mar 2025 00:05:52 +0000 (-0400) Subject: Add comment table migration X-Git-Url: https://littlesong.place/gitweb/gitweb.cgi?a=commitdiff_plain;h=c034bdcc7fad73bbbd06c028d2dd7381b6f33a4d;p=littlesongplace.git Add comment table migration --- diff --git a/main.py b/main.py index 4e71aa0..d5ecf34 100644 --- a/main.py +++ b/main.py @@ -1,4 +1,5 @@ import base64 +import enum import json import logging import os @@ -27,7 +28,7 @@ from werkzeug.middleware.proxy_fix import ProxyFix from yt_dlp import YoutubeDL from yt_dlp.utils import DownloadError -DB_VERSION = 3 +DB_VERSION = 4 DATA_DIR = Path(os.environ["DATA_DIR"]) if "DATA_DIR" in os.environ else Path(".") SCRIPT_DIR = Path(__file__).parent @@ -984,6 +985,7 @@ def get_db(): if db is None: db = g._database = sqlite3.connect(DATA_DIR / "database.db") db.cursor().execute("PRAGMA foreign_keys = ON") + db.row_factory = sqlite3.Row # Get current version user_version = query_db("pragma user_version", one=True)[0] @@ -994,9 +996,55 @@ def get_db(): with app.open_resource(schema_update_script, mode='r') as f: db.cursor().executescript(f.read()) db.commit() - db.row_factory = sqlite3.Row + + if DB_VERSION == 4: + # TODO: Remove after deploying + assign_thread_ids(db, "songs", "songid", threadtype=ThreadType.SONG) + assign_thread_ids(db, "users", "userid", threadtype=ThreadType.PROFILE) + assign_thread_ids(db, "playlists", "playlistid", threadtype=ThreadType.PLAYLIST) + + # Copy song comments to new comments table + cur = db.execute( + """\ + select commentid, threadid, sc.userid, replytoid, sc.created, content + from song_comments as sc + inner join songs on sc.songid = songs.songid + """) + for row in cur: + comment_cur = db.execute( + """\ + insert into comments (commentid, threadid, userid, replytoid, created, content) + values (?, ?, ?, ?, ?, ?) + """, + [row["commentid"], row["threadid"], row["userid"], row["replytoid"], row["created"], row["content"]]) + comment_cur.close() + cur.close() + + # Copy song comment notifications to new comment notifications table + cur = db.execute( + """\ + insert into comment_notifications + select * from song_comment_notifications + """) + + db.execute("PRAGMA user_version = 4").close() + + db.commit() + return db +# TODO: Remove after deploying +def assign_thread_ids(db, table, id_col, threadtype): + cur = db.execute(f"select * from {table}") + for row in cur: + thread_cur = db.execute("insert into comment_threads (threadtype) values (?) returning threadid", [threadtype]) + threadid = thread_cur.fetchone()[0] + thread_cur.close() + + song_cur = db.execute(f"update {table} set threadid = ? where {id_col} = ?", [threadid, row[id_col]]) + song_cur.close() + cur.close() + @app.teardown_appcontext def close_db(exception): db = getattr(g, '_database', None) @@ -1030,6 +1078,11 @@ def gen_key(): import secrets print(secrets.token_hex()) +class ThreadType(enum.IntEnum): + SONG = 0 + PROFILE = 1 + PLAYLIST = 2 + @dataclass class Song: songid: int @@ -1137,3 +1190,4 @@ class Song: collabs[songid] = query_db("select (name) from song_collaborators where songid = ?", [songid]) return tags, collabs + diff --git a/schema_update.sql b/schema_update.sql index 0885301..64c84fc 100644 --- a/schema_update.sql +++ b/schema_update.sql @@ -1,7 +1,7 @@ -- Create new comment tables CREATE TABLE comment_threads ( threadid INTEGER PRIMARY KEY, - threadtype INTEGER DEFAULT 0, + threadtype INTEGER NOT NULL ); CREATE TABLE comments ( @@ -36,5 +36,3 @@ ALTER TABLE users ADD COLUMN threadid INTEGER; -- Add playlist comment thread to playlists table ALTER TABLE playlists ADD COLUMN threadid INTEGER; -PRAGMA user_version = 4; -