Merge branch 'julius-dev' of Software_Engineering_I/greenvironment-server into develop

pull/1/head
Trivernis 5 years ago committed by Gitea
commit 1c4e586596

@ -79,6 +79,15 @@ export class QueryHelper {
this.pool = pgPool; this.pool = pgPool;
} }
/**
* Async init function
*/
public async init() {
await this.pool.connect();
await this.createTables();
await this.updateTableDefinitions();
}
/** /**
* creates all tables needed if a filepath was given with the constructor * creates all tables needed if a filepath was given with the constructor
*/ */
@ -86,7 +95,18 @@ export class QueryHelper {
if (this.tableCreationFile) { if (this.tableCreationFile) {
logger.info("Creating nonexistent tables..."); logger.info("Creating nonexistent tables...");
const tableSql = await fsx.readFile(this.tableCreationFile, "utf-8"); const tableSql = await fsx.readFile(this.tableCreationFile, "utf-8");
await this.query({text: tableSql}); const trans = await this.createTransaction();
await trans.begin();
try {
await trans.query({text: tableSql});
await trans.commit();
} catch (err) {
globals.logger.error(`Error on table creation ${err.message}`);
globals.logger.debug(err.stack);
await trans.rollback();
} finally {
trans.release();
}
} }
} }
@ -97,7 +117,18 @@ export class QueryHelper {
if (this.tableUpdateFile) { if (this.tableUpdateFile) {
logger.info("Updating table definitions..."); logger.info("Updating table definitions...");
const tableSql = await fsx.readFile(this.tableUpdateFile, "utf-8"); const tableSql = await fsx.readFile(this.tableUpdateFile, "utf-8");
await this.query({text: tableSql}); const trans = await this.createTransaction();
await trans.begin();
try {
await trans.query({text: tableSql});
await trans.commit();
} catch (err) {
globals.logger.error(`Error on table update ${err.message}`);
globals.logger.debug(err.stack);
await trans.rollback();
} finally {
trans.release();
}
} }
} }

