lib/chrono_model/adapter.rb in chrono_model-0.8.0 vs lib/chrono_model/adapter.rb in chrono_model-0.8.2
- old
+ new
@@ -556,10 +556,17 @@
logger.info "ChronoModel: upgrading #{table_name} from #{version} to #{current}"
chrono_create_view_for(table_name)
logger.info "ChronoModel: upgrade complete"
end
end
+ rescue => e
+ message = "ChronoModel structure upgrade failed: #{e.message}. Please drop dependent objects and then run ActiveRecord::Base.connection.chrono_setup!"
+
+ # Quite important, output it also to stderr.
+ #
+ logger.error message
+ $stderr.puts message
end
def chrono_metadata_for(table)
comment = select_value(
"SELECT obj_description(#{quote(table)}::regclass)",
@@ -697,10 +704,14 @@
#
# If a row in the history with the current ID and current timestamp already
# exists, update it with new data. This logic makes possible to "squash"
# together changes made in a transaction in a single history row.
#
+ # If you want to disable this behaviour, set the CHRONOMODEL_NO_SQUASH
+ # environment variable. This is useful when running scenarios inside
+ # cucumber, in which everything runs in the same transaction.
+ #
execute <<-SQL
CREATE OR REPLACE FUNCTION chronomodel_#{table}_update() RETURNS TRIGGER AS $$
DECLARE _now timestamp;
DECLARE _hid integer;
DECLARE _old record;
@@ -719,10 +730,10 @@
END IF;
_now := timezone('UTC', now());
_hid := NULL;
- SELECT hid INTO _hid FROM #{history} WHERE #{pk} = OLD.#{pk} AND lower(validity) = _now;
+ #{"SELECT hid INTO _hid FROM #{history} WHERE #{pk} = OLD.#{pk} AND lower(validity) = _now;" unless ENV['CHRONOMODEL_NO_SQUASH']}
IF _hid IS NOT NULL THEN
UPDATE #{history} SET ( #{fields} ) = ( #{values} ) WHERE hid = _hid;
ELSE
UPDATE #{history} SET validity = tsrange(lower(validity), _now)