mirror of
https://github.com/ergochat/ergo.git
synced 2025-12-20 02:00:11 -08:00
db: Remove SQLite db, hopefully looking up clients still works.Channel persistence is broken by this, will fix it later.
This commit is contained in:
parent
969eed394f
commit
ae69ef5cd6
7 changed files with 98 additions and 197 deletions
|
|
@ -4,79 +4,52 @@
|
|||
package irc
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"github.com/tidwall/buntdb"
|
||||
)
|
||||
|
||||
const (
|
||||
// 'version' of the database schema
|
||||
keySchemaVersion = "db.version"
|
||||
// key for the primary salt used by the ircd
|
||||
keySalt = "crypto.salt"
|
||||
)
|
||||
|
||||
func InitDB(buntpath string, path string) {
|
||||
// InitDB creates the database.
|
||||
func InitDB(path string) {
|
||||
// prepare kvstore db
|
||||
os.Remove(buntpath)
|
||||
store, err := buntdb.Open(buntpath)
|
||||
//TODO(dan): fail if already exists instead? don't want to overwrite good data
|
||||
os.Remove(path)
|
||||
store, err := buntdb.Open(path)
|
||||
if err != nil {
|
||||
log.Fatal(fmt.Sprintf("Failed to open datastore: %s", err.Error()))
|
||||
}
|
||||
defer store.Close()
|
||||
|
||||
err = store.Update(func(tx *buntdb.Tx) error {
|
||||
// set base db salt
|
||||
salt, err := NewSalt()
|
||||
encodedSalt := base64.StdEncoding.EncodeToString(salt)
|
||||
if err != nil {
|
||||
log.Fatal("Could not generate cryptographically-secure salt for the user:", err.Error())
|
||||
}
|
||||
tx.Set(keySalt, encodedSalt, nil)
|
||||
|
||||
// set schema version
|
||||
tx.Set(keySchemaVersion, "1", nil)
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
log.Fatal("Could not save bunt store:", err.Error())
|
||||
}
|
||||
|
||||
// prepare SQLite db
|
||||
os.Remove(path)
|
||||
db := OpenDB(path)
|
||||
defer db.Close()
|
||||
_, err = db.Exec(`
|
||||
CREATE TABLE channel (
|
||||
name TEXT NOT NULL UNIQUE,
|
||||
flags TEXT DEFAULT '',
|
||||
key TEXT DEFAULT '',
|
||||
topic TEXT DEFAULT '',
|
||||
user_limit INTEGER DEFAULT 0,
|
||||
ban_list TEXT DEFAULT '',
|
||||
except_list TEXT DEFAULT '',
|
||||
invite_list TEXT DEFAULT '')`)
|
||||
if err != nil {
|
||||
log.Fatal("initdb error: ", err)
|
||||
}
|
||||
}
|
||||
|
||||
// UpgradeDB upgrades the datastore to the latest schema.
|
||||
func UpgradeDB(path string) {
|
||||
db := OpenDB(path)
|
||||
alter := `ALTER TABLE channel ADD COLUMN %s TEXT DEFAULT ''`
|
||||
cols := []string{"ban_list", "except_list", "invite_list"}
|
||||
for _, col := range cols {
|
||||
_, err := db.Exec(fmt.Sprintf(alter, col))
|
||||
if err != nil {
|
||||
log.Fatal("updatedb error: ", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func OpenDB(path string) *sql.DB {
|
||||
db, err := sql.Open("sqlite3", path)
|
||||
if err != nil {
|
||||
log.Fatal("open db error: ", err)
|
||||
}
|
||||
return db
|
||||
return
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue