However, if you want to quickly pull the version from a command line, this rake task should do the trick*:
namespace :db do* 2nd version submitted by Doug as a comment.
desc "Returns the current schema version"
task :version => :environment do
puts "Current version: " + ActiveRecord::Migrator.current_version.to_s
end
end
Ahh cute. Rake is a lovely thing indeed :)
ReplyDeleteMaybe it's cause I'm on edge rails, but the return value from connection.execute(select) doesn't have a result method for me. This is the code that's working for me instead:
ReplyDeletenamespace :db do
desc "Returns the current schema version"
task :version => :environment do
connection = ActiveRecord::Base.connection
select = "select * from schema_info"
version = connection.execute(select).first["version"]
puts "Current version: #{version}"
end
end
(sorry don't know how to get whitespace formatting to work in blogger comments...)
Jacob, what DB are you using? My example (probably poorly) assumes postgresql.
ReplyDeleteWhat about just using ActiveRecord::Migrator.current_version?
ReplyDeleteThe last line of output from script/about lists the current schema revision. Setting RAILS_ENV allows you to query a particular environment's database.
ReplyDeleteDoug, thanks, I'll update the entry.
ReplyDeleteJamie, I've never used script/about, thanks for pointing that out.
Just to be sure, I like to wrap that in a begin/rescue block
ReplyDeletebegin
puts 'current version is ' + ActiveRecord::Migrator.current_version}.to_s
rescue
puts 'database has not been migrated yet: ' + $!.to_s
end
Thanks for this tip. I used it as a starting point so I can do:
ReplyDelete#going backwards
rake db:rollback:4
# going forward
rake db:rollback:-3
It's on my blog if you're curious.
And, much later, I turned this up while looking to do the same thing. I snagged the implementations offered by Doug and ncryptic and included them in a plugin called GenerallyUseful (with full credit to them and this post for the code).
ReplyDeleteThanks for the post!