Cleanup DB work
This commit is contained in:
parent
a40ec4dd78
commit
df646ff087
@ -4,4 +4,4 @@ COPY build/birdbot /usr/bin/birdbot
|
|||||||
|
|
||||||
VOLUME /etc/birdbot
|
VOLUME /etc/birdbot
|
||||||
|
|
||||||
ENTRYPOINT ["/usr/bin/birdbot", "-c=/etc/birdbot/birdbot.yaml"]
|
ENTRYPOINT ["/usr/bin/birdbot", "-c=/etc/birdbot/birdbot.yaml", "-db=/var/lib/birdbot/birdbot.db"]
|
@ -24,4 +24,7 @@ Example:
|
|||||||
docker run -it -v `pwd`:/etc/birdbot yeslayla/birdbot:latest
|
docker run -it -v `pwd`:/etc/birdbot yeslayla/birdbot:latest
|
||||||
```
|
```
|
||||||
|
|
||||||
In this example, your config is in the current directory and call `birdbot.yaml`
|
In this example, your config is in the current directory and call `birdbot.yaml`
|
||||||
|
|
||||||
|
### Persistant Data
|
||||||
|
|
||||||
|
5
main.go
5
main.go
@ -22,10 +22,13 @@ func main() {
|
|||||||
configDir, _ := os.UserConfigDir()
|
configDir, _ := os.UserConfigDir()
|
||||||
|
|
||||||
defaultConfigPath := path.Join(configDir, "birdbot", "config.yaml")
|
defaultConfigPath := path.Join(configDir, "birdbot", "config.yaml")
|
||||||
|
defaultDBPath := path.Join(configDir, "birdbot", "birdbot.db")
|
||||||
|
|
||||||
var config_file string
|
var config_file string
|
||||||
|
var db_file string
|
||||||
var version bool
|
var version bool
|
||||||
flag.StringVar(&config_file, "c", defaultConfigPath, "Path to config file")
|
flag.StringVar(&config_file, "c", defaultConfigPath, "Path to config file")
|
||||||
|
flag.StringVar(&db_file, "db", defaultDBPath, "Path to store persistant data")
|
||||||
flag.BoolVar(&version, "v", false, "List version")
|
flag.BoolVar(&version, "v", false, "List version")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
@ -51,7 +54,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
db := persistence.NewSqlite3Database()
|
db := persistence.NewSqlite3Database(db_file)
|
||||||
if err := db.MigrateUp(); err != nil {
|
if err := db.MigrateUp(); err != nil {
|
||||||
log.Fatal("Failed to migrate db: ", err)
|
log.Fatal("Failed to migrate db: ", err)
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package persistence
|
package persistence
|
||||||
|
|
||||||
|
// Database is an interface used to wrap persistant data
|
||||||
type Database interface {
|
type Database interface {
|
||||||
GetDiscordMessage(id string) (string, error)
|
GetDiscordMessage(id string) (string, error)
|
||||||
SetDiscordMessage(id string, messageID string) error
|
SetDiscordMessage(id string, messageID string) error
|
||||||
|
@ -5,6 +5,8 @@ import (
|
|||||||
"embed"
|
"embed"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
migrate "github.com/rubenv/sql-migrate"
|
migrate "github.com/rubenv/sql-migrate"
|
||||||
@ -17,8 +19,17 @@ type Sqlite3Database struct {
|
|||||||
db *sql.DB
|
db *sql.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSqlite3Database() *Sqlite3Database {
|
// NewSqlite3Database creates a new SqliteDB object
|
||||||
db, err := sql.Open("sqlite3", "./birdbot.db")
|
func NewSqlite3Database(path string) *Sqlite3Database {
|
||||||
|
dir := filepath.Dir(path)
|
||||||
|
if _, err := os.Stat(dir); os.IsNotExist(err) {
|
||||||
|
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
|
||||||
|
log.Printf("failed to create directory for db: %s", err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
db, err := sql.Open("sqlite3", path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("failed to open db: %s", err)
|
log.Printf("failed to open db: %s", err)
|
||||||
return nil
|
return nil
|
||||||
@ -36,6 +47,7 @@ func getMigrations() migrate.MigrationSource {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MigrateUp migrates the DB
|
||||||
func (db *Sqlite3Database) MigrateUp() error {
|
func (db *Sqlite3Database) MigrateUp() error {
|
||||||
|
|
||||||
n, err := migrate.Exec(db.db, "sqlite3", getMigrations(), migrate.Up)
|
n, err := migrate.Exec(db.db, "sqlite3", getMigrations(), migrate.Up)
|
||||||
@ -49,6 +61,7 @@ func (db *Sqlite3Database) MigrateUp() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MigrateUp destroys the DB
|
||||||
func (db *Sqlite3Database) MigrateDown() error {
|
func (db *Sqlite3Database) MigrateDown() error {
|
||||||
|
|
||||||
n, err := migrate.Exec(db.db, "sqlite3", getMigrations(), migrate.Down)
|
n, err := migrate.Exec(db.db, "sqlite3", getMigrations(), migrate.Down)
|
||||||
@ -62,18 +75,23 @@ func (db *Sqlite3Database) MigrateDown() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetDiscordMessage finds a discord message ID from a given local ID
|
||||||
func (db *Sqlite3Database) GetDiscordMessage(id string) (string, error) {
|
func (db *Sqlite3Database) GetDiscordMessage(id string) (string, error) {
|
||||||
|
|
||||||
var messageID string
|
var messageID string
|
||||||
row := db.db.QueryRow("SELECT message_id FROM discord_messages WHERE id = $1", id)
|
row := db.db.QueryRow("SELECT message_id FROM discord_messages WHERE id = $1", id)
|
||||||
|
|
||||||
if err := row.Scan(&messageID); err != nil {
|
if err := row.Scan(&messageID); err != nil {
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
return "", fmt.Errorf("failed to get discord message from sqlite3: %s", err)
|
return "", fmt.Errorf("failed to get discord message from sqlite3: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return messageID, nil
|
return messageID, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetDiscordMessage sets a discord message ID from a given local ID
|
||||||
func (db *Sqlite3Database) SetDiscordMessage(id string, messageID string) error {
|
func (db *Sqlite3Database) SetDiscordMessage(id string, messageID string) error {
|
||||||
|
|
||||||
statement, err := db.db.Prepare("INSERT OR IGNORE INTO discord_messages (id, message_id) VALUES (?, ?)")
|
statement, err := db.db.Prepare("INSERT OR IGNORE INTO discord_messages (id, message_id) VALUES (?, ?)")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user