Skip to content

Instantly share code, notes, and snippets.

@nilbus
Created November 4, 2014 17:08
Show Gist options
  • Select an option

  • Save nilbus/df74c1416afaec4fcc51 to your computer and use it in GitHub Desktop.

Select an option

Save nilbus/df74c1416afaec4fcc51 to your computer and use it in GitHub Desktop.
Check if there are pending migrations without loading the Rails app, for speedy checks. Store in the script directory.
#!/usr/bin/env ruby
# Consider loading the db driver directly instead of relying on relatively slow bundler
require 'bundler'
Bundler.setup
require 'rails'
require 'active_record'
# Load the Rails configuration without loading the application
config = Rails::Application::Configuration.new
config.root = File.expand_path(File.join(__FILE__, '../..'))
# Create a model to access already-run migration versions
class SchemaMigration < ActiveRecord::Base; end
SchemaMigration.configurations = config.database_configuration
SchemaMigration.establish_connection
# Calculate which if any migrations are pending
migrated = Set.new(SchemaMigration.all.map(&:version))
migrations = Set.new(Dir.glob('db/migrate/[0-9]*').map { |path| path.sub(/\D*(\d+)\D*/, '\1')})
pending = migrations-migrated
# Output the result
if pending.any?
puts "Pending migrations: #{pending.to_a.join(', ')}"
exit 1
else
puts "No pending migrations"
exit 0
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment