1

I wrote a stored procedure and want to execute it within a Rake task.

Before calling the stored procedure (through the "call" statement) I should create the procedure with the source command which fails because of the following error:

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 'source import_legacy_database.sql' at line 1: source import_legacy_database.sql

This error happens when the following line is executed:

ActiveRecord::Base.connection.execute "source import_legacy_database.sql"

The same command "source import_legacy_database.sql" runs fine when in the console, but not in the Rake task.

If I don't call that command then

ActiveRecord::Base.connection.execute "call import_legacy_database()"

fails because the database doesn't find the procedure because it does not exist.

Thanks in advanced.

2 Answers 2

2

I believe this is because the source command is specific to the implementation of the MySQL command line client, and is not part of the API implemented by ActiveRecord. The same thing would happen if you tried to use the DELIMITER command inside ActiveRecord, for example.

Sign up to request clarification or add additional context in comments.

2 Comments

is there any other way to execute sql statements in a Rake task?
@negarnil I'm not sure actually. I searched around a bit but didn't find an answer. I suppose you could load the file with Ruby code, and split it on the SQL delimiters to execute each of the resultant parts.
1

I finally used the sh Rake method and executed sql commands in batch mode http://dev.mysql.com/doc/refman/5.5/en/batch-mode.html

sh "mysql -u root mydb_development -e 'source import_legacy_database.sql'"

1 Comment

This is great if your app server and database server are on the same machine, but in most production environments when they are not, this method will not work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.