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
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 (
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);
-
--- /dev/null
+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);
+