Posted almost 6 years ago. Visible to the public. Repeats.

Your database tables should always have timestamps

Whenever you create a table from a database migration, remember to add updated_at and created_at timestamps to that table. Without those timestamps, investigating future bug reports will be hell. Always have timestamps.

Adding timestamps to new tables

When you create a table using create_table, you can add timestamps by using the timestamps shortcut:

Copy
class CreateEpisode < ActiveRecord::Migration def change create_table :episodes do |t| t.string :name t.timestamps end end end

ActiveRecord will automagically set those timestamps when you create or update a record.

Adding timestamps to existing tables

If you forgot to add timestamps in the past, take a look at your db/schema.rb and add timetamps for any table that is missing them. You can use the add_timestamps macros for that.

Rails >= 4.2

Since Rails 4.2 add_timestamps also adds a NOT NULL constraint by default. So you will see an error like "ERROR: column "created_at" contains null values" when you don't take care of it. Here are two approaches on how to handle this:

(a) Don't have a NOT NULL constraint:

Copy
class AddMissingTimestamps < ActiveRecord::Migration def change add_timestamps :shows, null: true add_timestamps :tags, null: true add_timestamps :users, null: true end end

(b) Fill the datetimes with an old time (to mark it as being backfilled):

Copy
class AddMissingTimestamps < ActiveRecord::Migration def change # Add timestamps column, but without a NOT NULL constraint add_timestamps :shows, null: true # Backfill missing data with a really old date time = Time.zone.parse('2000-01-01 00:00:00') update "UPDATE shows SET created_at = '#{time}'" update "UPDATE shows SET updated_at = '#{time}'" # Restore NOT NULL constraints to be in line with the Rails default change_column_null :shows, :created_at, false change_column_null :shows, :updated_at, false # ... end end

Rails < 4.2

Copy
class AddMissingTimestamps < ActiveRecord::Migration def change add_timestamps :shows add_timestamps :tags add_timestamps :users end end

Also

Always have timestamps.

By refactoring problematic code and creating automated tests, makandra can vastly improve the maintainability of your Rails application.

Owner of this card:

Avatar
Henning Koch
Last edit:
almost 2 years ago
by Henning Koch
Keywords:
Rails
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Henning Koch to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more