Skip to content

Commit

Permalink
Merge pull request #23 from wizarddevelopment/rake-task
Browse files Browse the repository at this point in the history
A rake task that checks for missing keys and exits with an error code.
  • Loading branch information
jenseng committed Apr 18, 2015
2 parents ab1ce99 + 65aaf9e commit 4ec0079
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/immigrant/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ class Railtie < Rails::Railtie
Immigrant.load
end
end

generators do
require "generators/immigration_generator"
require 'generators/immigration_generator'
end

rake_tasks do
require 'immigrant/task'
end
end
end
end
20 changes: 20 additions & 0 deletions lib/immigrant/task.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace :immigrant do
desc 'Checks for missing foreign key relationships in the database'
task check_keys: :environment do
Rails.application.eager_load!

keys, warnings = Immigrant::KeyFinder.new.infer_keys
warnings.values.each { |warning| $stderr.puts "WARNING: #{warning}" }

keys.each do |key|
column = key.options[:column]
pk = key.options[:primary_key]
$stderr.puts "Missing foreign key relationship on '#{key.from_table}.#{column}' to '#{key.to_table}.#{pk}'"
end

if keys.any?
puts 'Found missing foreign keys, run `rails generate immigration MigrationName` to create a migration to add them.'
exit keys.count
end
end
end

0 comments on commit 4ec0079

Please sign in to comment.