How to Check the Database Type in a Rails Migration

How do I check the Database type in a Rails Migration?

ActiveRecord::Base.connection will provide you with everything you ever wanted to know about the database connection established by boot.rb and environment.rb

ActiveRecord::Base.connection returns a lot of information. So you've got to know exactly what you're looking for.

As Marcel points out:

ActiveRecord::Base.connection.instance_of? 
ActiveRecord::ConnectionAdapters::MysqlAdapter

is probably the best method of determining if your database MySQL.

Despite relying on internal information that could change between ActiveRecord release, I prefer doing it this way:

ActiveRecord::Base.connection.instance_values["config"][:adapter] == "mysql"

Get database type of ActiveRecord object attribute


Product.columns_hash['name'].type # => :string

Note the using of strings instead of symbols.

About an alternative: sql_type

It maps logical Rails types to DB-specific data types. For general purpose things I wouldn't recommend using it: your production DB-engine will return the value which won't be the same as the sql_type for the very same column in your development DB (although they both were created with the same migration file). Example for boolean field:

# SQLite mapping:
:boolean => { :name => "boolean" }
# MySQL mapping:
:boolean => { :name => "tinyint", :limit => 1 }

Using type in both cases give you :boolean, as you specify in migrations.

Rails migrations with database-specific data types

FYI for anyone who happens across this page, I fixed this by adding this (actually uncommenting it) to my Rails config:

config.active_record.schema_format = :sql

ActiveRecord migration on specific database connection can't rollback

I found a reasonable solution by splitting change into up and down methods:

class AddFlavorToBikes < ActiveRecord::Migration
def up
VehicleBase.connection.tap do |db|
db.add_column :bikes, :flavor, :string
end
end

def down
VehicleBase.connection.tap do |db|
db.remove_column :bikes, :flavor
end
end
end

While not as graceful or DRY as a reversible migration, this allows db:migrate and db:rollback to work succesfully.

Using Rails Migration on different database than standard production or development

A bit late, but I was dealing with this problem today and I came up with this custom rake task:

namespace :db do
desc "Apply db tasks in custom databases, for example rake db:alter[db:migrate,test-es] applies db:migrate on the database defined as test-es in databases.yml"
task :alter, [:task,:database] => [:environment] do |t, args|
require 'activerecord'
puts "Applying #{args.task} on #{args.database}"
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations[args.database])
Rake::Task[args.task].invoke
end
end

How can I fix a typo in data migration?


rake db:rollback 

will rollback the migration, then you can fix the typo and run

rake db:migrate 

again to re run the migration.



Related Topics



Leave a reply



Submit