fast_seeder 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LGPL-LICENSE +165 -0
- data/README.md +94 -0
- data/Rakefile +59 -0
- data/lib/fast_seeder.rb +31 -0
- data/lib/fast_seeder/adapters.rb +10 -0
- data/lib/fast_seeder/adapters/base_adapter.rb +62 -0
- data/lib/fast_seeder/adapters/mysql_adapter.rb +7 -0
- data/lib/fast_seeder/adapters/postgresql_adapter.rb +7 -0
- data/lib/fast_seeder/adapters/sqlite_adapter.rb +65 -0
- data/lib/fast_seeder/record_set.rb +19 -0
- data/lib/fast_seeder/seeders.rb +9 -0
- data/lib/fast_seeder/seeders/base_seeder.rb +50 -0
- data/lib/fast_seeder/seeders/csv_seeder.rb +18 -0
- data/lib/fast_seeder/seeders/inline_seeder.rb +32 -0
- data/lib/fast_seeder/version.rb +3 -0
- metadata +94 -0
data/LGPL-LICENSE
ADDED
@@ -0,0 +1,165 @@
|
|
1
|
+
GNU LESSER GENERAL PUBLIC LICENSE
|
2
|
+
Version 3, 29 June 2007
|
3
|
+
|
4
|
+
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
5
|
+
Everyone is permitted to copy and distribute verbatim copies
|
6
|
+
of this license document, but changing it is not allowed.
|
7
|
+
|
8
|
+
|
9
|
+
This version of the GNU Lesser General Public License incorporates
|
10
|
+
the terms and conditions of version 3 of the GNU General Public
|
11
|
+
License, supplemented by the additional permissions listed below.
|
12
|
+
|
13
|
+
0. Additional Definitions.
|
14
|
+
|
15
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser
|
16
|
+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
17
|
+
General Public License.
|
18
|
+
|
19
|
+
"The Library" refers to a covered work governed by this License,
|
20
|
+
other than an Application or a Combined Work as defined below.
|
21
|
+
|
22
|
+
An "Application" is any work that makes use of an interface provided
|
23
|
+
by the Library, but which is not otherwise based on the Library.
|
24
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
25
|
+
of using an interface provided by the Library.
|
26
|
+
|
27
|
+
A "Combined Work" is a work produced by combining or linking an
|
28
|
+
Application with the Library. The particular version of the Library
|
29
|
+
with which the Combined Work was made is also called the "Linked
|
30
|
+
Version".
|
31
|
+
|
32
|
+
The "Minimal Corresponding Source" for a Combined Work means the
|
33
|
+
Corresponding Source for the Combined Work, excluding any source code
|
34
|
+
for portions of the Combined Work that, considered in isolation, are
|
35
|
+
based on the Application, and not on the Linked Version.
|
36
|
+
|
37
|
+
The "Corresponding Application Code" for a Combined Work means the
|
38
|
+
object code and/or source code for the Application, including any data
|
39
|
+
and utility programs needed for reproducing the Combined Work from the
|
40
|
+
Application, but excluding the System Libraries of the Combined Work.
|
41
|
+
|
42
|
+
1. Exception to Section 3 of the GNU GPL.
|
43
|
+
|
44
|
+
You may convey a covered work under sections 3 and 4 of this License
|
45
|
+
without being bound by section 3 of the GNU GPL.
|
46
|
+
|
47
|
+
2. Conveying Modified Versions.
|
48
|
+
|
49
|
+
If you modify a copy of the Library, and, in your modifications, a
|
50
|
+
facility refers to a function or data to be supplied by an Application
|
51
|
+
that uses the facility (other than as an argument passed when the
|
52
|
+
facility is invoked), then you may convey a copy of the modified
|
53
|
+
version:
|
54
|
+
|
55
|
+
a) under this License, provided that you make a good faith effort to
|
56
|
+
ensure that, in the event an Application does not supply the
|
57
|
+
function or data, the facility still operates, and performs
|
58
|
+
whatever part of its purpose remains meaningful, or
|
59
|
+
|
60
|
+
b) under the GNU GPL, with none of the additional permissions of
|
61
|
+
this License applicable to that copy.
|
62
|
+
|
63
|
+
3. Object Code Incorporating Material from Library Header Files.
|
64
|
+
|
65
|
+
The object code form of an Application may incorporate material from
|
66
|
+
a header file that is part of the Library. You may convey such object
|
67
|
+
code under terms of your choice, provided that, if the incorporated
|
68
|
+
material is not limited to numerical parameters, data structure
|
69
|
+
layouts and accessors, or small macros, inline functions and templates
|
70
|
+
(ten or fewer lines in length), you do both of the following:
|
71
|
+
|
72
|
+
a) Give prominent notice with each copy of the object code that the
|
73
|
+
Library is used in it and that the Library and its use are
|
74
|
+
covered by this License.
|
75
|
+
|
76
|
+
b) Accompany the object code with a copy of the GNU GPL and this license
|
77
|
+
document.
|
78
|
+
|
79
|
+
4. Combined Works.
|
80
|
+
|
81
|
+
You may convey a Combined Work under terms of your choice that,
|
82
|
+
taken together, effectively do not restrict modification of the
|
83
|
+
portions of the Library contained in the Combined Work and reverse
|
84
|
+
engineering for debugging such modifications, if you also do each of
|
85
|
+
the following:
|
86
|
+
|
87
|
+
a) Give prominent notice with each copy of the Combined Work that
|
88
|
+
the Library is used in it and that the Library and its use are
|
89
|
+
covered by this License.
|
90
|
+
|
91
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
92
|
+
document.
|
93
|
+
|
94
|
+
c) For a Combined Work that displays copyright notices during
|
95
|
+
execution, include the copyright notice for the Library among
|
96
|
+
these notices, as well as a reference directing the user to the
|
97
|
+
copies of the GNU GPL and this license document.
|
98
|
+
|
99
|
+
d) Do one of the following:
|
100
|
+
|
101
|
+
0) Convey the Minimal Corresponding Source under the terms of this
|
102
|
+
License, and the Corresponding Application Code in a form
|
103
|
+
suitable for, and under terms that permit, the user to
|
104
|
+
recombine or relink the Application with a modified version of
|
105
|
+
the Linked Version to produce a modified Combined Work, in the
|
106
|
+
manner specified by section 6 of the GNU GPL for conveying
|
107
|
+
Corresponding Source.
|
108
|
+
|
109
|
+
1) Use a suitable shared library mechanism for linking with the
|
110
|
+
Library. A suitable mechanism is one that (a) uses at run time
|
111
|
+
a copy of the Library already present on the user's computer
|
112
|
+
system, and (b) will operate properly with a modified version
|
113
|
+
of the Library that is interface-compatible with the Linked
|
114
|
+
Version.
|
115
|
+
|
116
|
+
e) Provide Installation Information, but only if you would otherwise
|
117
|
+
be required to provide such information under section 6 of the
|
118
|
+
GNU GPL, and only to the extent that such information is
|
119
|
+
necessary to install and execute a modified version of the
|
120
|
+
Combined Work produced by recombining or relinking the
|
121
|
+
Application with a modified version of the Linked Version. (If
|
122
|
+
you use option 4d0, the Installation Information must accompany
|
123
|
+
the Minimal Corresponding Source and Corresponding Application
|
124
|
+
Code. If you use option 4d1, you must provide the Installation
|
125
|
+
Information in the manner specified by section 6 of the GNU GPL
|
126
|
+
for conveying Corresponding Source.)
|
127
|
+
|
128
|
+
5. Combined Libraries.
|
129
|
+
|
130
|
+
You may place library facilities that are a work based on the
|
131
|
+
Library side by side in a single library together with other library
|
132
|
+
facilities that are not Applications and are not covered by this
|
133
|
+
License, and convey such a combined library under terms of your
|
134
|
+
choice, if you do both of the following:
|
135
|
+
|
136
|
+
a) Accompany the combined library with a copy of the same work based
|
137
|
+
on the Library, uncombined with any other library facilities,
|
138
|
+
conveyed under the terms of this License.
|
139
|
+
|
140
|
+
b) Give prominent notice with the combined library that part of it
|
141
|
+
is a work based on the Library, and explaining where to find the
|
142
|
+
accompanying uncombined form of the same work.
|
143
|
+
|
144
|
+
6. Revised Versions of the GNU Lesser General Public License.
|
145
|
+
|
146
|
+
The Free Software Foundation may publish revised and/or new versions
|
147
|
+
of the GNU Lesser General Public License from time to time. Such new
|
148
|
+
versions will be similar in spirit to the present version, but may
|
149
|
+
differ in detail to address new problems or concerns.
|
150
|
+
|
151
|
+
Each version is given a distinguishing version number. If the
|
152
|
+
Library as you received it specifies that a certain numbered version
|
153
|
+
of the GNU Lesser General Public License "or any later version"
|
154
|
+
applies to it, you have the option of following the terms and
|
155
|
+
conditions either of that published version or of any later version
|
156
|
+
published by the Free Software Foundation. If the Library as you
|
157
|
+
received it does not specify a version number of the GNU Lesser
|
158
|
+
General Public License, you may choose any version of the GNU Lesser
|
159
|
+
General Public License ever published by the Free Software Foundation.
|
160
|
+
|
161
|
+
If the Library as you received it specifies that a proxy can decide
|
162
|
+
whether future versions of the GNU Lesser General Public License shall
|
163
|
+
apply, that proxy's public statement of acceptance of any version is
|
164
|
+
permanent authorization for you to choose that version for the
|
165
|
+
Library.
|
data/README.md
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
# FastSeeder [![Build Status](https://secure.travis-ci.org/smartdict/smartdict-core.png)](http://travis-ci.org/greyblake/fast_seeder)
|
2
|
+
|
3
|
+
By [Sergey Potapov](https://github.com/greyblake)
|
4
|
+
|
5
|
+
Speeds up seeding database using multiple SQL inserts!
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add to Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem "fast_seeder"
|
13
|
+
```
|
14
|
+
|
15
|
+
|
16
|
+
## Usage
|
17
|
+
|
18
|
+
### Seeding from CSV file
|
19
|
+
|
20
|
+
Lets say you have CSV file `db/seeds/cities/ukraine.csv`:
|
21
|
+
|
22
|
+
```csv
|
23
|
+
Kharkov,1654
|
24
|
+
Lviv,1240
|
25
|
+
Kiev,600
|
26
|
+
```
|
27
|
+
|
28
|
+
You can populate DB with it using FastSeeder:
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
# Seeding from CSV file:
|
32
|
+
FastSeeder.seed_csv!(City, "cities/ukraine.csv", :name, :founded_in, :country => "Ukraine")
|
33
|
+
```
|
34
|
+
|
35
|
+
Technically it equals to:
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
City.create(:name => "Kharkov", :founded_in => 1654, :country => "Ukraine")
|
39
|
+
City.create(:name => "Lviv" , :founded_in => 1240, :country => "Ukraine")
|
40
|
+
City.create(:name => "Kiev" , :founded_in => 600 , :country => "Ukraine")
|
41
|
+
```
|
42
|
+
|
43
|
+
But does the job faster using only one SQL query instead of three.
|
44
|
+
As you also noticed it expects you to pass model class, path to CSV file(located in `db/seeds`),
|
45
|
+
CSV columns, and default values.
|
46
|
+
|
47
|
+
### Seeding in place
|
48
|
+
|
49
|
+
The next example is equivalent to showed above one, but uses inline data
|
50
|
+
without CSV file:
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
# Seeding in place:
|
54
|
+
FastSeeder.seed!(City, :name, :founded_in, :country => "Ukraine") do
|
55
|
+
record "Kharkov", 1654
|
56
|
+
record "Lviv" , 1240
|
57
|
+
record "Kiev" , 600
|
58
|
+
end
|
59
|
+
```
|
60
|
+
|
61
|
+
## Supported database adapters
|
62
|
+
|
63
|
+
* PostgreSQL
|
64
|
+
* MySQL(mysql, mysql2)
|
65
|
+
* SQLite3
|
66
|
+
|
67
|
+
## Running specs
|
68
|
+
|
69
|
+
```
|
70
|
+
rake spec:all # run specs with all supported adapters
|
71
|
+
rake spec:mysql # run specs with mysql adapter
|
72
|
+
rake spec:mysql2 # run specs with mysql2 adapter
|
73
|
+
rake spec:postgresql # run specs with postgresql adapter
|
74
|
+
rake spec:sqlite3 # run specs with sqlite3 adapter
|
75
|
+
```
|
76
|
+
|
77
|
+
## Credits
|
78
|
+
|
79
|
+
* [Sergey Potapov](https://github.com/greyblake) - creator and maintainer
|
80
|
+
|
81
|
+
## License
|
82
|
+
|
83
|
+
This program is free software: you can redistribute it and/or modify
|
84
|
+
it under the terms of the GNU Lesser General Public License as published by
|
85
|
+
the Free Software Foundation, either version 3 of the License, or
|
86
|
+
(at your option) any later version.
|
87
|
+
|
88
|
+
This program is distributed in the hope that it will be useful,
|
89
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
90
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
91
|
+
GNU Lesser General Public License for more details.
|
92
|
+
|
93
|
+
You should have received a copy of the GNU Lesser General Public License
|
94
|
+
along with this program. If not, see <http://www.gnu.org/licenses/lgpl.txt>
|
data/Rakefile
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
begin
|
3
|
+
require 'bundler/setup'
|
4
|
+
rescue LoadError
|
5
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
|
+
end
|
7
|
+
|
8
|
+
APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
|
9
|
+
load 'rails/tasks/engine.rake'
|
10
|
+
|
11
|
+
Bundler::GemHelper.install_tasks
|
12
|
+
|
13
|
+
require 'rspec/core'
|
14
|
+
require 'rspec/core/rake_task'
|
15
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
16
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
17
|
+
end
|
18
|
+
|
19
|
+
Rake::Task[:spec].enhance ['db:setup']
|
20
|
+
|
21
|
+
namespace :spec do
|
22
|
+
desc "run specs with postgresql adapter"
|
23
|
+
task :postgresql do
|
24
|
+
ENV['ADAPTER'] = 'postgresql'
|
25
|
+
Rake::Task[:spec].invoke
|
26
|
+
end
|
27
|
+
|
28
|
+
desc "run specs with mysql adapter"
|
29
|
+
task :mysql do
|
30
|
+
ENV['ADAPTER'] = 'mysql'
|
31
|
+
Rake::Task[:spec].invoke
|
32
|
+
end
|
33
|
+
|
34
|
+
desc "run specs with mysql2 adapter"
|
35
|
+
task :mysql2 do
|
36
|
+
ENV['ADAPTER'] = 'mysql2'
|
37
|
+
Rake::Task[:spec].invoke
|
38
|
+
end
|
39
|
+
|
40
|
+
desc "run specs with sqlite3 adapter"
|
41
|
+
task :sqlite3 do
|
42
|
+
ENV['ADAPTER'] = 'sqlite3'
|
43
|
+
Rake::Task[:spec].invoke
|
44
|
+
end
|
45
|
+
|
46
|
+
desc "run specs with all supported adapters"
|
47
|
+
task :all do
|
48
|
+
adapters = %w(postgresql mysql mysql2 sqlite3)
|
49
|
+
adapters.each do |adapter|
|
50
|
+
puts "=" * 80
|
51
|
+
puts adapter.center(80)
|
52
|
+
puts "=" * 80
|
53
|
+
system "rake spec:#{adapter}"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
|
59
|
+
task :default => 'spec:all'
|
data/lib/fast_seeder.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'csv'
|
2
|
+
require 'active_support/all'
|
3
|
+
|
4
|
+
module FastSeeder
|
5
|
+
extend self
|
6
|
+
extend ActiveSupport::Autoload
|
7
|
+
|
8
|
+
autoload :Seeders
|
9
|
+
autoload :Adapters
|
10
|
+
autoload :RecordSet
|
11
|
+
|
12
|
+
|
13
|
+
def self.seed_csv!(model_class, csv_file, *colums_and_default_values)
|
14
|
+
default_values = colums_and_default_values.extract_options!
|
15
|
+
columns = colums_and_default_values
|
16
|
+
seeder = Seeders::CsvSeeder.new(model_class, columns, default_values, csv_file)
|
17
|
+
seeder.seed!
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.seed!(model_class, *colums_and_default_values, &block)
|
21
|
+
default_values = colums_and_default_values.extract_options!
|
22
|
+
columns = colums_and_default_values
|
23
|
+
seeder = Seeders::InlineSeeder.new(model_class, columns, default_values, block)
|
24
|
+
seeder.seed!
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
def seeds_path
|
29
|
+
Rails.root + "db/seeds"
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
module FastSeeder
|
2
|
+
module Adapters
|
3
|
+
class BaseAdapter
|
4
|
+
def initialize(conn, record_set)
|
5
|
+
@conn = conn
|
6
|
+
@record_set = record_set
|
7
|
+
end
|
8
|
+
|
9
|
+
def insert!
|
10
|
+
sql = build_sql
|
11
|
+
@conn.execute(sql)
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def build_sql
|
18
|
+
columns_str = build_columns_str
|
19
|
+
values_str = build_values_str
|
20
|
+
"INSERT INTO #{@record_set.table} (#{columns_str}) VALUES #{values_str}"
|
21
|
+
end
|
22
|
+
|
23
|
+
def build_columns_str
|
24
|
+
column_names = @record_set.columns
|
25
|
+
column_names << 'created_at' if process_created_at?
|
26
|
+
column_names << 'updated_at' if process_updated_at?
|
27
|
+
column_names.join(", ")
|
28
|
+
end
|
29
|
+
|
30
|
+
def build_values_str
|
31
|
+
values = @record_set.values.map do |vals|
|
32
|
+
build_record_values_str(vals)
|
33
|
+
end
|
34
|
+
values.join(", ")
|
35
|
+
end
|
36
|
+
|
37
|
+
def build_record_values_str(values)
|
38
|
+
vals = values.dup
|
39
|
+
vals << Time.zone.now if process_created_at?
|
40
|
+
vals << Time.zone.now if process_updated_at?
|
41
|
+
quoted_vals = vals.map { |val| @conn.quote(val) }
|
42
|
+
joint_vals = quoted_vals.join(", ")
|
43
|
+
"(#{joint_vals})"
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
def process_created_at?
|
48
|
+
@process_created_at ||= process_timestamp?('created_at')
|
49
|
+
end
|
50
|
+
|
51
|
+
def process_updated_at?
|
52
|
+
@process_updated_at ||= process_timestamp?('updated_at')
|
53
|
+
end
|
54
|
+
|
55
|
+
def process_timestamp?(column_name)
|
56
|
+
@record_set.model_class.column_names.include?(column_name.to_s) &&
|
57
|
+
!@record_set.columns.include?(column_name.to_s)
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
module FastSeeder
|
2
|
+
module Adapters
|
3
|
+
class SqliteAdapter < BaseAdapter
|
4
|
+
|
5
|
+
private
|
6
|
+
|
7
|
+
# Build something similar to this:
|
8
|
+
# INSERT INTO 'tablename' (column1, column2)
|
9
|
+
# SELECT 'data1' AS 'column1', 'data2' AS 'column2'
|
10
|
+
# UNION SELECT 'data3', 'data4'
|
11
|
+
# UNION SELECT 'data5', 'data6'
|
12
|
+
def build_sql
|
13
|
+
values_str = build_values_str
|
14
|
+
columns_str = build_columns_str
|
15
|
+
"INSERT INTO '#{@record_set.table}' (#{columns_str})\n#{values_str}"
|
16
|
+
end
|
17
|
+
|
18
|
+
# Build values part like:
|
19
|
+
# SELECT 'data1' AS 'column1', 'data2' AS 'column2'
|
20
|
+
# UNION SELECT 'data3', 'data4'
|
21
|
+
# UNION SELECT 'data5', 'data6'
|
22
|
+
def build_values_str
|
23
|
+
first_record_vals = @record_set.values.first
|
24
|
+
other_vals = @record_set.values[1..-1]
|
25
|
+
first_str = build_first_record_srt(first_record_vals)
|
26
|
+
other_str = build_other_records_str(other_vals)
|
27
|
+
"#{first_str}\n#{other_str}"
|
28
|
+
end
|
29
|
+
|
30
|
+
# Build first line of values part like:
|
31
|
+
# SELECT 'data1' AS 'column1', 'data2' AS 'column2'
|
32
|
+
def build_first_record_srt(record_vals)
|
33
|
+
pairs = ActiveSupport::OrderedHash[@record_set.columns.zip(record_vals)]
|
34
|
+
pairs['created_at'] = Time.zone.now if process_created_at?
|
35
|
+
pairs['updated_at'] = Time.zone.now if process_updated_at?
|
36
|
+
joint_pairs = pairs.map do |column, value|
|
37
|
+
quoted_value = @conn.quote(value)
|
38
|
+
"#{quoted_value} AS '#{column}'"
|
39
|
+
end
|
40
|
+
data_str = joint_pairs.join(", ")
|
41
|
+
"SELECT #{data_str}"
|
42
|
+
end
|
43
|
+
|
44
|
+
# Build values part like this:
|
45
|
+
# UNION SELECT 'data3', 'data4'
|
46
|
+
# UNION SELECT 'data5', 'data6'
|
47
|
+
def build_other_records_str(records_vals)
|
48
|
+
records_vals.map do |record_vals|
|
49
|
+
build_record_str(record_vals)
|
50
|
+
end.join("\n")
|
51
|
+
end
|
52
|
+
|
53
|
+
# Build line of values part like:
|
54
|
+
# UNION SELECT 'data5', 'data6'
|
55
|
+
def build_record_str(record_vals)
|
56
|
+
vals = record_vals.dup
|
57
|
+
vals << Time.zone.now if process_created_at?
|
58
|
+
vals << Time.zone.now if process_updated_at?
|
59
|
+
data = vals.map { |val| @conn.quote(val) }.join(", ")
|
60
|
+
"UNION SELECT #{data}"
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module FastSeeder
|
2
|
+
class RecordSet
|
3
|
+
attr_reader :columns, :values, :model_class
|
4
|
+
|
5
|
+
def initialize(model_class, columns)
|
6
|
+
@model_class = model_class
|
7
|
+
@columns = columns.map(&:to_s)
|
8
|
+
@values = []
|
9
|
+
end
|
10
|
+
|
11
|
+
def <<(values)
|
12
|
+
@values << values
|
13
|
+
end
|
14
|
+
|
15
|
+
def table
|
16
|
+
@model_class.table_name
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module FastSeeder
|
2
|
+
module Seeders
|
3
|
+
class BaseSeeder
|
4
|
+
def initialize(model_class, declared_columns, default_values)
|
5
|
+
@model_class = model_class
|
6
|
+
@declared_columns = declared_columns
|
7
|
+
|
8
|
+
# Ordered hash is necessary to not mess columns and wrong values
|
9
|
+
@default_values = ActiveSupport::OrderedHash[*default_values.to_a.flatten]
|
10
|
+
end
|
11
|
+
|
12
|
+
def seed!
|
13
|
+
record_set = RecordSet.new(@model_class, columns)
|
14
|
+
specific_values.each do |record_vals|
|
15
|
+
record_set << merged_values_for(record_vals)
|
16
|
+
end
|
17
|
+
adapter = adapter_class.new(conn, record_set)
|
18
|
+
adapter.insert!
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def specific_values
|
24
|
+
raise NotImplementedError
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
def columns
|
29
|
+
@columns ||= @declared_columns + @default_values.keys
|
30
|
+
end
|
31
|
+
|
32
|
+
def merged_values_for(specific_vals)
|
33
|
+
specific_vals + @default_values.values
|
34
|
+
end
|
35
|
+
|
36
|
+
def adapter_class
|
37
|
+
case conn.adapter_name
|
38
|
+
when "PostgreSQL" then Adapters::PostgresqlAdapter
|
39
|
+
when "Mysql2", "MySQL" then Adapters::MysqlAdapter
|
40
|
+
when "SQLite" then Adapters::SqliteAdapter
|
41
|
+
else raise("Unsupported connection adapter: #{conn.adapter_name}")
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def conn
|
46
|
+
ActiveRecord::Base.connection
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module FastSeeder
|
2
|
+
module Seeders
|
3
|
+
class CsvSeeder < BaseSeeder
|
4
|
+
def initialize(model_class, declared_columns, default_values, csv_file)
|
5
|
+
super(model_class, declared_columns, default_values)
|
6
|
+
@csv_file = File.join(FastSeeder.seeds_path, csv_file)
|
7
|
+
end
|
8
|
+
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def specific_values
|
13
|
+
CSV.read(@csv_file)
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module FastSeeder
|
2
|
+
module Seeders
|
3
|
+
class InlineSeeder < BaseSeeder
|
4
|
+
class DataContext
|
5
|
+
attr_reader :records
|
6
|
+
|
7
|
+
def initialize(data_block)
|
8
|
+
@records = []
|
9
|
+
instance_eval(&data_block)
|
10
|
+
end
|
11
|
+
|
12
|
+
def record(*args)
|
13
|
+
@records << args
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
def initialize(model_class, declared_columns, default_values, data_block)
|
19
|
+
super(model_class, declared_columns, default_values)
|
20
|
+
@data_block = data_block
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def specific_values
|
27
|
+
DataContext.new(@data_block).records
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fast_seeder
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Sergey Potapov
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-08-05 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rails
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ~>
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 7
|
29
|
+
segments:
|
30
|
+
- 3
|
31
|
+
- 0
|
32
|
+
version: "3.0"
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
description: Speeds up seeding database using multiple SQL inserts
|
36
|
+
email:
|
37
|
+
- blake131313@gmail.com
|
38
|
+
executables: []
|
39
|
+
|
40
|
+
extensions: []
|
41
|
+
|
42
|
+
extra_rdoc_files: []
|
43
|
+
|
44
|
+
files:
|
45
|
+
- lib/fast_seeder/version.rb
|
46
|
+
- lib/fast_seeder/seeders.rb
|
47
|
+
- lib/fast_seeder/seeders/inline_seeder.rb
|
48
|
+
- lib/fast_seeder/seeders/csv_seeder.rb
|
49
|
+
- lib/fast_seeder/seeders/base_seeder.rb
|
50
|
+
- lib/fast_seeder/adapters.rb
|
51
|
+
- lib/fast_seeder/record_set.rb
|
52
|
+
- lib/fast_seeder/adapters/postgresql_adapter.rb
|
53
|
+
- lib/fast_seeder/adapters/sqlite_adapter.rb
|
54
|
+
- lib/fast_seeder/adapters/mysql_adapter.rb
|
55
|
+
- lib/fast_seeder/adapters/base_adapter.rb
|
56
|
+
- lib/fast_seeder.rb
|
57
|
+
- LGPL-LICENSE
|
58
|
+
- Rakefile
|
59
|
+
- README.md
|
60
|
+
homepage: https://github.com/greyblake/fast_seeder
|
61
|
+
licenses: []
|
62
|
+
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
hash: 3
|
74
|
+
segments:
|
75
|
+
- 0
|
76
|
+
version: "0"
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
hash: 3
|
83
|
+
segments:
|
84
|
+
- 0
|
85
|
+
version: "0"
|
86
|
+
requirements: []
|
87
|
+
|
88
|
+
rubyforge_project:
|
89
|
+
rubygems_version: 1.8.15
|
90
|
+
signing_key:
|
91
|
+
specification_version: 3
|
92
|
+
summary: Speeds up seeding database using multiple SQL inserts
|
93
|
+
test_files: []
|
94
|
+
|