Sha256: 0830fedece7deb0485c9395a81f055f3569289d01d901d67bbce4a9536867951

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

namespace :db do
  namespace :migrate do
    namespace :reset do
      desc 'Check the consistency of schema.rb'
      task :check do
        unless Rails.env.test?
          abort 'This task must be run under test environment'
        end

        original_env_schema = ENV['SCHEMA']
        original_env_verbose = ENV['VERBOSE']

        consistent = nil
        diff = ''
        Dir.mktmpdir(nil, Rails.root.join('tmp')) do |dir|
          schema_rb = Rails.root.join('db', 'schema.rb')
          generated_schema = File.join(dir, 'schema.rb')

          ENV['SCHEMA'] = generated_schema
          ENV['VERBOSE'] = 'false'

          Rake::Task['db:migrate:reset'].invoke

          consistent = FileUtils.compare_file(schema_rb, generated_schema)

          next if consistent
          next if `which diff`.empty?

          diff = `diff -u #{schema_rb} #{generated_schema}`
        end

        ENV['SCHEMA'] = original_env_schema
        ENV['VERBOSE'] = original_env_verbose

        if consistent
          puts 'ok'
          exit 0
        else
          puts 'ERROR: Generated schema is not consistent with db/schema.rb'
          puts diff
          exit 1
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
db_schema_checker-0.1.1 lib/tasks/db_schema_checker_tasks.rake