table_copy 0.0.8 → 0.0.9

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: 3febd5ce84fd65678b0993f9b1eddea26b053b17
4
- data.tar.gz: e0bd696e15c019f977597a32f6c8a5f50ec7aeb9
3
+ metadata.gz: 93757d1afc37f9c620e57c50504028503039419c
4
+ data.tar.gz: 61782d83b5cd3622adc3a49ad6f6a799bcec14a2
5
5
  SHA512:
6
- metadata.gz: 6deff2bfac7c138bc3666e3812d51ccea182bb6fe900fd9d8eaae314c024e0f24de2963d23d34703d94883dea15bc8eac0048198874a0dcbce573b497b2564a8
7
- data.tar.gz: 7245d1fe638e3effd048556dd93625754aa015fd253a40651a7107dd21e64e39afeafafe7f0f1dffe2f535b5073b5c60e9acfe91c8355192417c940c9c6eeceb
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 = 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]
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
- conn.exec("delete from #{table_name} where #{primary_key} in (select #{primary_key} from #{table_name} except select #{primary_key} from temp_#{table_name})")
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
@@ -1,3 +1,3 @@
1
1
  module TableCopy
2
- VERSION = "0.0.8"
2
+ VERSION = "0.0.9"
3
3
  end
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} (column1 integer, column2 varchar(123), column3 varchar(256)[])")
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(:dest) { TableCopy::PG::Destination.new(
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.create_table
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
- it 'deletes row that are not in the temp table' do
284
- expect {
285
- dest.delete_not_in_temp
286
- }.to change {
287
- db.row_count
288
- }.from(1).to(0)
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.8
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-07 00:00:00.000000000 Z
11
+ date: 2014-10-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler