beetle_etl 0.0.13 → 0.0.16

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7fb51d510b64c866a5b928cda8cfac73f8970805
4
- data.tar.gz: 114785c8b95adc867d5ec3cae6839acf338df7b8
3
+ metadata.gz: 07d1e92398037e916f19762ae9a373de8cd3edfa
4
+ data.tar.gz: c064dfbfd3dfd4103864c51d8742f537963abf8c
5
5
  SHA512:
6
- metadata.gz: 983c54e043e1671a20d452438d9e15058806fba4a4567a935b7a1c275a7221b0bee2d685d3f4bccc2ac1b3f5aae9430280101f6d9e198644e6b439af1c9ec33e
7
- data.tar.gz: cc6c0bf17ac7c8969e8daf69acaed3b91a918d971e2bbb26a605365972889245c2bb956916ca129953840ec12823e8349982d7a3ebbd1cec9dfb6b4e70e0e2a0
6
+ metadata.gz: b984f80cdba06d018d49fa9414cf5443b8098b0edf34f1eb149df32f5c718f1af08676ca05743e064bf7e19475ad32a31459feedb68e0a45b091378c8c869665
7
+ data.tar.gz: 3b80f7d9ca88ed49130a6ef6719e9d65fb28d0dca101a2ee23e191bcb413dcf93ae213fefe194e736169cca2b2ab3e0a67444f975a741a7bf323944c4328fb1d
@@ -6,25 +6,16 @@ module BeetleETL
6
6
  end
7
7
 
8
8
  def run
9
- assign_new_ids
10
- map_existing_ids
11
- end
12
-
13
- def assign_new_ids
14
- database.execute <<-SQL
15
- UPDATE #{stage_table_name_sql}
16
- SET id = nextval('#{table_name}_id_seq')
17
- WHERE transition = 'CREATE'
18
- SQL
19
- end
20
-
21
- def map_existing_ids
22
9
  database.execute <<-SQL
23
- UPDATE #{stage_table_name_sql} stage
24
- SET id = public.id
25
- FROM #{public_table_name_sql} public
26
- WHERE stage.transition IN ('KEEP', 'UPDATE', 'DELETE', 'UNDELETE')
27
- AND stage.external_id = public.external_id
10
+ UPDATE #{stage_table_name_sql} stage_update
11
+ SET id = COALESCE(public.id, nextval('#{table_name}_id_seq'))
12
+ FROM #{stage_table_name_sql} stage
13
+ LEFT OUTER JOIN #{public_table_name_sql} public
14
+ on (
15
+ stage.external_id = public.external_id
16
+ AND public.external_source = '#{external_source}'
17
+ )
18
+ WHERE stage_update.external_id = stage.external_id
28
19
  SQL
29
20
  end
30
21
 
@@ -12,7 +12,7 @@ module BeetleETL
12
12
  end
13
13
 
14
14
  def run
15
- %w(create update delete undelete).each do |transition|
15
+ %w(create update delete reinstate).each do |transition|
16
16
  public_send(:"load_#{transition}")
17
17
  end
18
18
  end
@@ -63,7 +63,7 @@ module BeetleETL
63
63
  SQL
64
64
  end
65
65
 
66
- def load_undelete
66
+ def load_reinstate
67
67
  database.execute <<-SQL
68
68
  UPDATE #{public_table_name_sql} public
69
69
  SET
@@ -72,7 +72,7 @@ module BeetleETL
72
72
  deleted_at = NULL
73
73
  FROM #{stage_table_name_sql} stage
74
74
  WHERE stage.id = public.id
75
- AND stage.transition = 'UNDELETE'
75
+ AND stage.transition = 'REINSTATE'
76
76
  SQL
77
77
  end
78
78
 
@@ -11,7 +11,7 @@ module BeetleETL
11
11
  end
12
12
 
13
13
  def run
14
- %w(create keep update delete undelete).each do |transition|
14
+ %w(create update delete reinstate).each do |transition|
15
15
  public_send(:"transition_#{transition}")
16
16
  end
17
17
  end
@@ -29,24 +29,6 @@ module BeetleETL
29
29
  SQL
30
30
  end
31
31
 
