Пример #1
0
func getDatabaseVersion(opts *r.ConnectOpts, session *r.Session) (int, error) {
	cursor, err := r.Branch(
		r.DB(opts.Database).TableList().Contains("migration_status"),
		r.DB(opts.Database).Table("migration_status").Get("revision").Field("value"),
		r.DB(opts.Database).TableCreate("migration_status").Do(func() r.Term {
			return r.DB(opts.Database).Table("migration_status").Insert(map[string]interface{}{
				"id":    "revision",
				"value": -1,
			}).Do(func() int {
				return -1
			})
		}),
	).Run(session)
	if err != nil {
		return -1, err
	}
	defer cursor.Close()
	var result int
	if err := cursor.One(&result); err != nil {
		return -1, err
	}

	return result, nil
}
Пример #2
0
)

type migration struct {
	Revision int
	Name     string
	Migrate  func(*r.ConnectOpts) []r.Term
	Revert   func(*r.ConnectOpts) []r.Term
}

var migrations = []migration{
	{
		Revision: 0,
		Name:     "create_tables",
		Migrate: func(opts *r.ConnectOpts) []r.Term {
			return []r.Term{
				r.DB(opts.Database).TableCreate("accounts"),
				r.Table("accounts").IndexCreate("alt_email"),
				r.Table("accounts").IndexCreate("main_address"),
				r.DB(opts.Database).TableCreate("addresses"),
				r.DB(opts.Database).TableCreate("applications"),
				r.DB(opts.Database).TableCreate("emails"),
				r.DB(opts.Database).TableCreate("keys"),
				r.DB(opts.Database).TableCreate("labels"),
				r.DB(opts.Database).TableCreate("resources"),
				r.DB(opts.Database).TableCreate("threads"),
				r.DB(opts.Database).TableCreate("tokens"),
			}
		},
		Revert: func(opts *r.ConnectOpts) []r.Term {
			return []r.Term{
				r.DB(opts.Database).TableDrop("accounts"),