1

When I run this piece of code with a task, it works

task :importGss => :environment do
    Gss.delete_all
    file = Rails.root + "app/assets/CSVs/gss.csv"
    csv_text = File.read(file)
    puts csv_text.size
    csv = CSV.parse(csv_text, :col_sep => ';', :headers => true)
    csv.each do |row|
    Gss.create!(row.to_hash)
end  

When I run it with a MVC, I have the following message :

ActiveRecord::StatementInvalid (SQLite3::BusyException: database is locked:

I have put the above code in a function in the Gss model. The import is launched from the browser with a GET that is routed to the controller that calls the model import function When the import is finished, the complete list of record should then be returned to the view. the csv file has 4k rows. The process of importing takes time and it seems that after precisely 60 seconds the GET is resend. Can someone explain me how to avoid this resending that crashes the import ?

1 Answer 1

4

Wrapping it in a transaction will make sure all the queries will be run together, rather than 1 at a time. This will drastically decrease the time taken to execute the import for that many rows.

task :importGss => :environment do
    Gss.delete_all
    file = Rails.root + "app/assets/CSVs/gss.csv"
    csv_text = File.read(file)
    puts csv_text.size
    csv = CSV.parse(csv_text, :col_sep => ';', :headers => true)

    ActiveRecord::Base.transaction do
      csv.each do |row|
        Gss.create!(row.to_hash)
      end  
    end
end  

Read more on transactions here: http://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html

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

3 Comments

Thanks ! It works know. What about this kind of timeout that resend the GET? Is there a way to avoid that ?
I'm not aware of any, I don't think the server controls i.e client side? I'm not sure though. The action takes less than 60 seconds now though right?
Yep, but if I need to upload lager files ? I wonder whether it comes from the browser. And in this case, whether it is possible to test the fact that the database is busy to avoid collision

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.