Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #11622 (new defect)

Opened 6 days ago

Last modified 4 days ago

Calling LOAD DATA LOCAL INFILE from a Migration fails with Mysql::Error: #42000The used command is not allowed with this MySQL version

Reported by: mrichman Assigned to: core
Priority: normal Milestone: 2.x
Component: ActiveRecord Version: edge
Severity: normal Keywords: migration mysql local infile
Cc:

Description

I am attempting to load data from a CSV file into MySQL during a migration. The query works fine from MySQL's query tool, but fails from within a migration. In both scenarios, I am connecting to MySQL as root@localhost.

Here is the raw query:

LOAD DATA LOCAL INFILE '/home/mrichman/rails_apps/myproject/db/migrate/geo_data.csv' INTO TABLE geo_data (zip_code, latitude, longitude, city, state, county);

Here is query in the migration:

csv_file = "#{RAILS_ROOT}/db/migrate/geo_data.csv" fields = '(zip_code, latitude, longitude, city, state, county)'

execute "LOAD DATA LOCAL INFILE '#{csv_file}' INTO TABLE geo_data " +

"FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY \"\"\"\" " + "LINES TERMINATED BY '\n' " + fields

This is the error produced by MySQL when calling rake db:migrate:

== 7 CreateGeoData: migrating ================================================= -- create_table(:geo_data)

-> 0.0232s

-- add_index("geo_data", zip_code?, {:name=>"zip_code_optimization"})

-> 0.0200s

-- execute("LOAD DATA LOCAL INFILE '/home/mrichman/rails_apps/myproject/db/migrate/geo_data.csv' INTO TABLE geo_data FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY \"\"\"\" LINES TERMINATED BY '\n' (zip_code, latitude, longitude, city, state, county)") rake aborted! Mysql::Error: #42000The used command is not allowed with this MySQL version: LOAD DATA LOCAL INFILE '/home/mrichman/rails_apps/myproject/db/migrate/geo_data.csv' INTO TABLE geo_data FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY """" LINES TERMINATED BY ' ' (zip_code, latitude, longitude, city, state, county)

(See full trace by running task with --trace)

And here is my database.yml:

development:

adapter: mysql database: myproject_development username: root password: mypassword host: localhost socket: /var/run/mysqld/mysqld.sock

I have verified that local-infile=1 is set and my user is granted all right, but this should make no difference unless ActiveRecord connects to MySQL with unusual parameters.

Change History

05/12/08 14:28:32 changed by mrichman

Update: after running sudo apt-get install mysql, this problem went away. It looks like whatever "generic" mechanism Rails uses to talk to MySQL (without the mysql gem installed) doesn't allow commands like local_infile.

Can someone please comment?

Thanks, Mark