Created
September 11, 2012 16:23
-
-
Save duelinmarkers/3699644 to your computer and use it in GitHub Desktop.
Migrations spike for RapidFTR
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# in lib/tasks | |
namespace :couchdb do | |
task :migrate => :environment do | |
migration_files = Dir[Rails.root.join('db/migrate/*.rb')] | |
migration_files_by_id = Hash[*migration_files.map {|path| [migration_number_from_file(path), path] }.flatten] | |
applied_migration_ids = SchemaMigration.all.map(&:id) | |
migration_ids_to_apply = migration_files_by_id.except(*applied_migration_ids).sort | |
if migration_ids_to_apply.empty? | |
puts "DB already up to date." | |
else | |
puts "Will apply migrations: #{migration_ids_to_apply.map(&:first).to_sentence}" | |
migration_ids_to_apply.each do |migration_id, path| | |
puts "Applying #{path}..." | |
start_time = Time.now | |
load path | |
seconds = Time.now - start_time | |
SchemaMigration.create!(:_id => migration_id, :file => path, :applied => Time.now, :seconds_elapsed => seconds) | |
puts " ...finished in #{seconds} seconds." | |
end | |
puts "DB migrated." | |
end | |
end | |
end | |
def migration_number_from_file(path) | |
path.match(/db\/migrate\/(\d+)_.+\.rb/)[1] | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# in app/models | |
class SchemaMigration < CouchRestRails::Document | |
use_database :schema_migration | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment