Skip to content

Instantly share code, notes, and snippets.

@durandom
Created June 29, 2016 14:52
Show Gist options
  • Save durandom/b23d7e12722b2ea4f6aee20178ee9cb2 to your computer and use it in GitHub Desktop.
Save durandom/b23d7e12722b2ea4f6aee20178ee9cb2 to your computer and use it in GitHub Desktop.
bundle exec add_index_to_type_column.rb db/schema.rb
#!/usr/bin/env ruby
require 'rubygems'
require 'active_support/core_ext/string'
ARGF.each_line("create_table") do |line|
line =~ / "([^"]+)"/
table = $1
if line =~ /t.string\s+"type"/
next if line =~ /t.index \["type"\]/
# check if nothing inherits from that class
unless system("ag '<\\s*:?:?#{table.classify}$' app/ 2>&1 > /dev/null")
filename= "app/models/#{table.singularize}.rb"
# check if NewWithTypeSti mixin is included
if File.exists?(filename)
if File.open(filename).grep(/NewWithTypeSti/).empty?
puts "# no STI for #{table} ?"
print '#'
end
else
puts "# #{filename} does not exist"
print '#'
end
end
puts " add_index :#{table}, :type"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment