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 CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
@@ -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.with_keys_disabled do
10
- Thing.load_data_infile(
11
- :path => FIXTURE_WITH_HEADERS,
12
- :columns => %w|id field_a field_b field_c|,
13
- :terminated_by => ",",
14
- :ignore => 1
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.with_keys_disabled do
27
- Thing.load_data_infile(
28
- :path => FIXTURE_WITHOUT_HEADERS,
29
- :terminated_by => ",",
30
- :columns => %w|id field_a field_b field_c|
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.with_keys_disabled do
43
- Thing.load_data_infile(
44
- :path => FIXTURE_WITHOUT_HEADERS,
45
- :terminated_by => ",",
46
- :columns => %w|id @field_a @field_b @field_c|,
47
- :mappings => {
48
- :field_a => "CONCAT('So ', @field_a)",
49
- :field_b => "CONCAT('Much ', @field_b)",
50
- :field_c => "@field_c * 10",
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",
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
7
+ - 2
8
8
  - 0
9
- version: 0.1.0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Emmanuel Oga