func initdb() { // retries for i := 0; i < 5; i++ { s, err := rethink.Connect(rethink.ConnectOpts{ Address: dbAddr, Database: dbName, }) if err != nil { log.Printf("unable to connect; retrying: %s", err) time.Sleep(2 * time.Second) continue } session = s } if session == nil { log.Fatalf("unable to get database connection") } // Create database rethink.DBCreate(dbName).Run(session) // Check if table exists _, err := rethink.Table(tblVisits).Run(session) if err != nil { // If not, create it if _, err = rethink.DB(dbName).TableCreate(tblVisits).Run(session); err != nil { log.Fatalf("error creating table: %s", err) } } }
func init() { Log = logrus.New() session, err := r.Connect(r.ConnectOpts{ Address: "172.17.8.150:32768", Database: "gettingstarted", }) if err != nil { log.WithFields(log.Fields{ "Error": err, }).Error("Error getting rethink") } // if err = r.DbCreate("gettingstarted").Run(session).Exec(); err != nil { resp, err := r.DBCreate("gettingstarted").RunWrite(session) if err != nil { log.WithFields(log.Fields{ "Error": err, }).Error("Error creating db") } fmt.Printf("RESP: %+v\n", resp.DBsCreated) // if err = r.TableCreate("bookmarks").Run(session).Exec(); err != nil { resp, err = r.DB("gettingstarted").TableCreate("bookmarks").RunWrite(session) if err != nil { log.WithFields(log.Fields{ "Error": err, }).Error("Error creating table") } fmt.Printf("RESP %+v\n", resp.TablesCreated) }
func NewManager(addr string, database string, authKey string, client *dockerclient.DockerClient, disableUsageInfo bool, authenticator auth.Authenticator) (Manager, error) { session, err := r.Connect(r.ConnectOpts{ Address: addr, Database: database, AuthKey: authKey, MaxIdle: 10, }) if err != nil { return nil, err } log.Info("checking database") r.DBCreate(database).Run(session) m := &DefaultManager{ database: database, authKey: authKey, session: session, authenticator: authenticator, store: store, client: client, storeKey: storeKey, disableUsageInfo: disableUsageInfo, } m.initdb() m.init() return m, nil }
func (d *Db) Setup() (*Db, error) { db := d.Name tables := []string{"hourly_state", "daily_summary", "hourly_summary"} rSession, err := r.Connect(r.ConnectOpts{ Address: d.Address, Database: db, }) if err != nil { return nil, err } d.Session = rSession log.Printf("Setting up database..") _, err = r.DBCreate(db).Run(rSession) if err != nil { log.Printf("Database already exists. Skipping..") } for _, tbl := range tables { log.Printf("Creating table %v", tbl) _, err = r.DB(db).TableCreate(tbl).Run(rSession) if err != nil { log.Printf("Table %v already exists. Skipping..", tbl) } } return d, nil }
func init() { flag.Parse() err := gcfg.ReadFileInto(&cfg, *configFile) if err != nil { log.Fatal(err) } session, err = r.Connect(r.ConnectOpts{ Address: cfg.Database.Host + ":" + cfg.Database.Port, //localhost:28015 Database: cfg.Database.DB, //DB: cats }) if err != nil { log.Fatal("Could not connect") } res, err := r.DBCreate(cfg.Database.DB).RunWrite(session) if err != nil { log.Println(err.Error()) } fmt.Printf("%d DB created\n", res.DBsCreated) r.DB(cfg.Database.DB).TableCreate("instacat").Run(session) log.Println("Create table instacat.") r.Table("instacat").IndexCreate("time").Run(session) log.Println("Create index time.") r.Table("instacat").IndexCreate("place", r.IndexCreateOpts{Geo: true}).Run(session) log.Println("Create index place.") }
// Cp copies all data from a database to another func Cp(conn *r.Session, src, dest string, force bool) { fmt.Println("Copying db", src, "to", dest) exists := false for _, db := range Ls(conn) { if db == dest { exists = true } } if exists { fmt.Print("Destination db ", dest, " already exists") if force { fmt.Println(", overwriting") } else { fmt.Println() return } } if !exists { _, err := r.DBCreate(dest).Run(conn) if err != nil { fmt.Println("Couldn't create db", dest) panic(err) } } for _, v := range LsTables(conn, src) { CpTable(conn, src, v, dest, v, true) } }
// InitDBs prepares a RethinkDB instance to be used by rethinkClient. // rethinkClients will error if they are pointed at databases that haven't had // InitDBs run on them // InitDBs should only be run once per instance of RethinkDB, it will error if // it's called a second time. func InitDBs(address string, databaseName string) error { session, err := gorethink.Connect(gorethink.ConnectOpts{Address: address}) if err != nil { return err } if _, err := gorethink.DBCreate(databaseName).RunWrite(session); err != nil { return err } for _, table := range tables { tableCreateOpts, ok := tableToTableCreateOpts[table] if ok { if _, err := gorethink.DB(databaseName).TableCreate(table, tableCreateOpts...).RunWrite(session); err != nil { return err } } else { if _, err := gorethink.DB(databaseName).TableCreate(table).RunWrite(session); err != nil { return err } } } for table, indexes := range tableToIndexes { for _, index := range indexes { if _, err := gorethink.DB(databaseName).Table(table).IndexCreate(index).RunWrite(session); err != nil { return err } } } return nil }
// CreateDatabase creates a new RethinkDB database with then given name. func (a *App) CreateDatabase(name string) error { resp, err := r.DBCreate(name).RunWrite(a.Session) if err != nil { return err } a.Logger.Printf("%d DBs created\n", resp.DBsCreated) return nil }
// CreateDatabase creates a new RethinkDB database with then given name. func CreateDatabase(name string) error { resp, err := rdb.DBCreate(name).RunWrite(RDB) if err != nil { return err } log.Printf("%d DBs created\n", resp.DBsCreated) return nil }
func bootstrapDB(session *r.Session, config Config) { r.DBCreate(config.DatabaseName).Run(session) r.DB(config.DatabaseName).TableCreate("Comment").Run(session) r.DB(config.DatabaseName).TableCreate("Posts").Run(session) r.DB(config.DatabaseName).TableCreate("Sessions").Run(session) r.DB(config.DatabaseName).TableCreate("Sites").Run(session) r.DB(config.DatabaseName).TableCreate("Template").Run(session) r.DB(config.DatabaseName).TableCreate("Users").Run(session) }
func createDB(database string, conn *r.Session) (string, error) { var response interface{} res, _ := r.DBList().Contains(database).Run(conn) res.One(&response) if response == false { return fmt.Sprintf("New database %s created", database), r.DBCreate(database).Exec(conn) } return fmt.Sprintf("Database %s already exists", database), nil }
func main() { config := a.NewConfig("config.gcfg") echo := echo.New() s := slack.New(config.Slack.Token) var session *r.Session session, err := r.Connect(r.ConnectOpts{ Address: fmt.Sprint(config.Database.URL, ":", config.Database.Port), Database: config.Database.Name, MaxIdle: 10, MaxOpen: 10, }) if err != nil { log.Fatalln(err.Error()) } session.SetMaxOpenConns(5) r.DBCreate(config.Database.Name).Exec(session) if err != nil { log.Println(err) } _, err = r.DB(config.Database.Name).TableCreate("quotes").RunWrite(session) if err != nil { fmt.Print(err) } _, err = r.DB(config.Database.Name).TableCreate("activities").RunWrite(session) if err != nil { fmt.Print(err) } // Middleware echo.Use(mw.Logger()) echo.Use(mw.Recover()) appcontext := &a.AppContext{ Slack: s, Config: config, Storage: &storage.Storage{ Name: "quotes", URL: "192.168.10.10", Session: session, }, } go appcontext.Monitor() //Routes a.Route(echo, appcontext) addr := fmt.Sprintf(":%d", config.App.Port) log.Printf("Starting server on: %s", addr) echo.Run(addr) }
func createDatabase(dbName string) { _, err := r.Branch( r.DBList().Contains(dbName), nil, r.DBCreate(dbName), ).Run(Session) if err != nil { panic(err) } }
func createDb(session *r.Session, dbName string) { err := r.DBCreate(dbName).Exec(session) if err != nil { fmt.Printf("%v", err) log.Fatalln(err) return } fmt.Printf("%v DB created.\n", dbName) }
func (c *Connection) InitDatabase() { r.DBCreate("magnet").Exec(c.session) r.TableCreate("users").Exec(c.session) r.TableCreate("bookmarks").Exec(c.session) _, err := r.DB("magnet").Table("bookmarks").IndexCreate("Created").RunWrite(c.session) if err != nil { log.Printf("Error creating index: %s", err) } r.TableCreate("sessions").Exec(c.session) }
// InitDBs prepares a RethinkDB instance to be used by the rethink server. // Rethink servers will error if they are pointed at databases that haven't had InitDBs run on them. // InitDBs is idempotent (unless rethink dies in the middle of the function) func InitDBs(address string, databaseName string) error { session, err := gorethink.Connect(gorethink.ConnectOpts{Address: address}) if err != nil { return err } if _, err := gorethink.DBCreate(databaseName).RunWrite(session); err != nil { if _, ok := err.(gorethink.RQLRuntimeError); ok { return nil } return err } for _, table := range tables { tableCreateOpts, ok := tableToTableCreateOpts[table] if ok { if _, err := gorethink.DB(databaseName).TableCreate(table, tableCreateOpts...).RunWrite(session); err != nil { return err } } else { if _, err := gorethink.DB(databaseName).TableCreate(table).RunWrite(session); err != nil { return err } } } for table, indexes := range tableToIndexes { for _, index := range indexes { if _, err := gorethink.DB(databaseName).Table(table).IndexCreate(index).RunWrite(session); err != nil { return err } } } if _, err := gorethink.DB(databaseName).Table(jobInfosTable).IndexCreateFunc( pipelineNameAndInputIndex, func(row gorethink.Term) interface{} { return []interface{}{ row.Field("pipeline_name"), row.Field("input").Field("repo").Field("name"), row.Field("input").Field("id"), } }).RunWrite(session); err != nil { return err } if _, err := gorethink.DB(databaseName).Table(jobInfosTable).IndexCreateFunc( inputIndex, func(row gorethink.Term) interface{} { return []interface{}{ row.Field("input").Field("repo").Field("name"), row.Field("input").Field("id"), } }).RunWrite(session); err != nil { return err } return nil }
func setupDB(t *testing.T) (sess *r.Session, dbName string, err error) { dbName = fmt.Sprintf("db%d", int32(time.Now().Unix())) sess, err = r.Connect(r.ConnectOpts{ Address: "localhost:28015", Database: dbName, }) if err != nil { return } _, err = r.DBCreate(dbName).Run(sess) if err != nil { t.Errorf("couldn't create table, err: %s ", err) return } r.DB(dbName).Wait().Run(sess) r.DB(dbName).TableCreate("test1", r.TableCreateOpts{PrimaryKey: "id"}).Exec(sess) r.DB(dbName).TableCreate("test2", r.TableCreateOpts{PrimaryKey: "id"}).Exec(sess) res, err := r.DB(dbName).TableList().Run(sess) if err != nil { t.Errorf("couldn't load table list, err: %s ", err) return } var tables []interface{} if err = res.All(&tables); err != nil { t.Errorf("couldn't load table list, err: %s ", err) return } if len(tables) != 2 { t.Errorf("table list off, %d %v ", len(tables), tables) return } for idx, n := range names { var rec = struct { Name string Age int }{Name: n, Age: 56 + idx} r.DB(dbName).Table("test1").Insert(rec).RunWrite(sess) } sess.Use(dbName) return }
// InitBookerDB intis the primary DB func InitBookerDB() { s, err := rdb.Connect(rdb.ConnectOpts{ Address: "localhost:28015", }) if err != nil { log.Println(err.Error()) } defer s.Close() resp, err := rdb.DBCreate("Booker").RunWrite(s) if err != nil { log.Println(err.Error()) } log.Printf("%d DBs created\n", resp.DBsCreated) }
func (a *API) initDB() error { _, err := r.DB(a.dbName).Run(a.session) // create db if strings.Contains(err.Error(), "does not exist") { if _, err := r.DBCreate(a.dbName).RunWrite(a.session); err != nil { return err } log.Debugf("database created: name=%s", a.dbName) } return nil }
func checkDatabase() { session := connect() defer killSession(session) databases, err := r.DBList().Run(session) var result string for databases.Next(&result) { if result == databaseName { return } } _, err = r.DBCreate(databaseName).Run(session) LogError(err) }
// InitDBs prepares a RethinkDB instance to be used by rethinkClient. // rethinkClients will error if they are pointed at databases that haven't had // InitDBs run on them // InitDBs should only be run once per instance of RethinkDB, it will error if // it's called a second time. func InitDBs(address string, databaseName string) error { session, err := gorethink.Connect(gorethink.ConnectOpts{Address: address}) if err != nil { return err } if _, err := gorethink.DBCreate(databaseName).RunWrite(session); err != nil { return err } if _, err := gorethink.DB(databaseName).TableCreate(runTable).RunWrite(session); err != nil { return err } if _, err := gorethink.DB(databaseName).TableCreate(statusTable).RunWrite(session); err != nil { return err } if _, err := gorethink.DB(databaseName).TableCreate( containerTable, gorethink.TableCreateOpts{ PrimaryKey: "container_id", }, ).RunWrite(session); err != nil { return err } if _, err := gorethink.DB(databaseName).TableCreate(logTable).RunWrite(session); err != nil { return err } if _, err := gorethink.DB(databaseName).TableCreate(pfsCommitMappingTable).RunWrite(session); err != nil { return err } if _, err := gorethink.DB(databaseName).Table(statusTable). IndexCreate("pipeline_run_id").RunWrite(session); err != nil { return err } if _, err := gorethink.DB(databaseName).Table(containerTable). IndexCreate("pipeline_run_id").RunWrite(session); err != nil { return err } if _, err := gorethink.DB(databaseName).Table(logTable). IndexCreate("pipeline_run_id").RunWrite(session); err != nil { return err } if _, err := gorethink.DB(databaseName).Table(pfsCommitMappingTable). IndexCreate("input_commit_id").RunWrite(session); err != nil { return err } return nil }
func main() { session, err := r.Connect(r.ConnectOpts{ Address: "localhost:32769", }) if err != nil { log.Fatalln(err.Error()) } r.DBCreate("db").RunWrite(session) r.DB("db").TableCreate("test").RunWrite(session) r.DB("db").Table("test").Delete().RunWrite(session) t := r.DB("db").Table("test") cur, err := t.Changes().Run(session) if err != nil { log.Fatalln(err.Error()) } //ch := make(chan Test) go func() { counter := 0 for { time.Sleep(2 * time.Second) wr, err := t.Insert(Test{ "foo", "bar", counter, }).RunWrite(session) if err != nil { log.Fatal(err) } log.Println(wr.Changes) counter = counter + 1 } }() for { var response Test for cur.Next(&response) { log.Println(response) } } }
// InitDBs prepares a RethinkDB instance to be used by the rethink server. // Rethink servers will error if they are pointed at databases that haven't had InitDBs run on them. func InitDBs(address string, databaseName string) error { session, err := connect(address) if err != nil { return err } if _, err := gorethink.DBCreate(databaseName).RunWrite(session); err != nil { return err } for _, table := range tables { tableCreateOpts, ok := tableToTableCreateOpts[table] if ok { if _, err := gorethink.DB(databaseName).TableCreate(table, tableCreateOpts...).RunWrite(session); err != nil { return err } } else { if _, err := gorethink.DB(databaseName).TableCreate(table).RunWrite(session); err != nil { return err } } } // Create indexes if _, err := gorethink.DB(databaseName).Table(jobInfosTable).IndexCreate(pipelineNameIndex).RunWrite(session); err != nil { return err } if _, err := gorethink.DB(databaseName).Table(jobInfosTable).IndexCreate(commitIndex).RunWrite(session); err != nil { return err } if _, err := gorethink.DB(databaseName).Table(jobInfosTable).IndexCreateFunc( pipelineNameAndCommitIndex, func(row gorethink.Term) interface{} { return []interface{}{ row.Field(pipelineNameIndex), row.Field(commitIndex), } }).RunWrite(session); err != nil { return err } if _, err := gorethink.DB(databaseName).Table(pipelineInfosTable).IndexCreate(pipelineShardIndex).RunWrite(session); err != nil { return err } return nil }
// NewRepoFromSession creates new repo func NewRepoFromSession(sess *gorethink.Session) blogalert.Repository { gorethink.DBCreate(Database).RunWrite(sess) gorethink.DB(Database).TableCreate(ArticleTable).RunWrite(sess) gorethink.DB(Database).TableCreate(BlogTable).RunWrite(sess) gorethink.DB(Database).TableCreate(SubscriptionTable).RunWrite(sess) gorethink.DB(Database).TableCreate(ArticleReadTable).RunWrite(sess) gorethink.DB(Database).Table(ArticleTable).IndexCreate("blog") gorethink.DB(Database).Table(ArticleTable).IndexCreate("ts") gorethink.DB(Database).Table(SubscriptionTable).IndexCreate("uid") gorethink.DB(Database).Table(SubscriptionTable).IndexCreate("blog") gorethink.DB(Database).Table(ArticleReadTable).IndexCreate("ts") gorethink.DB(Database).Table(ArticleReadTable).IndexCreate("uid") gorethink.DB(Database).Table(ArticleReadTable).IndexCreate("blog") return &repo{ session: sess, cache: newCache(), } }
func InitDB(dbName string) (session *r.Session, err error) { var cursor *r.Cursor session, err = r.Connect(r.ConnectOpts{ Address: os.Getenv("RETHINKDB_URL"), Database: dbName, MaxIdle: 10, Timeout: time.Second * 10, }) if err != nil { l.Println("Connect", err) return } cursor, err = r.DBList().Contains(dbName).Do(func(row r.Term) r.Term { return r.Branch( row.Eq(true), nil, r.DBCreate(dbName), ) }).Run(session) defer cursor.Close() for _, table := range tables { cursor, err = r.DB(dbName).TableList().Contains(table.name).Do(func(row r.Term) r.Term { return r.Branch( row.Eq(true), nil, r.DB(dbName).TableCreate(table.name, table.options), ) }).Run(session) defer cursor.Close() if err != nil { return } } return }
func setup() { dbs, err := r.DBList().Run(session) if err != nil { log.Fatalln(err.Error()) } defer dbs.Close() var response []string dbs.All(&response) log.Println(response) log.Println(contains(response, "test")) if contains(response, "recipes") { log.Println("DB already exists") } else { _, err := r.DBCreate("recipes").RunWrite(session) if err != nil { log.Fatalln(err.Error()) } } tables, err := r.DB("recipes").TableList().Run(session) if err != nil { log.Fatalln(err.Error()) } defer tables.Close() var tableList []string err = tables.All(&tableList) if contains(tableList, "recipes") { log.Println("Table already exists") } else { _, err := r.DB("recipes").TableCreate("recipes").RunWrite(session) if err != nil { log.Fatalln(err.Error()) } } }
func createUserDB(session *r.Session) { DBName = "authinator_test" resp, err := r.DBCreate(DBName).RunWrite(session) if err != nil { fmt.Println(err) } fmt.Printf("%d DB created\n", resp.DBsCreated) r.DB(DBName).TableCreate(TableName).Exec(session) fmt.Printf("Table created\n") dresp, err := r.DB(DBName).Table(TableName).Delete().RunWrite(session) if err != nil { fmt.Println(err) } fmt.Printf("%d rows deleted\n", dresp.Deleted) }
// Rethink inialiization func Rethink() { var err error // check credentials if Config.Rethink.Host == "" || Config.Rethink.Db == "" { log.Fatalln(errors.New("rethink host or db not defined")) } RSession, err = r.Connect(r.ConnectOpts{ Address: Config.Rethink.Host, Database: Config.Rethink.Db, MaxOpen: Config.Rethink.MaxOpen, }) if err != nil { log.Fatalln(err) return } r.DBCreate(Config.Rethink.Db).Run(RSession) }
func runCmdMigrate(cmd *cobra.Command, args []string) { session, err := r.Connect(r.ConnectOpts{ Address: migrateOpts.ConnectionAddr, }) if err != nil { fmt.Printf("Opening RethinkDB session failed: %s", err) os.Exit(1) } resp, err := r.DBCreate(users.DBName).RunWrite(session) if err != nil { fmt.Println(err) } fmt.Printf("%d DB created\n", resp.DBsCreated) r.DB(users.DBName).TableCreate(users.TableName).Exec(session) fmt.Printf("Table created\n") }
func createDatabaseIfNotExists(name string) error { // Check if the database exists. cur, err := r.DBList().Run(Session) if err != nil { return err } dbName := "" for cur.Next(&dbName) { if dbName == name { return nil } } // Create the database. log.L.Infof("Database: creating database %s", name) _, err = r.DBCreate(name).RunWrite(Session) if err != nil { return err } return nil }