carpamon
carpamon

Reputation: 6633

Cannot execute MySQL sql "source" command from Rake task using ActiveRecord

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.

Upvotes: 1

Views: 1123

Answers (2)

carpamon
carpamon

Reputation: 6633

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'"

Upvotes: 1

Michael Berkowski
Michael Berkowski

Reputation: 270727

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.

Upvotes: 2

Related Questions