Rails/Ruby How do I override the migration method timestamps

classpath, overriding, ruby-on-rails, timestamp

Solution

Put this in a monkey patch... Easy as!

class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::TableDefinition
  def timestamps(*args)
    options = args.extract_options!
    column(:created_at, :datetime, options)
    column(:updated_at, :datetime, options)
  end
end

As Maniek said. Updates to rails will be ignored because of this "fix".

But his initial offering does the same. Also to accommodate his fix, you'd need to go back through ol' migrations and replace "timestamps" with the new code. Add to that that you'd have to replace all future auto-generated migration too.

I don't think that fits well with DRY.. Nor does it fit in SPOT.

Just B carefulllll!

Problem

I'm attempting to write my own timestamps method that gets run during the migration. The one that is in place now adds a NOT_NULL constraint on the field, and I really really don't want that. The problem I have is that I have a multi schema'd database. Where each major client gets their own schema. When we on-board a new client we create a new tenant record then run a migration for a newly minted schema. The new schema is supposed to be an exact copy of the tables in the other schemas, except of course with no data. The last migration I ran was using a slightly older version of rails. Still in the 3's but a smidge older. When it created the timestamps they were NULLable. When I ran migration the other day (on a new rails)... Well all the fields are now NOT_NULL I have code that was developed with the idea that updated_at was only populated when the record was updated... not when it was created. (third party apps and database "functions" create the records).. The third party apps and database functions that create records are falling down on the new schema... I've gone in and removed all the NOT_NULL constraints on all the tables manually, but I don't want to have to write the cleanup right into my migration task, so that all future tables are corrected.. I figured the best thing to do was to override the timestamps method that was changed, back to one that didn't break existing code. So there's the reason I need to revert/override.. My question now is... How do I override the method. I can't see a clear class path to it and I'm not exactly sure how to override it..

Original source