32
- def transition_keep
33
- database.execute <<-SQL
34
- UPDATE #{stage_table_name_sql} stage
35
- SET transition = 'KEEP'
36
- WHERE EXISTS (
37
- SELECT 1
38
- FROM #{public_table_name} public
39
- WHERE public.external_id = stage.external_id
40
- AND public.external_source = '#{external_source}'
41
- AND public.deleted_at IS NULL
42
- AND
43
- (#{public_record_columns.join(', ')})
44
- IS NOT DISTINCT FROM
45
- (#{stage_record_columns.join(', ')})
46
- )
47
- SQL
48
- end
49
-
50
32
  def transition_update
51
33
  database.execute <<-SQL
52
34
  UPDATE #{stage_table_name_sql} stage
@@ -73,10 +55,7 @@ module BeetleETL
73
55
  public.external_id,
74
56
  'DELETE'
75
57
  FROM #{public_table_name_sql} public
76
- LEFT OUTER JOIN (
77
- SELECT *
78
- FROM #{stage_table_name_sql}
79
- ) stage
58
+ LEFT OUTER JOIN #{stage_table_name_sql} stage
80
59
  ON (stage.external_id = public.external_id)
81
60
  WHERE stage.external_id IS NULL
82
61
  AND public.external_source = '#{external_source}'
@@ -84,10 +63,10 @@ module BeetleETL
84
63
  SQL
85
64
  end
86
65
 
87
- def transition_undelete
66
+ def transition_reinstate
88
67
  database.execute <<-SQL
89
68
  UPDATE #{stage_table_name_sql} stage
90
- SET transition = 'UNDELETE'
69
+ SET transition = 'REINSTATE'
91
70
  WHERE EXISTS (
92
71
  SELECT 1
93
72
  FROM #{public_table_name_sql} public
@@ -1,3 +1,3 @@
1
1
  module BeetleETL
2
- VERSION = "0.0.13"
2
+ VERSION = "0.0.16"
3
3
  end
@@ -36,7 +36,7 @@ describe BeetleETL do
36
36
  end
37
37
 
38
38
  it 'performs all possible transitions', :feature do
39
- # create, keep, update, delete, undelete
39
+ # create, keep, update, delete, reinstate
40
40
 
41
41
  import1
42
42
  import2
@@ -108,7 +108,7 @@ describe BeetleETL do
108
108
  end
109
109
 
110
110
  def import3
111
- # undelete with update
111
+ # reinstate with update
112
112
  insert_into(:source__Organisation).values(
113
113
  [ :pkOrgId , :Name , :Adresse , :Abteilung ] ,
114
114
  [ 1 , 'Apple' , 'Apple Street' , 'iPhone' ] ,
@@ -4,6 +4,8 @@ module BeetleETL
4
4
  describe AssignIds do
5
5
 
6
6
  let(:external_source) { 'my_source' }
7
+ let(:another_source) { 'another_source' }
8
+
7
9
  subject { AssignIds.new(:example_table) }
8
10
 
9
11
  before do
@@ -12,19 +14,6 @@ module BeetleETL
12
14
  config.external_source = external_source
13
15
  config.database = test_database
14
16
  end
15
-
16
- test_database.create_table(subject.stage_table_name.to_sym) do
17
- Integer :id
18
- String :external_id, size: 255
19
- String :transition, size: 255
20
- end
21
-
22
- test_database.create_table(:example_table) do
23
- primary_key :id
24
- String :external_id, size: 255
25
- String :external_source, size: 255
26
- end
27
-
28
17
  end
29
18
 
30
19
  describe '#dependencies' do
@@ -34,66 +23,48 @@ module BeetleETL
34
23
  end
35
24
 
36
25
  describe '#run' do
37
- it 'runs all transitions' do
38
- %i(assign_new_ids map_existing_ids).each do |method|
39
- expect(subject).to receive(method)
26
+ before do
27
+ test_database.create_table(subject.stage_table_name.to_sym) do
28
+ Integer :id
29
+ String :external_id, size: 255
30
+ String :transition, size: 255
40
31
  end
41
32
 
42
- subject.run
33
+ test_database.create_table(:example_table) do
34
+ primary_key :id
35
+ String :external_id, size: 255
36
+ String :external_source, size: 255
37
+ end
43
38
  end
44
- end
45
39
 
46
- describe '#assign_new_ids' do
47
- it 'generates new ids for newly created records' do
48
- insert_into(:example_table).values(
49
- [ :external_id , :external_source ] ,
50
- [ 'keep_id' , external_source ] ,
51
- )
40
+ it 'assigns ids for' do
41
+ # - generated ones for new records
42
+ # - mapped ones by external_id for existing records
52
43
 
53
- insert_into(subject.stage_table_name.to_sym).values(
54
- [ :external_id , :transition ] ,
55
- [ 'create_id' , 'CREATE' ] ,
56
- [ 'keep_id' , 'KEEP' ] ,
57
- )
58
-
59
- subject.assign_new_ids
60
-
61
- expect(subject.stage_table_name.to_sym).to have_values(
62
- [ :id , :external_id , :transition ] ,
63
- [ 2 , 'create_id' , 'CREATE' ] ,
64
- [ nil , 'keep_id' , 'KEEP' ] ,
65
- )
66
- end
67
- end
68
-
69
- describe '#map_existing_ids' do
70
- it 'assigns ids for existing records by their external id' do
71
44
  insert_into(:example_table).values(
72
- [ :external_id , :external_source ] ,
73
- [ 'keep_id' , external_source ] ,
74
- [ 'update_id' , external_source ] ,
75
- [ 'delete_id' , external_source ] ,
76
- [ 'undelete_id' , external_source ] ,
45
+ [ :external_id , :external_source ] ,
46
+ [ 'a' , external_source ] ,
47
+ [ 'b' , external_source ] ,
48
+ [ 'c' , external_source ] ,
49
+ [ 'd' , another_source ] ,
77
50
  )
78
51
 
79
52
  insert_into(subject.stage_table_name.to_sym).values(
80
- [ :external_id , :transition ] ,
81
- [ 'create_id' , 'CREATE' ] ,
82
- [ 'keep_id' , 'KEEP' ] ,
83
- [ 'update_id' , 'UPDATE' ] ,
84
- [ 'delete_id' , 'DELETE' ] ,
85
- [ 'undelete_id' , 'UNDELETE' ] ,
53
+ [ :external_id ] ,
54
+ [ 'new value' ] ,
55
+ [ 'a' ] ,
56
+ [ 'b' ] ,
57
+ [ 'c' ] ,
86
58
  )
87
59
 
88
- subject.map_existing_ids
60
+ subject.run
89
61
 
90
62
  expect(subject.stage_table_name.to_sym).to have_values(
91
- [ :id , :external_id , :transition ] ,
92
- [ nil , 'create_id' , 'CREATE' ] ,
93
- [ 1 , 'keep_id' , 'KEEP' ] ,
94
- [ 2 , 'update_id' , 'UPDATE' ] ,
95
- [ 3 , 'delete_id' , 'DELETE' ] ,
96
- [ 4 , 'undelete_id' , 'UNDELETE' ] ,
63
+ [ :id , :external_id ] ,
64
+ [ 1 , 'a' ] ,
65
+ [ 2 , 'b' ] ,
66
+ [ 3 , 'c' ] ,
67
+ [ 5 , 'new value' ] ,
97
68
  )
98
69
  end
99
70
  end
@@ -65,7 +65,7 @@ module BeetleETL
65
65
 
66
66
  describe '#run' do
67
67
  it 'runs all load steps' do
68
- %w(create update delete undelete).each do |transition|
68
+ %w(create update delete reinstate).each do |transition|
69
69
  expect(subject).to receive(:"load_#{transition}")
70
70
  end
71
71
 
@@ -131,7 +131,7 @@ module BeetleETL
131
131
  end
132
132
  end
133
133
 
134
- describe '#load_undelete' do
134
+ describe '#load_reinstate' do
135
135
  it 'restores deleted records' do
136
136
  insert_into(:example_table).values(
137
137
  [ :id , :external_id , :external_source , :foo_id , :created_at , :updated_at , :deleted_at , :payload ] ,
@@ -140,10 +140,10 @@ module BeetleETL
140
140
 
141
141
  insert_into(subject.stage_table_name.to_sym).values(
142
142
  [ :id , :external_id , :transition , :external_foo_id , :foo_id , :payload ] ,
143
- [ 1 , 'external_id' , 'UNDELETE' , 'foo_id' , 33 , 'updated content' ] ,
143
+ [ 1 , 'external_id' , 'REINSTATE' , 'foo_id' , 33 , 'updated content' ] ,
144
144
  )
145
145
 
146
- subject.load_undelete
146
+ subject.load_reinstate
147
147
 
148
148
  expect(:example_table).to have_values(
149
149
  [ :id , :external_id , :external_source , :foo_id , :created_at , :updated_at , :deleted_at , :payload ] ,
@@ -46,7 +46,7 @@ module BeetleETL
46
46
 
47
47
  describe '#run' do
48
48
  it 'runs all transitions' do
49
- %w(create keep update delete undelete).each do |transition|
49
+ %w(create update delete reinstate).each do |transition|
50
50
  expect(subject).to receive(:"transition_#{transition}")
51
51
  end
52
52
 
@@ -79,32 +79,6 @@ module BeetleETL
79
79
  end
80
80
  end
81
81
 
82
- describe 'transition_keep' do
83
- it 'assigns KEEP if the record already exists and is not deleted, comparing all columns
84
- except externald_*_id columns and columns not contained in the stage table' do
85
-
86
- insert_into(:example_table).values(
87
- [ :external_id , :external_source , :payload , :ignored_attribute , :foo_id , :deleted_at ] ,
88
- [ 'existing' , external_source , 'existing content' , 'ignored content' , 1 , nil ] ,
89
- [ 'deleted' , external_source , 'deleted content' , 'ignored content' , 2 , 1.day.ago ] ,
90
- )
91
-
92
- insert_into(subject.stage_table_name.to_sym).values(
93
- [ :external_id , :payload , :foo_id , :external_foo_id ] ,
94
- [ 'existing' , 'existing content' , 1 , 'ignored column' ] ,
95
- [ 'deleted' , 'deleted content' , 2 , 'ignored column' ] ,
96
- )
97
-
98
- subject.transition_keep
99
-
100
- expect(subject.stage_table_name.to_sym).to have_values(
101
- [ :external_id , :transition ] ,
102
- [ 'existing' , 'KEEP' ] ,
103
- [ 'deleted' , nil ] ,
104
- )
105
- end
106
- end
107
-
108
82
  describe '#transition_update' do
109
83
  it 'assigns UPDATE to non-deleted records with changed values comparing all columns
110
84
  except externald_*_id columns and columns not contained in the stage table' do
@@ -151,8 +125,8 @@ module BeetleETL
151
125
  end
152
126
  end
153
127
 
154
- describe 'transition_undelete' do
155
- it 'assigns UNDELETE to previously deleted records' do
128
+ describe 'transition_reinstate' do
129
+ it 'assigns REINSTATE to previously deleted records' do
156
130
  insert_into(:example_table).values(
157
131
  [ :external_id , :external_source , :payload , :ignored_attribute , :foo_id , :deleted_at ] ,
158
132
  [ 'existing' , external_source , 'existing content' , 'ignored content' , 1 , nil ] ,
@@ -165,12 +139,12 @@ module BeetleETL
165
139
  [ 'deleted' , 'updated content' , 2 , 'ignored_column' ] ,
166
140
  )
167
141
 
168
- subject.transition_undelete
142
+ subject.transition_reinstate
169
143
 
170
144
  expect(subject.stage_table_name.to_sym).to have_values(
171
- [ :external_id , :transition ] ,
172
- [ 'existing' , nil ] ,
173
- [ 'deleted' , 'UNDELETE' ] ,
145
+ [ :external_id , :transition ] ,
146
+ [ 'existing' , nil ] ,
147
+ [ 'deleted' , 'REINSTATE' ] ,
174
148
  )
175
149
  end
176
150
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: beetle_etl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.13
4
+ version: 0.0.16
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luciano Maiwald
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-12 00:00:00.000000000 Z
11
+ date: 2015-04-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sequel