pg_shrink 0.0.3 → 0.0.4

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: 020bdbd21742730e6116d22053802378b25aabc2
4
- data.tar.gz: 626257ce75eeb12f13ec0207052e217dc0429954
3
+ metadata.gz: 91758d0f6d6dda346377c31c7e26739d44d209ed
4
+ data.tar.gz: 3456597445c2de0dcd3a6d956673ddac607b331d
5
5
  SHA512:
6
- metadata.gz: 1aa6aecd642ff837ca77d91b4a80f3e540c59b9e1df8137dfb0c43929d46290c22ed3b1d5c737dd707d5cc118eab733276ceee1e14f63f8b8c26a322e3aceef8
7
- data.tar.gz: 360fef93069129daacfd2cdeeb287b63a45709eea6b1bfe5f22c05b9dc58f28aaddb868a27878fe970a1d9ba78e3001e27ecb8e2a1eabf6aaf008a3a5515b5e4
6
+ metadata.gz: bf159b0db40e195a2a1caaabf7fd37e8a98eb415ade6ebaaa4258b7e7f1ccc21ed2f8ff3c45fd6388d56fcd2f0528cf68088b80dcfd3a65cac3283773e40fc3d
7
+ data.tar.gz: 10a7d63d18ab1a5cf52510b0cd6a4699def302188d53950256e7aa90808da92d80463e5795083ae2f3c5aae0f620f7af80ccb021584f0725091cac02d0e615eb
@@ -22,21 +22,37 @@ module PgShrink
22
22
  }
23
23
  end
24
24
 
25
- def self.valid_pg_url?(url)
25
+ def self.validate_pg_url!(url)
26
+ if url.blank?
27
+ abort("Error loading postgres: " +
28
+ "Please specify postgres url using -u <postgres_url>")
29
+ end
26
30
  uri = URI.parse(url)
27
- uri.scheme == 'postgres' && !uri.user.blank? && uri.path != '/'
31
+ if uri.scheme == 'postgres' && !uri.user.blank? && uri.path != '/'
32
+ return true
33
+ else
34
+ abort("Error loading postgres: " +
35
+ "#{url} is not a valid postgres url")
36
+ end
28
37
  rescue => ex
29
- false
38
+ abort(
39
+ "Error loading postgres: " +
40
+ "#{url} is not a valid postgres url"
41
+ )
30
42
  end
31
43
 
32
44
  def self.run(options)
33
45
  unless File.exists?(options[:config])
34
- raise "Could not find file: #{options[:config]}"
46
+ if options[:config] == 'Shrinkfile'
47
+ abort("Error loading Shrinkfile: " +
48
+ "Please specify location using -c <path/to/Shrinkfile>")
49
+ else
50
+ abort("Error loading Shrinkfile: " +
51
+ "Could not find file at: #{options[:config]}")
52
+ end
35
53
  end
36
54
 
37
- unless valid_pg_url?(options[:url])
38
- raise "Invalid postgres url: #{options[:url]}"
39
- end
55
+ validate_pg_url!(options[:url])
40
56
 
41
57
  database = Database::Postgres.new(:postgres_url => options[:url])
42
58
 
@@ -49,8 +65,7 @@ module PgShrink
49
65
  cont = gets
50
66
  cont = cont.strip
51
67
  unless cont == 'y' || cont == 'Y'
52
- puts 'Aborting!'
53
- exit 1
68
+ abort('Aborting!')
54
69
  end
55
70
  end
56
71
 
@@ -17,8 +17,9 @@ module PgShrink
17
17
  yield table if block_given?
18
18
  end
19
19
 
20
- def remove_table(table_name)
20
+ def remove_table(table_name, opts = {})
21
21
  table = self.table(table_name)
22
+ table.update_options(opts)
22
23
  table.mark_for_removal!
23
24
  end
24
25
 
@@ -41,6 +41,9 @@ module PgShrink
41
41
  def records_in_batches(table_name)
42
42
  table = self.table(table_name)
43
43
  primary_key = table.primary_key
44
+ unless primary_key
45
+ raise "Error: Records in batches called on table without a primary key"
46
+ end
44
47
  max_id = self.connection["select max(#{primary_key}) from #{table_name}"].
45
48
  first[:max]
46
49
  i = 1;
@@ -2,6 +2,7 @@ module PgShrink
2
2
  class SubTableFilter < SubTableOperator
