table_copy 0.0.7 → 0.0.8

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: e1b6fbde1c068309a2bb36fb4aeba74a6f56efcc
4
- data.tar.gz: bfafe52a0a0bf3792293cb9c8a58bae71a288b2a
3
+ metadata.gz: 3febd5ce84fd65678b0993f9b1eddea26b053b17
4
+ data.tar.gz: e0bd696e15c019f977597a32f6c8a5f50ec7aeb9
5
5
  SHA512:
6
- metadata.gz: f875ce996caa3a33633af3a04368d2ce8d3a0ac0787cee6c1c13ba22ccf908869bde6dfb39f4b2def4ca20ba60ec15865e5ecd7e3fbcaabc83cbbb1cfdac6b9e
7
- data.tar.gz: 5e193bcf30ec23fa9a53f11785e1fdcb990ad78c31fe51bd0faac515bfa378a2f394f0d18107248c4a73938de1e71954e311595f6c041219ebccefb775192071
6
+ metadata.gz: 6deff2bfac7c138bc3666e3812d51ccea182bb6fe900fd9d8eaae314c024e0f24de2963d23d34703d94883dea15bc8eac0048198874a0dcbce573b497b2564a8
7
+ data.tar.gz: 7245d1fe638e3effd048556dd93625754aa015fd253a40651a7107dd21e64e39afeafafe7f0f1dffe2f535b5073b5c60e9acfe91c8355192417c940c9c6eeceb
@@ -2,18 +2,18 @@ require 'pg'
2
2
 
3
3
  module TableCopy
4
4
  class Copier
5
- attr_reader :source_table, :destination_table
5
+ attr_reader :source, :destination
6
6
 
7
- def initialize(source_table, destination_table)
8
- @source_table = source_table
9
- @destination_table = destination_table
7
+ def initialize(source, destination)
8
+ @source = source
9
+ @destination = destination
10
10
  end
11
11
 
12
12
  def update
13
13
  with_rescue do
14
- if destination_table.none?
14
+ if destination.none?
15
15
  droppy
16
- elsif (max_sequence = destination_table.max_sequence)
16
+ elsif (max_sequence = destination.max_sequence)
17
17
  update_data(max_sequence)
18
18
  else
19
19
  diffy_update
@@ -22,74 +22,74 @@ module TableCopy
22
22
  end
23
23
 
24
24
  def droppy
25
- logger.info { "Droppy #{destination_table.table_name}" }
26
- views = destination_table.query_views
25
+ logger.info { "Droppy #{destination.table_name}" }
26
+ views = destination.query_views
27
27
 
28
- destination_table.transaction do
29
- destination_table.drop(cascade: true)
28
+ destination.transaction do
29
+ destination.drop(cascade: true)
30
30
  create_table
31
- moved_count = destination_table.copy_data_from(source_table)
32
- logger.info { "#{moved_count} rows moved to #{destination_table.table_name}" }
33
- destination_table.create_indexes
34
- logger.info { "Completed #{source_table.indexes.count} indexes on #{destination_table.table_name}." }
31
+ moved_count = destination.copy_data_from(source)
32
+ logger.info { "#{moved_count} rows moved to #{destination.table_name}" }
33
+ destination.create_indexes
34
+ logger.info { "Completed #{source.indexes.count} indexes on #{destination.table_name}." }
35
35
  end
36
36
 
37
- destination_table.create_views(views).each do |view_name, view_status|
38
- logger.info { "#{view_status ? 'Created' : 'Failed to create'} view #{view_name} for #{destination_table.table_name}" }
37
+ destination.create_views(views).each do |view_name, view_status|
38
+ logger.info { "#{view_status ? 'Created' : 'Failed to create'} view #{view_name} for #{destination.table_name}" }
39
39
  end
40
40
  end
41
41
 
42
42
  def find_deletes
