func (r *Report) saveMySQL() error { res, reserr := datastore.EncodeBlob(&r.Resources) if reserr != nil { return reserr } dat, daterr := datastore.EncodeBlob(&r.Data) if daterr != nil { return daterr } tx, err := datastore.Dbh.Begin() if err != nil { return err } // Up to this point I was going the INSERT or UPDATE without using // MySQL specific syntax, to keep MySQL and any future Postgres // SQL more similar, but now I'm thinking that this should try and // leverage more of each database's capabilities. Thus, here we shall // do the very MySQL-specific INSERT ... ON DUPLICATE KEY UPDATE // syntax. _, err = tx.Exec("INSERT INTO reports (run_id, node_name, start_time, end_time, total_res_count, status, run_list, resources, data, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW()) ON DUPLICATE KEY UPDATE start_time = ?, end_time = ?, total_res_count = ?, status = ?, run_list = ?, resources = ?, data = ?, updated_at = NOW()", r.RunID, r.NodeName, r.StartTime, r.EndTime, r.TotalResCount, r.Status, r.RunList, res, dat, r.StartTime, r.EndTime, r.TotalResCount, r.Status, r.RunList, res, dat) if err != nil { tx.Rollback() return err } tx.Commit() return nil }
func (r *Report) savePostgreSQL() error { res, reserr := datastore.EncodeBlob(&r.Resources) if reserr != nil { return reserr } dat, daterr := datastore.EncodeBlob(&r.Data) if daterr != nil { return daterr } tx, err := datastore.Dbh.Begin() if err != nil { return err } // Up to this point I was going the INSERT or UPDATE without using // MySQL specific syntax, to keep MySQL and any future Postgres // SQL more similar, but now I'm thinking that this should try and // leverage more of each database's capabilities. Thus, here we shall // do the very MySQL-specific INSERT ... ON DUPLICATE KEY UPDATE // syntax. _, err = tx.Exec("SELECT goiardi.merge_reports($1, $2, $3, $4, $5, $6, $7, $8, $9)", r.RunID, r.NodeName, r.StartTime, r.EndTime, r.TotalResCount, r.Status, r.RunList, res, dat) if err != nil { tx.Rollback() return err } tx.Commit() return nil }
func (e *ChefEnvironment) saveEnvironmentPostgreSQL() util.Gerror { dab, daerr := datastore.EncodeBlob(&e.Default) if daerr != nil { return util.CastErr(daerr) } oab, oaerr := datastore.EncodeBlob(&e.Override) if oaerr != nil { return util.CastErr(oaerr) } cvb, cverr := datastore.EncodeBlob(&e.CookbookVersions) if cverr != nil { return util.CastErr(cverr) } tx, err := datastore.Dbh.Begin() if err != nil { gerr := util.CastErr(err) return gerr } _, err = tx.Exec("SELECT goiardi.merge_environments($1, $2, $3, $4, $5)", e.Name, e.Description, dab, oab, cvb) if err != nil { tx.Rollback() gerr := util.CastErr(err) return gerr } tx.Commit() return nil }
func (e *ChefEnvironment) saveEnvironmentMySQL() util.Gerror { dab, daerr := datastore.EncodeBlob(&e.Default) if daerr != nil { return util.CastErr(daerr) } oab, oaerr := datastore.EncodeBlob(&e.Override) if oaerr != nil { return util.CastErr(oaerr) } cvb, cverr := datastore.EncodeBlob(&e.CookbookVersions) if cverr != nil { return util.CastErr(cverr) } tx, err := datastore.Dbh.Begin() if err != nil { return util.CastErr(err) } _, err = tx.Exec("INSERT INTO environments (name, description, default_attr, override_attr, cookbook_vers, created_at, updated_at) VALUES (?, ?, ?, ?, ?, NOW(), NOW()) ON DUPLICATE KEY UPDATE description = ?, default_attr = ?, override_attr = ?, cookbook_vers = ?, updated_at = NOW()", e.Name, e.Description, dab, oab, cvb, e.Description, dab, oab, cvb) if err != nil { tx.Rollback() gerr := util.CastErr(err) return gerr } tx.Commit() return nil }
func (r *Role) savePostgreSQL() error { rlb, rlerr := datastore.EncodeBlob(&r.RunList) if rlerr != nil { return rlerr } erb, ererr := datastore.EncodeBlob(&r.EnvRunLists) if ererr != nil { return ererr } dab, daerr := datastore.EncodeBlob(&r.Default) if daerr != nil { return daerr } oab, oaerr := datastore.EncodeBlob(&r.Override) if oaerr != nil { return oaerr } tx, err := datastore.Dbh.Begin() if err != nil { return err } _, err = tx.Exec("SELECT goiardi.merge_roles($1, $2, $3, $4, $5, $6)", r.Name, r.Description, rlb, erb, dab, oab) if err != nil { tx.Rollback() return err } tx.Commit() return nil }
func (r *Role) saveMySQL() error { rlb, rlerr := datastore.EncodeBlob(&r.RunList) if rlerr != nil { return rlerr } erb, ererr := datastore.EncodeBlob(&r.EnvRunLists) if ererr != nil { return ererr } dab, daerr := datastore.EncodeBlob(&r.Default) if daerr != nil { return daerr } oab, oaerr := datastore.EncodeBlob(&r.Override) if oaerr != nil { return oaerr } tx, err := datastore.Dbh.Begin() if err != nil { return err } _, err = tx.Exec("INSERT INTO roles (name, description, run_list, env_run_lists, default_attr, override_attr, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, NOW(), NOW()) ON DUPLICATE KEY UPDATE description = ?, run_list = ?, env_run_lists = ?, default_attr = ?, override_attr = ?, updated_at = NOW()", r.Name, r.Description, rlb, erb, dab, oab, r.Description, rlb, erb, dab, oab) if err != nil { tx.Rollback() return err } tx.Commit() return nil }
func (db *DataBag) newDBItemPostgreSQL(dbiID string, rawDbagItem map[string]interface{}) (*DataBagItem, error) { rawb, rawerr := datastore.EncodeBlob(&rawDbagItem) if rawerr != nil { return nil, rawerr } dbi := &DataBagItem{ Name: db.fullDBItemName(dbiID), ChefType: "data_bag_item", JSONClass: "Chef::DataBagItem", DataBagName: db.Name, RawData: rawDbagItem, origName: dbiID, dataBagID: db.id, } tx, err := datastore.Dbh.Begin() if err != nil { return nil, err } // make sure this data bag didn't go away while we were doing something // else err = tx.QueryRow("SELECT goiardi.insert_dbi($1, $2, $3, $4, $5)", db.Name, dbi.Name, dbi.origName, db.id, rawb).Scan(&dbi.id) if err != nil { tx.Rollback() return nil, err } tx.Commit() return dbi, nil }
func (n *Node) saveSQL() error { // prepare the complex structures for saving rlb, rlerr := datastore.EncodeBlob(&n.RunList) if rlerr != nil { return rlerr } aab, aaerr := datastore.EncodeBlob(&n.Automatic) if aaerr != nil { return aaerr } nab, naerr := datastore.EncodeBlob(&n.Normal) if naerr != nil { return naerr } dab, daerr := datastore.EncodeBlob(&n.Default) if daerr != nil { return daerr } oab, oaerr := datastore.EncodeBlob(&n.Override) if oaerr != nil { return oaerr } tx, err := datastore.Dbh.Begin() if err != nil { return err } if config.Config.UseMySQL { err = n.saveMySQL(tx, rlb, aab, nab, dab, oab) } else if config.Config.UsePostgreSQL { err = n.savePostgreSQL(tx, rlb, aab, nab, dab, oab) } if err != nil { tx.Rollback() return err } tx.Commit() return nil }
func (s *Sandbox) savePostgreSQL() error { ckb, ckerr := datastore.EncodeBlob(&s.Checksums) if ckerr != nil { return ckerr } tx, err := datastore.Dbh.Begin() if err != nil { return err } _, err = tx.Exec("SELECT goiardi.merge_sandboxes($1, $2, $3, $4)", s.ID, s.CreationTime, ckb, s.Completed) if err != nil { tx.Rollback() return err } tx.Commit() return nil }
func (db *DataBag) newDBItemMySQL(dbiID string, rawDbagItem map[string]interface{}) (*DataBagItem, error) { rawb, rawerr := datastore.EncodeBlob(&rawDbagItem) if rawerr != nil { return nil, rawerr } dbi := &DataBagItem{ Name: db.fullDBItemName(dbiID), ChefType: "data_bag_item", JSONClass: "Chef::DataBagItem", DataBagName: db.Name, RawData: rawDbagItem, origName: dbiID, dataBagID: db.id, } tx, err := datastore.Dbh.Begin() // make sure this data bag didn't go away while we were doing something // else found, ferr := checkForDataBagSQL(tx, db.Name) if ferr != nil { tx.Rollback() return nil, err } else if !found { tx.Rollback() err = fmt.Errorf("aiiiie! The data bag %s was deleted from the db while we were doing something else", db.Name) return nil, err } res, err := tx.Exec("INSERT INTO data_bag_items (name, orig_name, data_bag_id, raw_data, created_at, updated_at) VALUES (?, ?, ?, ?, NOW(), NOW())", dbi.Name, dbi.origName, db.id, rawb) if err != nil { tx.Rollback() return nil, err } did, err := res.LastInsertId() if err != nil { tx.Rollback() return nil, err } dbi.id = int32(did) tx.Commit() return dbi, nil }
func (dbi *DataBagItem) updateDBItemSQL() error { rawb, rawerr := datastore.EncodeBlob(&dbi.RawData) if rawerr != nil { return rawerr } tx, err := datastore.Dbh.Begin() if err != nil { return err } if config.Config.UseMySQL { _, err = tx.Exec("UPDATE data_bag_items SET raw_data = ?, updated_at = NOW() WHERE id = ?", rawb, dbi.id) } else if config.Config.UsePostgreSQL { _, err = tx.Exec("UPDATE goiardi.data_bag_items SET raw_data = $1, updated_at = NOW() WHERE id = $2", rawb, dbi.id) } if err != nil { terr := tx.Rollback() if terr != nil { err = fmt.Errorf("updating data bag item %s in data bag %s had an error '%s', and then rolling back the transaction gave another erorr '%s'", dbi.origName, dbi.DataBagName, err.Error(), terr.Error()) } return err } tx.Commit() return nil }
func (cbv *CookbookVersion) updateCookbookVersionSQL() util.Gerror { // Preparing the complex data structures to be saved defb, deferr := datastore.EncodeBlob(cbv.Definitions) if deferr != nil { gerr := util.Errorf(deferr.Error()) gerr.SetStatus(http.StatusInternalServerError) return gerr } libb, liberr := datastore.EncodeBlob(cbv.Libraries) if liberr != nil { gerr := util.Errorf(liberr.Error()) gerr.SetStatus(http.StatusInternalServerError) return gerr } attb, atterr := datastore.EncodeBlob(cbv.Attributes) if atterr != nil { gerr := util.Errorf(atterr.Error()) gerr.SetStatus(http.StatusInternalServerError) return gerr } recb, recerr := datastore.EncodeBlob(cbv.Recipes) if recerr != nil { gerr := util.Errorf(recerr.Error()) gerr.SetStatus(http.StatusInternalServerError) return gerr } prob, proerr := datastore.EncodeBlob(cbv.Providers) if proerr != nil { gerr := util.Errorf(proerr.Error()) gerr.SetStatus(http.StatusInternalServerError) return gerr } resb, reserr := datastore.EncodeBlob(cbv.Resources) if reserr != nil { gerr := util.Errorf(reserr.Error()) gerr.SetStatus(http.StatusInternalServerError) return gerr } temb, temerr := datastore.EncodeBlob(cbv.Templates) if temerr != nil { gerr := util.Errorf(temerr.Error()) gerr.SetStatus(http.StatusInternalServerError) return gerr } roob, rooerr := datastore.EncodeBlob(cbv.RootFiles) if rooerr != nil { gerr := util.Errorf(rooerr.Error()) gerr.SetStatus(http.StatusInternalServerError) return gerr } filb, filerr := datastore.EncodeBlob(cbv.Files) if filerr != nil { gerr := util.Errorf(filerr.Error()) gerr.SetStatus(http.StatusInternalServerError) return gerr } metb, meterr := datastore.EncodeBlob(cbv.Metadata) if meterr != nil { gerr := util.Errorf(meterr.Error()) gerr.SetStatus(http.StatusInternalServerError) return gerr } /* version already validated */ maj, min, patch, _ := extractVerNums(cbv.Version) if config.Config.UseMySQL { return cbv.updateCookbookVersionMySQL(defb, libb, attb, recb, prob, resb, temb, roob, filb, metb, maj, min, patch) } else if config.Config.UsePostgreSQL { return cbv.updateCookbookVersionPostgreSQL(defb, libb, attb, recb, prob, resb, temb, roob, filb, metb, maj, min, patch) } gerr := util.Errorf("Somehow we ended up in an impossible place trying to use an unsupported db engine") gerr.SetStatus(http.StatusInternalServerError) return gerr }