jdx-parts 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 05ffbcef9fe5c93a9124bdcbc42810a6cbe30afc1908ed5c8b047e5d9bf4bcf3
4
+ data.tar.gz: f0cce8f11d0aaba7bd058ec87bdcd40a607500cb2577649a273dd6fafd1b7ab2
5
+ SHA512:
6
+ metadata.gz: 8885650884acbf1b511d6631a037ced3f99ecb37f13c627846dda75094209cc0efbab34ce41c292ba5d6fcac2b1c5cf70f8ed34813c9bdf01d48ed3f75bdbe11
7
+ data.tar.gz: 4961dc3283737ae7aabbf83ecd5c37c2041b05071533fdd3a6f79f79d1b9bb714f1497da78337ac45898b18176d68976189349dec76415b3ef174d6841228371
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,55 @@
1
+ Metrics/LineLength:
2
+ Enabled: false
3
+
4
+ Style/Documentation:
5
+ Enabled: false
6
+
7
+ AllCops:
8
+ Exclude:
9
+ - bin/*
10
+ TargetRubyVersion: 2.5.0
11
+
12
+ Metrics/BlockLength:
13
+ Enabled: false
14
+
15
+ Metrics/MethodLength:
16
+ Enabled: false
17
+
18
+ Metrics/ClassLength:
19
+ Enabled: false
20
+
21
+ Metrics/ModuleLength:
22
+ Enabled: false
23
+
24
+ Style/ClassAndModuleChildren:
25
+ Enabled: false
26
+
27
+ Style/GlobalVars:
28
+ Enabled: false
29
+
30
+ Layout/IndentationWidth:
31
+ Enabled: false
32
+
33
+ Naming/UncommunicativeMethodParamName:
34
+ MinNameLength: 1
35
+
36
+ Layout/CaseIndentation:
37
+ EnforcedStyle: 'end'
38
+
39
+ Layout/EndAlignment:
40
+ EnforcedStyleAlignWith: start_of_line
41
+
42
+ Layout/MultilineMethodCallIndentation:
43
+ EnforcedStyle: indented
44
+
45
+ Layout/AlignHash:
46
+ EnforcedLastArgumentHashStyle: ignore_implicit
47
+
48
+ Layout/AlignParameters:
49
+ EnforcedStyle: with_fixed_indentation
50
+
51
+ Layout/IndentFirstHashElement:
52
+ EnforcedStyle: consistent
53
+
54
+ Style/ParallelAssignment:
55
+ Enabled: false
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.1
7
+ before_install: gem install bundler -v 2.0.1
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at kaushik@jobadx.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in parts.gemspec
4
+ gemspec
@@ -0,0 +1,73 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ parts (0.1.0)
5
+ activerecord (~> 5.0.7)
6
+ pg (~> 1.1)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activemodel (5.0.7.2)
12
+ activesupport (= 5.0.7.2)
13
+ activerecord (5.0.7.2)
14
+ activemodel (= 5.0.7.2)
15
+ activesupport (= 5.0.7.2)
16
+ arel (~> 7.0)
17
+ activesupport (5.0.7.2)
18
+ concurrent-ruby (~> 1.0, >= 1.0.2)
19
+ i18n (>= 0.7, < 2)
20
+ minitest (~> 5.1)
21
+ tzinfo (~> 1.1)
22
+ arel (7.1.4)
23
+ ast (2.4.0)
24
+ concurrent-ruby (1.1.5)
25
+ diff-lcs (1.3)
26
+ i18n (1.6.0)
27
+ concurrent-ruby (~> 1.0)
28
+ jaro_winkler (1.5.2)
29
+ minitest (5.11.3)
30
+ parallel (1.17.0)
31
+ parser (2.6.3.0)
32
+ ast (~> 2.4.0)
33
+ pg (1.1.4)
34
+ rainbow (3.0.0)
35
+ rake (10.5.0)
36
+ rspec (3.8.0)
37
+ rspec-core (~> 3.8.0)
38
+ rspec-expectations (~> 3.8.0)
39
+ rspec-mocks (~> 3.8.0)
40
+ rspec-core (3.8.0)
41
+ rspec-support (~> 3.8.0)
42
+ rspec-expectations (3.8.3)
43
+ diff-lcs (>= 1.2.0, < 2.0)
44
+ rspec-support (~> 3.8.0)
45
+ rspec-mocks (3.8.0)
46
+ diff-lcs (>= 1.2.0, < 2.0)
47
+ rspec-support (~> 3.8.0)
48
+ rspec-support (3.8.0)
49
+ rubocop (0.71.0)
50
+ jaro_winkler (~> 1.5.1)
51
+ parallel (~> 1.10)
52
+ parser (>= 2.6)
53
+ rainbow (>= 2.2.2, < 4.0)
54
+ ruby-progressbar (~> 1.7)
55
+ unicode-display_width (>= 1.4.0, < 1.7)
56
+ ruby-progressbar (1.10.1)
57
+ thread_safe (0.3.6)
58
+ tzinfo (1.2.5)
59
+ thread_safe (~> 0.1)
60
+ unicode-display_width (1.6.0)
61
+
62
+ PLATFORMS
63
+ ruby
64
+
65
+ DEPENDENCIES
66
+ bundler (~> 2.0)
67
+ parts!
68
+ rake (~> 10.0)
69
+ rspec (~> 3.0)
70
+ rubocop (~> 0.71.0)
71
+
72
+ BUNDLED WITH
73
+ 2.0.1
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 kaushik vinay
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,234 @@
1
+ # Parts
2
+
3
+ Parts is an ActiveRecord migrations and model helpers for creating and managing PostgreSQL 11+ declarative partitions!. This project is heavily
4
+ influnced from pg_party gem.
5
+
6
+ Features:
7
+
8
+ - Migration methods for partition specific database operations
9
+ - Model methods for querying partitioned data
10
+ - Model methods for creating adhoc partitions
11
+
12
+ Limitations:
13
+
14
+ - Partition tables are not represented correctly in db/schema.rb � please use the :sql schema format
15
+ - Complex partition keys are not supported yet.
16
+
17
+ ## Installation
18
+
19
+ Add this line to your application's Gemfile:
20
+
21
+ ```ruby
22
+ gem 'parts'
23
+ ```
24
+
25
+ And then execute:
26
+
27
+ $ bundle
28
+
29
+ Or install it yourself as:
30
+
31
+ $ gem install parts
32
+
33
+ ## Usage
34
+
35
+ ### Migration Examples
36
+
37
+ Create range partition on `created_at` with two child partitions:
38
+
39
+ ```ruby
40
+ class CreateSomeRangeRecord < ActiveRecord::Migration[5.0]
41
+ def up
42
+ current_date = Date.current
43
+
44
+ create_range_parent :some_range_records, :created_at do |t|
45
+ t.text :some_value
46
+ t.timestamps
47
+ end
48
+
49
+ add_index :some_range_records, :some_value
50
+
51
+ create_range_child_of :some_range_records,
52
+ "some_range_records_#{current_date.strftime('y%Ym%mw%V')}",
53
+ start_range: current_date.beginning_of_week,
54
+ end_range: current_date.next_week.beginning_of_week
55
+
56
+ next_date = current_date.next_week
57
+
58
+ create_range_child_of :some_range_records,
59
+ "some_range_records_#{next_date.strftime('y%Ym%mw%V')}",
60
+ start_range: next_date.beginning_of_week,
61
+ end_range: next_date.next_week.beginning_of_week
62
+ end
63
+ end
64
+ ```
65
+
66
+ Create list partition on `status` with two child partitions, second child partition `some_list_records_inactive` is further
67
+ range partitioned on `created_at`.
68
+
69
+ ```ruby
70
+ class CreateSomeListRecord < ActiveRecord::Migration[5.0]
71
+ def up
72
+ create_list_parent :some_list_records, :status do |t|
73
+ t.text :some_value
74
+ t.string :status
75
+
76
+ t.timestamps
77
+ end
78
+
79
+ create_list_child_of :some_list_records,
80
+ :some_list_records_active,
81
+ :active
82
+
83
+ create_sub_parent :some_list_records_inactive,
84
+ parent_table_name: :some_list_records,
85
+ parent_type: :list,
86
+ parent_values: :inactive,
87
+ type: :range,
88
+ partition_key: :created_at
89
+
90
+ current_date = Date.current
91
+
92
+ create_range_child_of :some_list_records_inactive,
93
+ "some_list_records_inactive_#{current_date.strftime('y%Ym%mw%V')}",
94
+ start_range: current_date.beginning_of_week,
95
+ end_range: current_date.next_week.beginning_of_week
96
+
97
+ next_date = current_date.next_week
98
+
99
+ create_range_child_of :some_list_records_inactive,
100
+ "some_list_records_inactive_#{next_date.strftime('y%Ym%mw%V')}",
101
+ start_range: next_date.beginning_of_week,
102
+ end_range: next_date.next_week.beginning_of_week
103
+ end
104
+ end
105
+ ```
106
+
107
+ Create hash partition on `id` with 10 child partitions:
108
+
109
+ ```ruby
110
+ class CreateSomeRangeRecord < ActiveRecord::Migration[5.0]
111
+ def up
112
+ create_hash_parent :some_huge_table, :id do |t|
113
+ t.text :some_value
114
+ t.timestamps
115
+ end
116
+
117
+ add_index :some_range_records, :some_value
118
+
119
+ # Creates 10 child hash table
120
+ create_hash_children_of :some_huge_table, 10
121
+ end
122
+ end
123
+ ```
124
+
125
+ Attach an existing table to a range partition:
126
+
127
+ ```ruby
128
+ class AttachRangePartition < ActiveRecord::Migration[5.0]
129
+ def up
130
+ current_date = Date.current
131
+
132
+ attach_range_child_to \
133
+ :some_existing_range_parent_partition_table,
134
+ :some_range_records,
135
+ start_range: current_date,
136
+ end_range: current_date + 1.day
137
+ end
138
+ end
139
+ ```
140
+
141
+ Attach an existing table to a list partition:
142
+
143
+ ```ruby
144
+ class AttachListPartition < ActiveRecord::Migration[5.0]
145
+ def up
146
+ attach_list_child_to \
147
+ :some_existing_list_parent_partition_table,
148
+ :some_list_records,
149
+ values: 'active'
150
+ end
151
+ end
152
+ ```
153
+
154
+ Detach a child table from any partition:
155
+
156
+ ```ruby
157
+ class DetachPartition < ActiveRecord::Migration[5.0]
158
+ def up
159
+ detach_child_from :parent_table, :child_table
160
+ end
161
+ end
162
+ ```
163
+
164
+ ### Model Examples
165
+
166
+ Extend your model to support partitioning methods within the model:
167
+
168
+ ```ruby
169
+ class SomeRangeRecord < ApplicationRecord
170
+ extend Parts::ModelMethods
171
+ end
172
+ ```
173
+
174
+ Create child partition from range partition model:
175
+
176
+ ```ruby
177
+ current_date = Date.current
178
+
179
+ SomeRangeRecord.create_range_child("some_range_record_xyz", start_range: current_date + 1.day, end_range: current_date + 2.days)
180
+ ```
181
+
182
+ Create child partition from list partition model:
183
+
184
+ ```ruby
185
+ SomeListRecord.create_list_child("some_list_record_active", :active)
186
+ ```
187
+
188
+ List currently attached partitions:
189
+
190
+ ```ruby
191
+ SomeRangeRecord.partitions
192
+
193
+ SomeListRecord.partitions
194
+ ```
195
+
196
+ Retrieve ActiveRecord model class scoped to a child partition:
197
+
198
+ ```ruby
199
+ SomeRangeRecord.in_partition(:some_range_records_partition_name)
200
+
201
+ SomeListRecord.in_partition(:some_list_records_partition_name)
202
+
203
+ #check for partitions within sub-partitions
204
+
205
+ SomeListRecord.in_partition(:some_list_sub_parent_partition).partitions
206
+ ```
207
+
208
+ Check if any model is partitioned:
209
+
210
+ ```ruby
211
+ SomeNonPartitionedTable.partitioned?
212
+ # => false
213
+
214
+ SomePartitionedTable.partitioned?
215
+ # => true
216
+ ```
217
+
218
+ ## Development
219
+
220
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
221
+
222
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
223
+
224
+ ## Contributing
225
+
226
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/parts. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
227
+
228
+ ## License
229
+
230
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
231
+
232
+ ## Code of Conduct
233
+
234
+ Everyone interacting in the Parts project�s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/parts/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'parts'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'parts/version'
4
+ require 'active_support/lazy_load_hooks'
5
+
6
+ module Parts; end
7
+
8
+ ActiveSupport.on_load(:active_record) do
9
+ require 'active_record/connection_adapters/postgresql_adapter'
10
+ require 'parts/partition_adapter'
11
+ require 'parts/model_methods'
12
+ require 'parts/generic_model_methods'
13
+
14
+ extend Parts::GenericModelMethods
15
+
16
+ ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.include(
17
+ Parts::PartitionAdapter
18
+ )
19
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Parts
4
+ module GenericModelMethods
5
+ def partitioned?
6
+ connection.select_values("SELECT 1 FROM pg_partitioned_table WHERE partrelid = #{connection.quote(table_name)}::regclass LIMIT 1;").any?
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Parts
4
+ module ModelMethods
5
+ def partitions
6
+ connection.select_values <<~SQL
7
+ SELECT inhrelid::regclass::text FROM pg_inherits WHERE inhparent = #{connection.quote(table_name)}::regclass;
8
+ SQL
9
+ end
10
+
11
+ def in_partition(child_table_name)
12
+ Class.new(self) do
13
+ self.table_name = child_table_name
14
+
15
+ def self.name
16
+ superclass.name
17
+ end
18
+
19
+ def self.allocate
20
+ superclass.allocate
21
+ end
22
+
23
+ def self.new(*args, &blk)
24
+ superclass.new(*args, &blk)
25
+ end
26
+ end
27
+ end
28
+
29
+ def create_range_child(child_table_name, start_range:, end_range:)
30
+ transaction { connection.create_range_child_of(table_name, child_table_name, start_range: start_range, end_range: end_range) }
31
+ end
32
+
33
+ def create_list_child(child_table_name, values)
34
+ transaction { connection.create_list_child_of(table_name, child_table_name, values) }
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,139 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Parts
4
+ module PartitionAdapter
5
+ def create_range_parent(table_name, partition_key, **options, &blk)
6
+ create_parent(table_name, :range, partition_key, options, &blk)
7
+ end
8
+
9
+ def create_list_parent(table_name, partition_key, **options, &blk)
10
+ create_parent(table_name, :list, partition_key, options, &blk)
11
+ end
12
+
13
+ def create_hash_parent(table_name, partition_key, **options, &blk)
14
+ create_parent(table_name, :hash, partition_key, options, &blk)
15
+ end
16
+
17
+ def create_range_child_of(parent_table_name, table_name, start_range:, end_range:, **options)
18
+ create_child_of(parent_table_name, table_name, range_constraint_clause(start_range, end_range), **options)
19
+ end
20
+
21
+ def create_list_child_of(parent_table_name, table_name, values, **options)
22
+ create_child_of(parent_table_name, table_name, list_constraint_clause(values), **options)
23
+ end
24
+
25
+ def create_hash_child_of(parent_table_name, table_name, modulus:, remainder:, **options)
26
+ create_child_of(parent_table_name, table_name, hash_constraint_clause(modulus, remainder), **options)
27
+ end
28
+
29
+ def create_hash_children_of(parent_table_name, total, **options)
30
+ raise "'total' must be an Integer" unless total.is_a?(Integer)
31
+
32
+ total.times do |t|
33
+ create_hash_child_of(parent_table_name, "#{parent_table_name}_#{t}", modulus: total, remainder: t, **options)
34
+ end
35
+ end
36
+
37
+ def detach_child_from(parent_table_name, child_table_name, lock_timeout: 5)
38
+ execute <<~SQL
39
+ SET LOCAL lock_timeout TO '#{lock_timeout}s';
40
+ ALTER TABLE #{quote_table_name(parent_table_name)}
41
+ DETACH PARTITION #{quote_table_name(child_table_name)};
42
+ SQL
43
+ end
44
+
45
+ def attach_list_child_to(parent_table_name, child_table_name, values, **options)
46
+ attach_child_to(parent_table_name, child_table_name, list_constraint_clause(values), **options)
47
+ end
48
+
49
+ def attach_range_child_to(parent_table_name, child_table_name, start_range:, end_range:)
50
+ attach_child_to(parent_table_name, child_table_name, range_constraint_clause(start_range, end_range), **options)
51
+ end
52
+
53
+ def attach_hash_child_to(parent_table_name, child_table_name, modulus:, remainder:, **options)
54
+ attach_child_to(parent_table_name, child_table_name, hash_constraint_clause(modulus, remainder), **options)
55
+ end
56
+
57
+ def attach_child_to(parent_table_name, child_table_name, constraint_clause, lock_timeout: 5)
58
+ execute <<~SQL
59
+ SET LOCAL lock_timeout TO '#{lock_timeout}s';
60
+ ALTER TABLE #{quote_table_name(parent_table_name)}
61
+ ATTACH PARTITION #{quote_table_name(child_table_name)}
62
+ FOR VALUES #{constraint_clause};
63
+ SQL
64
+ end
65
+
66
+ def create_sub_parent(table_name, parent_table_name:, parent_type:, parent_values:, type:, partition_key:, lock_timeout: 5) # rubocop:disable Metrics/ParameterLists
67
+
68
+ constraint_clause = case parent_type
69
+ when :list
70
+ list_constraint_clause(parent_values)
71
+ when :range
72
+ range_constraint_clause(parent_values[0], parent_values[1])
73
+ when :hash
74
+ hash_constraint_clause(parent_values[0], parent_values[1])
75
+ end
76
+
77
+ execute <<~SQL
78
+ SET LOCAL lock_timeout TO '#{lock_timeout}s';
79
+ CREATE TABLE #{table_name} PARTITION OF #{parent_table_name} FOR VALUES #{constraint_clause} PARTITION BY #{type.to_s.upcase} (#{partition_key});
80
+ SQL
81
+ end
82
+
83
+ private
84
+
85
+ def create_parent(table_name, type, partition_key, options)
86
+ options[:options] = "PARTITION BY #{type.to_s.upcase} (#{quote_partition_key(partition_key)})"
87
+ id = options.fetch(:id, :bigserial)
88
+ primary_key = options.fetch(:primary_key) { calculate_primary_key(table_name) } if id != false
89
+ options[:id] = false
90
+
91
+ validate_primary_key(primary_key)
92
+
93
+ create_table(table_name, **options) do |t|
94
+ t.column(primary_key, id, null: false) if id != false
95
+
96
+ yield(t) if block_given?
97
+ end
98
+ end
99
+
100
+ def create_child_of(parent_table_name, table_name, constraint_clause, lock_timeout: 5)
101
+ execute <<~SQL
102
+ SET LOCAL lock_timeout TO '#{lock_timeout}s';
103
+ CREATE TABLE #{table_name} PARTITION OF #{parent_table_name} FOR VALUES #{constraint_clause};
104
+ SQL
105
+ end
106
+
107
+ def calculate_primary_key(table_name)
108
+ ActiveRecord::Base.get_primary_key(table_name.to_s.singularize).to_sym
109
+ end
110
+
111
+ def validate_primary_key(key)
112
+ raise ArgumentError, 'composite primary key not supported' if key.is_a?(Array)
113
+ end
114
+
115
+ def quote_partition_key(key)
116
+ if key.is_a?(Proc)
117
+ key.call.to_s
118
+ else
119
+ quote_column_name(key)
120
+ end
121
+ end
122
+
123
+ def quote_collection(values)
124
+ Array.wrap(values).map(&method(:quote)).join(',')
125
+ end
126
+
127
+ def range_constraint_clause(start_range, end_range)
128
+ "FROM (#{quote_collection(start_range)}) TO (#{quote_collection(end_range)})"
129
+ end
130
+
131
+ def list_constraint_clause(values)
132
+ "IN (#{quote_collection(values)})"
133
+ end
134
+
135
+ def hash_constraint_clause(modulus, remainder)
136
+ "WITH (MODULUS #{modulus}, REMAINDER #{remainder})"
137
+ end
138
+ end
139
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Parts
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'parts/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'jdx-parts'
9
+ spec.version = Parts::VERSION
10
+ spec.authors = ['kaushik vinay']
11
+ spec.email = ['kaushik@jobadx.com']
12
+
13
+ spec.summary = 'ActiveRecord PostgreSQL Declarative Partitioning'
14
+ spec.description = 'Migrations and model helpers for creating and managing PostgreSQL 11+ partitions'
15
+ spec.homepage = 'https://bitbucket.org/kaushikvinaytg/parts'
16
+ spec.license = 'MIT'
17
+
18
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
19
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
20
+ if spec.respond_to?(:metadata)
21
+ spec.metadata['homepage_uri'] = spec.homepage
22
+ spec.metadata['source_code_uri'] = 'https://bitbucket.org/kaushikvinaytg/parts/src'
23
+ else
24
+ raise 'RubyGems 2.0 or newer is required to protect against ' \
25
+ 'public gem pushes.'
26
+ end
27
+
28
+ # Specify which files should be added to the gem when it is released.
29
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
30
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
31
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
32
+ end
33
+ spec.bindir = 'exe'
34
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
35
+ spec.require_paths = ['lib']
36
+
37
+ spec.add_runtime_dependency 'activerecord', '~> 5.0.7'
38
+ spec.add_runtime_dependency 'pg'
39
+
40
+ spec.add_development_dependency 'bundler', '~> 2.0'
41
+ spec.add_development_dependency 'rake', '~> 10.0'
42
+ spec.add_development_dependency 'rspec', '~> 3.0'
43
+ spec.add_development_dependency 'rubocop', '~> 0.71.0'
44
+ end
metadata ADDED
@@ -0,0 +1,148 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jdx-parts
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - kaushik vinay
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-02-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 5.0.7
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 5.0.7
27
+ - !ruby/object:Gem::Dependency
28
+ name: pg
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.71.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.71.0
97
+ description: Migrations and model helpers for creating and managing PostgreSQL 11+
98
+ partitions
99
+ email:
100
+ - kaushik@jobadx.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".rspec"
107
+ - ".rubocop.yml"
108
+ - ".travis.yml"
109
+ - CODE_OF_CONDUCT.md
110
+ - Gemfile
111
+ - Gemfile.lock
112
+ - LICENSE.txt
113
+ - README.md
114
+ - Rakefile
115
+ - bin/console
116
+ - bin/setup
117
+ - lib/parts.rb
118
+ - lib/parts/generic_model_methods.rb
119
+ - lib/parts/model_methods.rb
120
+ - lib/parts/partition_adapter.rb
121
+ - lib/parts/version.rb
122
+ - parts.gemspec
123
+ homepage: https://bitbucket.org/kaushikvinaytg/parts
124
+ licenses:
125
+ - MIT
126
+ metadata:
127
+ homepage_uri: https://bitbucket.org/kaushikvinaytg/parts
128
+ source_code_uri: https://bitbucket.org/kaushikvinaytg/parts/src
129
+ post_install_message:
130
+ rdoc_options: []
131
+ require_paths:
132
+ - lib
133
+ required_ruby_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ required_rubygems_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ requirements: []
144
+ rubygems_version: 3.0.4
145
+ signing_key:
146
+ specification_version: 4
147
+ summary: ActiveRecord PostgreSQL Declarative Partitioning
148
+ test_files: []