43
- logger.info { "Find deletes #{destination_table.table_name}" }
44
- destination_table.transaction do
45
- destination_table.create_temp(source_table.fields_ddl)
46
- moved_count = destination_table.copy_data_from(source_table, temp: true, pk_only: true)
47
- logger.info { "#{moved_count} rows moved to temp_#{destination_table.table_name}" }
48
- destination_table.delete_not_in_temp
49
- logger.info { "Deletetions from #{destination_table.table_name} complete." }
43
+ logger.info { "Find deletes #{destination.table_name}" }
44
+ destination.transaction do
45
+ destination.create_temp(source.fields_ddl)
46
+ moved_count = destination.copy_data_from(source, temp: true, pk_only: true)
47
+ logger.info { "#{moved_count} rows moved to temp_#{destination.table_name}" }
48
+ destination.delete_not_in_temp
49
+ logger.info { "Deletetions from #{destination.table_name} complete." }
50
50
  end
51
51
  end
52
52
 
53
53
  def diffy
54
- logger.info { "Diffy #{destination_table.table_name}" }
55
- destination_table.transaction do
56
- destination_table.create_temp(source_table.fields_ddl)
57
- moved_count = destination_table.copy_data_from(source_table, temp: true)
58
- logger.info { "#{moved_count} rows moved to temp_#{destination_table.table_name}" }
59
- destination_table.copy_from_temp
60
- logger.info { "Upsert to #{destination_table.table_name} complete" }
61
- destination_table.delete_not_in_temp
62
- logger.info { "Deletetions from #{destination_table.table_name} complete." }
54
+ logger.info { "Diffy #{destination.table_name}" }
55
+ destination.transaction do
56
+ destination.create_temp(source.fields_ddl)
57
+ moved_count = destination.copy_data_from(source, temp: true)
58
+ logger.info { "#{moved_count} rows moved to temp_#{destination.table_name}" }
59
+ destination.copy_from_temp
60
+ logger.info { "Upsert to #{destination.table_name} complete" }
61
+ destination.delete_not_in_temp
62
+ logger.info { "Deletetions from #{destination.table_name} complete." }
63
63
  end
64
64
  end
65
65
 
66
66
  private
67
67
 
68
68
  def diffy_update
69
- logger.info "Diffy Update #{destination_table.table_name}"
70
- destination_table.transaction do
71
- destination_table.create_temp(source_table.fields_ddl)
72
- moved_count = destination_table.copy_data_from(source_table, temp: true)
73
- logger.info "#{moved_count} rows moved to temp_#{destination_table.table_name}"
74
- destination_table.copy_from_temp
75
- logger.info "Upsert to #{destination_table.table_name} complete."
69
+ logger.info "Diffy Update #{destination.table_name}"
70
+ destination.transaction do
71
+ destination.create_temp(source.fields_ddl)
72
+ moved_count = destination.copy_data_from(source, temp: true)
73
+ logger.info "#{moved_count} rows moved to temp_#{destination.table_name}"
74
+ destination.copy_from_temp
75
+ logger.info "Upsert to #{destination.table_name} complete."
76
76
  end
77
77
  end
78
78
 
79
79
  def update_data(max_sequence)
80
- logger.info "Update #{destination_table.table_name}"
81
- destination_table.transaction do
82
- destination_table.create_temp(source_table.fields_ddl)
83
- moved_count = destination_table.copy_data_from(source_table, temp: true, update: max_sequence)
84
- logger.info "#{moved_count} rows moved to temp_#{destination_table.table_name}"
85
- destination_table.copy_from_temp(except: nil)
86
- logger.info "Upsert to #{destination_table.table_name} complete."
80
+ logger.info "Update #{destination.table_name}"
81
+ destination.transaction do
82
+ destination.create_temp(source.fields_ddl)
83
+ moved_count = destination.copy_data_from(source, temp: true, update: max_sequence)
84
+ logger.info "#{moved_count} rows moved to temp_#{destination.table_name}"
85
+ destination.copy_from_temp(except: nil)
86
+ logger.info "Upsert to #{destination.table_name} complete."
87
87
  end
