From daf1edffb72199e377a6748044a3e1731f58c0a1 Mon Sep 17 00:00:00 2001 From: Karmanyaah Malhotra Date: Sun, 28 Feb 2021 15:02:11 -0500 Subject: [PATCH] database issues --- database/database.go | 3 +++ database/message.go | 2 +- database/portal.go | 13 ++++--------- groupmeExt/message.go | 6 +++--- 4 files changed, 11 insertions(+), 13 deletions(-) diff --git a/database/database.go b/database/database.go index 06b946f..0cdcc73 100644 --- a/database/database.go +++ b/database/database.go @@ -17,6 +17,7 @@ package database import ( + "os" "strings" _ "github.com/lib/pq" @@ -48,6 +49,8 @@ func New(dbType string, uri string, baseLog log.Logger) (*Database, error) { if dbType == "sqlite3" { //_, _ = conn.Exec("PRAGMA foreign_keys = ON") + log.Fatalln("no sqlite for now only postgresql") + os.Exit(1) conn = sqlite.Open(uri) } else { conn = postgres.Open(uri) diff --git a/database/message.go b/database/message.go index 3cf4781..e9e22b5 100644 --- a/database/message.go +++ b/database/message.go @@ -83,7 +83,7 @@ type Message struct { Timestamp uint64 `gorm:"notNull;default:0"` Content *groupmeExt.Message `gorm:"type:TEXT;notNull"` - // Portal Portal `gorm:"foreignKey:JID;"` //`gorm:"foreignKey:Chat.Receiver,Chat.JID;references:jid,receiver;constraint:onDelete:CASCADE;"`TODO + Portal Portal `gorm:"foreignKey:chat_jid,chat_receiver;references:jid,receiver;constraint:onDelete:CASCADE;"` } // func (msg *Message) Scan(row Scannable) *Message { diff --git a/database/portal.go b/database/portal.go index c8c3e2a..c8a7b5e 100644 --- a/database/portal.go +++ b/database/portal.go @@ -17,8 +17,6 @@ package database import ( - "strings" - "gorm.io/gorm" log "maunium.net/go/maulogger/v2" "maunium.net/go/mautrix/id" @@ -27,8 +25,8 @@ import ( ) type PortalKey struct { - JID types.GroupMeID - Receiver types.GroupMeID + JID types.GroupMeID `gorm:"primaryKey"` + Receiver types.GroupMeID `gorm:"primaryKey"` } func GroupPortalKey(jid types.GroupMeID) PortalKey { @@ -39,9 +37,6 @@ func GroupPortalKey(jid types.GroupMeID) PortalKey { } func NewPortalKey(jid, receiver types.GroupMeID) PortalKey { - if strings.HasSuffix(jid, "@g.us") { - receiver = jid - } return PortalKey{ JID: jid, Receiver: receiver, @@ -127,8 +122,8 @@ type Portal struct { Name string Topic string Avatar string - AvatarURL id.ContentURI //`gorm:"-"` //TODO:STORE AVATAR - Encrypted bool `gorm:"notNull;default:false"` + AvatarURL id.ContentURI + Encrypted bool `gorm:"notNull;default:false"` } // func (portal *Portal) Scan(row Scannable) *Portal { diff --git a/groupmeExt/message.go b/groupmeExt/message.go index ad80e22..18a9c58 100644 --- a/groupmeExt/message.go +++ b/groupmeExt/message.go @@ -12,13 +12,13 @@ import ( type Message struct{ groupme.Message } func (m *Message) Scan(value interface{}) error { - bytes, ok := value.([]byte) + bytes, ok := value.(string) if !ok { - return errors.New(fmt.Sprint("Failed to unmarshal JSONB value:", value)) + return errors.New(fmt.Sprint("Failed to unmarshal json value:", value)) } message := Message{} - err := json.Unmarshal(bytes, &message) + err := json.Unmarshal([]byte(bytes), &message) *m = Message(message) return err