title: Creating mysql function from rails migration
body: <p>I'm following <a href="https://stackoverflow.com/a/13928762">This Post</a> and trying to add the <strong>levenshtein</strong> mysql function through rails migration as follows:</p>
  
  <pre><code>class AddLevenshteinFunctionToMysql &lt; ActiveRecord::Migration
    def self.up
      ActiveRecord::Base.connection.execute &lt;&lt;-SQL
        DELIMITER $$
        CREATE FUNCTION levenshtein( s1 VARCHAR(255), s2 VARCHAR(255) )
        RETURNS INT
        DETERMINISTIC
        .
        .
        .
      SQL
    end
  
    def self.down
      ActiveRecord::Base.connection.execute &lt;&lt;-SQL
        DROP FUNCTION levenshtein;
      SQL
    end
  end
  </code></pre>
  
  <p>But, I'm getting error like: </p>
  
  <blockquote>
    <p>Mysql2::Error: You have an error in your SQL syntax; check the manual
    that corresponds to your MySQL server version for the right syntax to
    use near 'DELIMITER $$</p>
  </blockquote>
  
  <p><strong>The exact code runs fine when I try it directly in MySQL console.</strong> But I want to add it through rails migration. What is wrong?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>If I remove the DELIMITERs, then I get following error (line 39 is END$$)</p>
  
  <blockquote>
    <p>Mysql2::Error: You have an error in your SQL syntax; check the manual
    that corresponds to your MySQL server version for the right syntax to
    use near '' at line 39</p>
  </blockquote>
  
  <p><strong>EDIT</strong></p>
  
  <p>I got a nice answer explaining the cause of the error, but not the fix. <a href="https://stackoverflow.com/a/10259528/2968762">ANSWER</a></p>