Parsing a SQL file directly into activerecord::execute metho

Hello folks,

I’m trying to do something like:

  1. db = ActiveRecord::Base.establish_connection(:adapter => “mysql”,
    :database => “app_development”, :username => “root”)
  2. Cmd = File.read(‘commands.sql’) <— Command.sql contains multiple
    valid SQL statement. I’ve tested this directly by piping it into MySQL
    from the command line
  3. ActiveRecord::Base.connection.execute(cmd)

This seems to not work as I would expect. My guess is that there is
some problem with the File.read parser.

Any ideas? The SQL file is pretty large. I bet the parser is doing
something to some of the input characters.

Best,
B

PS: Before anyone starts beating on the, do it the Rails way drum, the
file is big. Processes relatively large (up to several hundred million
records) tables, and works much faster when doing it in SQL directly. I
promise.


Brian C.

Try this:

ActiveRecord::Base.connection.execute(IO.read("#{RAILS_ROOT}/command.sql"))

Should do it for ya. If not, make sure your connection is working
proberly (can type in a command and executes). Let us know!

Also, where are you executing this from? The console? And migration?
And where is the SQL file in relation to your app directory?

-Nick