Skip to content

Commit

Permalink
Add instance method lock_model_at_build?
Browse files Browse the repository at this point in the history
  • Loading branch information
sudoremo committed Mar 14, 2024
1 parent e75c9ed commit 0e99989
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 1.5.5 (2024-03-14)

* Add instance method `lock_model_at_build?` to
`RailsOps::Operation::Model::Load` in order to allow dynamic decision whether
locking should occur.

## 1.5.4 (2024-01-10)

* Update documentation
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1196,6 +1196,22 @@ One caveat is, that shared locking is only supported for MySQl (MariaDB),
Postgresql and Oracle DB databases, any other database will always use an
exlusive lock.

You can also dynamically enable or disable locking by creating an instance
method `lock_model_at_build?`:

```ruby
class Operations::User::Update < RailsOps::Operation::Model::Load
model ::User

protected

def lock_model_at_build?
# Example: Lock based on a parameter
osparams.lock
end
end
```

### Creating models

For creating models, you can use the base class
Expand Down
6 changes: 5 additions & 1 deletion lib/rails_ops/operation/model/load.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ def self.lock_model_at_build?
_lock_model_at_build.nil? ? RailsOps.config.lock_models_at_build? : _lock_model_at_build
end

def lock_model_at_build?
self.class._lock_model_at_build?
end

# Method to set the lock mode, which can either be :exclusive to use
# an exclusive write lock, or :shared to use a shared lock. Please note
# that currently, :shared only works for MySql, Postgresql and Oracle DB,
Expand Down Expand Up @@ -93,7 +97,7 @@ def extract_id_from_params

def lock_relation(relation)
# Directly return the relation if we don't want to lock the relation
return relation unless self.class.lock_model_at_build?
return relation unless lock_model_at_build?

if self.class.lock_mode_or_default == :shared
# Lock the relation in shared mode
Expand Down

0 comments on commit 0e99989

Please sign in to comment.