Beispiel #1
0
// reset all migration
// run all migration's down function
func Reset() error {
	sm := sortMap(migrationMap)
	i := 0
	for j := len(sm) - 1; j >= 0; j-- {
		v := sm[j]
		if isRollBack(v.name) {
			beego.Info("skip the", v.name)
			time.Sleep(1 * time.Second)
			continue
		}
		beego.Info("start reset:", v.name)
		v.m.Reset()
		v.m.Down()
		err := v.m.Exec(v.name, "down")
		if err != nil {
			beego.Error("execute error:", err)
			time.Sleep(2 * time.Second)
			return err
		}
		i++
		beego.Info("end reset:", v.name)
	}
	beego.Info("total success reset:", i, " migration")
	time.Sleep(2 * time.Second)
	return nil
}
Beispiel #2
0
func (c *ApiController) Get() {
	url := c.Input().Get("url")
	resp := utils.GetUrl(url, nil)
	html := resp.String()
	beego.Info(fmt.Sprint("Get from url[%s] return html length: [%d]", url, len(html)))
	var parser utils.ZingParser
	err, album := parser.ToAlbum(url, html)
	if err != nil {
		beego.Error(err.Error())
		return
	}
	staticDir := beego.AppPath + "/" + beego.StaticDir["/file"]
	album.Folder = staticDir + "/" + album.Title
	// if err := os.Mkdir(album.Folder, 0777); err != nil {
	// 	beego.Error(err.Error())
	// 	return
	// }
	beego.Info("Album folder: " + album.Folder + ".zip")
	zfile, err := os.Create(album.Folder + ".zip")
	if err != nil {
		beego.Error(err.Error())
		return
	}
	w := zip.NewWriter(zfile)
	// w := zip.NewWriter(c.Ctx.ResponseWriter)
	// album.Song = album.Song[0:1]
	for i, song := range album.Song {
		beego.Info("Downloading: " + song.Source)
		song.Filename = song.Title + " - " + song.Performer + "." + song.Type
		// song.Filename = album.Folder + "/" + song.Filename
		resp := grequests.Get(song.Source, nil)
		beego.Info("Download status:", resp.StatusCode)
		if resp.Error != nil {
			beego.Error(resp.Error.Error())
		} else {
			beego.Info(fmt.Sprintf("Downloaded %s. Done: %d/%d.", song.Source, i+1, len(album.Song)))
			wr, err := w.Create(song.Filename)
			if err == nil {
				_, err = wr.Write(resp.Bytes())
			}
			if err != nil {
				beego.Error(err.Error())
			}
		}
	}
	// c.Ctx.Output.Header("Content-Description", "File Transfer")
	// c.Ctx.Output.Header("Content-Type", "application/octet-stream")
	// c.Ctx.Output.Header("Content-Transfer-Encoding", "binary")
	// c.Ctx.Output.Header("Expires", "0")
	// c.Ctx.Output.Header("Cache-Control", "must-revalidate")
	// c.Ctx.Output.Header("Pragma", "public")
	// c.Ctx.Output.Header("Content-Disposition", "attachment; filename="+album.Title+".zip")
	if err := w.Close(); err != nil {
		beego.Error(err.Error())
		return
	}
	c.Data["json"] = `{"success":"true"}`
	c.ServeJson()
}
Beispiel #3
0
//rollback the migration by the name
func Rollback(name string) error {
	if v, ok := migrationMap[name]; ok {
		beego.Info("start rollback")
		v.Reset()
		v.Down()
		err := v.Exec(name, "down")
		if err != nil {
			beego.Error("execute error:", err)
			time.Sleep(2 * time.Second)
			return err
		}
		beego.Info("end rollback")
		time.Sleep(2 * time.Second)
		return nil
	} else {
		beego.Error("not exist the migrationMap name:" + name)
		time.Sleep(2 * time.Second)
		return errors.New("not exist the migrationMap name:" + name)
	}
}
Beispiel #4
0
// execute the sql already add in the sql
func (m *Migration) Exec(name, status string) error {
	o := orm.NewOrm()
	for _, s := range m.sqls {
		beego.Info("exec sql:", s)
		r := o.Raw(s)
		_, err := r.Exec()
		if err != nil {
			return err
		}
	}
	return m.addOrUpdateRecord(name, status)
}
Beispiel #5
0
// upgrate the migration from lasttime
func Upgrade(lasttime int64) error {
	sm := sortMap(migrationMap)
	i := 0
	for _, v := range sm {
		if v.created > lasttime {
			beego.Info("start upgrade", v.name)
			v.m.Reset()
			v.m.Up()
			err := v.m.Exec(v.name, "up")
			if err != nil {
				beego.Error("execute error:", err)
				time.Sleep(2 * time.Second)
				return err
			}
			beego.Info("end upgrade:", v.name)
			i++
		}
	}
	beego.Info("total success upgrade:", i, " migration")
	time.Sleep(2 * time.Second)
	return nil
}
Beispiel #6
0
func isRollBack(name string) bool {
	o := orm.NewOrm()
	var maps []orm.Params
	num, err := o.Raw("select * from migrations where `name` = ? order by id_migration desc", name).Values(&maps)
	if err != nil {
		beego.Info("get name has error", err)
		return false
	}
	if num <= 0 {
		return false
	}
	if maps[0]["status"] == "rollback" {
		return true
	}
	return false
}