pg_party 1.5.0 → 1.7.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b833474e06e9a20278f5ad629e4c26f290baeeac56a0aa9c4ec6dc1104023393
4
- data.tar.gz: 881f312c5714dc37760ef49537a5a200b46e72bace05a944890fe78dcc110f71
3
+ metadata.gz: 47b9e6d4d46ab349625ffcb49760675371e99412bdeac53993a7965dd08224cf
4
+ data.tar.gz: '08005f8354b7619a3e7745a67c157b2265851f0b7bc25568bfbd84d56e29a2b4'
5
5
  SHA512:
6
- metadata.gz: 6b9a253b6fe399aa6908bc4d7a4e8625622fadf4877af642d22c52f1e52c211a89b23e4cf3d4bec94b461fc9daf4db5de23384091255a33d055872cd337f97ef
7
- data.tar.gz: 8425ced0e015ceada85d6fedf1b553358a774ff39b85a0d4700c058a8747a24049e4f6d52a7d138722cb7cca3e04f75d883eed51f10df8f8ddbe88de629ead6a
6
+ metadata.gz: b6f5d302c2d4bdccc61ed263db7d04723551ab83bffdb1c89e229d8a602733329aa7568ff66ddcac7d4ffcf4a8c44ebd47536e3a50aa5f48a5876504224b8258
7
+ data.tar.gz: 0b5d15d2f46417a2a1eb54d56be0ba2b0a7c4a665c2c1b6ae38600891cb91260bb667f5d88cdd28b97d1b50370b4ce098ca895de3d8cdb2f58970aab474d76fc
data/README.md CHANGED
@@ -21,9 +21,20 @@
21
21
 
22
22
  - Partition tables are not represented correctly in `db/schema.rb` — please use the `:sql` schema format
23
23
 
24
+ ## Compatibility
25
+
26
+ This gem is tested against:
27
+
28
+ - Rails: 6.1, 7.0, 7.1
29
+ - Ruby: 3.0, latest (currently 3.2 at the time of this commit)
30
+ - PostgreSQL: 11, 12, 13, 14, 15, 16
31
+
24
32
  ## Future Work
25
33
 
26
- - Automatic partition creation (via cron or some other means)
34
+ I plan to separate out the model functionality into a new gem and port the migration functionality into [pg\_ha\_migrations](https://github.com/braintree/pg_ha_migrations) (some of which has already been done).
35
+ I will continue to maintain this gem (bugfixes / support for new versions of Rails) until that work is complete.
36
+
37
+ I originally planned to add a feature for automatic partition creation, but I think that functionality would be better served by [pg\_partman](https://github.com/pgpartman/pg_partman).
27
38
 
28
39
  ## Installation
29
40
 
@@ -117,7 +117,7 @@ module PgParty
117
117
  ON pg_tables.tablename::regclass = pg_inherits.inhparent::regclass
118
118
  WHERE pg_tables.schemaname = current_schema() AND
119
119
  pg_tables.tablename = #{quote(table_name)}
120
- ]).each_with_object(_accumulator) do |partition, acc|
120
+ ], "SCHEMA").each_with_object(_accumulator) do |partition, acc|
121
121
  acc << partition
122
122
  next unless include_subpartitions
123
123
 
@@ -133,7 +133,7 @@ module PgParty
133
133
  ON pg_tables.tablename::regclass = pg_inherits.inhrelid::regclass
134
134
  WHERE pg_tables.schemaname = current_schema() AND
135
135
  pg_tables.tablename = #{quote(table_name)}
136
- ]).first
136
+ ], "SCHEMA").first
137
137
  return parent if parent.nil? || !traverse
138
138
 
139
139
  while (parents_parent = parent_for_table_name(parent)) do
@@ -174,12 +174,12 @@ module PgParty
174
174
  SELECT relkind FROM pg_catalog.pg_class AS c
175
175
  JOIN pg_catalog.pg_namespace AS ns ON c.relnamespace = ns.oid
176
176
  WHERE relname = #{quote(table_name)} AND nspname = current_schema()
177
- ]).first == 'p'
177
+ ], "SCHEMA").first == 'p'
178
178
  end
179
179
 
180
180
  private
181
181
 
182
- def create_partition(table_name, type, partition_key, **options)
182
+ def create_partition(table_name, type, partition_key, **options, &blk)
183
183
  modified_options = options.except(:id, :primary_key, :template, :create_with_primary_key)
184
184
  template = options.fetch(:template, PgParty.config.create_template_tables)
185
185
  id = options.fetch(:id, :bigserial)
@@ -200,18 +200,18 @@ module PgParty
200
200
  end
201
201
  modified_options[:options] = partition_by_clause(type, partition_key)
202
202
 
