Migration Timestamps deleted_at Magic Field

Posted by Curtis Miller Curtis Miller

I was recently working on a project using edge rails and saw that it supported something like what used to be called sexy migrations. Basically, you can make your migration files look a whole lot cleaner.

Before

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class CreateUsers < ActiveRecord::Migration
  def self.up
    create_table :users do |t|
      t.column :first_name, string
      t.column :last_name, string
      t.column :birthday, date
      t.column :created_at, datetime
      t.column :updated_at, datetime
    end
  end

  def self.down
    drop_table :users
  end
end

After

1
2
3
4
5
6
7
8
9
10
11
12
13
class CreateUsers < ActiveRecord::Migration
  def self.up
    create_table :users do |t|
      t.string      :first_name, :last_name
      t.date        :birthday
      t.timestamps
    end
  end

  def self.down
    drop_table :users
  end
end

Looks much better doesn't it? For what I was doing though, I wanted to use the acts_as_paranoid functionality to never delete anything. The acts_as_paranoid plugin updates the row with a deleted_at timestamp and then filters out 'deleted' ones whenever it queries the database. Of course, I could just add t.datetime :deleted_at to each table. But instead of going back to the old way of doing things, I simply extended the built in timestamps functionality by placing the following in a file in the lib directory and then requiring it in the environment.

lib/custom_schema_definitions.rb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
module ActiveRecord
  module ConnectionAdapters #:nodoc:
    class TableDefinition

      # Adds a deleted_at column when timestamps is called from a migration.
      def timestamps_with_deleted_at
        timestamps_without_deleted_at
        column(:deleted_at, :datetime)
      end

      alias_method_chain :timestamps, :deleted_at
    end
  end
end

config/environment.rb (add)

require "custom_schema_definitions"

Now, we can run our migration as we did before and the t.timestamps line will also give us a deleted_at column. Share your paranoid ramblings in the comments.

Note: I believe this only works in Edge Rails for the moment as the new migrations and alias_method_chain are not present in the latest stable version of Rails.



Velocity Labs

Need web application development, maintenance for your existing app, or a third party code review?

Velocity Labs can help.

Hire us!