changed sqlite to postgresql

This commit is contained in:
Dominic Renner 2019-03-06 10:59:52 +01:00
parent 8c2cb26fd7
commit 6547a2ce1f
5 changed files with 14 additions and 13 deletions

View file

@ -19,7 +19,7 @@ package database
import ( import (
"database/sql" "database/sql"
_ "github.com/mattn/go-sqlite3" _ "github.com/lib/pq"
log "maunium.net/go/maulogger/v2" log "maunium.net/go/maulogger/v2"
) )
@ -35,7 +35,8 @@ type Database struct {
} }
func New(file string) (*Database, error) { 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 { if err != nil {
return nil, err return nil, err
} }

View file

@ -40,7 +40,7 @@ func (mq *MessageQuery) CreateTable() error {
jid VARCHAR(255), jid VARCHAR(255),
mxid VARCHAR(255) NOT NULL UNIQUE, mxid VARCHAR(255) NOT NULL UNIQUE,
sender VARCHAR(25) NOT NULL, sender VARCHAR(25) NOT NULL,
content BLOB NOT NULL, content bytea NOT NULL,
PRIMARY KEY (chat_jid, chat_receiver, jid), PRIMARY KEY (chat_jid, chat_receiver, jid),
FOREIGN KEY (chat_jid, chat_receiver) REFERENCES portal(jid, receiver) FOREIGN KEY (chat_jid, chat_receiver) REFERENCES portal(jid, receiver)

View file

@ -70,7 +70,7 @@ func (pq *PortalQuery) CreateTable() error {
avatar VARCHAR(255) NOT NULL, avatar VARCHAR(255) NOT NULL,
PRIMARY KEY (jid, receiver), PRIMARY KEY (jid, receiver),
FOREIGN KEY (receiver) REFERENCES user(mxid) FOREIGN KEY (receiver) REFERENCES whatsapp_user(mxid)
)`) )`)
return err return err
} }

View file

@ -34,7 +34,7 @@ func (pq *PuppetQuery) CreateTable() error {
jid VARCHAR(25) PRIMARY KEY, jid VARCHAR(25) PRIMARY KEY,
avatar VARCHAR(255), avatar VARCHAR(255),
displayname VARCHAR(255), displayname VARCHAR(255),
name_quality TINYINT name_quality SMALLINT
)`) )`)
return err return err
} }

View file

@ -34,7 +34,7 @@ type UserQuery struct {
} }
func (uq *UserQuery) CreateTable() error { 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, mxid VARCHAR(255) PRIMARY KEY,
jid VARCHAR(25) UNIQUE, jid VARCHAR(25) UNIQUE,
@ -43,8 +43,8 @@ func (uq *UserQuery) CreateTable() error {
client_id VARCHAR(255), client_id VARCHAR(255),
client_token VARCHAR(255), client_token VARCHAR(255),
server_token VARCHAR(255), server_token VARCHAR(255),
enc_key BLOB, enc_key bytea,
mac_key BLOB mac_key bytea
)`) )`)
return err return err
} }
@ -57,7 +57,7 @@ func (uq *UserQuery) New() *User {
} }
func (uq *UserQuery) GetAll() (users []*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 { if err != nil || rows == nil {
return nil return nil
} }
@ -69,7 +69,7 @@ func (uq *UserQuery) GetAll() (users []*User) {
} }
func (uq *UserQuery) GetByMXID(userID types.MatrixUserID) *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 { if row == nil {
return nil return nil
} }
@ -77,7 +77,7 @@ func (uq *UserQuery) GetByMXID(userID types.MatrixUserID) *User {
} }
func (uq *UserQuery) GetByJID(userID types.WhatsAppID) *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 { if row == nil {
return nil return nil
} }
@ -150,7 +150,7 @@ func (user *User) sessionUnptr() (sess whatsapp.Session) {
func (user *User) Insert() { func (user *User) Insert() {
sess := user.sessionUnptr() 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, user.ManagementRoom,
sess.ClientId, sess.ClientToken, sess.ServerToken, sess.EncKey, sess.MacKey) sess.ClientId, sess.ClientToken, sess.ServerToken, sess.EncKey, sess.MacKey)
if err != nil { if err != nil {
@ -160,7 +160,7 @@ func (user *User) Insert() {
func (user *User) Update() { func (user *User) Update() {
sess := user.sessionUnptr() 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, user.jidPtr(), user.ManagementRoom,
sess.ClientId, sess.ClientToken, sess.ServerToken, sess.EncKey, sess.MacKey, sess.ClientId, sess.ClientToken, sess.ServerToken, sess.EncKey, sess.MacKey,
user.MXID) user.MXID)