203
- create_table(table_name, **modified_options) do |td|
203
+ migration_or_adapter(blk).create_table(table_name, **modified_options) do |td|
204
204
  if !modified_options[:id] && id == :uuid
205
- td.column(primary_key, id, null: false, default: uuid_function)
205
+ td.column(primary_key, id, null: false, default: "gen_random_uuid()")
206
206
  elsif !modified_options[:id] && id
207
207
  td.column(primary_key, id, null: false)
208
208
  end
209
209
 
210
- yield(td) if block_given?
210
+ blk&.call(td)
211
211
  end
212
212
 
213
213
  # Rails 4 has a bug where uuid columns are always nullable
214
- change_column_null(table_name, primary_key, false) if !modified_options[:id] && id == :uuid
214
+ migration_or_adapter(blk).change_column_null(table_name, primary_key, false) if !modified_options[:id] && id == :uuid
215
215
 
216
216
  return unless template
217
217
 
@@ -410,10 +410,6 @@ module PgParty
410
410
  "PARTITION BY #{type.to_s.upcase} (#{quote_partition_key(partition_key)})"
411
411
  end
412
412
 
413
- def uuid_function
414
- try(:supports_pgcrypto_uuid?) ? "gen_random_uuid()" : "uuid_generate_v4()"
415
- end
416
-
417
413
  def hashed_table_name(table_name, key)
418
414
  return "#{table_name}_#{Digest::MD5.hexdigest(key)[0..6]}" if key
419
415
 
@@ -424,7 +420,8 @@ module PgParty
424
420
  def index_valid?(index_name)
425
421
  select_values(
426
422
  "SELECT relname FROM pg_class, pg_index WHERE pg_index.indisvalid = false AND "\
427
- "pg_index.indexrelid = pg_class.oid AND relname = #{quote(index_name)}"
423
+ "pg_index.indexrelid = pg_class.oid AND relname = #{quote(index_name)}",
424
+ "SCHEMA"
428
425
  ).empty?
429
426
  end
430
427
 
@@ -455,5 +452,10 @@ module PgParty
455
452
  def postgres_major_version
456
453
  __getobj__.send(:postgresql_version)/10000
457
454
  end
455
+
456
+ def migration_or_adapter(blk)
457
+ blk_receiver = blk&.binding&.receiver
458
+ blk_receiver.is_a?(ActiveRecord::Migration) ? blk_receiver : self
459
+ end
458
460
  end
459
461
  end
@@ -8,13 +8,14 @@ module PgParty
8
8
  return super
9
9
  end
10
10
 
11
- partitions = begin
12
- ActiveRecord::Base.connection.select_values(
13
- "SELECT DISTINCT inhrelid::regclass::text FROM pg_inherits"
14
- )
15
- rescue
16
- []
17
- end
11
+ partitions = ActiveRecord::Base.connection.select_values(<<-SQL, "SCHEMA")
12
+ SELECT
13
+ inhrelid::regclass::text
14
+ FROM
15
+ pg_inherits
16
+ JOIN pg_class AS p ON inhparent = p.oid
17
+ WHERE p.relkind = 'p'
18
+ SQL
18
19
 
19
20
  excluded_tables = partitions.flat_map { |table| ["-T", "*.#{table}"] }
20
21
 
@@ -36,7 +36,7 @@ module PgParty
36
36
  if complex_partition_key
37
37
  complex_partition_key_query("(#{partition_key}) = (?)", value)
38
38
  else
39
- where_partition_key(:eq, value)
39
+ where(partition_key_arel(:eq, value))
40
40
  end
41
41
  end
42
42
 
@@ -48,9 +48,7 @@ module PgParty
48
48
  end_range
49
49
  )
50
50
  else
51
- where_partition_key(:gteq, start_range).merge(
52
- where_partition_key(:lt, end_range)
53
- )
51
+ where(partition_key_arel(:gteq, start_range).and(partition_key_arel(:lt, end_range)))
54
52
  end
55
53
  end
56
54
 
@@ -146,7 +144,7 @@ module PgParty
146
144
  from(subquery, current_alias)
147
145
  end
148
146
 
149
- def where_partition_key(meth, values)
147
+ def partition_key_arel(meth, values)
150
148
  partition_key_array = Array.wrap(partition_key)
151
149
  values = Array.wrap(values)
152
150
 
@@ -154,7 +152,7 @@ module PgParty
154
152
  raise "number of provided values does not match the number of partition key columns"
155
153
  end
156
154
 
157
- arel_query = partition_key_array.zip(values).inject(nil) do |obj, (column, value)|
155
+ partition_key_array.zip(values).inject(nil) do |obj, (column, value)|
158
156
  node = current_arel_table[column].send(meth, value)
159
157
 
160
158
  if obj.nil?
