Ejemplo n.º 1
0
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
}
Ejemplo n.º 2
0
func createDatabase(dbName string) {
	_, err := r.Branch(
		r.DBList().Contains(dbName),
		nil,
		r.DBCreate(dbName),
	).Run(Session)
	if err != nil {
		panic(err)
	}
}
Ejemplo n.º 3
0
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)
}
Ejemplo n.º 4
0
Archivo: ls.go Proyecto: andreis/rekit
// Ls returns all db names
func Ls(conn *r.Session) []string {
	var dbs []string
	term, err := r.DBList().Run(conn)
	if err != nil {
		fmt.Println("Couldn't run DBList!", err)
		panic(err)
	}
	err = term.All(&dbs)
	if err != nil {
		fmt.Println("Ls couldn't unfold a term")
		panic(err)
	}
	return dbs
}
Ejemplo n.º 5
0
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
}
Ejemplo n.º 6
0
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())
		}
	}
}
Ejemplo n.º 7
0
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
}