@ -42,8 +42,12 @@ namespace dataaccess {
* Initializes everything that needs to be initialized asynchronous. * Initializes everything that needs to be initialized asynchronous.
*/ */
export async function init() { export async function init() {
await queryHelper.createTables(); try {
await queryHelper.updateTableDefinitions(); await queryHelper.init();
} catch (err) {
globals.logger.error(err.message);
globals.logger.debug(err.stack);
}
} }
/** /**

@ -15,20 +15,6 @@ DO $$BEGIN
END $BODY$; END $BODY$;
END IF; END IF;
IF NOT function_exists('cast_to_votetype') THEN
CREATE FUNCTION cast_to_votetype(text) RETURNS votetype LANGUAGE plpgsql AS $BODY$
BEGIN
RETURN CASE WHEN $1::votetype IS NULL THEN 'UPVOTE' ELSE $1::votetype END;
END $BODY$;
END IF;
IF NOT function_exists('cast_to_posttype') THEN
CREATE FUNCTION cast_to_posttype(text) RETURNS posttype LANGUAGE plpgsql AS $BODY$
BEGIN
RETURN CASE WHEN $1::posttype IS NULL THEN 'MISC' ELSE $1::posttype END;
END $BODY$;
END IF;
END$$; END$$;
--create types --create types
@ -48,16 +34,37 @@ DO $$ BEGIN
END$$; END$$;
-- create functions relying on types
DO $$ BEGIN
IF NOT function_exists('cast_to_votetype') THEN
CREATE FUNCTION cast_to_votetype(text) RETURNS votetype LANGUAGE plpgsql AS $BODY$
BEGIN
RETURN CASE WHEN $1::votetype IS NULL THEN 'UPVOTE' ELSE $1::votetype END;
END $BODY$;
END IF;
IF NOT function_exists('cast_to_posttype') THEN
CREATE FUNCTION cast_to_posttype(text) RETURNS posttype LANGUAGE plpgsql AS $BODY$
BEGIN
RETURN CASE WHEN $1::posttype IS NULL THEN 'MISC' ELSE $1::posttype END;
END $BODY$;
END IF;
END$$;
-- create tables -- create tables
DO $$ BEGIN
CREATE TABLE IF NOT EXISTS "user_sessions" ( CREATE TABLE IF NOT EXISTS "user_sessions" (
"sid" varchar NOT NULL, "sid" varchar NOT NULL,
"sess" json NOT NULL, "sess" json NOT NULL,
"expire" timestamp(6) NOT NULL, "expire" timestamp(6) NOT NULL,
PRIMARY KEY ("sid") NOT DEFERRABLE INITIALLY IMMEDIATE PRIMARY KEY ("sid") NOT DEFERRABLE INITIALLY IMMEDIATE
) WITH (OIDS=FALSE); ) WITH (OIDS=FALSE);
CREATE TABLE IF NOT EXISTS users ( CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
name varchar(128) NOT NULL, name varchar(128) NOT NULL,
handle varchar(128) UNIQUE NOT NULL, handle varchar(128) UNIQUE NOT NULL,
@ -65,9 +72,9 @@ CREATE TABLE IF NOT EXISTS users (
email varchar(128) UNIQUE NOT NULL, email varchar(128) UNIQUE NOT NULL,
greenpoints INTEGER DEFAULT 0, greenpoints INTEGER DEFAULT 0,
joined_at TIMESTAMP DEFAULT now() joined_at TIMESTAMP DEFAULT now()
); );
CREATE TABLE IF NOT EXISTS posts ( CREATE TABLE IF NOT EXISTS posts (
id BIGSERIAL PRIMARY KEY, id BIGSERIAL PRIMARY KEY,
upvotes INTEGER DEFAULT 0, upvotes INTEGER DEFAULT 0,
downvotes INTEGER DEFAULT 0, downvotes INTEGER DEFAULT 0,
@ -75,54 +82,56 @@ CREATE TABLE IF NOT EXISTS posts (
content text, content text,
author SERIAL REFERENCES users (id) ON DELETE CASCADE, author SERIAL REFERENCES users (id) ON DELETE CASCADE,
type posttype NOT NULL DEFAULT 'MISC' type posttype NOT NULL DEFAULT 'MISC'
); );
CREATE TABLE IF NOT EXISTS votes ( CREATE TABLE IF NOT EXISTS votes (
user_id SERIAL REFERENCES users (id) ON DELETE CASCADE, user_id SERIAL REFERENCES users (id) ON DELETE CASCADE,
item_id BIGSERIAL REFERENCES posts (id) ON DELETE CASCADE, item_id BIGSERIAL REFERENCES posts (id) ON DELETE CASCADE,
vote_type votetype DEFAULT 'DOWNVOTE', vote_type votetype DEFAULT 'DOWNVOTE',
PRIMARY KEY (user_id, item_id) PRIMARY KEY (user_id, item_id)
); );
CREATE TABLE IF NOT EXISTS events ( CREATE TABLE IF NOT EXISTS events (
id BIGSERIAL PRIMARY KEY, id BIGSERIAL PRIMARY KEY,
time TIMESTAMP, time TIMESTAMP,
owner SERIAL REFERENCES users (id) owner SERIAL REFERENCES users (id)
); );
CREATE TABLE IF NOT EXISTS event_members ( CREATE TABLE IF NOT EXISTS event_members (
event BIGSERIAL REFERENCES events (id), event BIGSERIAL REFERENCES events (id),
member SERIAL REFERENCES users (id), member SERIAL REFERENCES users (id),
PRIMARY KEY (event, member) PRIMARY KEY (event, member)
); );
CREATE TABLE IF NOT EXISTS chats ( CREATE TABLE IF NOT EXISTS chats (
id BIGSERIAL PRIMARY KEY id BIGSERIAL PRIMARY KEY
); );
CREATE TABLE IF NOT EXISTS chat_messages ( CREATE TABLE IF NOT EXISTS chat_messages (
chat BIGSERIAL REFERENCES chats (id) ON DELETE CASCADE, chat BIGSERIAL REFERENCES chats (id) ON DELETE CASCADE,
author SERIAL REFERENCES users (id) ON DELETE SET NULL, author SERIAL REFERENCES users (id) ON DELETE SET NULL,
content VARCHAR(1024) NOT NULL, content VARCHAR(1024) NOT NULL,
created_at TIMESTAMP DEFAULT now(), created_at TIMESTAMP DEFAULT now(),
PRIMARY KEY (chat, author, created_at) PRIMARY KEY (chat, author, created_at)
); );
CREATE TABLE IF NOT EXISTS chat_members ( CREATE TABLE IF NOT EXISTS chat_members (
chat BIGSERIAL REFERENCES chats (id) ON DELETE CASCADE, chat BIGSERIAL REFERENCES chats (id) ON DELETE CASCADE,
member SERIAL REFERENCES users (id) ON DELETE CASCADE, member SERIAL REFERENCES users (id) ON DELETE CASCADE,
PRIMARY KEY (chat, member) PRIMARY KEY (chat, member)
); );
CREATE TABLE IF NOT EXISTS user_friends ( CREATE TABLE IF NOT EXISTS user_friends (
user_id SERIAL REFERENCES users (id) ON DELETE CASCADE, user_id SERIAL REFERENCES users (id) ON DELETE CASCADE,
friend_id SERIAL REFERENCES users (id) ON DELETE CASCADE, friend_id SERIAL REFERENCES users (id) ON DELETE CASCADE,
PRIMARY KEY (user_id, friend_id) PRIMARY KEY (user_id, friend_id)
); );
CREATE TABLE IF NOT EXISTS requests ( CREATE TABLE IF NOT EXISTS requests (
sender SERIAL REFERENCES users (id) ON DELETE CASCADE, sender SERIAL REFERENCES users (id) ON DELETE CASCADE,
receiver SERIAL REFERENCES users (id) ON DELETE CASCADE, receiver SERIAL REFERENCES users (id) ON DELETE CASCADE,
type requesttype DEFAULT 'FRIENDREQUEST', type requesttype DEFAULT 'FRIENDREQUEST',
PRIMARY KEY (sender, receiver, type) PRIMARY KEY (sender, receiver, type)
); );
END $$;

Loading…
Cancel
Save