aboutsummaryrefslogtreecommitdiffstats
path: root/golang/cmd/fatcatd/reinit.go
blob: b7cfa46888dc2392f017eec0030628fbcf73d4f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

package main

import (
    "github.com/spf13/viper"
    log "github.com/sirupsen/logrus"
    "github.com/go-pg/pg"
    "github.com/spf13/cobra"
    "github.com/gobuffalo/packr"
)

var reinitCmd = &cobra.Command{
    Use:    "reinit [options]",
    Short:  "Reset database schema",
    Run:    func(cmd *cobra.Command, args[] string) {
        main_reinit()
    },
}

func main_reinit() {

    box := packr.NewBox("../../sql")
    sql_schema, err := box.MustString("fatcat-schema.sql")
    if err != nil {
        log.Panicf("finding SQL file: {}", err)
    }

    db_options, err := pg.ParseURL(viper.GetString("db_url"))
    if err != nil {
        log.Panicf("parsing DB string: {}", err)
    }
    db := pg.Connect(db_options)
    defer db.Close()

    log.Info("Starting load...")
    _, err = db.Exec(sql_schema)
    if err != nil {
        log.Fatalf("Error loading SQL: {}", err)
    }
    log.Info("Success!")

}