table_copy 0.0.7 → 0.0.8
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/copier.rb +48 -48
- data/lib/table_copy/pg/source.rb +8 -8
- data/lib/table_copy/version.rb +1 -1
- data/spec/lib/table_copy_spec.rb +2 -2
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3febd5ce84fd65678b0993f9b1eddea26b053b17
|
4
|
+
data.tar.gz: e0bd696e15c019f977597a32f6c8a5f50ec7aeb9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6deff2bfac7c138bc3666e3812d51ccea182bb6fe900fd9d8eaae314c024e0f24de2963d23d34703d94883dea15bc8eac0048198874a0dcbce573b497b2564a8
|
7
|
+
data.tar.gz: 7245d1fe638e3effd048556dd93625754aa015fd253a40651a7107dd21e64e39afeafafe7f0f1dffe2f535b5073b5c60e9acfe91c8355192417c940c9c6eeceb
|
data/lib/table_copy/copier.rb
CHANGED
@@ -2,18 +2,18 @@ require 'pg'
|
|
2
2
|
|
3
3
|
module TableCopy
|
4
4
|
class Copier
|
5
|
-
attr_reader :
|
5
|
+
attr_reader :source, :destination
|
6
6
|
|
7
|
-
def initialize(
|
8
|
-
@
|
9
|
-
@
|
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
|
14
|
+
if destination.none?
|
15
15
|
droppy
|
16
|
-
elsif (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 #{
|
26
|
-
views =
|
25
|
+
logger.info { "Droppy #{destination.table_name}" }
|
26
|
+
views = destination.query_views
|
27
27
|
|
28
|
-
|
29
|
-
|
28
|
+
destination.transaction do
|
29
|
+
destination.drop(cascade: true)
|
30
30
|
create_table
|
31
|
-
moved_count =
|
32
|
-
logger.info { "#{moved_count} rows moved to #{
|
33
|
-
|
34
|
-
logger.info { "Completed #{
|
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
|
-
|
38
|
-
logger.info { "#{view_status ? 'Created' : 'Failed to create'} view #{view_name} for #{
|
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 #{
|
44
|
-
|
45
|
-
|
46
|
-
moved_count =
|
47
|
-
logger.info { "#{moved_count} rows moved to temp_#{
|
48
|
-
|
49
|
-
logger.info { "Deletetions from #{
|
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 #{
|
55
|
-
|
56
|
-
|
57
|
-
moved_count =
|
58
|
-
logger.info { "#{moved_count} rows moved to temp_#{
|
59
|
-
|
60
|
-
logger.info { "Upsert to #{
|
61
|
-
|
62
|
-
logger.info { "Deletetions from #{
|
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 #{
|
70
|
-
|
71
|
-
|
72
|
-
moved_count =
|
73
|
-
logger.info "#{moved_count} rows moved to temp_#{
|
74
|
-
|
75
|
-
logger.info "Upsert to #{
|
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 #{
|
81
|
-
|
82
|
-
|
83
|
-
moved_count =
|
84
|
-
logger.info "#{moved_count} rows moved to temp_#{
|
85
|
-
|
86
|
-
logger.info "Upsert to #{
|
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 #{
|
92
|
-
|
91
|
+
logger.info { "Creating table #{destination.table_name}" }
|
92
|
+
destination.create(source.fields_ddl)
|
93
93
|
end
|
94
94
|
|
95
95
|
def with_rescue
|
data/lib/table_copy/pg/source.rb
CHANGED
@@ -21,11 +21,11 @@ module TableCopy
|
|
21
21
|
end
|
22
22
|
|
23
23
|
def fields_ddl
|
24
|
-
|
24
|
+
fields_objects.map(&:ddl).join(",\n ")
|
25
25
|
end
|
26
26
|
|
27
27
|
def indexes
|
28
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
72
|
-
|
71
|
+
with_conn do |conn|
|
72
|
+
conn.exec(indexes_sql)
|
73
73
|
end
|
74
74
|
end
|
75
75
|
|
data/lib/table_copy/version.rb
CHANGED
data/spec/lib/table_copy_spec.rb
CHANGED
@@ -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.
|
86
|
-
expect(copier.
|
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.
|
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-
|
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.
|
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:
|