I'm following This Post and trying to add the levenshtein mysql function through rails migration as follows:
class AddLevenshteinFunctionToMysql < ActiveRecord::Migration
def self.up
ActiveRecord::Base.connection.execute <<-SQL
DELIMITER $$
CREATE FUNCTION levenshtein( s1 VARCHAR(255), s2 VARCHAR(255) )
RETURNS INT
DETERMINISTIC
.
.
.
SQL
end
def self.down
ActiveRecord::Base.connection.execute <<-SQL
DROP FUNCTION levenshtein;
SQL
end
end
But, I'm getting error like:
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 $$
The exact code runs fine when I try it directly in MySQL console. But I want to add it through rails migration. What is wrong?
EDIT
If I remove the DELIMITERs, then I get following error (line 39 is END$$)
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
EDIT
I got a nice answer explaining the cause of the error, but not the fix. ANSWER