Sha256: 8a1d79a378593b1ee7f00ffd534240570b8f6020510b1ff90c3c552b92a51d52

Contents?: true

Size: 1.6 KB

Versions: 1

Compression:

Stored size: 1.6 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']

        schema_format = if ActiveRecord.gem_version < Gem::Version.create('7.0.0')
                          ActiveRecord::Base.schema_format
                        else
                          ActiveRecord.schema_format
                        end

        schema_path = if schema_format == :ruby
                        'db/schema.rb'
                      else
                        'db/structure.sql'
                      end

        consistent = nil
        diff = ''
        Dir.mktmpdir(nil, Rails.root.join('tmp')) do |dir|
          old_schema = Rails.root.join(schema_path)
          new_schema = File.join(dir, File.basename(schema_path))

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

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

          consistent = FileUtils.compare_file(old_schema, new_schema)

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

          diff = `diff -u #{old_schema} #{new_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.2.1 lib/tasks/db_schema_checker_tasks.rake