database issues

This commit is contained in:
Karmanyaah Malhotra 2021-02-28 15:02:11 -05:00
parent b8044adf3f
commit daf1edffb7
4 changed files with 11 additions and 13 deletions

View File

@ -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)

View File

@ -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 {

View File

@ -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 {

View File

@ -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