]> littlesong.place Git - littlesongplace.git/commitdiff
Add schema update file - tests broken
authorChris Fulljames <christianfulljames@gmail.com>
Wed, 29 Jan 2025 12:31:09 +0000 (07:31 -0500)
committerChris Fulljames <christianfulljames@gmail.com>
Wed, 29 Jan 2025 12:31:09 +0000 (07:31 -0500)
main.py
schema.sql
schema_update.sql [new file with mode: 0644]

diff --git a/main.py b/main.py
index 8be4b4e4163e15e456a68a5a3f06722e4a5c0477..6de9afeed4069059d8357b15ffbe1d37c07524c1 100644 (file)
--- a/main.py
+++ b/main.py
@@ -622,6 +622,10 @@ def get_db():
     db = getattr(g, '_database', None)
     if db is None:
         db = g._database = sqlite3.connect(DATA_DIR / "database.db")
+        if os.path.exists('schema_update.sql'):
+            with app.open_resource('schema_update.sql', mode='r') as f:
+                db.cursor().executescript(f.read())
+            db.commit()
         db.row_factory = sqlite3.Row
     return db
 
index d1e17fba1612316ff6421f05cc069245790bbbb8..9cbab0334ec9af64a31bceae8990f05209bfb787 100644 (file)
@@ -17,7 +17,6 @@ CREATE TABLE songs (
     description TEXT,
     FOREIGN KEY(userid) REFERENCES users(userid)
 );
-CREATE INDEX idx_songs_by_user ON songs(userid);
 
 DROP TABLE IF EXISTS song_collaborators;
 CREATE TABLE song_collaborators (
@@ -37,27 +36,4 @@ CREATE TABLE song_tags (
 CREATE INDEX idx_song_tags_tag ON song_tags(tag);
 
 DROP TABLE IF EXISTS song_comments;
-CREATE TABLE song_comments (
-    commentid INTEGER PRIMARY KEY,
-    songid INTEGER NOT NULL,
-    userid INTEGER NOT NULL,
-    replytoid INTEGER,
-    created TEXT NOT NULL,
-    content TEXT NOT NULL,
-    FOREIGN KEY(songid) REFERENCES songs(songid),
-    FOREIGN KEY(userid) REFERENCES users(userid)
-);
-CREATE INDEX idx_comments_by_song ON song_comments(songid);
-CREATE INDEX idx_comments_by_user ON song_comments(userid);
-CREATE INDEX idx_comments_by_replyto ON song_comments(replytoid);
-
 DROP TABLE IF EXISTS song_comment_notifications;
-CREATE TABLE song_comment_notifications (
-    notificationid INTEGER PRIMARY KEY,
-    commentid INTEGER NOT NULL,
-    targetuserid INTEGER NOT NULL,
-    FOREIGN KEY(commentid) REFERENCES song_comments(commentid),
-    FOREIGN KEY(targetuserid) REFERENCES users(userid)
-);
-CREATE INDEX idx_song_comment_notifications_by_target ON song_comment_notifications(targetuserid);
-
diff --git a/schema_update.sql b/schema_update.sql
new file mode 100644 (file)
index 0000000..6e81b3e
--- /dev/null
@@ -0,0 +1,25 @@
+CREATE INDEX IF NOT EXISTS idx_songs_by_user ON songs(userid);
+
+CREATE TABLE IF NOT EXISTS song_comments (
+    commentid INTEGER PRIMARY KEY,
+    songid INTEGER NOT NULL,
+    userid INTEGER NOT NULL,
+    replytoid INTEGER,
+    created TEXT NOT NULL,
+    content TEXT NOT NULL,
+    FOREIGN KEY(songid) REFERENCES songs(songid),
+    FOREIGN KEY(userid) REFERENCES users(userid)
+);
+CREATE INDEX IF NOT EXISTS idx_comments_by_song ON song_comments(songid);
+CREATE INDEX IF NOT EXISTS idx_comments_by_user ON song_comments(userid);
+CREATE INDEX IF NOT EXISTS idx_comments_by_replyto ON song_comments(replytoid);
+
+CREATE TABLE IF NOT EXISTS song_comment_notifications (
+    notificationid INTEGER PRIMARY KEY,
+    commentid INTEGER NOT NULL,
+    targetuserid INTEGER NOT NULL,
+    FOREIGN KEY(commentid) REFERENCES song_comments(commentid),
+    FOREIGN KEY(targetuserid) REFERENCES users(userid)
+);
+CREATE INDEX IF NOT EXISTS idx_song_comment_notifications_by_target ON song_comment_notifications(targetuserid);
+