table_copy 0.0.8 → 0.0.9
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.
- checksums.yaml +4 -4
- data/lib/table_copy/pg/destination.rb +21 -9
- data/lib/table_copy/version.rb +1 -1
- data/spec/db.rb +5 -1
- data/spec/lib/table_copy/pg/destination_spec.rb +39 -9
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 93757d1afc37f9c620e57c50504028503039419c
|
4
|
+
data.tar.gz: 61782d83b5cd3622adc3a49ad6f6a799bcec14a2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 79613aeac02a20849882a9523a75a819b4afa2e2492ef16e229d3c51f427deda9c644cabd38deae0eafac11f6a2d0ded606100bc5c30b3d978efcb18add99686
|
7
|
+
data.tar.gz: 4d64aefcbaa43f0eaf07bc52844c3076b1e702191b7302d1e945b48de7abd80da3cd1d334d7d0ccdd0d57e5682997d4403e1c1c63fffc71b48e4267ff7a08a86
|
@@ -1,15 +1,17 @@
|
|
1
1
|
module TableCopy
|
2
2
|
module PG
|
3
3
|
class Destination
|
4
|
-
attr_reader :table_name, :conn_method, :indexes, :fields, :primary_key, :sequence_field
|
4
|
+
attr_reader :table_name, :conn_method, :indexes, :fields, :primary_key, :sequence_field, :after_create, :soft_delete_field
|
5
5
|
|
6
6
|
def initialize(args)
|
7
|
-
@table_name
|
8
|
-
@primary_key
|
9
|
-
@sequence_field
|
10
|
-
@conn_method
|
11
|
-
@indexes
|
12
|
-
@fields
|
7
|
+
@table_name = args[:table_name]
|
8
|
+
@primary_key = args[:primary_key]
|
9
|
+
@sequence_field = args[:sequence_field]
|
10
|
+
@conn_method = args[:conn_method]
|
11
|
+
@indexes = args[:indexes] || []
|
12
|
+
@fields = args[:fields]
|
13
|
+
@after_create = args[:after_create]
|
14
|
+
@soft_delete_field = args[:soft_delete_field]
|
13
15
|
end
|
14
16
|
|
15
17
|
def transaction
|
@@ -26,9 +28,11 @@ module TableCopy
|
|
26
28
|
end
|
27
29
|
|
28
30
|
def create(fields_ddl)
|
31
|
+
sd = ", #{soft_delete_field} bool default false" if soft_delete_field
|
29
32
|
with_conn do |conn|
|
30
|
-
conn.exec("create table #{table_name} (#{fields_ddl})")
|
33
|
+
conn.exec("create table #{table_name} (#{fields_ddl}#{sd})")
|
31
34
|
end
|
35
|
+
after_create.call(table_name) if after_create
|
32
36
|
end
|
33
37
|
|
34
38
|
def drop(opts={})
|
@@ -97,7 +101,11 @@ module TableCopy
|
|
97
101
|
|
98
102
|
def delete_not_in_temp
|
99
103
|
with_conn do |conn|
|
100
|
-
|
104
|
+
if soft_delete_field
|
105
|
+
conn.exec("update #{table_name} set #{soft_delete_field}=true where #{not_in_temp} and (#{soft_delete_field} is null or #{soft_delete_field} != true)")
|
106
|
+
else
|
107
|
+
conn.exec("delete from #{table_name} where #{not_in_temp}")
|
108
|
+
end
|
101
109
|
end
|
102
110
|
end
|
103
111
|
|
@@ -123,6 +131,10 @@ module TableCopy
|
|
123
131
|
|
124
132
|
private
|
125
133
|
|
134
|
+
def not_in_temp
|
135
|
+
"#{primary_key} in (select #{primary_key} from #{table_name} except select #{primary_key} from temp_#{table_name})"
|
136
|
+
end
|
137
|
+
|
126
138
|
attr_reader :primary_key
|
127
139
|
|
128
140
|
def fields_list
|
data/lib/table_copy/version.rb
CHANGED
data/spec/db.rb
CHANGED
@@ -28,7 +28,11 @@ class DB
|
|
28
28
|
end
|
29
29
|
|
30
30
|
def create_table(name=table_name)
|
31
|
-
conn.exec("create table #{name} (
|
31
|
+
conn.exec("create table #{name} (#{create_fields_ddl})")
|
32
|
+
end
|
33
|
+
|
34
|
+
def create_fields_ddl
|
35
|
+
"column1 integer, column2 varchar(123), column3 varchar(256)[]"
|
32
36
|
end
|
33
37
|
|
34
38
|
def create_view(name=view_name, t_name: table_name)
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'table_copy/pg/destination'
|
2
|
+
require 'table_copy/pg/source'
|
2
3
|
require 'table_copy/pg/index'
|
3
4
|
|
4
5
|
describe TableCopy::PG::Destination do
|
@@ -6,14 +7,16 @@ describe TableCopy::PG::Destination do
|
|
6
7
|
let(:view_name) { 'view_name' }
|
7
8
|
let(:db) { DB.new(table_name: table_name, view_name: view_name) }
|
8
9
|
|
9
|
-
let(:
|
10
|
+
let(:conf) { {
|
10
11
|
table_name: table_name,
|
11
12
|
conn_method: db.method(:with_conn),
|
12
13
|
indexes: [ TableCopy::PG::Index.new(table_name, nil, ['column1']) ],
|
13
14
|
fields: [ 'column1', 'column2', 'column3' ],
|
14
15
|
primary_key: 'column1',
|
15
16
|
sequence_field: 'column1'
|
16
|
-
|
17
|
+
} }
|
18
|
+
|
19
|
+
let(:dest) { TableCopy::PG::Destination.new(conf) }
|
17
20
|
|
18
21
|
after do
|
19
22
|
db.drop_table
|
@@ -27,7 +30,7 @@ describe TableCopy::PG::Destination do
|
|
27
30
|
|
28
31
|
context 'a table exists' do
|
29
32
|
before do
|
30
|
-
db.
|
33
|
+
dest.create(db.create_fields_ddl)
|
31
34
|
end
|
32
35
|
|
33
36
|
let(:expected_view) {
|
@@ -280,12 +283,28 @@ describe TableCopy::PG::Destination do
|
|
280
283
|
db.insert_data
|
281
284
|
end
|
282
285
|
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
286
|
+
context 'no soft delete' do
|
287
|
+
it 'deletes row that are not in the temp table' do
|
288
|
+
expect {
|
289
|
+
dest.delete_not_in_temp
|
290
|
+
}.to change {
|
291
|
+
db.row_count
|
292
|
+
}.from(1).to(0)
|
293
|
+
end
|
294
|
+
end
|
295
|
+
|
296
|
+
context 'with soft delete' do
|
297
|
+
let(:soft_delete_field) { 'is_soft_deleted' }
|
298
|
+
let(:conf2) { conf.merge(soft_delete_field: soft_delete_field) }
|
299
|
+
let(:dest) { TableCopy::PG::Destination.new(conf2) }
|
300
|
+
|
301
|
+
it 'sets the field to true' do
|
302
|
+
expect {
|
303
|
+
dest.delete_not_in_temp
|
304
|
+
}.to change {
|
305
|
+
db.with_conn { |c| c.exec("select #{soft_delete_field} from #{table_name}").first[soft_delete_field]}
|
306
|
+
}.from('f').to('t')
|
307
|
+
end
|
289
308
|
end
|
290
309
|
end
|
291
310
|
end
|
@@ -299,6 +318,17 @@ describe TableCopy::PG::Destination do
|
|
299
318
|
db.table_exists?
|
300
319
|
}.from(false).to(true)
|
301
320
|
end
|
321
|
+
|
322
|
+
context 'an after_create proc is specified' do
|
323
|
+
let(:after_create) { double }
|
324
|
+
let(:conf2) { conf.merge(after_create: after_create) }
|
325
|
+
let(:dest) { TableCopy::PG::Destination.new(conf2) }
|
326
|
+
|
327
|
+
it 'calls the after_create proc, passing the table_name' do
|
328
|
+
expect(after_create).to receive(:call).with(dest.table_name)
|
329
|
+
dest.create('column1 integer')
|
330
|
+
end
|
331
|
+
end
|
302
332
|
end
|
303
333
|
|
304
334
|
describe '#create_temp' do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: table_copy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- TLH
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-10-
|
11
|
+
date: 2014-10-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|