@@ -163,8 +161,6 @@ module PgParty
163
161
  obj.and(node)
164
162
  end
165
163
  end
166
-
167
- where(arel_query)
168
164
  end
169
165
  end
170
166
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PgParty
4
- VERSION = "1.5.0"
4
+ VERSION = "1.7.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg_party
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.0
4
+ version: 1.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Krage
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-01-18 00:00:00.000000000 Z
11
+ date: 2023-10-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -16,20 +16,20 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '5.0'
19
+ version: '6.1'
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: '6.2'
22
+ version: '7.2'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
27
  - - ">="
28
28
  - !ruby/object:Gem::Version
29
- version: '5.0'
29
+ version: '6.1'
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: '6.2'
32
+ version: '7.2'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: ruby2_keywords
35
35
  requirement: !ruby/object:Gem::Requirement
@@ -100,20 +100,6 @@ dependencies:
100
100
  - - "~>"
101
101
  - !ruby/object:Gem::Version
102
102
  version: '1.3'
103
- - !ruby/object:Gem::Dependency
104
- name: database_cleaner
105
- requirement: !ruby/object:Gem::Requirement
106
- requirements:
107
- - - "~>"
108
- - !ruby/object:Gem::Version
109
- version: '1.7'
110
- type: :development
111
- prerelease: false
112
- version_requirements: !ruby/object:Gem::Requirement
113
- requirements:
114
- - - "~>"
115
- - !ruby/object:Gem::Version
116
- version: '1.7'
117
103
  - !ruby/object:Gem::Dependency
118
104
  name: nokogiri
119
105
  requirement: !ruby/object:Gem::Requirement
@@ -182,14 +168,14 @@ dependencies:
182
168
  requirements:
183
169
  - - "~>"
184
170
  - !ruby/object:Gem::Version
185
- version: '3.8'
171
+ version: '6.0'
186
172
  type: :development
187
173
  prerelease: false
188
174
  version_requirements: !ruby/object:Gem::Requirement
189
175
  requirements:
190
176
  - - "~>"
191
177
  - !ruby/object:Gem::Version
192
- version: '3.8'
178
+ version: '6.0'
193
179
  - !ruby/object:Gem::Dependency
194
180
  name: rspec_junit_formatter
195
181
  requirement: !ruby/object:Gem::Requirement
@@ -210,14 +196,14 @@ dependencies:
210
196
  requirements:
211
197
  - - "~>"
212
198
  - !ruby/object:Gem::Version
213
- version: 0.17.0
199
+ version: '0.21'
214
200
  type: :development
215
201
  prerelease: false
216
202
  version_requirements: !ruby/object:Gem::Requirement
217
203
  requirements:
218
204
  - - "~>"
219
205
  - !ruby/object:Gem::Version
220
- version: 0.17.0
206
+ version: '0.21'
221
207
  - !ruby/object:Gem::Dependency
222
208
  name: timecop
223
209
  requirement: !ruby/object:Gem::Requirement
@@ -232,6 +218,20 @@ dependencies:
232
218
  - - "~>"
233
219
  - !ruby/object:Gem::Version
234
220
  version: '0.9'
221
+ - !ruby/object:Gem::Dependency
222
+ name: psych
223
+ requirement: !ruby/object:Gem::Requirement
224
+ requirements:
225
+ - - "~>"
226
+ - !ruby/object:Gem::Version
227
+ version: '3.3'
228
+ type: :development
229
+ prerelease: false
230
+ version_requirements: !ruby/object:Gem::Requirement
231
+ requirements:
232
+ - - "~>"
233
+ - !ruby/object:Gem::Version
234
+ version: '3.3'
235
235
  description: Migrations and model helpers for creating and managing PostgreSQL 10
236
236
  partitions
237
237
  email:
@@ -261,7 +261,7 @@ homepage: https://github.com/rkrage/pg_party
261
261
  licenses:
262
262
  - MIT
263
263
  metadata: {}
264
- post_install_message:
264
+ post_install_message:
265
265
  rdoc_options: []
266
266
  require_paths:
267
267
  - lib
@@ -269,15 +269,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
269
269
  requirements:
270
270
  - - ">="
271
271
  - !ruby/object:Gem::Version
272
- version: 2.5.0
272
+ version: 3.0.0
273
273
  required_rubygems_version: !ruby/object:Gem::Requirement
274
274
  requirements:
275
275
  - - ">="
276
276
  - !ruby/object:Gem::Version
277
277
  version: '0'
278
278
  requirements: []
279
- rubygems_version: 3.1.4
280
- signing_key:
279
+ rubygems_version: 3.3.7
280
+ signing_key:
281
281
  specification_version: 4
282
282
  summary: ActiveRecord PostgreSQL Partitioning
283
283
  test_files: []