Sha256: f1a158ab0454a0cda1f72a846e9162793c93646371184dc832118e08b5794f97

Contents?: true

Size: 1.03 KB

Versions: 4

Compression:

Stored size: 1.03 KB

Contents

namespace :db do

  desc 'Find missing foreign key database indexes'
  task :missing_indexes => :environment do
    c = ActiveRecord::Base.connection

    c.tables.collect do |t|
      columns = c.columns(t).collect(&:name).select { |x| x.ends_with?('_id') || x.ends_with?('_type') }
      indexed_columns = c.indexes(t).collect(&:columns).flatten.uniq
      unindexed = columns - indexed_columns
      unless unindexed.empty?
        puts "#{t}:\n\t #{unindexed.join(", ")}"
      end
    end
  end

  namespace :missing_indexes do
    desc 'Print migration statements for missing foreign key database indexes'
    task :migrators => :environment do
      c = ActiveRecord::Base.connection

      c.tables.collect do |t|
        columns = c.columns(t).collect(&:name).select { |x| x.ends_with?('_id') || x.ends_with?('_type') }
        indexed_columns = c.indexes(t).collect(&:columns).flatten.uniq
        unindexed = columns - indexed_columns
        unindexed.each do |c|
          puts "add_index :#{t}, :#{c}"
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
iqvoc-4.14.5 lib/tasks/db.rake
iqvoc-4.14.4 lib/tasks/db.rake
iqvoc-4.13.2 lib/tasks/db.rake
iqvoc-4.13.0 lib/tasks/db.rake