mi-v1/cmd/mi-init/main.go

61 lines
1.1 KiB
Go
Raw Permalink Normal View History

2020-01-11 21:06:44 +00:00
package main
import (
"flag"
"log"
"github.com/facebookgo/flagenv"
"github.com/hashicorp/go-multierror"
_ "github.com/joho/godotenv/autoload"
r "gopkg.in/rethinkdb/rethinkdb-go.v6"
"within.website/mi/rethink"
)
var (
rethinkDBURL = flag.String("rethinkdb-url", "rethinkdb2://admin@127.0.0.1:28015/mi", "RethinkDB URL")
)
func main() {
flagenv.Parse()
flag.Parse()
session, err := rethink.GetSession(*rethinkDBURL)
if err != nil {
log.Fatal(err)
}
err = r.DB("mi").Info().Exec(session)
2020-01-12 02:51:33 +00:00
if err != nil {
if err := r.DBCreate("mi").Exec(session); err != nil {
log.Fatal(err)
}
2020-01-11 21:06:44 +00:00
}
tables := []string{
"mastodon",
"blogposts",
"reddit_comments",
"reddit_posts",
2020-01-11 22:24:59 +00:00
"tweets",
2020-01-12 02:29:41 +00:00
"switches",
2020-01-11 21:06:44 +00:00
}
var result error
for _, tbl := range tables {
2020-01-12 02:51:33 +00:00
if err := r.DB("mi").Table(tbl).Info().Exec(session); err != nil {
if err := r.DB("mi").TableCreate(tbl).Exec(session); err != nil {
result = multierror.Append(result, err)
} else {
log.Printf("created table %s", tbl)
}
2020-01-11 21:06:44 +00:00
}
}
if result != nil {
log.Fatal(result)
}
2020-01-15 22:38:22 +00:00
r.DB("mi").Table("switches").IndexCreate("started_at").Exec(session)
2020-01-11 21:06:44 +00:00
}