Skip to content

Instantly share code, notes, and snippets.

@duelinmarkers
Created September 11, 2012 16:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duelinmarkers/3699644 to your computer and use it in GitHub Desktop.
Save duelinmarkers/3699644 to your computer and use it in GitHub Desktop.
Migrations spike for RapidFTR
# 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
# 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