Skip to content

Commit

Permalink
Allow setting nulls and updating nulls
Browse files Browse the repository at this point in the history
  • Loading branch information
marostr committed Sep 16, 2016
1 parent 4741849 commit 8824af9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
6 changes: 3 additions & 3 deletions lib/sequel_postgresql_triggers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def pgt_created_at(table, column, opts={})

# Makes all given columns in the given table immutable, so an exception
# is raised if there is an attempt to modify the value when updating the
# record. Arguments:
# record. Allows updating from/to NULL. Arguments:
# * table : name of table
# * columns : All columns in the table that should be immutable. Can end with a hash of options, see module documentation.
def pgt_immutable(table, *columns)
Expand All @@ -89,7 +89,7 @@ def pgt_immutable(table, *columns)
old = "OLD.#{quote_identifier(c)}"
new = "NEW.#{quote_identifier(c)}"
<<-END
IF #{new} IS DISTINCT FROM #{old} THEN
IF #{new} IS DISTINCT FROM #{old} AND #{old} IS NOT NULL AND #{new} IS NOT NULL THEN
RAISE EXCEPTION 'Attempted #{c} update: Old: %, New: %', #{old}, #{new};
END IF;
END
Expand Down Expand Up @@ -142,7 +142,7 @@ def pgt_sum_cache(main_table, main_table_id_column, sum_column, summed_table, su
# summed table for the matching id. The join table must have NOT NULL constraints
# on the foreign keys to the main table and summed table and a
# composite unique constraint on both foreign keys.
#
#
# Arguments:
# * opts : option hash, see module documentation, and below.
# * :main_table: name of table holding sum cache column
Expand Down
44 changes: 23 additions & 21 deletions spec/sequel_postgresql_triggers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,31 +49,31 @@

DB[:entries] << {:id=>2, :account_id=>1}
DB[:accounts].order(:id).select_map(:num_entries).must_equal [2, 0]

DB[:entries] << {:id=>3, :account_id=>nil}
DB[:accounts].order(:id).select_map(:num_entries).must_equal [2, 0]

DB[:entries].where(:id=>3).update(:account_id=>2)
DB[:accounts].order(:id).select_map(:num_entries).must_equal [2, 1]

DB[:entries].where(:id=>2).update(:account_id=>2)
DB[:accounts].order(:id).select_map(:num_entries).must_equal [1, 2]

DB[:entries].where(:id=>2).update(:account_id=>nil)
DB[:accounts].order(:id).select_map(:num_entries).must_equal [1, 1]

DB[:entries].where(:id=>2).update(:id=>4)
DB[:accounts].order(:id).select_map(:num_entries).must_equal [1, 1]

DB[:entries].where(:id=>4).update(:account_id=>2)
DB[:accounts].order(:id).select_map(:num_entries).must_equal [1, 2]

DB[:entries].where(:id=>4).update(:account_id=>nil)
DB[:accounts].order(:id).select_map(:num_entries).must_equal [1, 1]

DB[:entries].filter(:id=>4).delete
DB[:accounts].order(:id).select_map(:num_entries).must_equal [1, 1]

DB[:entries].delete
DB[:accounts].order(:id).select_map(:num_entries).must_equal [0, 0]
end
Expand Down Expand Up @@ -128,11 +128,13 @@
end

it "Should handle NULL values correctly" do
proc{DB[:accounts].update(:balance=>nil)}.must_raise(Sequel::DatabaseError)
DB[:accounts].update(:balance=>nil)
DB[:accounts].delete
DB[:accounts] << {:id=>1, :balance=>nil}
DB[:accounts].update(:balance=>nil)
proc{DB[:accounts].update(:balance=>0)}.must_raise(Sequel::DatabaseError)
DB[:accounts].update(:balance=>0)
proc{DB[:accounts].update(:balance=>1)}.must_raise(Sequel::DatabaseError)
DB[:accounts].update(:balance=>nil)
end
end

Expand All @@ -157,34 +159,34 @@

DB[:entries] << {:id=>2, :account_id=>1, :amount=>200}
DB[:accounts].order(:id).select_map(:balance).must_equal [300, 0]

DB[:entries] << {:id=>3, :account_id=>nil, :amount=>500}
DB[:accounts].order(:id).select_map(:balance).must_equal [300, 0]

DB[:entries].where(:id=>3).update(:account_id=>2)
DB[:accounts].order(:id).select_map(:balance).must_equal [300, 500]

DB[:entries].exclude(:id=>2).update(:amount=>Sequel.*(:amount, 2))
DB[:accounts].order(:id).select_map(:balance).must_equal [400, 1000]

DB[:entries].where(:id=>2).update(:account_id=>2)
DB[:accounts].order(:id).select_map(:balance).must_equal [200, 1200]

DB[:entries].where(:id=>2).update(:account_id=>nil)
DB[:accounts].order(:id).select_map(:balance).must_equal [200, 1000]

DB[:entries].where(:id=>2).update(:id=>4)
DB[:accounts].order(:id).select_map(:balance).must_equal [200, 1000]

DB[:entries].where(:id=>4).update(:account_id=>2)
DB[:accounts].order(:id).select_map(:balance).must_equal [200, 1200]

DB[:entries].where(:id=>4).update(:account_id=>nil)
DB[:accounts].order(:id).select_map(:balance).must_equal [200, 1000]

DB[:entries].filter(:id=>4).delete
DB[:accounts].order(:id).select_map(:balance).must_equal [200, 1000]

DB[:entries].delete
DB[:accounts].order(:id).select_map(:balance).must_equal [0, 0]
end
Expand Down

0 comments on commit 8824af9

Please sign in to comment.