3
3
 
4
4
  def propagate!(old_parent_data, new_parent_data)
5
+ return if (old_parent_data.empty? && new_parent_data.empty?)
5
6
  old_batch_keys = old_parent_data.map {|record| record[@opts[:primary_key]]}
6
7
  new_batch_keys = new_parent_data.map {|record| record[@opts[:primary_key]]}
7
8
 
@@ -9,6 +9,7 @@ module PgShrink
9
9
  end
10
10
 
11
11
  def propagate!(old_parent_data, new_parent_data)
12
+ return if (old_parent_data.empty? && new_parent_data.empty?)
12
13
  old_batch = old_parent_data.index_by {|record| record[@opts[:primary_key]]}
13
14
  new_batch = new_parent_data.index_by {|record| record[@opts[:primary_key]]}
14
15
 
@@ -60,7 +60,9 @@ module PgShrink
60
60
  if primary_key
61
61
  deleted_keys = old_records.map {|r| r[primary_key]} -
62
62
  new_records.map {|r| r[primary_key]}
63
- self.database.delete_records(table_name, primary_key => deleted_keys)
63
+ if deleted_keys.any?
64
+ self.database.delete_records(table_name, primary_key => deleted_keys)
65
+ end
64
66
  else
65
67
  # TODO: Do we need to speed this up? Or is this an unusual enough
66
68
  # case that we can leave it slow?
@@ -120,10 +122,14 @@ module PgShrink
120
122
  end
121
123
 
122
124
  def filter!
123
- self.filters.each do |filter|
124
- self.records_in_batches do |batch|
125
- self.filter_batch(batch) do |record|
126
- filter.apply(record)
125
+ if remove? && can_just_remove?
126
+ remove!
127
+ else
128
+ self.filters.each do |filter|
129
+ self.records_in_batches do |batch|
130
+ self.filter_batch(batch) do |record|
131
+ filter.apply(record)
132
+ end
127
133
  end
128
134
  end
129
135
  end
@@ -139,12 +145,26 @@ module PgShrink
139
145
  end
140
146
  end
141
147
 
142
- # We use a filter for this, so that all other dependencies etc behave
143
- # as would be expected.
148
+ def can_just_remove?
149
+ self.subtable_filters.empty? && self.subtable_sanitizers.empty? && !@lock
150
+ end
151
+
152
+ # Mark @remove and add filter so that if we're in the simple case we can
153
+ # just remove! and if not we can just go through filters and all
154
+ # dependencies will be handled
144
155
  def mark_for_removal!
156
+ @remove = true
145
157
  self.filter_by { false }
146
158
  end
147
159
 
160
+ def remove?
161
+ !!@remove
162
+ end
163
+
164
+ def remove!
165
+ self.database.delete_records(table_name, {})
166
+ end
167
+
148
168
  # Check explicitly for nil because we want to be able to set primary_key
149
169
  # to false for e.g. join tables
150
170
  def primary_key
@@ -1,3 +1,3 @@
1
1
  module PgShrink
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -0,0 +1,6 @@
1
+ test:
2
+ database: test_pg_shrink
3
+ user: postgres
4
+ password:
5
+ host: localhost
6
+ port:
@@ -81,6 +81,28 @@ describe PgShrink::Database::Postgres do
81
81
  db.filter!
82
82
  expect(db.connection["select * from test_table"].all.size).to eq(0)
83
83
  end
84
+ describe "on a table with no primary key" do
85
+ before(:all) do
86
+ PgSpecHelper.create_table(db.connection, :no_primary_key,
87
+ {'name' => 'character(128)',
88
+ 'test' => 'integer'}, nil)
89
+ end
90
+ before(:each) do
91
+ (1..20).each do |i|
92
+ db.connection.run(
93
+ "insert into no_primary_key (name, test) values ('test', #{i})"
94
+ )
95
+ end
96
+
97
+ end
98
+
99
+ it "can still remove the whole table" do
100
+ db.remove_table(:no_primary_key, :primary_key => false)
101
+ db.filter!
102
+ expect(db.connection["select * from no_primary_key"].all.size).to eq(0)
103
+
104
+ end
105
+ end
84
106
  end
85
107
  end
86
108
  end
@@ -88,7 +88,7 @@ describe PgShrink::Table do
88
88
 
