From 2ed4f9793a67ab49156d4ac524e4f5a74b3098a0 Mon Sep 17 00:00:00 2001 From: Trivernis Date: Tue, 1 Oct 2019 15:13:36 +0200 Subject: [PATCH] Changed to table creation - changed creation and update to transaction type - fixed creation file --- src/lib/QueryHelper.ts | 26 ++++++++++++++++++++++++-- src/sql/create-tables.sql | 34 ++++++++++++++++++++-------------- 2 files changed, 44 insertions(+), 16 deletions(-) diff --git a/src/lib/QueryHelper.ts b/src/lib/QueryHelper.ts index 1b8aa37..83b36fc 100644 --- a/src/lib/QueryHelper.ts +++ b/src/lib/QueryHelper.ts @@ -95,7 +95,18 @@ export class QueryHelper { if (this.tableCreationFile) { logger.info("Creating nonexistent tables..."); 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(); + } } } @@ -106,7 +117,18 @@ export class QueryHelper { if (this.tableUpdateFile) { logger.info("Updating table definitions..."); 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(); + } } } diff --git a/src/sql/create-tables.sql b/src/sql/create-tables.sql index 128492b..70e49e2 100644 --- a/src/sql/create-tables.sql +++ b/src/sql/create-tables.sql @@ -15,20 +15,6 @@ DO $$BEGIN END $BODY$; 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$$; --create types @@ -48,6 +34,26 @@ DO $$ BEGIN 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 DO $$ BEGIN