add_index and remove_index Now Quiet on Duplicate Index

UPDATE: added support for remove_index :users, :name=>:index_name

Mysql::Error: Duplicate key name ‘index_xxx’: CREATE INDEX `index_xxx` ON `users` (`xxx`) ?
Mysql::Error: Can’t DROP ‘index_xxx’; check that column/key exists: DROP INDEX `index_xxx` ON users ?
nevermore!

class TestAddRemoveIndex < ActiveRecord::Migration
  def self.up
    add_index :users, [:a, :b], :quiet=>true
    add_index :users, [:a, :b], :quiet=>true
  end

  def self.down
    remove_index :users, [:a, :b], :quiet=>true
    remove_index :users, [:a, :b], :quiet=>true
  end
end

These patches allow you to add indices in a migration that will only be created if they do not already exist, or to remove indices only if they already exist through :quiet=>true.

module ActiveRecord::ConnectionAdapters::SchemaStatements

  def add_index_with_quiet(table_name, column_names, options = {})
    quiet = options.delete(:quiet)
    add_index_without_quiet table_name, column_names, options
  rescue
    raise unless quiet and $!.message =~ /^Mysql::Error: Duplicate key name/i
    puts "Failed to create index #{table_name} #{column_names.inspect} #{options.inspect}"
  end
  alias_method_chain :add_index, :quiet

  def remove_index_with_quiet(table_name, *args)
    options = args.extract_options!
    quiet = options.delete(:quiet)
    if args.empty?
      remove_index_without_quiet table_name, options
    else
      remove_index_without_quiet table_name, *args
    end
  rescue
  rescue
    raise unless quiet and $!.message =~ /^Mysql::Error: Can't DROP/i
    puts "Failed to drop index #{table_name} #{(args[1]||options[:name]).inspect}"
  end
  alias_method_chain :remove_index, :quiet

end

Automated SSH Login with Password and Additional Commands

The normal answer to most ssh related problems is: “use public/private keys”
but sometimes this does not work…

What i did regularly was this:

  • ssh to xxx@asdads.com
  • enter password
  • sudo su admin
  • cd /apps/hosts/some_project

Thats not very complicated, but its frustrating. What I do now is: “sshxx”, which executes this excpect script in ~/bin/sshxx

#!/usr/bin/expect
set pass [lindex $argv 0] #get first argument
spawn ssh xxx@asdads.com
expect "assword" #matches Password and password
send "$pass\r"
expect "xxx@" #wait for the prompt
send "sudo su admin\r"
send "cd /apps/hosts/some_project\r"
send "clear\r" #clean up the mess
interact

(For Ubuntu you need to “sudo apt get install expect” first)

All ActionView Helpers on Strings

Insert all the helpers you want and “go!”.strip_tags.auto_link.truncate(25)

# ActionView text helpers
# https://grosser.it/2009/05/30/all-actionview-helpers-on-strings/
class String
  %w[auto_link excerpt highlight sanitize simple_format strip_tags word_wrap].each do |method|
    define_method method do |*args|
      ActionController::Base.helpers.send method, self, *args
    end
  end

  # called with a number or with :length=>10
  def truncate(*args)
    if args.first.is_a?(Hash)
      ActionController::Base.helpers.truncate(self, *args)
    else
      ActionController::Base.helpers.truncate(self, :length=>args[0])
    end
  end
end