Skip to content
This repository was archived by the owner on May 21, 2024. It is now read-only.

Commit c1c96ec

Browse files
committed
Merge pull request #60 from smeyfroi/master
Tidy up CSV destination
2 parents 2f0aa35 + 84fe68a commit c1c96ec

File tree

4 files changed

+29
-12
lines changed

4 files changed

+29
-12
lines changed

lib/etl/control/destination.rb

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,7 @@ def generators
9494

9595
# Get the order of elements from the source order
9696
def order_from_source
97-
order = []
98-
control.sources.first.definition.each do |item|
99-
case item
100-
when Hash
101-
order << item[:name]
102-
else
103-
order << item
104-
end
105-
end
106-
order
97+
control.sources.first.order
10798
end
10899

109100
# Return true if the row is allowed. The row will not be allowed if the

lib/etl/control/destination/csv_destination.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,13 @@ def initialize(control, configuration, mapping={})
4848
@enclose = true & configuration[:enclose]
4949
@unique = configuration[:unique] ? configuration[:unique] + scd_required_fields : configuration[:unique]
5050
@unique.uniq! unless @unique.nil?
51-
@order = mapping[:order] ? mapping[:order] + scd_required_fields : order_from_source
51+
@write_header = configuration[:write_header]
52+
@order = mapping[:order] + scd_required_fields if mapping[:order]
5253
@order.uniq! unless @order.nil?
53-
raise ControlError, "Order required in mapping" unless @order
54+
end
55+
56+
def order
57+
@order ||= order_from_source
5458
end
5559

5660
# Close the destination. This will flush the buffer and close the underlying stream or connection.
@@ -63,6 +67,11 @@ def close
6367
# Flush the destination buffer
6468
def flush
6569
#puts "Flushing buffer (#{file}) with #{buffer.length} rows"
70+
if @write_header && !@header_written
71+
f << order
72+
@header_written = true
73+
end
74+
6675
buffer.flatten.each do |row|
6776
#puts "row change type: #{row.change_type}"
6877
# check to see if this row's compound key constraint already exists

lib/etl/control/source.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,19 @@ def read_locally
125125
Engine.read_locally
126126
end
127127

128+
# Get the order of fields that this source will present to the pipeline
129+
def order
130+
order = []
131+
definition.each do |item|
132+
case item
133+
when Hash
134+
order << item[:name]
135+
else
136+
order << item
137+
end
138+
end
139+
order
140+
end
128141
end
129142
end
130143
end

lib/etl/control/source/file_source.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ def each
5353
end
5454
end
5555

56+
def order
57+
@parser.fields.collect {|field| field.name}
58+
end
59+
5660
private
5761
# Copy source data to a local directory structure
5862
def copy_sources

0 commit comments

Comments
 (0)