pg_party 1.4.0 → 1.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +20 -19
- data/lib/pg_party/adapter_decorator.rb +25 -4
- data/lib/pg_party/model/shared_methods.rb +3 -3
- data/lib/pg_party/version.rb +1 -1
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b833474e06e9a20278f5ad629e4c26f290baeeac56a0aa9c4ec6dc1104023393
|
4
|
+
data.tar.gz: 881f312c5714dc37760ef49537a5a200b46e72bace05a944890fe78dcc110f71
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6b9a253b6fe399aa6908bc4d7a4e8625622fadf4877af642d22c52f1e52c211a89b23e4cf3d4bec94b461fc9daf4db5de23384091255a33d055872cd337f97ef
|
7
|
+
data.tar.gz: 8425ced0e015ceada85d6fedf1b553358a774ff39b85a0d4700c058a8747a24049e4f6d52a7d138722cb7cca3e04f75d883eed51f10df8f8ddbe88de629ead6a
|
data/README.md
CHANGED
@@ -66,7 +66,10 @@ These values can be accessed and set via `PgParty.config` and `PgParty.configure
|
|
66
66
|
- Default: `true`
|
67
67
|
- `create_with_primary_key`
|
68
68
|
- Whether to add primary key constraints to partitioned (parent) tables by default.
|
69
|
-
* This is
|
69
|
+
* This behavior is disabled by default as this configuration usually requires composite primary keys to be specified
|
70
|
+
and ActiveRecord does not natively support composite primary keys. There are workarounds such as the
|
71
|
+
[composite_primary_keys gem](https://github.com/composite-primary-keys/composite_primary_keys).
|
72
|
+
* This is not supported for Postgres 10 (requires Postgres 11+)
|
70
73
|
* Primary key constraints must include all partition keys, for example: `primary_key: [:id, :created_at], partition_key: :created_at`
|
71
74
|
* Partition keys cannot use expressions
|
72
75
|
* Can be overridden via the `create_with_primary_key:` option when creating partitioned tables
|
@@ -86,7 +89,8 @@ PgParty.configure do |c|
|
|
86
89
|
c.caching_ttl = 60
|
87
90
|
c.schema_exclude_partitions = false
|
88
91
|
c.include_subpartitions_in_partition_list = true
|
89
|
-
# Postgres 11+ users starting fresh may
|
92
|
+
# Postgres 11+ users starting fresh may consider the below options to rely on Postgres' native features instead of
|
93
|
+
# this gem's template tables feature.
|
90
94
|
c.create_template_tables = false
|
91
95
|
c.create_with_primary_key = true
|
92
96
|
end
|
@@ -221,7 +225,7 @@ class CreateSomeListRecord < ActiveRecord::Migration[5.1]
|
|
221
225
|
end
|
222
226
|
```
|
223
227
|
|
224
|
-
Create _hash_ partitioned table on `
|
228
|
+
Create _hash_ partitioned table on `account_id` with two partitions (Postgres 11+ required):
|
225
229
|
* A hash partition can be used to spread keys evenly(ish) across partitions
|
226
230
|
* `modulus:` should always equal the total number of partitions planned for the table
|
227
231
|
* `remainder:` is an integer which should be in the range of 0 to modulus-1
|
@@ -232,7 +236,11 @@ class CreateSomeHashRecord < ActiveRecord::Migration[5.1]
|
|
232
236
|
# symbol is used for partition keys referring to individual columns
|
233
237
|
# create_with_primary_key: true, template: false on Postgres 11 will rely on PostgreSQL's native partition schema
|
234
238
|
# management vs this gem's template tables
|
235
|
-
|
239
|
+
# Note composite primary keys will require a workaround in ActiveRecord, such as through the use of the composite_primary_keys gem
|
240
|
+
create_hash_partition :some_hash_records, partition_key: :account_id, primary_key: [:id, :account_id],
|
241
|
+
create_with_primary_key: true, template: false do |t|
|
242
|
+
t.bigserial :id, null: false
|
243
|
+
t.bigint :account_id, null: false
|
236
244
|
t.text :some_value
|
237
245
|
t.timestamps
|
238
246
|
end
|
@@ -252,26 +260,18 @@ class CreateSomeHashRecord < ActiveRecord::Migration[5.1]
|
|
252
260
|
end
|
253
261
|
```
|
254
262
|
|
255
|
-
Advanced example with subpartitioning: Create _list_ partitioned table on `
|
256
|
-
with default partitions
|
257
|
-
* We can use Postgres 11's support for primary keys vs template tables, using the composite primary key `[:id, :created_at]`
|
258
|
-
to ensure all partition keys are present in the primary key
|
263
|
+
Advanced example with subpartitioning: Create _list_ partitioned table on `account_id` subpartitioned by _range_ on `created_at`
|
264
|
+
with default partitions. This example is for a table with no primary key... perhaps for some analytics use case.
|
259
265
|
* Default partitions are only supported in Postgres 11+
|
260
266
|
|
261
267
|
```ruby
|
262
268
|
class CreateSomeListSubpartitionedRecord < ActiveRecord::Migration[5.1]
|
263
269
|
def up
|
264
|
-
|
265
|
-
|
266
|
-
:
|
267
|
-
partition_key: [:id],
|
268
|
-
primary_key: [:id, :created_at],
|
269
|
-
template: false,
|
270
|
-
create_with_primary_key: true do |t|
|
271
|
-
|
272
|
-
t.integer :id
|
270
|
+
create_list_partition :some_list_subpartitioned_records, partition_key: :account_id, id: false,
|
271
|
+
template: false do |t|
|
272
|
+
t.bigint :account_id, null: false
|
273
273
|
t.text :some_value
|
274
|
-
t.
|
274
|
+
t.created_at
|
275
275
|
end
|
276
276
|
|
277
277
|
create_default_partition_of \
|
@@ -331,7 +331,8 @@ To avoid explicit index creation for _every_ new partition, we've introduced the
|
|
331
331
|
For every call to `create_list_partition` and `create_range_partition`, a clone `<table_name>_template` is created.
|
332
332
|
Indexes, constraints, etc. created on the template table will propagate to new partitions in calls to `create_list_partition_of` and `create_range_partition_of`:
|
333
333
|
* Subpartitions will correctly clone from template tables if a template table exists for the top-level ancestor
|
334
|
-
* When using Postgres 11 or higher, you may wish to disable template tables and use the native features instead, see [Configuration](#configuration)
|
334
|
+
* When using Postgres 11 or higher, you may wish to disable template tables and use the native features instead, see [Configuration](#configuration)\
|
335
|
+
but this may result in you using composite primary keys, which is not natively supported by ActiveRecord.
|
335
336
|
|
336
337
|
```ruby
|
337
338
|
class CreateSomeListRecord < ActiveRecord::Migration[5.1]
|
@@ -149,9 +149,9 @@ module PgParty
|
|
149
149
|
'`disable_ddl_transaction!` and break out this operation into its own migration.'
|
150
150
|
end
|
151
151
|
|
152
|
-
index_name, index_type, index_columns, index_options, algorithm, using =
|
153
|
-
|
154
|
-
|
152
|
+
index_name, index_type, index_columns, index_options, algorithm, using = extract_index_options(
|
153
|
+
add_index_options(table_name, column_name, **options)
|
154
|
+
)
|
155
155
|
|
156
156
|
# Postgres limits index name to 63 bytes (characters). We will use 8 characters for a `_random_suffix`
|
157
157
|
# on partitions to ensure no conflicts, leaving 55 chars for the specified index name
|
@@ -200,7 +200,7 @@ 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
|
+
create_table(table_name, **modified_options) do |td|
|
204
204
|
if !modified_options[:id] && id == :uuid
|
205
205
|
td.column(primary_key, id, null: false, default: uuid_function)
|
206
206
|
elsif !modified_options[:id] && id
|
@@ -316,6 +316,27 @@ module PgParty
|
|
316
316
|
"#{quote_table_name(table_name)} #{using} (#{columns})#{options}"
|
317
317
|
end
|
318
318
|
|
319
|
+
def extract_index_options(add_index_options_result)
|
320
|
+
# Rails 6.1 changes the result of #add_index_options
|
321
|
+
index_definition = add_index_options_result.first
|
322
|
+
return add_index_options_result unless index_definition.is_a?(ActiveRecord::ConnectionAdapters::IndexDefinition)
|
323
|
+
|
324
|
+
index_columns = if index_definition.columns.is_a?(String)
|
325
|
+
index_definition.columns
|
326
|
+
else
|
327
|
+
quoted_columns_for_index(index_definition.columns, index_definition.column_options)
|
328
|
+
end
|
329
|
+
|
330
|
+
[
|
331
|
+
index_definition.name,
|
332
|
+
index_definition.unique ? 'UNIQUE' : index_definition.type,
|
333
|
+
index_columns,
|
334
|
+
index_definition.where ? " WHERE #{index_definition.where}" : nil,
|
335
|
+
add_index_options_result.second, # algorithm option
|
336
|
+
index_definition.using ? "USING #{index_definition.using}" : nil
|
337
|
+
]
|
338
|
+
end
|
339
|
+
|
319
340
|
def drop_indices_if_exist(index_names)
|
320
341
|
index_names.uniq.each { |name| execute "DROP INDEX IF EXISTS #{quote_column_name(name)}" }
|
321
342
|
end
|
@@ -8,7 +8,7 @@ module PgParty
|
|
8
8
|
def reset_primary_key
|
9
9
|
return base_class.primary_key if self != base_class
|
10
10
|
|
11
|
-
partitions = partitions(include_subpartitions:
|
11
|
+
partitions = partitions(include_subpartitions: PgParty.config.include_subpartitions_in_partition_list)
|
12
12
|
return get_primary_key(base_class.name) if partitions.empty?
|
13
13
|
|
14
14
|
first_partition = partitions.detect { |p| !connection.table_partitioned?(p) }
|
@@ -23,8 +23,8 @@ module PgParty
|
|
23
23
|
connection.schema_cache.data_source_exists?(target_table)
|
24
24
|
end
|
25
25
|
|
26
|
-
def partitions(
|
27
|
-
PgParty::ModelDecorator.new(self).partitions(
|
26
|
+
def partitions(**args)
|
27
|
+
PgParty::ModelDecorator.new(self).partitions(**args)
|
28
28
|
end
|
29
29
|
|
30
30
|
def in_partition(*args)
|
data/lib/pg_party/version.rb
CHANGED
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.
|
4
|
+
version: 1.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan Krage
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-01-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -19,7 +19,7 @@ dependencies:
|
|
19
19
|
version: '5.0'
|
20
20
|
- - "<"
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: '6.
|
22
|
+
version: '6.2'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -29,7 +29,7 @@ dependencies:
|
|
29
29
|
version: '5.0'
|
30
30
|
- - "<"
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: '6.
|
32
|
+
version: '6.2'
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
34
|
name: ruby2_keywords
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
@@ -92,14 +92,14 @@ dependencies:
|
|
92
92
|
requirements:
|
93
93
|
- - "~>"
|
94
94
|
- !ruby/object:Gem::Version
|
95
|
-
version: '1.
|
95
|
+
version: '1.3'
|
96
96
|
type: :development
|
97
97
|
prerelease: false
|
98
98
|
version_requirements: !ruby/object:Gem::Requirement
|
99
99
|
requirements:
|
100
100
|
- - "~>"
|
101
101
|
- !ruby/object:Gem::Version
|
102
|
-
version: '1.
|
102
|
+
version: '1.3'
|
103
103
|
- !ruby/object:Gem::Dependency
|
104
104
|
name: database_cleaner
|
105
105
|
requirement: !ruby/object:Gem::Requirement
|