From: Chris Fulljames Date: Wed, 29 Jan 2025 12:31:09 +0000 (-0500) Subject: Add schema update file - tests broken X-Git-Url: https://littlesong.place/gitweb/gitweb.cgi?a=commitdiff_plain;h=d2b9906f974a1b380abdb59e6fecbc8bdc4248d9;p=littlesongplace.git Add schema update file - tests broken --- diff --git a/main.py b/main.py index 8be4b4e..6de9afe 100644 --- 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 diff --git a/schema.sql b/schema.sql index d1e17fb..9cbab03 100644 --- a/schema.sql +++ b/schema.sql @@ -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 index 0000000..6e81b3e --- /dev/null +++ b/schema_update.sql @@ -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); +