beginWorkspaceGroupWrite serializes writes for one user before touching group rows. SQLite uses BEGIN IMMEDIATE; Postgres takes the strongest required user lock first, so cap and name checks cannot race.
(ctx context.Context, userID string)
| 89 | // group rows. SQLite uses BEGIN IMMEDIATE; Postgres takes the strongest |
| 90 | // required user lock first, so cap and name checks cannot race. |
| 91 | func (s *Store) beginWorkspaceGroupWrite(ctx context.Context, userID string) (*sql.Tx, error) { |
| 92 | tx, err := s.db.BeginTx(ctx, nil) |
| 93 | if err != nil { |
| 94 | return nil, err |
| 95 | } |
| 96 | query := `SELECT id FROM users WHERE id = ?` |
| 97 | if s.dialect.Driver() == DriverPostgres { |
| 98 | query += ` FOR NO KEY UPDATE` |
| 99 | } |
| 100 | var id string |
| 101 | if err := tx.QueryRowContext(ctx, s.q(query), userID).Scan(&id); err != nil { |
| 102 | _ = tx.Rollback() |
| 103 | return nil, err |
| 104 | } |
| 105 | return tx, nil |
| 106 | } |
| 107 | |
| 108 | func (s *Store) workspaceGroupNameAvailable(ctx context.Context, tx *sql.Tx, userID, id, name string) error { |
| 109 | // EqualFold also handles Unicode names on SQLite, whose lower() folds ASCII. |
no test coverage detected