load_data_infile 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION +1 -1
- data/lib/load_data_infile.rb +7 -0
- data/spec/load_data_infile_spec.rb +21 -27
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/lib/load_data_infile.rb
CHANGED
@@ -49,7 +49,10 @@ module LoadDataInfile
|
|
49
49
|
# optionally_enclosed_by :: [OPTIONAL] Character
|
50
50
|
# table :: [OPTIONAL] Table name. Defaults to quoted_table_name (won't work if used from an abstract class, e.g. ActiveRecord::Base')
|
51
51
|
# terminated_by :: [OPTIONAL] Character
|
52
|
+
# disable_keys :: [OPTIONAL] true or false. Defaults to true. Disables foreign keys while running the import.
|
52
53
|
def load_data_infile(options = {})
|
54
|
+
disable_keys_option = !options.member?(:disable_keys) || options[:disable_keys]
|
55
|
+
|
53
56
|
c = Context.new
|
54
57
|
|
55
58
|
if options[:low_priority]
|
@@ -91,7 +94,11 @@ module LoadDataInfile
|
|
91
94
|
c.mappings = "SET #{s}"
|
92
95
|
end
|
93
96
|
|
97
|
+
disable_keys(c.table_name) if disable_keys_option
|
98
|
+
|
94
99
|
connection.execute(ERB.new(LOAD_DATA_INFILE_SQL).result(c.binding).gsub(/^\s*\n/, ""))
|
100
|
+
ensure
|
101
|
+
enable_keys(c.table_name) if disable_keys_option
|
95
102
|
end
|
96
103
|
|
97
104
|
class Context < OpenStruct
|
@@ -6,14 +6,12 @@ describe LoadDataInfile do
|
|
6
6
|
end
|
7
7
|
|
8
8
|
it "loads data from a csv file with headers into an ActiveRecord table" do
|
9
|
-
Thing.
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
)
|
16
|
-
end
|
9
|
+
Thing.load_data_infile(
|
10
|
+
:path => FIXTURE_WITH_HEADERS,
|
11
|
+
:columns => %w|id field_a field_b field_c|,
|
12
|
+
:terminated_by => ",",
|
13
|
+
:ignore => 1
|
14
|
+
)
|
17
15
|
Thing.all.map(&:attributes).should == [{
|
18
16
|
"id" => 71,
|
19
17
|
"field_a" => "Hello",
|
@@ -23,13 +21,11 @@ describe LoadDataInfile do
|
|
23
21
|
end
|
24
22
|
|
25
23
|
it "loads data from a csv file without headers into an ActiveRecord table" do
|
26
|
-
Thing.
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
)
|
32
|
-
end
|
24
|
+
Thing.load_data_infile(
|
25
|
+
:path => FIXTURE_WITHOUT_HEADERS,
|
26
|
+
:terminated_by => ",",
|
27
|
+
:columns => %w|id field_a field_b field_c|
|
28
|
+
)
|
33
29
|
Thing.all.map(&:attributes).should == [{
|
34
30
|
"id" => 61,
|
35
31
|
"field_a" => "live",
|
@@ -39,18 +35,16 @@ describe LoadDataInfile do
|
|
39
35
|
end
|
40
36
|
|
41
37
|
it "loads data from a csv file with mapping" do
|
42
|
-
Thing.
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
)
|
53
|
-
end
|
38
|
+
Thing.load_data_infile(
|
39
|
+
:path => FIXTURE_WITHOUT_HEADERS,
|
40
|
+
:terminated_by => ",",
|
41
|
+
:columns => %w|id @field_a @field_b @field_c|,
|
42
|
+
:mappings => {
|
43
|
+
:field_a => "CONCAT('So ', @field_a)",
|
44
|
+
:field_b => "CONCAT('Much ', @field_b)",
|
45
|
+
:field_c => "@field_c * 10",
|
46
|
+
}
|
47
|
+
)
|
54
48
|
Thing.all.map(&:attributes).should == [{
|
55
49
|
"id" => 61,
|
56
50
|
"field_a" => "So live",
|