From b8c4054e0933b2a9e8578b45e86bc83fcc80af85 Mon Sep 17 00:00:00 2001 From: Jonathan Bernard Date: Thu, 10 Dec 2015 18:14:16 -0600 Subject: [PATCH] Initial schema. --- service/README.md | 10 +++ service/database.json | 5 ++ .../main/db/20151209054632-initial-schema.js | 30 ++++++++ .../20151209054632-initial-schema-down.sql | 10 +++ .../sqls/20151209054632-initial-schema-up.sql | 77 +++++++++++++++++++ 5 files changed, 132 insertions(+) create mode 100644 service/README.md create mode 100644 service/database.json create mode 100644 service/src/main/db/20151209054632-initial-schema.js create mode 100644 service/src/main/db/sqls/20151209054632-initial-schema-down.sql create mode 100644 service/src/main/db/sqls/20151209054632-initial-schema-up.sql diff --git a/service/README.md b/service/README.md new file mode 100644 index 0000000..f693792 --- /dev/null +++ b/service/README.md @@ -0,0 +1,10 @@ +## Database + +Uses [db-migrate][http://db-migrate.readthedocs.org/en/latest/] to manage +database migrations. Migration scripts live if `src/main/db`. + +Database environment configuration lives in `database.json`. + +To initialize a new database do: + + db-migrate -m src/main/db up diff --git a/service/database.json b/service/database.json new file mode 100644 index 0000000..f3c4189 --- /dev/null +++ b/service/database.json @@ -0,0 +1,5 @@ +{ + "defaultEnv": "dev", + "sql-file": true, + "dev": "postgres://jonathan@localhost/wdiwtlt" +} diff --git a/service/src/main/db/20151209054632-initial-schema.js b/service/src/main/db/20151209054632-initial-schema.js new file mode 100644 index 0000000..b2d3b31 --- /dev/null +++ b/service/src/main/db/20151209054632-initial-schema.js @@ -0,0 +1,30 @@ +var dbm = global.dbm || require('db-migrate'); +var type = dbm.dataType; +var fs = require('fs'); +var path = require('path'); + +exports.up = function(db, callback) { + var filePath = path.join(__dirname + '/sqls/20151209054632-initial-schema-up.sql'); + fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){ + if (err) return callback(err); + console.log('received data: ' + data); + + db.runSql(data, function(err) { + if (err) return callback(err); + callback(); + }); + }); +}; + +exports.down = function(db, callback) { + var filePath = path.join(__dirname + '/sqls/20151209054632-initial-schema-down.sql'); + fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){ + if (err) return callback(err); + console.log('received data: ' + data); + + db.runSql(data, function(err) { + if (err) return callback(err); + callback(); + }); + }); +}; diff --git a/service/src/main/db/sqls/20151209054632-initial-schema-down.sql b/service/src/main/db/sqls/20151209054632-initial-schema-down.sql new file mode 100644 index 0000000..dbd911c --- /dev/null +++ b/service/src/main/db/sqls/20151209054632-initial-schema-down.sql @@ -0,0 +1,10 @@ +DROP TABLE artists_albums; +DROP TABLE albums_images; +DROP TABLE artists_images; +DROP TABLE albums; +DROP TABLE artists; +DROP TABLE media_files_tags; +DROP TABLE bookmarks; +DROP TABLE playlists; +DROP TABLE tags; +DROP TABLE media_files; diff --git a/service/src/main/db/sqls/20151209054632-initial-schema-up.sql b/service/src/main/db/sqls/20151209054632-initial-schema-up.sql new file mode 100644 index 0000000..393da47 --- /dev/null +++ b/service/src/main/db/sqls/20151209054632-initial-schema-up.sql @@ -0,0 +1,77 @@ +CREATE TABLE media_files ( + id SERIAL PRIMARY KEY, + name VARCHAR NOT NULL, + artist_id INTEGER, + album_id INTEGER, + play_count INTEGER NOT NULL DEFAULT 0, + comment VARCHAR DEFAULT '' +); + +CREATE TABLE tags ( + id SERIAL PRIMARY KEY, + name VARCHAR UNIQUE NOT NULL +); + +CREATE TABLE playlists ( + id SERIAL PRIMARY KEY, + name VARCHAR NOT NULL, + media_file_ids INTEGER[] NOT NULL +); + +CREATE TABLE bookmarks ( + id SERIAL PRIMARY KEY, + name VARCHAR, + playlist_id INTEGER NOT NULL, + media_file_id INTEGER NOT NULL, + play_index INTEGER NOT NULL, + FOREIGN KEY (playlist_id) REFERENCES playlists(id), + FOREIGN KEY (media_file_id) REFERENCES media_files(id) +); + +CREATE TABLE media_files_tags ( + id SERIAL PRIMARY KEY, + media_file_id INTEGER, + tag_id INTEGER, + FOREIGN KEY (media_file_id) REFERENCES media_files(id), + FOREIGN KEY (tag_id) REFERENCES tags(id) +); + +CREATE TABLE artists ( + id SERIAL PRIMARY KEY, + name VARCHAR NOT NULL +); + +CREATE TABLE albums ( + id SERIAL PRIMARY KEY, + name VARCHAR NOT NULL, + year INTEGER +); + +CREATE TABLE images ( + id SERIAL PRIMARY KEY, + url VARCHAR +); + +CREATE TABLE artists_images ( + id SERIAL PRIMARY KEY, + artist_id INTEGER, + image_id INTEGER, + FOREIGN KEY (artist_id) REFERENCES artists (id), + FOREIGN KEY (image_id) REFERENCES images (id) +); + +CREATE TABLE albums_images ( + id SERIAL PRIMARY KEY, + album_id INTEGER, + image_id INTEGER, + FOREIGN KEY (album_id) REFERENCES albums (id), + FOREIGN KEY (image_id) REFERENCES images (id) +); + +CREATE TABLE artists_albums ( + artist_id INTEGER NOT NULL, + album_id INTEGER NOT NULL, + PRIMARY KEY (artist_id, album_id), + FOREIGN KEY (artist_id) REFERENCES artists (id), + FOREIGN KEY (album_id) REFERENCES albums (id) +);