88
88
  end
89
89
 
90
90
  def create_table
91
- logger.info { "Creating table #{destination_table.table_name}" }
92
- destination_table.create(source_table.fields_ddl)
91
+ logger.info { "Creating table #{destination.table_name}" }
92
+ destination.create(source.fields_ddl)
93
93
  end
94
94
 
95
95
  def with_rescue
@@ -21,11 +21,11 @@ module TableCopy
21
21
  end
22
22
 
23
23
  def fields_ddl
24
- @fields_ddl ||= fields_objects.map(&:ddl).join(",\n ")
24
+ fields_objects.map(&:ddl).join(",\n ")
25
25
  end
26
26
 
27
27
  def indexes
28
- @indexes ||= viable_index_columns.map { |name, columns| TableCopy::PG::Index.new(table_name, name, columns) }
28
+ viable_index_columns.map { |name, columns| TableCopy::PG::Index.new(table_name, name, columns) }
29
29
  end
30
30
 
31
31
  def copy_from(fields_list_arg, where=nil)
@@ -37,7 +37,7 @@ module TableCopy
37
37
  end
38
38
 
39
39
  def fields
40
- @field_names ||= fields_objects.map(&:name)
40
+ fields_objects.map(&:name)
41
41
  end
42
42
 
43
43
  private
@@ -47,19 +47,19 @@ module TableCopy
47
47
  end
48
48
 
49
49
  def fields_objects
50
- @fields_objects ||= with_conn do |conn|
50
+ with_conn do |conn|
51
51
  conn.exec(fields_sql).map { |r| TableCopy::PG::Field.new(r) }
52
52
  end
53
53
  end
54
54
 
55
55
  def viable_index_columns
56
- @viable_index_columns ||= index_columns.select do |name, columns|
56
+ index_columns.select do |name, columns|
57
57
  (columns - fields).empty?
58
58
  end
59
59
  end
60
60
 
61
61
  def index_columns
62
- @index_columns ||= raw_indexes.inject({}) do |indexes, ri|
62
+ raw_indexes.inject({}) do |indexes, ri|
63
63
  index_name = ri['index_name']
64
64
  indexes[index_name] ||= []
65
65
  indexes[index_name] << ri['column_name']
@@ -68,8 +68,8 @@ module TableCopy
68
68
  end
69
69
 
70
70
  def raw_indexes
71
- @raw_indexes || with_conn do |conn|
72
- @raw_indexes = conn.exec(indexes_sql)
71
+ with_conn do |conn|
72
+ conn.exec(indexes_sql)
73
73
  end
74
74
  end
75
75
 
@@ -1,3 +1,3 @@
1
1
  module TableCopy
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
@@ -82,8 +82,8 @@ describe TableCopy do
82
82
 
83
83
  copier = tc.links[link_name]
84
84
  expect(copier).to be_kind_of TableCopy::Copier
85
- expect(copier.source_table).to eq source
86
- expect(copier.destination_table).to eq destination
85
+ expect(copier.source).to eq source
86
+ expect(copier.destination).to eq destination
87
87
  end
88
88
  end
89
89
  end
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.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - TLH
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-11 00:00:00.000000000 Z
11
+ date: 2014-10-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -133,7 +133,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
133
133
  version: '0'
134
134
  requirements: []
135
135
  rubyforge_project:
136
- rubygems_version: 2.1.11
136
+ rubygems_version: 2.2.2
137
137
  signing_key:
138
138
  specification_version: 4
139
139
  summary: Move full tables between databases.
@@ -146,3 +146,4 @@ test_files:
146
146
  - spec/lib/table_copy/pg/source_spec.rb
147
147
  - spec/lib/table_copy_spec.rb
148
148
  - spec/spec_helper.rb
149
+ has_rdoc: