Speed up seeding database in Rails application using multiple SQL inserts!
The project is deprecated and no longer supported. Please consider using activerecord-import instead.
Add to Gemfile:
gem "fast_seeder"
Lets say you have CSV file db/seeds/cities/ukraine.csv
:
Kharkov,1654
Lviv,1240
Kiev,600
You can populate DB with it using FastSeeder:
# Seeding from CSV file:
FastSeeder.seed_csv!(City, "cities/ukraine.csv", :name, :founded_in, :country => "Ukraine")
Technically it equals to:
City.create(:name => "Kharkov", :founded_in => 1654, :country => "Ukraine")
City.create(:name => "Lviv" , :founded_in => 1240, :country => "Ukraine")
City.create(:name => "Kiev" , :founded_in => 600 , :country => "Ukraine")
But does the job faster using only one SQL query instead of three.
As you also noticed it expects you to pass model class, path to CSV file(located in db/seeds
),
CSV columns, and default values.
The next example is equivalent to showed above one, but uses inline data without CSV file:
# Seeding in place:
FastSeeder.seed!(City, :name, :founded_in, :country => "Ukraine") do
record "Kharkov", 1654
record "Lviv" , 1240
record "Kiev" , 600
end
- PostgreSQL
- MySQL(mysql, mysql2)
- SQLite3
rake spec:all # run specs with all supported adapters
rake spec:mysql # run specs with mysql adapter
rake spec:mysql2 # run specs with mysql2 adapter
rake spec:postgresql # run specs with postgresql adapter
rake spec:sqlite3 # run specs with sqlite3 adapter
Gemfile:
gem "fast_seeder", "0.0.2"
- Sergey Potapov - creator and maintainer
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along with this program. If not, see http://www.gnu.org/licenses/lgpl.txt