Wednesday, August 15, 2012

Override ActiveRecord Adapter Default Database Types Easily

Just added a new gem called activerecord-native_db_types_override that lets you easily override database types.

Do you want to change the Rails default length of string in database column definitions created by your Rails 3.1-4.0 migrations?

NativeDbTypesOverride::Options.configure({
  ActiveRecord::ConnectionAdapters::PostgreSQLAdapter => {
    :string => { :name => "varchar", :limit => 4096 }
  }
})

How about making datetimes and timestamps stored as timestamptz in postgres?

NativeDbTypesOverride::Options.configure({
  ActiveRecord::ConnectionAdapters::PostgreSQLAdapter => {
    :datetime => { :name => "timestamptz" },
    :timestamp => { :name => "timestamptz" }
  }
})

Do you need support for multiple adapters because you are using Apartment or SecondBase?

NativeDbTypesOverride::Options.configure({
  ActiveRecord::ConnectionAdapters::PostgreSQLAdapter => {
    :datetime => { :name => "timestamptz" },
    :timestamp => { :name => "timestamptz" }
  },
  ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter => {
    :boolean => { :name => "varchar", :limit => 1 }
  }
})

What about support for some other adapter other than the default ones in Rails 3.1-4.0?

NativeDbTypesOverride::Options.configure({
  ActiveRecord::ConnectionAdapters::SomeDbAdapter => {
    :boolean => { :name => "NUMBER", :limit => 1 }
  }
})

Check out activerecord-native_db_types_override and let me know if you'd like to contribute or have any suggestions.

No comments: