Initial schema.

This commit is contained in:
Jonathan Bernard 2015-12-10 18:14:16 -06:00
parent 8e196e0c84
commit b8c4054e09
5 changed files with 132 additions and 0 deletions

10
service/README.md Normal file
View File

@ -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

5
service/database.json Normal file
View File

@ -0,0 +1,5 @@
{
"defaultEnv": "dev",
"sql-file": true,
"dev": "postgres://jonathan@localhost/wdiwtlt"
}

View File

@ -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();
});
});
};

View File

@ -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;

View File

@ -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)
);