3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-12-28 05:32:49 +01:00
ergo/irc/database.go

49 lines
1.0 KiB
Go
Raw Normal View History

package irc
import (
"database/sql"
2014-03-08 03:14:02 +01:00
"fmt"
_ "github.com/mattn/go-sqlite3"
"log"
"os"
)
func InitDB(path string) {
os.Remove(path)
db := OpenDB(path)
defer db.Close()
_, err := db.Exec(`
CREATE TABLE channel (
name TEXT NOT NULL UNIQUE,
2014-03-08 03:14:02 +01:00
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 {
2014-03-06 08:07:55 +01:00
log.Fatal("initdb error: ", err)
}
}
2014-03-08 03:14:02 +01:00
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 {
2014-03-06 08:07:55 +01:00
log.Fatal("open db error: ", err)
}
return db
}