Skip to content

Instantly share code, notes, and snippets.

@seanhandley
Created October 14, 2010 14:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save seanhandley/626250 to your computer and use it in GitHub Desktop.
Save seanhandley/626250 to your computer and use it in GitHub Desktop.
STORED_PROCEDURE = 'find_the_id'
STORED_PROCEDURE_FILE = 'StoredProcedure.sql'
class AddStoredProcedure < ActiveRecord::Migration
def self.up
sql_directory = File.join(File.dirname(__FILE__), 'sql')
conf = Rails::Configuration.new.database_configuration[RAILS_ENV]
sql_file = File.join(sql_directory, STORED_PROCEDURE_FILE)
host = conf['host'] ? conf['host'] : 'localhost'
database = conf['database']
username = conf['username'] ? conf['username'] : 'root'
password = conf['password'] ? conf['password'] : ''
cmd_line = 'mysql5 -h ' + host + ' -D ' + database + ' -u ' + username
cmd_line += ' -p ' + password if password.nil?
cmd_line += ' < ' + sql_file.to_s
unless system(cmd_line)
raise Exception, 'Error creating stored procedure ' + STORED_PROCEDURE
end
end
def self.down
execute 'DROP PROCEDURE ' + STORED_PROCEDURE
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment