Commit Graph

275 Commits

Author SHA1 Message Date
Charles Strahan b543a11ca0 transaction blocks: don't swallow panics (#2774)
This improves upon #2767.

Previously, the code would swallow any panics, which isn't ideal;
panic is intended to be used when a critical error arises,
where the process should fail fast instead of trying to limp along.

This now defers the any recovery (if desired) to the client code.
2019-12-05 17:54:32 +08:00
Jason Lee 59408390c2 Add `db.Transaction` method for create Transaction block. (#2767)
* Add `db.Transaction` method for create Transaction block.

example:

```go
func CreateAnimals(db *gorm.DB) error {
  db.Transaction(func(tx *gorm.DB) error {
    if err := tx.Create(&Animal{Name: "Giraffe"}).Error; err != nil {
      // return any error will rollback
      return err
    }

    if err := tx.Create(&Animal{Name: "Lion"}).Error; err != nil {
      return err
    }

    // return nil will commit
    return nil
  })
}
```

* Ensure rollback when commit has error.
2019-11-19 16:08:00 +08:00
kouha.shu 2ee239a4c0 Update main.go 2019-10-23 10:40:34 +09:00
kouha.shu d926a05bec add warning comment 2019-10-23 10:38:05 +09:00
macklin-10x 5b3e40ac12 Rename expr type to make it public. (#2604) 2019-10-17 23:44:34 +08:00
zaneli beb591e642 Fix function name of comment 2019-06-24 20:38:13 +09:00
Jinzhu 153ce22c99 Test Save with specfied table name 2019-06-11 17:30:14 +08:00
Ruben de Vries c44c6027fb add an override on the DB instance instead of using the global NowFunc. (#2142) 2019-06-11 12:22:21 +08:00
Tyler Stillwater fec06da6a3 Add BeginTx for parity with sql.DB.BeginTx (#2227) 2019-06-11 12:22:21 +08:00
Adam S Levy f301f86e29 Add RollbackUnlessCommitted() (#2126) 2019-06-11 12:22:21 +08:00
John Barker 280dd011a1 Don't AddError for Rollback on ErrTxDone (#2434) 2019-06-11 12:22:20 +08:00
Emir Beganović 962ba3d7ae
Merge branch 'master' into master 2019-05-08 11:13:47 +04:00
John Barker 8b127471f1 Pass logger into Callback{} so that logs are printed consistently 2019-05-07 10:42:20 -06:00
Emir Beganovic 96d52f25b0 Use RWMutex 2019-04-14 12:41:14 +04:00
Emir Beganovic 59594877da Fix unsafe concurrent SingularTable method call 2019-04-14 12:30:11 +04:00
Momo733 1c62bf1e57
fix save err when specify a table name
s.New() will clear all search conditions and search value,when I use Table() to set a table name. Then FirstOrCreate() will use struct name as my database table name,so It doesn't work.
2019-04-13 14:23:35 +08:00
kuangzhiqiang d239c4cab8 error log show trace file (#2296) 2019-03-10 20:03:55 +08:00
Jinzhu d7ef7871a4 Fix tests 2019-03-10 19:33:49 +08:00
李鹏 ac6c89ec0c search不需要再clone,dbClone内的search已经是一个全新的了 (#2179) 2019-01-02 21:25:37 +08:00
Sai 5ad6f621e6 logMode codes more readable (#2216) 2018-12-13 21:04:51 +08:00
Eyal Posener 123d4f50ef lock TagSettings structure when modified (#1796)
The map is modified in different places in the code which results in race conditions
on execution.
This commit locks the map with read-write lock when it is modified
2018-09-10 07:11:00 +08:00
gstvg 282f11af19 Support only preloading (#1926)
* add support for only preloading relations on an already populated model

* Update callback_query.go

comments
2018-09-10 06:52:32 +08:00
Aaron Leung 73e7561e20 Use sync.Map for DB.values (#2064)
* Replace the regular map with a sync.Map to avoid fatal concurrent map reads/writes

* fix the formatting
2018-09-10 06:26:29 +08:00
Artemij Shepelev 0e04d414d5 Race fix. Changes modelStructsMap implementation from map with mutex to sync.Map (#2022)
* fix (https://github.com/jinzhu/gorm/issues/1407)

* changed map with mutex to sync.Map (https://github.com/jinzhu/gorm/issues/1407)

* removed newModelStructsMap func

* commit to rerun pipeline, comment changed
2018-08-19 07:09:21 +08:00
antness d68403b29d do not close wrapped *sql.DB (#1985) 2018-07-27 07:43:09 +08:00
Kevin ac3ec858a6 Edit DB.clone(), DB.Dialect(), and Scope.Dialect() preserve transactions (#1939)
* Edit DB.clone(), DB.Dialect(), and Scope.Dialect() preserve transactions.

* Adds a test case for tables creations and autoMigrate in the same transaction.
2018-07-27 07:35:53 +08:00
lrita a58b98acee Do not panic if Begin().Error was ignored (#1830) (#1881) 2018-05-12 01:28:15 -05:00
Daniel McDonald 35efe68ba7 add simple input validation on gorm.Open function (#1855)
Simply check if the passed-in database source meets the expected types
and, if not, early return with error.
2018-05-02 09:37:51 -05:00
Giuseppe 919c6db4f8 Do not panic if Begin().Error was ignored (#1830) 2018-04-16 09:18:51 -05:00
Jinzhu 48a20a6e9f Add SubQuery method 2018-02-22 12:04:12 +08:00
Jinzhu fe3c94cd2d Add Take method, close #1228 2018-02-13 09:18:42 +08:00
Jinzhu 7a8c2bbff8 Refactor build SQL condition 2018-02-12 00:06:31 +08:00
Emil Davtyan c0359226dc Removed unnecessary cloning. (#1462)
`NewScope` clones `DB` no need to chain a call to clone with `NewScope`.
2018-02-10 19:31:55 +08:00
Amit Yadav 0e1cb6ece9 Add support to remove foreign key constraints (#1686) 2018-02-09 22:20:26 +08:00
Jinzhu c3bb6aaa82 Fix QueryExpr tests 2017-08-11 23:24:00 +08:00
Jinzhu e5432b14d2 Add QueryExpr, thanks @ManReinsp for PR #1548 2017-08-11 18:41:04 +08:00
liu-xuewen 10e217e2bc Print affected rows (#1541)
* fix better

* add the rows number that the sql result affected or returned
2017-07-23 16:04:22 +08:00
Jinzhu b21c0bef9b Merge pull request #1412 from slayer/master
prevent nil pointer dereference on closed connection
2017-04-18 22:53:27 +08:00
Emil Davtyan 5ed4c3f290 Allow open to take transaction.
Need to skip the ping, otherwise results in a nil dereference.
2017-04-18 15:33:13 +02:00
Vladislav Moskovets 72a60c5df4 prevent nil pointer dereference on closed connection 2017-03-31 14:26:51 +03:00
Russ Egan 45f1a95051 Replace all use of *sql.DB with sqlCommon
Exporting sqlCommon as SQLCommon.

This allows passing alternate implementations of the database connection, or wrapping the connection with middleware.  This change didn't change any usages of the database variables.  All usages were already only using the functions defined in SQLCommon.

This does cause a breaking change in Dialect, since *sql.DB was referenced in the interface.
2017-03-14 17:02:14 -04:00
Jinzhu 1558522aaa Refactor 2017-02-05 15:10:28 +08:00
Maxime Song 58cbc9c4b5 fix typo 2017-01-04 15:53:49 +08:00
Jinzhu 0f2ceb5a77 Add gorm:association:source for association operations for plugins to extend GORM 2016-12-05 18:30:07 +08:00
Jinzhu 45fc640bf3 Merge pull request #1252 from slockij/block-global-update-delete
Block global updates / deletes
2016-11-10 08:45:29 +08:00
Jinzhu 53d09952be Fix AddError for DB 2016-11-09 10:22:42 +08:00
slockij e26cb8dbc4 In some cases (Error not checked, missed data) one can perform very harmful operation - global update or delete (all records)
This is to prevent it.
2016-11-04 17:54:43 +01:00
Caleb Thompson c063624c91
Make gorm.Errors available for use outside gorm
gorm.Errors, which usefully implements `error` for an `[]error` as
returned by `DB.GetError()` was already exported, but because it used a
private field `errors`, it was not able to be created due to the
compile-time error:

    implicit assignment of unexported field 'errors' in gorm.Errors literal

The trivial solution would be to export the `errors` field on
`gorm.Errors`, but this led to the issue that the common pattern of
checking `err != nil` failed because a struct{error: nil} != nil.

We can take advantage of type aliasing here to make Errors an []error,
which can in fact be nil and would pass `err != nil` on the happy path.

* Remove `(Errors) GetErrors()`, as it's less useful when Errors is an
  []error which can be iterated over. While this is technically a
  breaking change, we never expose an Errors and its difficult to build
  one (it can be done with the existing `(Errors) Add(error)`), but
  awkwardly. This removal can be reverted without issue and we can make
  it an identity method, but it seemed an opportune time to reduce API
  surface area on something that likely isn't used.
* Remove errorsInterface, as it's not useful without `(Errors)
  GetErrors()`
* Change `(*Errors) Add(error)` => `(Errors) Add(error...) Errors`
  because we can't modify even a *Errors when it's a type alias. This is
  more idiomatic as it follows the pattern of `slice = append(slice,
  element)` Go developers are familiar with.
2016-10-25 11:22:50 -05:00
Jinzhu c1b9cf186e Merge pull request #1210 from baijum/error-zero-args
If no arguments, return error at once
2016-10-23 22:58:23 +08:00
Jinzhu 89b7cbe89c Fix RowsAffected not returned for FirstOrCreate 2016-10-21 11:30:17 +08:00