pg_party 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 25658551c69b0e57019a97f4e2e5fe71abc7768a
4
+ data.tar.gz: ee1b84c479f68ba16272825512365637e2b39a1c
5
+ SHA512:
6
+ metadata.gz: 6f6601857aee8c04fd9824ee121502f9ef04b8fd35f67a751ddfd9dd04008e459968b636768a957e35a3bcc6a39ec84a75ce90292141b1149cf52247901c3ef5
7
+ data.tar.gz: 32cbf53c8e2db08651e5007a05b2eaaa3dc5cdb3c1a1d2d579e9a04817ec6841a84098392cfb8f1ef32f29887c9b426f49b41fc632c82a576e9397bee4614683
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Ryan Krage
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.
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # PgParty
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/pg_party.svg)][rubygems]
4
+ [![Build Status](https://circleci.com/gh/rkrage/pg_party.svg?&style=shield)][circle]
5
+
6
+ [rubygems]: https://rubygems.org/gems/pg_party
7
+ [circle]: https://circleci.com/gh/rkrage/pg_party
8
+
9
+ Active Record migrations and model helpers for creating and managing PostgreSQL 10 partitions!
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'pg_party'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install pg_party
26
+
27
+ ## Usage
28
+
29
+ TODO: Write usage instructions here
30
+
31
+ ## Development
32
+
33
+ TODO: Write development instructions here
34
+
35
+ ## Contributing
36
+
37
+ Bug reports and pull requests are welcome on GitHub at https://github.com/rkrage/pg_party. 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.
38
+
39
+ ## License
40
+
41
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
42
+
43
+ ## Code of Conduct
44
+
45
+ Everyone interacting in the PgParty project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/rkrage/pg_party/blob/master/CODE_OF_CONDUCT.md).
data/lib/pg_party.rb ADDED
@@ -0,0 +1,16 @@
1
+ require "pg_party/version"
2
+ require "active_support"
3
+
4
+ ActiveSupport.on_load(:active_record) do
5
+ require "pg_party/connection_handling"
6
+ require "pg_party/model_methods"
7
+
8
+ extend PgParty::ConnectionHandling
9
+ extend PgParty::ModelMethods
10
+
11
+ require "pg_party/connection_adapters/abstract_adapter"
12
+
13
+ ActiveRecord::ConnectionAdapters::AbstractAdapter.class_eval do
14
+ include PgParty::ConnectionAdapters::AbstractAdapter
15
+ end
16
+ end
@@ -0,0 +1,33 @@
1
+ module PgParty
2
+ module ConnectionAdapters
3
+ module AbstractAdapter
4
+ def create_range_partition(*args)
5
+ raise NotImplementedError, "#create_range_partition is not implemented"
6
+ end
7
+
8
+ def create_list_partition(*args)
9
+ raise NotImplementedError, "#create_list_partition is not implemented"
10
+ end
11
+
12
+ def create_range_partition_of(*args)
13
+ raise NotImplementedError, "#create_range_partition_of is not implemented"
14
+ end
15
+
16
+ def create_list_partition_of(*args)
17
+ raise NotImplementedError, "#create_list_partition_of is not implemented"
18
+ end
19
+
20
+ def attach_range_partition(*args)
21
+ raise NotImplementedError, "#attach_range_partition is not implemented"
22
+ end
23
+
24
+ def attach_list_partition(*args)
25
+ raise NotImplementedError, "#attach_list_partition is not implemented"
26
+ end
27
+
28
+ def detach_partition(*args)
29
+ raise NotImplementedError, "#detach_partition is not implemented"
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,125 @@
1
+ require "digest"
2
+
3
+ module PgParty
4
+ module ConnectionAdapters
5
+ module PostgreSQLAdapter
6
+ def create_range_partition(table_name, partition_key:, **options, &blk)
7
+ create_partition(table_name, :range, partition_key, **options, &blk)
8
+ end
9
+
10
+ def create_list_partition(table_name, partition_key:, **options, &blk)
11
+ create_partition(table_name, :list, partition_key, **options, &blk)
12
+ end
13
+
14
+ def create_range_partition_of(table_name, start_range:, end_range:, **options)
15
+ if options[:name]
16
+ child_table_name = options[:name]
17
+ else
18
+ child_table_name = hashed_table_name(table_name, "#{start_range}#{end_range}")
19
+ end
20
+
21
+ constraint_clause = "FROM (#{quote(start_range)}) TO (#{quote(end_range)})"
22
+
23
+ create_partition_of(table_name, child_table_name, constraint_clause, **options)
24
+ end
25
+
26
+ def create_list_partition_of(table_name, values:, **options)
27
+ if options[:name]
28
+ child_table_name = options[:name]
29
+ else
30
+ child_table_name = hashed_table_name(table_name, values.to_s)
31
+ end
32
+
33
+ constraint_clause = "IN (#{Array.wrap(values).map(&method(:quote)).join(",")})"
34
+
35
+ create_partition_of(table_name, child_table_name, constraint_clause, **options)
36
+ end
37
+
38
+ def attach_range_partition(parent_table_name, child_table_name, start_range:, end_range:)
39
+ execute(<<-SQL)
40
+ ALTER TABLE #{quote_table_name(parent_table_name)}
41
+ ATTACH PARTITION #{quote_table_name(child_table_name)}
42
+ FOR VALUES FROM (#{quote(start_range)}) TO (#{quote(end_range)})
43
+ SQL
44
+ end
45
+
46
+ def attach_list_partition(parent_table_name, child_table_name, values:)
47
+ execute(<<-SQL)
48
+ ALTER TABLE #{quote_table_name(parent_table_name)}
49
+ ATTACH PARTITION #{quote_table_name(child_table_name)}
50
+ FOR VALUES IN (#{Array.wrap(values).map(&method(:quote)).join(",")})
51
+ SQL
52
+ end
53
+
54
+ def detach_partition(parent_table_name, child_table_name)
55
+ execute(<<-SQL)
56
+ ALTER TABLE #{quote_table_name(parent_table_name)}
57
+ DETACH PARTITION #{quote_table_name(child_table_name)}
58
+ SQL
59
+ end
60
+
61
+ private
62
+
63
+ def create_partition(table_name, type, partition_key, **options)
64
+ modified_options = options.except(:id, :primary_key)
65
+ id = options.fetch(:id, :bigserial)
66
+ primary_key = options.fetch(:primary_key, :id)
67
+
68
+ raise ArgumentError, "composite primary key not supported" if primary_key.is_a?(Array)
69
+
70
+ modified_options[:id] = false
71
+ modified_options[:options] = "PARTITION BY #{type.to_s.upcase} ((#{quote_partition_key(partition_key)}))"
72
+
73
+ create_table(table_name, modified_options) do |td|
74
+ if id == :uuid
75
+ td.send(id, primary_key, null: false, default: uuid_function)
76
+ elsif id
77
+ td.send(id, primary_key, null: false)
78
+ end
79
+
80
+ yield td if block_given?
81
+ end
82
+ end
83
+
84
+ def create_partition_of(table_name, child_table_name, constraint_clause, **options)
85
+ primary_key = options.fetch(:primary_key, :id)
86
+ index = options.fetch(:index, true)
87
+ partition_key = options[:partition_key]
88
+
89
+ raise ArgumentError, "composite primary key not supported" if primary_key.is_a?(Array)
90
+
91
+ partition_clause = <<-SQL
92
+ PARTITION OF #{quote_table_name(table_name)}
93
+ FOR VALUES #{constraint_clause}
94
+ SQL
95
+
96
+ create_table(child_table_name, id: false, options: partition_clause)
97
+
98
+ if primary_key
99
+ execute(<<-SQL)
100
+ ALTER TABLE #{quote_table_name(child_table_name)}
101
+ ADD PRIMARY KEY (#{quote_column_name(primary_key)})
102
+ SQL
103
+ end
104
+
105
+ if index && partition_key && primary_key != partition_key
106
+ add_index(child_table_name, "((#{quote_partition_key(partition_key)}))")
107
+ end
108
+
109
+ child_table_name
110
+ end
111
+
112
+ def quote_partition_key(key)
113
+ key.to_s.split("::").map(&method(:quote_column_name)).join("::")
114
+ end
115
+
116
+ def uuid_function
117
+ try(:supports_pgcrypto_uuid?) ? "gen_random_uuid()" : "uuid_generate_v4()"
118
+ end
119
+
120
+ def hashed_table_name(table_name, key)
121
+ "#{table_name}_#{Digest::MD5.hexdigest(key)[0..6]}"
122
+ end
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,15 @@
1
+ module PgParty
2
+ module ConnectionHandling
3
+ def establish_connection(*args)
4
+ super.tap do
5
+ if defined?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter)
6
+ require "pg_party/connection_adapters/postgresql_adapter"
7
+
8
+ ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.class_eval do
9
+ include PgParty::ConnectionAdapters::PostgreSQLAdapter
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,21 @@
1
+ require "pg_party/injected_model_methods"
2
+
3
+ module PgParty
4
+ module InjectedListModelMethods
5
+ include InjectedModelMethods
6
+
7
+ def create_partition(values:, **options)
8
+ modified_options = options.merge(
9
+ values: values,
10
+ primary_key: primary_key,
11
+ partition_key: partition_key
12
+ )
13
+
14
+ connection.create_list_partition_of(table_name, **modified_options)
15
+ end
16
+
17
+ def partition_key_in(*values)
18
+ where(partition_key_as_arel.in(values.flatten))
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,23 @@
1
+ module PgParty
2
+ module InjectedModelMethods
3
+ attr_reader :partition_key, :partition_column, :partition_cast
4
+
5
+ def partition_key_matching(value)
6
+ where(partition_key_as_arel.eq(value))
7
+ end
8
+
9
+ private
10
+
11
+ def partition_key_as_arel
12
+ arel_column = arel_table[partition_column]
13
+
14
+ if partition_cast
15
+ quoted_cast = connection.quote_column_name(partition_cast)
16
+
17
+ Arel::Nodes::NamedFunction.new("CAST", [arel_column.as(quoted_cast)])
18
+ else
19
+ arel_column
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,24 @@
1
+ require "pg_party/injected_model_methods"
2
+
3
+ module PgParty
4
+ module InjectedRangeModelMethods
5
+ include InjectedModelMethods
6
+
7
+ def create_partition(start_range:, end_range:, **options)
8
+ modified_options = options.merge(
9
+ start_range: start_range,
10
+ end_range: end_range,
11
+ primary_key: primary_key,
12
+ partition_key: partition_key
13
+ )
14
+
15
+ connection.create_range_partition_of(table_name, **modified_options)
16
+ end
17
+
18
+ def partition_key_in(start_range, end_range)
19
+ node = partition_key_as_arel
20
+
21
+ where(node.gteq(start_range).and(node.lt(end_range)))
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,23 @@
1
+ module PgParty
2
+ module ModelMethods
3
+ def range_partition_by(key)
4
+ @partition_key = key
5
+ @partition_column, @partition_cast = key.to_s.split("::")
6
+
7
+ require "pg_party/injected_range_model_methods"
8
+ extend InjectedRangeModelMethods
9
+ end
10
+
11
+ def list_partition_by(key)
12
+ @partition_key = key
13
+ @partition_column, @partition_cast = key.to_s.split("::")
14
+
15
+ require "pg_party/injected_list_model_methods"
16
+ extend InjectedListModelMethods
17
+ end
18
+
19
+ def partitioned?
20
+ @partition_key.present?
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ module PgParty
2
+ VERSION = "0.2.0"
3
+ end
metadata ADDED
@@ -0,0 +1,224 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pg_party
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Ryan Krage
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-09-15 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'
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'
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.20'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.20'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.15'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.15'
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-rails
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.6'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.6'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec-its
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.2'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.2'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry-byebug
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '3.4'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '3.4'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec_junit_formatter
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.3'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0.3'
125
+ - !ruby/object:Gem::Dependency
126
+ name: appraisal
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '2.2'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '2.2'
139
+ - !ruby/object:Gem::Dependency
140
+ name: combustion
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '0.7'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '0.7'
153
+ - !ruby/object:Gem::Dependency
154
+ name: database_cleaner
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '1.6'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '1.6'
167
+ - !ruby/object:Gem::Dependency
168
+ name: timecop
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - "~>"
172
+ - !ruby/object:Gem::Version
173
+ version: '0.9'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - "~>"
179
+ - !ruby/object:Gem::Version
180
+ version: '0.9'
181
+ description: Migrations and model helpers for creating and managing PostgreSQL 10
182
+ partitions
183
+ email:
184
+ - krage.ryan@gmail.com
185
+ executables: []
186
+ extensions: []
187
+ extra_rdoc_files: []
188
+ files:
189
+ - LICENSE.txt
190
+ - README.md
191
+ - lib/pg_party.rb
192
+ - lib/pg_party/connection_adapters/abstract_adapter.rb
193
+ - lib/pg_party/connection_adapters/postgresql_adapter.rb
194
+ - lib/pg_party/connection_handling.rb
195
+ - lib/pg_party/injected_list_model_methods.rb
196
+ - lib/pg_party/injected_model_methods.rb
197
+ - lib/pg_party/injected_range_model_methods.rb
198
+ - lib/pg_party/model_methods.rb
199
+ - lib/pg_party/version.rb
200
+ homepage: https://github.com/rkrage/pg_party
201
+ licenses:
202
+ - MIT
203
+ metadata: {}
204
+ post_install_message:
205
+ rdoc_options: []
206
+ require_paths:
207
+ - lib
208
+ required_ruby_version: !ruby/object:Gem::Requirement
209
+ requirements:
210
+ - - ">="
211
+ - !ruby/object:Gem::Version
212
+ version: '0'
213
+ required_rubygems_version: !ruby/object:Gem::Requirement
214
+ requirements:
215
+ - - ">="
216
+ - !ruby/object:Gem::Version
217
+ version: '0'
218
+ requirements: []
219
+ rubyforge_project:
220
+ rubygems_version: 2.6.11
221
+ signing_key:
222
+ specification_version: 4
223
+ summary: ActiveRecord PostgreSQL Partitioning
224
+ test_files: []