module Sequel::Plugins::SelectOnSkippedUpdate

  1. lib/sequel/plugins/select_on_skipped_update.rb

This plugin changes the default model behavior so that for existing objects, if Model#save_changes skips the save as the object is not modified, or if the UPDATE query is skipped as there were no columns to update, Sequel will issue a SELECT query to make sure the object still exists. This is only done if require_modification is set for the object, so that these cases will raise the same error that Sequel would raise if the UPDATE require was run but did not modify a row.

class Item < Sequel::Model
  plugin :select_on_skipped_update
end

item = Item[1]

# Delete the item from the database
Item.where(id: 1).delete

# By default, this would return nil, but with the plugin,
# this raises Sequel::NoExistingRow.
item.save_changes

# By default, this would return the object without issuing the
# UPDATE query (as the columns to update is empty. With the plugin,
# this raises Sequel::NoExistingRow.
item.save(columns: [])