Add AllowGlobalUpdate mode
This commit is contained in:
		
							parent
							
								
									cc6a64adfb
								
							
						
					
					
						commit
						ebdb4edda8
					
				| @ -51,7 +51,7 @@ func Delete(db *gorm.DB) { | |||||||
| 				} | 				} | ||||||
| 			} | 			} | ||||||
| 
 | 
 | ||||||
| 			if _, ok := db.Statement.Clauses["WHERE"]; !ok { | 			if _, ok := db.Statement.Clauses["WHERE"]; !db.AllowGlobalUpdate && !ok { | ||||||
| 				db.AddError(gorm.ErrMissingWhereClause) | 				db.AddError(gorm.ErrMissingWhereClause) | ||||||
| 				return | 				return | ||||||
| 			} | 			} | ||||||
|  | |||||||
| @ -69,7 +69,7 @@ func Update(db *gorm.DB) { | |||||||
| 			db.Statement.Build("UPDATE", "SET", "WHERE") | 			db.Statement.Build("UPDATE", "SET", "WHERE") | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		if _, ok := db.Statement.Clauses["WHERE"]; !ok { | 		if _, ok := db.Statement.Clauses["WHERE"]; !db.AllowGlobalUpdate && !ok { | ||||||
| 			db.AddError(gorm.ErrMissingWhereClause) | 			db.AddError(gorm.ErrMissingWhereClause) | ||||||
| 			return | 			return | ||||||
| 		} | 		} | ||||||
|  | |||||||
							
								
								
									
										7
									
								
								gorm.go
									
									
									
									
									
								
							
							
						
						
									
										7
									
								
								gorm.go
									
									
									
									
									
								
							| @ -32,6 +32,8 @@ type Config struct { | |||||||
| 	DisableAutomaticPing bool | 	DisableAutomaticPing bool | ||||||
| 	// DisableForeignKeyConstraintWhenMigrating
 | 	// DisableForeignKeyConstraintWhenMigrating
 | ||||||
| 	DisableForeignKeyConstraintWhenMigrating bool | 	DisableForeignKeyConstraintWhenMigrating bool | ||||||
|  | 	// AllowGlobalUpdate allow global update
 | ||||||
|  | 	AllowGlobalUpdate bool | ||||||
| 
 | 
 | ||||||
| 	// ClauseBuilders clause builder
 | 	// ClauseBuilders clause builder
 | ||||||
| 	ClauseBuilders map[string]clause.ClauseBuilder | 	ClauseBuilders map[string]clause.ClauseBuilder | ||||||
| @ -61,6 +63,7 @@ type Session struct { | |||||||
| 	PrepareStmt            bool | 	PrepareStmt            bool | ||||||
| 	WithConditions         bool | 	WithConditions         bool | ||||||
| 	SkipDefaultTransaction bool | 	SkipDefaultTransaction bool | ||||||
|  | 	AllowGlobalUpdate      bool | ||||||
| 	Context                context.Context | 	Context                context.Context | ||||||
| 	Logger                 logger.Interface | 	Logger                 logger.Interface | ||||||
| 	NowFunc                func() time.Time | 	NowFunc                func() time.Time | ||||||
| @ -154,6 +157,10 @@ func (db *DB) Session(config *Session) *DB { | |||||||
| 		tx.Config.SkipDefaultTransaction = true | 		tx.Config.SkipDefaultTransaction = true | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | 	if config.AllowGlobalUpdate { | ||||||
|  | 		txConfig.AllowGlobalUpdate = true | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
| 	if config.Context != nil { | 	if config.Context != nil { | ||||||
| 		tx.Statement = tx.Statement.clone() | 		tx.Statement = tx.Statement.clone() | ||||||
| 		tx.Statement.DB = tx | 		tx.Statement.DB = tx | ||||||
|  | |||||||
| @ -98,7 +98,7 @@ func (sd SoftDeleteDeleteClause) ModifyStatement(stmt *Statement) { | |||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		if _, ok := stmt.Clauses["WHERE"]; !ok { | 		if _, ok := stmt.Clauses["WHERE"]; !stmt.DB.AllowGlobalUpdate && !ok { | ||||||
| 			stmt.DB.AddError(ErrMissingWhereClause) | 			stmt.DB.AddError(ErrMissingWhereClause) | ||||||
| 			return | 			return | ||||||
| 		} | 		} | ||||||
|  | |||||||
| @ -118,4 +118,8 @@ func TestBlockGlobalDelete(t *testing.T) { | |||||||
| 	if err := DB.Delete(&User{}).Error; err == nil || !errors.Is(err, gorm.ErrMissingWhereClause) { | 	if err := DB.Delete(&User{}).Error; err == nil || !errors.Is(err, gorm.ErrMissingWhereClause) { | ||||||
| 		t.Errorf("should returns missing WHERE clause while deleting error") | 		t.Errorf("should returns missing WHERE clause while deleting error") | ||||||
| 	} | 	} | ||||||
|  | 
 | ||||||
|  | 	if err := DB.Session(&gorm.Session{AllowGlobalUpdate: true}).Delete(&User{}).Error; err != nil { | ||||||
|  | 		t.Errorf("should returns no error while enable global update, but got err %v", err) | ||||||
|  | 	} | ||||||
| } | } | ||||||
|  | |||||||
| @ -222,6 +222,10 @@ func TestBlockGlobalUpdate(t *testing.T) { | |||||||
| 	if err := DB.Model(&User{}).Update("name", "jinzhu").Error; err == nil || !errors.Is(err, gorm.ErrMissingWhereClause) { | 	if err := DB.Model(&User{}).Update("name", "jinzhu").Error; err == nil || !errors.Is(err, gorm.ErrMissingWhereClause) { | ||||||
| 		t.Errorf("should returns missing WHERE clause while updating error, got err %v", err) | 		t.Errorf("should returns missing WHERE clause while updating error, got err %v", err) | ||||||
| 	} | 	} | ||||||
|  | 
 | ||||||
|  | 	if err := DB.Session(&gorm.Session{AllowGlobalUpdate: true}).Model(&User{}).Update("name", "jinzhu").Error; err != nil { | ||||||
|  | 		t.Errorf("should returns no error while enable global update, but got err %v", err) | ||||||
|  | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func TestSelectWithUpdate(t *testing.T) { | func TestSelectWithUpdate(t *testing.T) { | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Jinzhu
						Jinzhu