89
89
  context "when a subtable filter is specified" do
90
90
  let(:database) {PgShrink::Database.new}
91
- let(:table) { PgShrink::Table.new(database, :test_table) }
91
+ let(:table) { PgShrink::Table.new(database, :test_table, :primary_key => false) }
92
92
 
93
93
  before(:each) do
94
94
  table.filter_subtable(:subtable)
@@ -133,12 +133,8 @@ describe PgShrink::Table do
133
133
  table.mark_for_removal!
134
134
  end
135
135
 
136
- it "should by default remove all" do
137
- expect(table).to receive(:records_in_batches).and_yield(test_data)
138
- expect(table).to receive(:delete_records) do |old_batch, new_batch|
139
- expect(old_batch).to eq(test_data)
140
- expect(new_batch).to eq([])
141
- end
136
+ it "should run remove! if there are no dependencies" do
137
+ expect(table).to receive(:remove!)
142
138
  table.shrink!
143
139
  end
144
140
 
@@ -47,6 +47,14 @@ describe PgShrink do
47
47
  end
48
48
  end
49
49
 
50
+ it "Should not run delete if there is nothing filtered" do
51
+ database.filter_table(:users) do |f|
52
+ f.filter_by {true}
53
+ end
54
+ expect(database).not_to receive(:delete_records)
55
+ database.shrink!
56
+ end
57
+
50
58
  describe "with a test shrinkfile" do
51
59
  let(:shrinkfile) {"spec/Shrinkfile.basic"}
52
60
  let(:url) {database.connection_string}
@@ -15,7 +15,12 @@ module PgSpecHelper
15
15
  end
16
16
 
17
17
  def self.pg_config
18
- @pg_config ||= YAML.load_file('spec/pg_config.yml')['test']
18
+ @pg_config ||= if File.exists?('spec/pg_config.yml')
19
+ YAML.load_file('spec/pg_config.yml')['test']
20
+ else
21
+ {"database"=>"test_pg_shrink", "user"=>"postgres", "password"=>nil,
22
+ "host"=>"localhost", "port"=>nil}
23
+ end
19
24
  end
20
25
 
21
26
  def self.drop_table_if_exists(connection, table)
metadata CHANGED
@@ -1,195 +1,195 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg_shrink
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Ball
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-29 00:00:00.000000000 Z
11
+ date: 2014-06-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pg
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - '>='
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - '>='
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activesupport
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - '>='
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ">="
38
+ - - '>='
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: sequel
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ">="
45
+ - - '>='
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ">="
52
+ - - '>='
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: bundler
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - "~>"
59
+ - - ~>
60
60
  - !ruby/object:Gem::Version
61
61
  version: '1.6'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - "~>"
66
+ - - ~>
67
67
  - !ruby/object:Gem::Version
68
68
  version: '1.6'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rake
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ">="
73
+ - - '>='
74
74
  - !ruby/object:Gem::Version
75
75
  version: '0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - ">="
80
+ - - '>='
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: rspec
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - ">="
87
+ - - '>='
88
88
  - !ruby/object:Gem::Version
89
89
  version: '0'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - ">="
94
+ - - '>='
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: rspec-nc
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
- - - ">="
101
+ - - '>='
102
102
  - !ruby/object:Gem::Version
103
103
  version: '0'
104
104
  type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
- - - ">="
108
+ - - '>='
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
111
  - !ruby/object:Gem::Dependency
112
112
  name: rspec-mocks
113
113
  requirement: !ruby/object:Gem::Requirement
114
114
  requirements:
115
- - - ">="
115
+ - - '>='
116
116
  - !ruby/object:Gem::Version
117
117
  version: '0'
118
118
  type: :development
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
- - - ">="
122
+ - - '>='
123
123
  - !ruby/object:Gem::Version
124
124
  version: '0'
125
125
  - !ruby/object:Gem::Dependency
126
126
  name: guard
127
127
  requirement: !ruby/object:Gem::Requirement
128
128
  requirements:
129
- - - ">="
129
+ - - '>='
130
130
  - !ruby/object:Gem::Version
131
131
  version: '0'
132
132
  type: :development
133
133
  prerelease: false
134
134
  version_requirements: !ruby/object:Gem::Requirement
135
135
  requirements:
136
- - - ">="
136
+ - - '>='
137
137
  - !ruby/object:Gem::Version
