Browse Source

DB transactions with context

Vova Tkach 5 years ago
parent
commit
239dd8414d

+ 6 - 9
engine/sqlw/sqlw.go

@@ -1,8 +1,5 @@
 package sqlw
 
-// TODO: rework all with context from request
-// https://golang.org/pkg/database/sql/
-
 import (
 	"context"
 	"database/sql"
@@ -76,8 +73,10 @@ func (this *DB) QueryRow(ctx context.Context, query string, args ...interface{})
 	return this.db.QueryRow(query, args...)
 }
 
-func (this *DB) Begin() (*Tx, error) {
-	tx, err := this.db.Begin()
+func (this *DB) Begin(ctx context.Context) (*Tx, error) {
+	tx, err := this.db.BeginTx(ctx, &sql.TxOptions{
+		Isolation: sql.LevelDefault,
+	})
 	if err != nil {
 		if consts.ParamDebug {
 			log("[TX] TRANSACTION START", time.Now(), err, true)
@@ -112,13 +111,11 @@ func (this *DB) Exec(ctx context.Context, query string, args ...interface{}) (sq
 	return this.db.ExecContext(ctx, query, args...)
 }
 
-// TODO: review
-// https://golang.org/pkg/database/sql/
-func (this *DB) Transaction(queries func(tx *Tx) error) error {
+func (this *DB) Transaction(ctx context.Context, queries func(tx *Tx) error) error {
 	if queries == nil {
 		return errors.New("queries is not set for transaction")
 	}
-	tx, err := this.Begin()
+	tx, err := this.Begin(ctx)
 	if err != nil {
 		return err
 	}

+ 1 - 1
modules/module_blog_act_delete.go

@@ -18,7 +18,7 @@ func (this *Modules) RegisterAction_BlogDelete() *Action {
 			return
 		}
 
-		if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
+		if err := wrap.DB.Transaction(wrap.R.Context(), func(tx *wrapper.Tx) error {
 			// Block rows
 			if _, err := tx.Exec("SELECT id FROM blog_posts WHERE id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
 				return err

+ 2 - 2
modules/module_blog_act_modify.go

@@ -57,7 +57,7 @@ func (this *Modules) RegisterAction_BlogModify() *Action {
 
 		if pf_id == "0" {
 			var lastID int64 = 0
-			if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
+			if err := wrap.DB.Transaction(wrap.R.Context(), func(tx *wrapper.Tx) error {
 				// Insert row
 				res, err := tx.Exec(
 					`INSERT INTO blog_posts SET
@@ -133,7 +133,7 @@ func (this *Modules) RegisterAction_BlogModify() *Action {
 			wrap.ResetCacheBlocks()
 			wrap.Write(`window.location='/cp/blog/modify/` + utils.Int64ToStr(lastID) + `/';`)
 		} else {
-			if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
+			if err := wrap.DB.Transaction(wrap.R.Context(), func(tx *wrapper.Tx) error {
 				// Block rows
 				if _, err := tx.Exec("SELECT id FROM blog_posts WHERE id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
 					return err

+ 1 - 1
modules/module_blog_categories_act_delete.go

@@ -18,7 +18,7 @@ func (this *Modules) RegisterAction_BlogCategoriesDelete() *Action {
 			return
 		}
 
-		err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
+		err := wrap.DB.Transaction(wrap.R.Context(), func(tx *wrapper.Tx) error {
 			// Block rows
 			if _, err := tx.Exec("SELECT id FROM blog_cats FOR UPDATE;"); err != nil {
 				return err

+ 3 - 3
modules/module_blog_categories_act_modify.go

@@ -9,7 +9,7 @@ import (
 
 func (this *Modules) blog_ActionCategoryAdd(wrap *wrapper.Wrapper, pf_id, pf_name, pf_alias, pf_parent string) (error, int64) {
 	var lastID int64 = 0
-	return wrap.DB.Transaction(func(tx *wrapper.Tx) error {
+	return wrap.DB.Transaction(wrap.R.Context(), func(tx *wrapper.Tx) error {
 		// Block rows
 		if _, err := tx.Exec("SELECT id FROM blog_cats FOR UPDATE;"); err != nil {
 			return err
@@ -45,7 +45,7 @@ func (this *Modules) blog_ActionCategoryUpdate(wrap *wrapper.Wrapper, pf_id, pf_
 
 	if utils.StrToInt(pf_parent) == parentId {
 		// If parent not changed, just update category data
-		return wrap.DB.Transaction(func(tx *wrapper.Tx) error {
+		return wrap.DB.Transaction(wrap.R.Context(), func(tx *wrapper.Tx) error {
 			// Process
 			if _, err := tx.Exec(`
 				UPDATE blog_cats SET
@@ -68,7 +68,7 @@ func (this *Modules) blog_ActionCategoryUpdate(wrap *wrapper.Wrapper, pf_id, pf_
 	// TODO: Fix parent change
 
 	// Parent is changed, move category to new parent
-	return wrap.DB.Transaction(func(tx *wrapper.Tx) error {
+	return wrap.DB.Transaction(wrap.R.Context(), func(tx *wrapper.Tx) error {
 		// Block all rows
 		if _, err := tx.Exec("SELECT id FROM blog_cats FOR UPDATE;"); err != nil {
 			return err

+ 1 - 1
modules/module_index_act_delete.go

@@ -18,7 +18,7 @@ func (this *Modules) RegisterAction_IndexDelete() *Action {
 			return
 		}
 
-		err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
+		err := wrap.DB.Transaction(wrap.R.Context(), func(tx *wrapper.Tx) error {
 			// Process
 			if _, err := tx.Exec("DELETE FROM pages WHERE id = ?;", utils.StrToInt(pf_id)); err != nil {
 				return err

+ 2 - 2
modules/module_index_act_modify.go

@@ -46,7 +46,7 @@ func (this *Modules) RegisterAction_IndexModify() *Action {
 		if pf_id == "0" {
 			// Add new page
 			var lastID int64 = 0
-			if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
+			if err := wrap.DB.Transaction(wrap.R.Context(), func(tx *wrapper.Tx) error {
 				res, err := tx.Exec(
 					`INSERT INTO pages SET
 						user = ?,
@@ -86,7 +86,7 @@ func (this *Modules) RegisterAction_IndexModify() *Action {
 			wrap.Write(`window.location='/cp/index/modify/` + utils.Int64ToStr(lastID) + `/';`)
 		} else {
 			// Update page
-			if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
+			if err := wrap.DB.Transaction(wrap.R.Context(), func(tx *wrapper.Tx) error {
 				_, err := tx.Exec(
 					`UPDATE pages SET
 						name = ?,

+ 1 - 1
modules/module_index_act_mysql_setup.go

@@ -65,7 +65,7 @@ func (this *Modules) RegisterAction_IndexMysqlSetup() *Action {
 		}
 
 		// Start transaction
-		tx, err := db.Begin()
+		tx, err := db.Begin(wrap.R.Context())
 		if err != nil {
 			wrap.MsgError(err.Error())
 			return

+ 1 - 1
modules/module_shop_act_attach_product_to.go

@@ -21,7 +21,7 @@ func (this *Modules) RegisterAction_ShopAttachProductTo() *Action {
 			return
 		}
 
-		if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
+		if err := wrap.DB.Transaction(wrap.R.Context(), func(tx *wrapper.Tx) error {
 			// Check parent
 			var count int
 			if err := tx.QueryRow(

+ 1 - 1
modules/module_shop_act_delete.go

@@ -18,7 +18,7 @@ func (this *Modules) RegisterAction_ShopDelete() *Action {
 			return
 		}
 
-		if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
+		if err := wrap.DB.Transaction(wrap.R.Context(), func(tx *wrapper.Tx) error {
 			// Block rows
 			if _, err := tx.Exec("SELECT id FROM shop_products WHERE id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
 				return err

+ 1 - 1
modules/module_shop_act_detach.go

@@ -18,7 +18,7 @@ func (this *Modules) RegisterAction_ShopDetach() *Action {
 			return
 		}
 
-		if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
+		if err := wrap.DB.Transaction(wrap.R.Context(), func(tx *wrapper.Tx) error {
 			if _, err := tx.Exec(`
 				UPDATE shop_products SET
 					parent_id = NULL,

+ 1 - 1
modules/module_shop_act_duplicate.go

@@ -22,7 +22,7 @@ func (this *Modules) RegisterAction_ShopDuplicate() *Action {
 		}
 
 		var lastID int64 = 0
-		if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
+		if err := wrap.DB.Transaction(wrap.R.Context(), func(tx *wrapper.Tx) error {
 			// Block rows
 			if _, err := tx.Exec("SELECT id FROM shop_products WHERE id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
 				return err

+ 1 - 1
modules/module_shop_act_images_reorder.go

@@ -31,7 +31,7 @@ func (this *Modules) RegisterAction_ShopImagesReorder() *Action {
 		}
 
 		if len(orders.Items) > 0 {
-			if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
+			if err := wrap.DB.Transaction(wrap.R.Context(), func(tx *wrapper.Tx) error {
 				for _, value := range orders.Items {
 					if _, err := tx.Exec("UPDATE shop_product_images SET ord = ? WHERE id = ?;", value.Order, value.Id); err != nil {
 						return err

+ 2 - 2
modules/module_shop_act_modify.go

@@ -103,7 +103,7 @@ func (this *Modules) RegisterAction_ShopModify() *Action {
 
 		if pf_id == "0" {
 			var lastID int64 = 0
-			if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
+			if err := wrap.DB.Transaction(wrap.R.Context(), func(tx *wrapper.Tx) error {
 				// Insert row
 				res, err := tx.Exec(
 					`INSERT INTO shop_products SET
@@ -215,7 +215,7 @@ func (this *Modules) RegisterAction_ShopModify() *Action {
 
 			wrap.Write(`window.location='/cp/shop/modify/` + utils.Int64ToStr(lastID) + `/';`)
 		} else {
-			if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
+			if err := wrap.DB.Transaction(wrap.R.Context(), func(tx *wrapper.Tx) error {
 				// Block rows
 				if _, err := tx.Exec("SELECT id FROM shop_products WHERE id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
 					return err

+ 1 - 1
modules/module_shop_act_order.go

@@ -97,7 +97,7 @@ func (this *Modules) RegisterAction_ShopOrder() *Action {
 		})
 
 		var lastID int64 = 0
-		if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
+		if err := wrap.DB.Transaction(wrap.R.Context(), func(tx *wrapper.Tx) error {
 			// Insert row
 			res, err := tx.Exec(
 				`INSERT INTO shop_orders SET

+ 1 - 1
modules/module_shop_act_order_set_status.go

@@ -29,7 +29,7 @@ func (this *Modules) RegisterAction_ShopOrderSetStatus() *Action {
 			return
 		}
 
-		if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
+		if err := wrap.DB.Transaction(wrap.R.Context(), func(tx *wrapper.Tx) error {
 			if _, err := tx.Exec(
 				`UPDATE shop_orders SET
 					status = ?

+ 1 - 1
modules/module_shop_act_upload_delete.go

@@ -26,7 +26,7 @@ func (this *Modules) RegisterAction_ShopUploadDelete() *Action {
 			return
 		}
 
-		if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
+		if err := wrap.DB.Transaction(wrap.R.Context(), func(tx *wrapper.Tx) error {
 			// Block rows
 			if _, err := tx.Exec("SELECT id FROM shop_products WHERE id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
 				return err

+ 1 - 1
modules/module_shop_act_upload_image.go

@@ -52,7 +52,7 @@ func (this *Modules) RegisterAction_ShopUploadImage() *Action {
 								target_file_name := utils.Int64ToStr(time.Now().Unix()+int64(i-1)) + filepath.Ext(handler.Filename)
 								target_file_full := wrap.DHtdocs + string(os.PathSeparator) + "products" + string(os.PathSeparator) + "images" + string(os.PathSeparator) + pf_id + string(os.PathSeparator) + target_file_name
 								var lastID int64 = 0
-								if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
+								if err := wrap.DB.Transaction(wrap.R.Context(), func(tx *wrapper.Tx) error {
 									// Block rows
 									if _, err := tx.Exec("SELECT id FROM shop_products WHERE id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
 										return err

+ 1 - 1
modules/module_shop_attributes_act_delete.go

@@ -18,7 +18,7 @@ func (this *Modules) RegisterAction_ShopAttributesDelete() *Action {
 			return
 		}
 
-		err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
+		err := wrap.DB.Transaction(wrap.R.Context(), func(tx *wrapper.Tx) error {
 			// Block rows
 			if _, err := tx.Exec("SELECT id FROM shop_filters WHERE id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
 				return err

+ 2 - 2
modules/module_shop_attributes_act_modify.go

@@ -46,7 +46,7 @@ func (this *Modules) RegisterAction_ShopAttributesModify() *Action {
 
 		if pf_id == "0" {
 			var lastID int64 = 0
-			if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
+			if err := wrap.DB.Transaction(wrap.R.Context(), func(tx *wrapper.Tx) error {
 				// Insert row
 				res, err := tx.Exec(
 					`INSERT INTO shop_filters SET
@@ -96,7 +96,7 @@ func (this *Modules) RegisterAction_ShopAttributesModify() *Action {
 
 			wrap.Write(`window.location='/cp/shop/attributes-modify/` + utils.Int64ToStr(lastID) + `/';`)
 		} else {
-			if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
+			if err := wrap.DB.Transaction(wrap.R.Context(), func(tx *wrapper.Tx) error {
 				// Block rows
 				if _, err := tx.Exec("SELECT id FROM shop_filters WHERE id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
 					return err

+ 1 - 1
modules/module_shop_categories_act_delete.go

@@ -18,7 +18,7 @@ func (this *Modules) RegisterAction_ShopCategoriesDelete() *Action {
 			return
 		}
 
-		err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
+		err := wrap.DB.Transaction(wrap.R.Context(), func(tx *wrapper.Tx) error {
 			// Block rows
 			if _, err := tx.Exec("SELECT id FROM shop_cats FOR UPDATE;"); err != nil {
 				return err

+ 3 - 3
modules/module_shop_categories_act_modify.go

@@ -9,7 +9,7 @@ import (
 
 func (this *Modules) shop_ActionCategoryAdd(wrap *wrapper.Wrapper, pf_id, pf_name, pf_alias, pf_parent string) (error, int64) {
 	var lastID int64 = 0
-	return wrap.DB.Transaction(func(tx *wrapper.Tx) error {
+	return wrap.DB.Transaction(wrap.R.Context(), func(tx *wrapper.Tx) error {
 		// Block rows
 		if _, err := tx.Exec("SELECT id FROM shop_cats FOR UPDATE;"); err != nil {
 			return err
@@ -45,7 +45,7 @@ func (this *Modules) shop_ActionCategoryUpdate(wrap *wrapper.Wrapper, pf_id, pf_
 
 	if utils.StrToInt(pf_parent) == parentId {
 		// If parent not changed, just update category data
-		return wrap.DB.Transaction(func(tx *wrapper.Tx) error {
+		return wrap.DB.Transaction(wrap.R.Context(), func(tx *wrapper.Tx) error {
 			// Process
 			if _, err := tx.Exec(`
 				UPDATE shop_cats SET
@@ -68,7 +68,7 @@ func (this *Modules) shop_ActionCategoryUpdate(wrap *wrapper.Wrapper, pf_id, pf_
 	// TODO: Fix parent change
 
 	// Parent is changed, move category to new parent
-	return wrap.DB.Transaction(func(tx *wrapper.Tx) error {
+	return wrap.DB.Transaction(wrap.R.Context(), func(tx *wrapper.Tx) error {
 		// Block all rows
 		if _, err := tx.Exec("SELECT id FROM shop_cats FOR UPDATE;"); err != nil {
 			return err

+ 1 - 1
modules/module_shop_currencies_act_delete.go

@@ -18,7 +18,7 @@ func (this *Modules) RegisterAction_ShopCurrenciesDelete() *Action {
 			return
 		}
 
-		err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
+		err := wrap.DB.Transaction(wrap.R.Context(), func(tx *wrapper.Tx) error {
 			// Process
 			if _, err := tx.Exec(
 				`DELETE FROM shop_currencies WHERE id = ?;`,

+ 2 - 2
modules/module_shop_currencies_act_modify.go

@@ -44,7 +44,7 @@ func (this *Modules) RegisterAction_ShopCurrenciesModify() *Action {
 
 		if pf_id == "0" {
 			var lastID int64 = 0
-			if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
+			if err := wrap.DB.Transaction(wrap.R.Context(), func(tx *wrapper.Tx) error {
 				// Insert row
 				res, err := tx.Exec(
 					`INSERT INTO shop_currencies SET
@@ -80,7 +80,7 @@ func (this *Modules) RegisterAction_ShopCurrenciesModify() *Action {
 
 			wrap.Write(`window.location='/cp/shop/currencies-modify/` + utils.Int64ToStr(lastID) + `/';`)
 		} else {
-			if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
+			if err := wrap.DB.Transaction(wrap.R.Context(), func(tx *wrapper.Tx) error {
 				// Block rows
 				if _, err := tx.Exec("SELECT id FROM shop_currencies WHERE id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
 					return err

+ 1 - 1
modules/module_users_act_delete.go

@@ -18,7 +18,7 @@ func (this *Modules) RegisterAction_UsersDelete() *Action {
 			return
 		}
 
-		err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
+		err := wrap.DB.Transaction(wrap.R.Context(), func(tx *wrapper.Tx) error {
 			// Block rows
 			if _, err := tx.Exec("SELECT id FROM blog_cats WHERE user = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
 				return err

+ 3 - 3
modules/module_users_act_modify.go

@@ -57,7 +57,7 @@ func (this *Modules) RegisterAction_UsersModify() *Action {
 			}
 
 			var lastID int64 = 0
-			if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
+			if err := wrap.DB.Transaction(wrap.R.Context(), func(tx *wrapper.Tx) error {
 				res, err := tx.Exec(
 					`INSERT INTO users SET
 						first_name = ?,
@@ -92,7 +92,7 @@ func (this *Modules) RegisterAction_UsersModify() *Action {
 		} else {
 			// Update user
 			if pf_password == "" {
-				if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
+				if err := wrap.DB.Transaction(wrap.R.Context(), func(tx *wrapper.Tx) error {
 					_, err := tx.Exec(
 						`UPDATE users SET
 							first_name = ?,
@@ -119,7 +119,7 @@ func (this *Modules) RegisterAction_UsersModify() *Action {
 					return
 				}
 			} else {
-				if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
+				if err := wrap.DB.Transaction(wrap.R.Context(), func(tx *wrapper.Tx) error {
 					_, err := tx.Exec(
 						`UPDATE users SET
 							first_name = ?,