cranium 0.7 → 0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE.txt +1 -1
- data/cranium.gemspec +1 -1
- data/features/import/delete_from_table_based_on_csv.feature +39 -0
- data/lib/cranium/data_importer.rb +4 -2
- data/lib/cranium/dsl/import_definition.rb +1 -0
- data/lib/cranium/import_strategy.rb +1 -0
- data/lib/cranium/import_strategy/delete.rb +34 -0
- data/spec/cranium/data_importer_spec.rb +26 -5
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3d6f7888fe46d32c156a48c081e57ddf8942d483b6a42c1b69faa4e0f276a128
|
4
|
+
data.tar.gz: 9f49d912ca7cf12a8d3e90e1e6d00565099f4bf0fe4cee90e29f439c345a86df
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fb4ed093460aaf4eddde16e0ed1236e34d232ef128047683111459a836c7c544a4c5fe3ad5d05abb6609927affaf286c88f8269695153f21c1513b4c802445ea
|
7
|
+
data.tar.gz: 65730273e55b87a3ba2b35f303f21613af3e7cf9328a8f7612cee60c37c54e606f2d50a3891597275ea084ce4405bc445c3d7277d2f5cb3d31b7be1ae805a740
|
data/LICENSE.txt
CHANGED
data/cranium.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |spec|
|
2
2
|
spec.name = 'cranium'
|
3
|
-
spec.version = '0.
|
3
|
+
spec.version = '0.8'
|
4
4
|
spec.authors = ['Emarsys Technologies']
|
5
5
|
spec.email = ['smart-insight-dev@emarsys.com']
|
6
6
|
spec.description = %q{Provides Extract, Transform and Load functionality for loading data from CSV files to a Greenplum database.}
|
@@ -0,0 +1,39 @@
|
|
1
|
+
Feature: Delete rows from table provided by CSV file
|
2
|
+
|
3
|
+
Scenario: Successful delete
|
4
|
+
Given a database table called "dim_contact" with the following fields:
|
5
|
+
| field_name | field_type |
|
6
|
+
| source_id | TEXT |
|
7
|
+
And only the following rows in the "dim_contact" database table:
|
8
|
+
| source_id (i) |
|
9
|
+
| 1 |
|
10
|
+
| 2 |
|
11
|
+
| 3 |
|
12
|
+
| 4 |
|
13
|
+
| 5 |
|
14
|
+
And a "deleted_contacts_extract.csv" data file containing:
|
15
|
+
"""
|
16
|
+
source_id
|
17
|
+
3
|
18
|
+
4
|
19
|
+
"""
|
20
|
+
And the following definition:
|
21
|
+
"""
|
22
|
+
source :deleted_contacts_extract do
|
23
|
+
field :source_id, String
|
24
|
+
end
|
25
|
+
|
26
|
+
import :deleted_contacts_extract do
|
27
|
+
into :dim_contact
|
28
|
+
put :source_id
|
29
|
+
|
30
|
+
delete_on :source_id
|
31
|
+
end
|
32
|
+
"""
|
33
|
+
When I execute the definition
|
34
|
+
Then the process should exit successfully
|
35
|
+
And the "dim_contact" table should contain:
|
36
|
+
| source_id |
|
37
|
+
| 1 |
|
38
|
+
| 2 |
|
39
|
+
| 5 |
|
@@ -17,14 +17,16 @@ class Cranium::DataImporter
|
|
17
17
|
private
|
18
18
|
|
19
19
|
def importer_for_definition(import_definition)
|
20
|
-
if [!import_definition.merge_fields.empty?, !import_definition.delete_insert_on.empty?, import_definition.truncate_insert].count(true) > 1
|
21
|
-
raise StandardError, "Import should not combine merge_on, delete_insert_on and truncate_insert settings"
|
20
|
+
if [!import_definition.merge_fields.empty?, !import_definition.delete_insert_on.empty?, !import_definition.delete_on.empty?, import_definition.truncate_insert].count(true) > 1
|
21
|
+
raise StandardError, "Import should not combine merge_on, delete_insert_on, delete_on and truncate_insert settings"
|
22
22
|
end
|
23
23
|
|
24
24
|
if !import_definition.merge_fields.empty?
|
25
25
|
Cranium::ImportStrategy::Merge.new(import_definition)
|
26
26
|
elsif !import_definition.delete_insert_on.empty?
|
27
27
|
Cranium::ImportStrategy::DeleteInsert.new(import_definition)
|
28
|
+
elsif !import_definition.delete_on.empty?
|
29
|
+
Cranium::ImportStrategy::Delete.new(import_definition)
|
28
30
|
elsif import_definition.truncate_insert
|
29
31
|
Cranium::ImportStrategy::TruncateInsert.new(import_definition)
|
30
32
|
else
|
@@ -2,6 +2,7 @@ module Cranium::ImportStrategy
|
|
2
2
|
|
3
3
|
autoload :Base, 'cranium/import_strategy/base'
|
4
4
|
autoload :DeleteInsert, 'cranium/import_strategy/delete_insert'
|
5
|
+
autoload :Delete, 'cranium/import_strategy/delete'
|
5
6
|
autoload :TruncateInsert, 'cranium/import_strategy/truncate_insert'
|
6
7
|
autoload :Delta, 'cranium/import_strategy/delta'
|
7
8
|
autoload :Merge, 'cranium/import_strategy/merge'
|
@@ -0,0 +1,34 @@
|
|
1
|
+
class Cranium::ImportStrategy::Delete < Cranium::ImportStrategy::Base
|
2
|
+
|
3
|
+
def import_from(source_table)
|
4
|
+
@source_table = source_table
|
5
|
+
|
6
|
+
delete_existing_records
|
7
|
+
puts @source_table
|
8
|
+
database[@source_table].count
|
9
|
+
end
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def delete_existing_records
|
16
|
+
database.
|
17
|
+
from(Sequel.as(target_table, "target"), Sequel.as(@source_table, "source")).
|
18
|
+
where(delete_by_fields.qualify keys_with: :source, values_with: :target).
|
19
|
+
delete
|
20
|
+
end
|
21
|
+
|
22
|
+
|
23
|
+
|
24
|
+
def delete_by_fields
|
25
|
+
Cranium::Sequel::Hash[delete_field_mapping]
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
|
30
|
+
def delete_field_mapping
|
31
|
+
import_definition.field_associations.select { |_, target_field| import_definition.delete_on.include? target_field }
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
@@ -1,11 +1,12 @@
|
|
1
1
|
require_relative '../spec_helper'
|
2
2
|
|
3
3
|
describe Cranium::DataImporter do
|
4
|
+
let(:connection) { double 'a_connection' }
|
4
5
|
|
5
6
|
before do
|
6
|
-
connection = double
|
7
7
|
allow(Cranium::Database).to receive(:connection).and_return connection
|
8
8
|
allow(connection).to receive(:transaction).and_yield
|
9
|
+
allow(Cranium.application).to receive(:apply_hook).with :after_import
|
9
10
|
end
|
10
11
|
|
11
12
|
let(:importer) { Cranium::DataImporter.new }
|
@@ -13,12 +14,32 @@ describe Cranium::DataImporter do
|
|
13
14
|
|
14
15
|
describe "#import" do
|
15
16
|
|
17
|
+
context "when called with delete_on strategy" do
|
18
|
+
it "calls Delete strategy" do
|
19
|
+
import_strategy = instance_double Cranium::ImportStrategy::Delete
|
20
|
+
allow(Cranium::ImportStrategy::Delete).to receive(:new).with(definition).and_return import_strategy
|
21
|
+
expect(import_strategy).to receive(:import).and_return 0
|
22
|
+
definition.delete_on :source_id
|
23
|
+
|
24
|
+
importer.import definition
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context "when called with both merge and delete_on fields set" do
|
29
|
+
it "should raise an exception" do
|
30
|
+
definition.delete_on :source_id
|
31
|
+
definition.merge_on :another_field
|
32
|
+
|
33
|
+
expect { importer.import(definition) }.to raise_error StandardError, "Import should not combine merge_on, delete_insert_on, delete_on and truncate_insert settings"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
16
37
|
context "when called with both merge and delete_insert fields set" do
|
17
38
|
it "should raise an exception" do
|
18
39
|
definition.delete_insert_on :some_field
|
19
40
|
definition.merge_on :another_field
|
20
41
|
|
21
|
-
expect { importer.import(definition) }.to raise_error StandardError, "Import should not combine merge_on, delete_insert_on and truncate_insert settings"
|
42
|
+
expect { importer.import(definition) }.to raise_error StandardError, "Import should not combine merge_on, delete_insert_on, delete_on and truncate_insert settings"
|
22
43
|
end
|
23
44
|
end
|
24
45
|
|
@@ -27,7 +48,7 @@ describe Cranium::DataImporter do
|
|
27
48
|
definition.truncate_insert true
|
28
49
|
definition.merge_on :another_field
|
29
50
|
|
30
|
-
expect { importer.import(definition) }.to raise_error StandardError, "Import should not combine merge_on, delete_insert_on and truncate_insert settings"
|
51
|
+
expect { importer.import(definition) }.to raise_error StandardError, "Import should not combine merge_on, delete_insert_on, delete_on and truncate_insert settings"
|
31
52
|
end
|
32
53
|
end
|
33
54
|
|
@@ -36,7 +57,7 @@ describe Cranium::DataImporter do
|
|
36
57
|
definition.delete_insert_on :some_field
|
37
58
|
definition.truncate_insert true
|
38
59
|
|
39
|
-
expect { importer.import(definition) }.to raise_error StandardError, "Import should not combine merge_on, delete_insert_on and truncate_insert settings"
|
60
|
+
expect { importer.import(definition) }.to raise_error StandardError, "Import should not combine merge_on, delete_insert_on, delete_on and truncate_insert settings"
|
40
61
|
end
|
41
62
|
end
|
42
63
|
|
@@ -46,7 +67,7 @@ describe Cranium::DataImporter do
|
|
46
67
|
definition.merge_on :another_field
|
47
68
|
definition.truncate_insert true
|
48
69
|
|
49
|
-
expect { importer.import(definition) }.to raise_error StandardError, "Import should not combine merge_on, delete_insert_on and truncate_insert settings"
|
70
|
+
expect { importer.import(definition) }.to raise_error StandardError, "Import should not combine merge_on, delete_insert_on, delete_on and truncate_insert settings"
|
50
71
|
end
|
51
72
|
end
|
52
73
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cranium
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.8'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Emarsys Technologies
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-10-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pg
|
@@ -187,6 +187,7 @@ files:
|
|
187
187
|
- features/archive.feature
|
188
188
|
- features/extract/incremental_extract.feature
|
189
189
|
- features/extract/simple_extract.feature
|
190
|
+
- features/import/delete_from_table_based_on_csv.feature
|
190
191
|
- features/import/import_csv_to_database_as_delta.feature
|
191
192
|
- features/import/import_csv_to_database_with_delete_insert_merging.feature
|
192
193
|
- features/import/import_csv_to_database_with_truncate_insert.feature
|
@@ -246,6 +247,7 @@ files:
|
|
246
247
|
- lib/cranium/file_utils.rb
|
247
248
|
- lib/cranium/import_strategy.rb
|
248
249
|
- lib/cranium/import_strategy/base.rb
|
250
|
+
- lib/cranium/import_strategy/delete.rb
|
249
251
|
- lib/cranium/import_strategy/delete_insert.rb
|
250
252
|
- lib/cranium/import_strategy/delta.rb
|
251
253
|
- lib/cranium/import_strategy/merge.rb
|
@@ -317,7 +319,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
317
319
|
- !ruby/object:Gem::Version
|
318
320
|
version: '0'
|
319
321
|
requirements: []
|
320
|
-
rubygems_version: 3.0.
|
322
|
+
rubygems_version: 3.0.3
|
321
323
|
signing_key:
|
322
324
|
specification_version: 4
|
323
325
|
summary: Pure Ruby ETL framework
|
@@ -325,6 +327,7 @@ test_files:
|
|
325
327
|
- features/archive.feature
|
326
328
|
- features/extract/incremental_extract.feature
|
327
329
|
- features/extract/simple_extract.feature
|
330
|
+
- features/import/delete_from_table_based_on_csv.feature
|
328
331
|
- features/import/import_csv_to_database_as_delta.feature
|
329
332
|
- features/import/import_csv_to_database_with_delete_insert_merging.feature
|
330
333
|
- features/import/import_csv_to_database_with_truncate_insert.feature
|