From 6547a2ce1f9d7039e77dcbfebb55fae1c5440f2b Mon Sep 17 00:00:00 2001 From: Dominic Renner Date: Wed, 6 Mar 2019 10:59:52 +0100 Subject: [PATCH] changed sqlite to postgresql --- database/database.go | 5 +++-- database/message.go | 2 +- database/portal.go | 2 +- database/puppet.go | 2 +- database/user.go | 16 ++++++++-------- 5 files changed, 14 insertions(+), 13 deletions(-) diff --git a/database/database.go b/database/database.go index 0e4e964..3a36939 100644 --- a/database/database.go +++ b/database/database.go @@ -19,7 +19,7 @@ package database import ( "database/sql" - _ "github.com/mattn/go-sqlite3" + _ "github.com/lib/pq" log "maunium.net/go/maulogger/v2" ) @@ -35,7 +35,8 @@ type Database struct { } func New(file string) (*Database, error) { - conn, err := sql.Open("sqlite3", file) + connStr := "postgres://synapse:changeme@db/whatsapp?sslmode=disable" + conn, err := sql.Open("postgres", connStr) if err != nil { return nil, err } diff --git a/database/message.go b/database/message.go index c6a2790..c8f6f9e 100644 --- a/database/message.go +++ b/database/message.go @@ -40,7 +40,7 @@ func (mq *MessageQuery) CreateTable() error { jid VARCHAR(255), mxid VARCHAR(255) NOT NULL UNIQUE, sender VARCHAR(25) NOT NULL, - content BLOB NOT NULL, + content bytea NOT NULL, PRIMARY KEY (chat_jid, chat_receiver, jid), FOREIGN KEY (chat_jid, chat_receiver) REFERENCES portal(jid, receiver) diff --git a/database/portal.go b/database/portal.go index 60eb486..ffb4318 100644 --- a/database/portal.go +++ b/database/portal.go @@ -70,7 +70,7 @@ func (pq *PortalQuery) CreateTable() error { avatar VARCHAR(255) NOT NULL, PRIMARY KEY (jid, receiver), - FOREIGN KEY (receiver) REFERENCES user(mxid) + FOREIGN KEY (receiver) REFERENCES whatsapp_user(mxid) )`) return err } diff --git a/database/puppet.go b/database/puppet.go index 3e1b169..bde6116 100644 --- a/database/puppet.go +++ b/database/puppet.go @@ -34,7 +34,7 @@ func (pq *PuppetQuery) CreateTable() error { jid VARCHAR(25) PRIMARY KEY, avatar VARCHAR(255), displayname VARCHAR(255), - name_quality TINYINT + name_quality SMALLINT )`) return err } diff --git a/database/user.go b/database/user.go index 090d70a..2f09555 100644 --- a/database/user.go +++ b/database/user.go @@ -34,7 +34,7 @@ type UserQuery struct { } func (uq *UserQuery) CreateTable() error { - _, err := uq.db.Exec(`CREATE TABLE IF NOT EXISTS user ( + _, err := uq.db.Exec(`CREATE TABLE IF NOT EXISTS whatsapp_user ( mxid VARCHAR(255) PRIMARY KEY, jid VARCHAR(25) UNIQUE, @@ -43,8 +43,8 @@ func (uq *UserQuery) CreateTable() error { client_id VARCHAR(255), client_token VARCHAR(255), server_token VARCHAR(255), - enc_key BLOB, - mac_key BLOB + enc_key bytea, + mac_key bytea )`) return err } @@ -57,7 +57,7 @@ func (uq *UserQuery) New() *User { } func (uq *UserQuery) GetAll() (users []*User) { - rows, err := uq.db.Query("SELECT * FROM user") + rows, err := uq.db.Query("SELECT * FROM whatsapp_user") if err != nil || rows == nil { return nil } @@ -69,7 +69,7 @@ func (uq *UserQuery) GetAll() (users []*User) { } func (uq *UserQuery) GetByMXID(userID types.MatrixUserID) *User { - row := uq.db.QueryRow("SELECT * FROM user WHERE mxid=?", userID) + row := uq.db.QueryRow("SELECT * FROM whatsapp_user WHERE mxid=?", userID) if row == nil { return nil } @@ -77,7 +77,7 @@ func (uq *UserQuery) GetByMXID(userID types.MatrixUserID) *User { } func (uq *UserQuery) GetByJID(userID types.WhatsAppID) *User { - row := uq.db.QueryRow("SELECT * FROM user WHERE jid=?", stripSuffix(userID)) + row := uq.db.QueryRow("SELECT * FROM whatsapp_user WHERE jid=?", stripSuffix(userID)) if row == nil { return nil } @@ -150,7 +150,7 @@ func (user *User) sessionUnptr() (sess whatsapp.Session) { func (user *User) Insert() { sess := user.sessionUnptr() - _, err := user.db.Exec("INSERT INTO user VALUES (?, ?, ?, ?, ?, ?, ?, ?)", user.MXID, user.jidPtr(), + _, err := user.db.Exec("INSERT INTO whatsapp_user VALUES (?, ?, ?, ?, ?, ?, ?, ?)", user.MXID, user.jidPtr(), user.ManagementRoom, sess.ClientId, sess.ClientToken, sess.ServerToken, sess.EncKey, sess.MacKey) if err != nil { @@ -160,7 +160,7 @@ func (user *User) Insert() { func (user *User) Update() { sess := user.sessionUnptr() - _, err := user.db.Exec("UPDATE user SET jid=?, management_room=?, client_id=?, client_token=?, server_token=?, enc_key=?, mac_key=? WHERE mxid=?", + _, err := user.db.Exec("UPDATE whatsapp_user SET jid=?, management_room=?, client_id=?, client_token=?, server_token=?, enc_key=?, mac_key=? WHERE mxid=?", user.jidPtr(), user.ManagementRoom, sess.ClientId, sess.ClientToken, sess.ServerToken, sess.EncKey, sess.MacKey, user.MXID)