2018-08-13 20:24:44 +00:00
|
|
|
// mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
|
2019-01-12 13:54:04 +00:00
|
|
|
// Copyright (C) 2019 Tulir Asokan
|
2018-08-13 20:24:44 +00:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
2019-03-06 09:59:52 +00:00
|
|
|
_ "github.com/lib/pq"
|
2019-03-06 21:57:38 +00:00
|
|
|
_ "github.com/mattn/go-sqlite3"
|
2019-01-11 19:17:31 +00:00
|
|
|
|
|
|
|
log "maunium.net/go/maulogger/v2"
|
2019-05-16 17:14:32 +00:00
|
|
|
"maunium.net/go/mautrix-whatsapp/database/upgrades"
|
2021-02-13 05:53:35 +00:00
|
|
|
|
|
|
|
"gorm.io/driver/postgres"
|
|
|
|
"gorm.io/driver/sqlite"
|
|
|
|
"gorm.io/gorm"
|
2018-08-13 20:24:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Database struct {
|
2021-02-13 05:53:35 +00:00
|
|
|
*gorm.DB
|
2019-08-25 13:29:35 +00:00
|
|
|
log log.Logger
|
|
|
|
dialect string
|
2018-08-13 20:24:44 +00:00
|
|
|
|
2018-08-22 22:12:26 +00:00
|
|
|
User *UserQuery
|
|
|
|
Portal *PortalQuery
|
|
|
|
Puppet *PuppetQuery
|
|
|
|
Message *MessageQuery
|
2018-08-13 20:24:44 +00:00
|
|
|
}
|
|
|
|
|
2021-01-25 19:01:54 +00:00
|
|
|
func New(dbType string, uri string, baseLog log.Logger) (*Database, error) {
|
2021-02-13 05:53:35 +00:00
|
|
|
|
|
|
|
var conn gorm.Dialector
|
2018-08-13 20:24:44 +00:00
|
|
|
|
2019-07-17 21:06:50 +00:00
|
|
|
if dbType == "sqlite3" {
|
2021-02-13 05:53:35 +00:00
|
|
|
//_, _ = conn.Exec("PRAGMA foreign_keys = ON")
|
|
|
|
conn = sqlite.Open(uri)
|
|
|
|
} else {
|
|
|
|
conn = postgres.Open(uri)
|
|
|
|
}
|
|
|
|
print("no")
|
|
|
|
gdb, err := gorm.Open(conn, &gorm.Config{
|
|
|
|
// Logger: baseLog,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic("failed to connect database")
|
2019-07-17 21:06:50 +00:00
|
|
|
}
|
2018-08-13 20:24:44 +00:00
|
|
|
db := &Database{
|
2021-02-13 05:53:35 +00:00
|
|
|
DB: gdb,
|
2021-01-25 19:01:54 +00:00
|
|
|
log: baseLog.Sub("Database"),
|
2019-08-25 13:29:35 +00:00
|
|
|
dialect: dbType,
|
2018-08-13 20:24:44 +00:00
|
|
|
}
|
|
|
|
db.User = &UserQuery{
|
|
|
|
db: db,
|
2018-08-16 16:20:07 +00:00
|
|
|
log: db.log.Sub("User"),
|
2018-08-13 20:24:44 +00:00
|
|
|
}
|
2018-08-16 12:59:18 +00:00
|
|
|
db.Portal = &PortalQuery{
|
|
|
|
db: db,
|
2018-08-16 16:20:07 +00:00
|
|
|
log: db.log.Sub("Portal"),
|
2018-08-16 12:59:18 +00:00
|
|
|
}
|
|
|
|
db.Puppet = &PuppetQuery{
|
|
|
|
db: db,
|
2018-08-16 16:20:07 +00:00
|
|
|
log: db.log.Sub("Puppet"),
|
2018-08-16 12:59:18 +00:00
|
|
|
}
|
2018-08-22 22:12:26 +00:00
|
|
|
db.Message = &MessageQuery{
|
|
|
|
db: db,
|
|
|
|
log: db.log.Sub("Message"),
|
|
|
|
}
|
2018-08-13 20:24:44 +00:00
|
|
|
return db, nil
|
|
|
|
}
|
|
|
|
|
2019-08-25 13:29:35 +00:00
|
|
|
func (db *Database) Init() error {
|
2021-02-13 05:53:35 +00:00
|
|
|
err := db.AutoMigrate(&Portal{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = db.AutoMigrate(&Puppet{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = db.AutoMigrate(&Message{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = db.AutoMigrate(&mxRegistered{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = db.AutoMigrate(&mxUserProfile{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-25 13:29:35 +00:00
|
|
|
return upgrades.Run(db.log.Sub("Upgrade"), db.dialect, db.DB)
|
2018-08-16 16:20:07 +00:00
|
|
|
}
|
|
|
|
|
2018-08-13 20:24:44 +00:00
|
|
|
type Scannable interface {
|
|
|
|
Scan(...interface{}) error
|
|
|
|
}
|