From 9dde1894380a7ec36651cb195079ce3259f7a060 Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Wed, 6 Mar 2019 23:03:19 +0100 Subject: [PATCH] Make message.CreateTable usable on postgres and sqlite --- database/message.go | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/database/message.go b/database/message.go index 886bd56..2e5f7e0 100644 --- a/database/message.go +++ b/database/message.go @@ -18,6 +18,7 @@ package database import ( "bytes" + "strings" "database/sql" "encoding/json" @@ -34,18 +35,33 @@ type MessageQuery struct { } func (mq *MessageQuery) CreateTable(dbType string) error { - _, err := mq.db.Exec(`CREATE TABLE IF NOT EXISTS message ( - chat_jid VARCHAR(255), - chat_receiver VARCHAR(255), - jid VARCHAR(255), - mxid VARCHAR(255) NOT NULL UNIQUE, - sender VARCHAR(255) NOT NULL, - content bytea NOT NULL, + if strings.ToLower(dbType) == "postgres" { + _, err := mq.db.Exec(`CREATE TABLE IF NOT EXISTS message ( + chat_jid VARCHAR(255), + chat_receiver VARCHAR(255), + jid VARCHAR(255), + mxid VARCHAR(255) NOT NULL UNIQUE, + sender VARCHAR(255) NOT NULL, + content bytea NOT NULL, - PRIMARY KEY (chat_jid, chat_receiver, jid), - FOREIGN KEY (chat_jid, chat_receiver) REFERENCES portal(jid, receiver) - )`) + PRIMARY KEY (chat_jid, chat_receiver, jid), + FOREIGN KEY (chat_jid, chat_receiver) REFERENCES portal(jid, receiver) + )`) + return err + } else { + _, err := mq.db.Exec(`CREATE TABLE IF NOT EXISTS message ( + chat_jid VARCHAR(255), + chat_receiver VARCHAR(255), + jid VARCHAR(255), + mxid VARCHAR(255) NOT NULL UNIQUE, + sender VARCHAR(255) NOT NULL, + content BLOB NOT NULL, + + PRIMARY KEY (chat_jid, chat_receiver, jid), + FOREIGN KEY (chat_jid, chat_receiver) REFERENCES portal(jid, receiver) + )`) return err + } } func (mq *MessageQuery) New() *Message {