Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

The touch trigger should update the timestamp column when it is curre…

…ntly NULL
  • Loading branch information...
commit 6d3fe934489077bc5f77ab41337f0261e384940b 1 parent c1e575e
@magnetised magnetised authored
View
6 lib/sequel_postgresql_triggers.rb
@@ -126,11 +126,11 @@ def pgt_touch(main_table, touch_table, column, expr, opts={})
sql = <<-SQL
BEGIN
IF (TG_OP = 'INSERT') THEN
- UPDATE #{quote_schema_table(touch_table)} SET #{quote_identifier(column)} = CURRENT_TIMESTAMP WHERE #{cond['NEW']} AND #{quote_identifier(column)} <> CURRENT_TIMESTAMP;
+ UPDATE #{quote_schema_table(touch_table)} SET #{quote_identifier(column)} = CURRENT_TIMESTAMP WHERE #{cond['NEW']} AND ((#{quote_identifier(column)} <> CURRENT_TIMESTAMP) OR (#{quote_identifier(column)} IS NULL));
ELSIF (TG_OP = 'UPDATE') THEN
- UPDATE #{quote_schema_table(touch_table)} SET #{quote_identifier(column)} = CURRENT_TIMESTAMP WHERE #{cond['NEW']} AND #{quote_identifier(column)} <> CURRENT_TIMESTAMP;
+ UPDATE #{quote_schema_table(touch_table)} SET #{quote_identifier(column)} = CURRENT_TIMESTAMP WHERE #{cond['NEW']} AND ((#{quote_identifier(column)} <> CURRENT_TIMESTAMP) OR (#{quote_identifier(column)} IS NULL));
ELSIF (TG_OP = 'DELETE') THEN
- UPDATE #{quote_schema_table(touch_table)} SET #{quote_identifier(column)} = CURRENT_TIMESTAMP WHERE #{cond['OLD']} AND #{quote_identifier(column)} <> CURRENT_TIMESTAMP;
+ UPDATE #{quote_schema_table(touch_table)} SET #{quote_identifier(column)} = CURRENT_TIMESTAMP WHERE #{cond['OLD']} AND ((#{quote_identifier(column)} <> CURRENT_TIMESTAMP) OR (#{quote_identifier(column)} IS NULL));
END IF;
RETURN NULL;
END;
View
9 spec/sequel_postgresql_triggers_spec.rb
@@ -220,5 +220,14 @@
DB[:children].delete
DB[:parents].get(:changed_on).should > time
end
+
+ specify "Should update the timestamp on the related table if that timestamp is initially NULL" do
+ DB.pgt_touch(:children, :parents, :changed_on, :id1=>:parent_id1)
+ DB[:parents] << {:id1=>1, :changed_on=>nil}
+ DB[:children] << {:id=>1, :parent_id1=>1}
+ changed_on = DB[:parents].get(:changed_on)
+ changed_on.should_not == nil
+ changed_on.strftime('%F').should == Date.today.strftime('%F')
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.