Make message.CreateTable usable on postgres and sqlite

This commit is contained in:
Nicolas Werner 2019-03-06 23:03:19 +01:00
parent d46578894e
commit 9dde189438

View file

@ -18,6 +18,7 @@ package database
import ( import (
"bytes" "bytes"
"strings"
"database/sql" "database/sql"
"encoding/json" "encoding/json"
@ -34,6 +35,7 @@ type MessageQuery struct {
} }
func (mq *MessageQuery) CreateTable(dbType string) error { func (mq *MessageQuery) CreateTable(dbType string) error {
if strings.ToLower(dbType) == "postgres" {
_, err := mq.db.Exec(`CREATE TABLE IF NOT EXISTS message ( _, err := mq.db.Exec(`CREATE TABLE IF NOT EXISTS message (
chat_jid VARCHAR(255), chat_jid VARCHAR(255),
chat_receiver VARCHAR(255), chat_receiver VARCHAR(255),
@ -46,6 +48,20 @@ func (mq *MessageQuery) CreateTable(dbType string) error {
FOREIGN KEY (chat_jid, chat_receiver) REFERENCES portal(jid, receiver) FOREIGN KEY (chat_jid, chat_receiver) REFERENCES portal(jid, receiver)
)`) )`)
return err 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 { func (mq *MessageQuery) New() *Message {