lockForUpdate must emit FOR UPDATE on databases that support it and skip it on SQLite, where the syntax does not exist. The dummy dialector is used because SQLite drivers strip locking clauses from the generated SQL, which would mask what the helper itself does.
(t *testing.T)
| 16 | // The dummy dialector is used because SQLite drivers strip locking clauses |
| 17 | // from the generated SQL, which would mask what the helper itself does. |
| 18 | func TestLockForUpdateEmitsRowLock(t *testing.T) { |
| 19 | dummyDB, err := gorm.Open(tests.DummyDialector{}, &gorm.Config{DryRun: true}) |
| 20 | require.NoError(t, err) |
| 21 | buildSQL := func() string { |
| 22 | var rows []Redemption |
| 23 | return lockForUpdate(dummyDB).Where("id = ?", 1).Find(&rows).Statement.SQL.String() |
| 24 | } |
| 25 | |
| 26 | t.Cleanup(func() { |
| 27 | common.SetDatabaseTypes(common.DatabaseTypeSQLite, common.DatabaseTypeSQLite) |
| 28 | }) |
| 29 | |
| 30 | common.SetDatabaseTypes(common.DatabaseTypeMySQL, common.DatabaseTypeSQLite) |
| 31 | assert.Contains(t, buildSQL(), "FOR UPDATE") |
| 32 | |
| 33 | common.SetDatabaseTypes(common.DatabaseTypePostgreSQL, common.DatabaseTypeSQLite) |
| 34 | assert.Contains(t, buildSQL(), "FOR UPDATE") |
| 35 | |
| 36 | common.SetDatabaseTypes(common.DatabaseTypeSQLite, common.DatabaseTypeSQLite) |
| 37 | assert.NotContains(t, buildSQL(), "FOR UPDATE") |
| 38 | } |
nothing calls this directly
no test coverage detected