2014-10-20 05:30:44 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var conf = require('./config.js');
|
|
|
|
var sqlite3 = require('sqlite3');
|
|
|
|
var paths = require('path');
|
|
|
|
|
2014-10-21 04:47:13 +00:00
|
|
|
// database handles
|
|
|
|
var dbs = {};
|
|
|
|
|
2014-10-20 05:30:44 +00:00
|
|
|
exports.initializeDatabases = initializeDatabases;
|
|
|
|
|
2014-10-21 04:47:13 +00:00
|
|
|
exports.dbs = dbs;
|
2014-10-20 05:30:44 +00:00
|
|
|
|
|
|
|
function getDatabasePath(name) {
|
|
|
|
return paths.join(conf.config.paths.db, name + '.sqlite3');
|
|
|
|
}
|
|
|
|
|
|
|
|
function initializeDatabases() {
|
|
|
|
// :TODO: this will need to change if more DB's are added
|
2015-06-26 05:19:32 +00:00
|
|
|
dbs.user = new sqlite3.Database(getDatabasePath('user'));
|
|
|
|
dbs.message = new sqlite3.Database(getDatabasePath('message'));
|
2014-10-20 05:30:44 +00:00
|
|
|
|
2015-05-11 03:39:39 +00:00
|
|
|
dbs.user.serialize(function serialized() {
|
2014-10-20 05:30:44 +00:00
|
|
|
createUserTables();
|
2015-07-13 04:56:33 +00:00
|
|
|
createInitialUserValues();
|
2014-10-20 05:30:44 +00:00
|
|
|
});
|
2015-06-26 05:19:32 +00:00
|
|
|
|
|
|
|
dbs.message.serialize(function serialized() {
|
|
|
|
createMessageBaseTables();
|
2015-07-13 04:56:33 +00:00
|
|
|
createInitialMessageValues();
|
2015-06-26 05:19:32 +00:00
|
|
|
});
|
2014-10-20 05:30:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function createUserTables() {
|
2014-10-21 04:47:13 +00:00
|
|
|
dbs.user.run(
|
2014-10-20 05:30:44 +00:00
|
|
|
'CREATE TABLE IF NOT EXISTS user (' +
|
|
|
|
' id INTEGER PRIMARY KEY,' +
|
|
|
|
' user_name VARCHAR NOT NULL,' +
|
|
|
|
' UNIQUE(user_name)' +
|
|
|
|
');'
|
|
|
|
);
|
|
|
|
|
2014-10-21 04:47:13 +00:00
|
|
|
// :TODO: create FK on delete/etc.
|
|
|
|
|
|
|
|
dbs.user.run(
|
2014-10-20 05:30:44 +00:00
|
|
|
'CREATE TABLE IF NOT EXISTS user_property (' +
|
|
|
|
' user_id INTEGER NOT NULL,' +
|
|
|
|
' prop_name VARCHAR NOT NULL,' +
|
|
|
|
' prop_value VARCHAR,' +
|
2014-10-27 04:06:41 +00:00
|
|
|
' UNIQUE(user_id, prop_name),' +
|
|
|
|
' FOREIGN KEY(user_id) REFERENCES user(id) ON DELETE CASCADE' +
|
2014-10-20 05:30:44 +00:00
|
|
|
');'
|
|
|
|
);
|
2015-05-11 03:39:39 +00:00
|
|
|
|
|
|
|
dbs.user.run(
|
2015-07-02 23:04:13 +00:00
|
|
|
'CREATE TABLE IF NOT EXISTS user_group (' +
|
|
|
|
' group_id INTEGER PRIMARY KEY,' +
|
|
|
|
' group_name VARCHAR NOT NULL,' +
|
|
|
|
' UNIQUE(group_name)' +
|
2015-05-11 03:39:39 +00:00
|
|
|
');'
|
|
|
|
);
|
|
|
|
|
|
|
|
dbs.user.run(
|
|
|
|
'CREATE TABLE IF NOT EXISTS user_group_member (' +
|
|
|
|
' group_id INTEGER NOT NULL,' +
|
|
|
|
' user_id INTEGER NOT NULL,' +
|
|
|
|
' UNIQUE(group_id, user_id),' +
|
|
|
|
' FOREIGN KEY(group_id) REFERENCES user_group(group_id) ON DELETE CASCADE' +
|
|
|
|
');'
|
|
|
|
);
|
2015-07-27 04:51:06 +00:00
|
|
|
|
|
|
|
dbs.user.run(
|
|
|
|
'CREATE TABLE IF NOT EXISTS user_login_history (' +
|
|
|
|
' user_id INTEGER NOT NULL,' +
|
|
|
|
' user_name VARCHAR NOT NULL,' +
|
|
|
|
' timestamp DATETIME NOT NULL' +
|
|
|
|
');'
|
|
|
|
);
|
2015-05-11 03:39:39 +00:00
|
|
|
}
|
|
|
|
|
2015-06-26 05:19:32 +00:00
|
|
|
function createMessageBaseTables() {
|
2015-07-02 23:04:13 +00:00
|
|
|
|
2015-06-26 05:19:32 +00:00
|
|
|
dbs.message.run(
|
2015-07-02 23:04:13 +00:00
|
|
|
'CREATE TABLE IF NOT EXISTS message_area (' +
|
|
|
|
' area_id INTEGER PRIMARY KEY,' +
|
|
|
|
' area_name VARCHAR NOT NULL,' +
|
|
|
|
' UNIQUE(area_name)' +
|
|
|
|
');'
|
|
|
|
);
|
|
|
|
|
|
|
|
dbs.message.run(
|
|
|
|
'CREATE TABLE IF NOT EXISTS message_area_group (' +
|
|
|
|
' area_id INTEGER NOT NULL,' +
|
|
|
|
' group_id INTEGER NOT NULL' + // FK @ user.sqlite::user_group::group_id
|
|
|
|
');'
|
|
|
|
);
|
|
|
|
|
|
|
|
dbs.message.run(
|
2015-07-13 04:56:33 +00:00
|
|
|
'CREATE TABLE IF NOT EXISTS message (' +
|
|
|
|
' message_id INTEGER PRIMARY KEY,' +
|
|
|
|
' area_id INTEGER NOT NULL,' +
|
|
|
|
' message_uuid VARCHAR(36) NOT NULL,' +
|
|
|
|
' reply_to_message_id INTEGER,' +
|
|
|
|
' to_user_name VARCHAR NOT NULL,' +
|
|
|
|
' from_user_name VARCHAR NOT NULL,' +
|
|
|
|
' subject,' + // FTS @ message_fts
|
|
|
|
' message,' + // FTS @ message_fts
|
|
|
|
' modified_timestamp DATETIME NOT NULL,' +
|
|
|
|
' view_count INTEGER NOT NULL DEFAULT 0,' +
|
|
|
|
' UNIQUE(message_uuid),' +
|
2015-07-02 23:04:13 +00:00
|
|
|
' FOREIGN KEY(area_id) REFERENCES message_area(area_id)' +
|
2015-06-26 05:19:32 +00:00
|
|
|
');'
|
|
|
|
);
|
|
|
|
|
|
|
|
dbs.message.run(
|
2015-06-27 05:12:12 +00:00
|
|
|
'CREATE VIRTUAL TABLE IF NOT EXISTS message_fts USING fts4 (' +
|
2015-06-26 05:19:32 +00:00
|
|
|
' content="message",' +
|
|
|
|
' subject,' +
|
|
|
|
' message' +
|
|
|
|
');'
|
|
|
|
);
|
2015-06-27 05:12:12 +00:00
|
|
|
|
2015-07-06 23:30:33 +00:00
|
|
|
dbs.message.run(
|
2015-07-07 04:37:11 +00:00
|
|
|
'CREATE TRIGGER IF NOT EXISTS message_before_update BEFORE UPDATE ON message BEGIN' +
|
2015-07-06 23:30:33 +00:00
|
|
|
' DELETE FROM message_fts WHERE docid=old.rowid;' +
|
|
|
|
'END;' +
|
2015-07-07 04:37:11 +00:00
|
|
|
'CREATE TRIGGER IF NOT EXISTS message_before_delete BEFORE DELETE ON message BEGIN' +
|
2015-07-06 23:30:33 +00:00
|
|
|
' DELETE FROM message_fts WHERE docid=old.rowid;' +
|
|
|
|
'END;' +
|
|
|
|
'' +
|
2015-07-07 04:37:11 +00:00
|
|
|
'CREATE TRIGGER IF NOT EXISTS message_after_update AFTER UPDATE ON message BEGIN' +
|
2015-07-06 23:30:33 +00:00
|
|
|
' INSERT INTO message_fts(docid, subject, message) VALUES(new.rowid, new.subject, new.message);' +
|
|
|
|
'END;' +
|
2015-07-07 04:37:11 +00:00
|
|
|
'CREATE TRIGGER IF NOT EXISTS message_after_insert AFTER INSERT ON message BEGIN' +
|
2015-07-06 23:30:33 +00:00
|
|
|
' INSERT INTO message_fts(docid, subject, message) VALUES(new.rowid, new.subject, new.message);' +
|
|
|
|
'END;'
|
|
|
|
);
|
|
|
|
|
2015-06-27 05:12:12 +00:00
|
|
|
dbs.message.run(
|
2015-07-17 04:57:08 +00:00
|
|
|
'CREATE TABLE IF NOT EXISTS message_meta (' +
|
|
|
|
' message_id INTEGER NOT NULL,' +
|
2015-07-17 14:07:43 +00:00
|
|
|
' meta_category INTEGER NOT NULL,' +
|
2015-07-17 04:57:08 +00:00
|
|
|
' meta_name VARCHAR NOT NULL,' +
|
|
|
|
' meta_value VARCHAR NOT NULL,' +
|
|
|
|
' UNIQUE(message_id, meta_category, meta_name, meta_value),' +
|
|
|
|
' FOREIGN KEY(message_id) REFERENCES message(message_id)' +
|
2015-07-02 23:04:13 +00:00
|
|
|
');'
|
|
|
|
);
|
|
|
|
|
|
|
|
dbs.message.run(
|
|
|
|
'CREATE TABLE IF NOT EXISTS hash_tag (' +
|
|
|
|
' hash_tag_id INTEGER PRIMARY KEY,' +
|
|
|
|
' hash_tag_name VARCHAR NOT NULL,' +
|
|
|
|
' UNIQUE(hash_tag_name)' +
|
2015-06-27 05:12:12 +00:00
|
|
|
');'
|
|
|
|
);
|
|
|
|
|
|
|
|
dbs.message.run(
|
2015-07-02 23:04:13 +00:00
|
|
|
'CREATE TABLE IF NOT EXISTS message_hash_tag (' +
|
|
|
|
' hash_tag_id INTEGER NOT NULL,' +
|
|
|
|
' message_id INTEGER NOT NULL' +
|
2015-06-27 05:12:12 +00:00
|
|
|
');'
|
|
|
|
);
|
2015-06-26 05:19:32 +00:00
|
|
|
}
|
|
|
|
|
2015-07-13 04:56:33 +00:00
|
|
|
function createInitialMessageValues() {
|
|
|
|
//
|
|
|
|
// Area ID 1: Private / Local
|
|
|
|
//
|
|
|
|
dbs.message.run(
|
|
|
|
'INSERT OR IGNORE INTO message_area ' +
|
|
|
|
'VALUES(1, "Local Private Messages");'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function createInitialUserValues() {
|
2015-05-11 03:39:39 +00:00
|
|
|
dbs.user.run(
|
|
|
|
'INSERT OR IGNORE INTO user_group ' +
|
|
|
|
'VALUES(1, "users");'
|
|
|
|
);
|
2015-05-11 22:39:28 +00:00
|
|
|
|
|
|
|
dbs.user.run(
|
|
|
|
'INSERT OR IGNORE INTO user_group ' +
|
|
|
|
'VALUES(2, "sysops");'
|
|
|
|
);
|
2014-10-20 05:30:44 +00:00
|
|
|
}
|