138
138
  version: '0'
139
139
  - !ruby/object:Gem::Dependency
140
140
  name: guard-rspec
141
141
  requirement: !ruby/object:Gem::Requirement
142
142
  requirements:
143
- - - ">="
143
+ - - '>='
144
144
  - !ruby/object:Gem::Version
145
145
  version: '0'
146
146
  type: :development
147
147
  prerelease: false
148
148
  version_requirements: !ruby/object:Gem::Requirement
149
149
  requirements:
150
- - - ">="
150
+ - - '>='
151
151
  - !ruby/object:Gem::Version
152
152
  version: '0'
153
153
  - !ruby/object:Gem::Dependency
154
154
  name: pry
155
155
  requirement: !ruby/object:Gem::Requirement
156
156
  requirements:
157
- - - ">="
157
+ - - '>='
158
158
  - !ruby/object:Gem::Version
159
159
  version: '0'
160
160
  type: :development
161
161
  prerelease: false
162
162
  version_requirements: !ruby/object:Gem::Requirement
163
163
  requirements:
164
- - - ">="
164
+ - - '>='
165
165
  - !ruby/object:Gem::Version
166
166
  version: '0'
167
167
  - !ruby/object:Gem::Dependency
168
168
  name: pry-remote
169
169
  requirement: !ruby/object:Gem::Requirement
170
170
  requirements:
171
- - - ">="
171
+ - - '>='
172
172
  - !ruby/object:Gem::Version
173
173
  version: '0'
174
174
  type: :development
175
175
  prerelease: false
176
176
  version_requirements: !ruby/object:Gem::Requirement
177
177
  requirements:
178
- - - ">="
178
+ - - '>='
179
179
  - !ruby/object:Gem::Version
180
180
  version: '0'
181
181
  - !ruby/object:Gem::Dependency
182
182
  name: pry-nav
183
183
  requirement: !ruby/object:Gem::Requirement
184
184
  requirements:
185
- - - ">="
185
+ - - '>='
186
186
  - !ruby/object:Gem::Version
187
187
  version: '0'
188
188
  type: :development
189
189
  prerelease: false
190
190
  version_requirements: !ruby/object:Gem::Requirement
191
191
  requirements:
192
- - - ">="
192
+ - - '>='
193
193
  - !ruby/object:Gem::Version
194
194
  version: '0'
195
195
  description: pg_shrink makes it simple to shrink and sanitize a psql database
@@ -200,8 +200,8 @@ executables:
200
200
  extensions: []
201
201
  extra_rdoc_files: []
202
202
  files:
203
- - ".gitignore"
204
- - ".rspec"
203
+ - .gitignore
204
+ - .rspec
205
205
  - Gemfile
206
206
  - Guardfile
207
207
  - README.md
@@ -220,7 +220,7 @@ files:
220
220
  - lib/pg_shrink/version.rb
221
221
  - pg_shrink.gemspec
222
222
  - spec/Shrinkfile.basic
223
- - spec/pg_config.yml
223
+ - spec/pg_config.example.yml
224
224
  - spec/pg_shrink/database/postgres_spec.rb
225
225
  - spec/pg_shrink/database_spec.rb
226
226
  - spec/pg_shrink/table_spec.rb
@@ -237,23 +237,23 @@ require_paths:
237
237
  - lib
238
238
  required_ruby_version: !ruby/object:Gem::Requirement
239
239
  requirements:
240
- - - ">="
240
+ - - '>='
241
241
  - !ruby/object:Gem::Version
242
242
  version: '0'
243
243
  required_rubygems_version: !ruby/object:Gem::Requirement
244
244
  requirements:
245
- - - ">="
245
+ - - '>='
246
246
  - !ruby/object:Gem::Version
247
247
  version: '0'
248
248
  requirements: []
249
249
  rubyforge_project:
250
- rubygems_version: 2.2.2
250
+ rubygems_version: 2.0.3
251
251
  signing_key:
252
252
  specification_version: 4
253
253
  summary: ''
254
254
  test_files:
255
255
  - spec/Shrinkfile.basic
256
- - spec/pg_config.yml
256
+ - spec/pg_config.example.yml
257
257
  - spec/pg_shrink/database/postgres_spec.rb
258
258
  - spec/pg_shrink/database_spec.rb
259
259
  - spec/pg_shrink/table_spec.rb