pg_party 0.4.2 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/pg_party.rb +9 -5
- data/lib/pg_party/adapter_decorator.rb +22 -8
- data/lib/pg_party/model/shared_methods.rb +10 -0
- data/lib/pg_party/model_decorator.rb +22 -0
- data/lib/pg_party/version.rb +1 -1
- metadata +12 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2fdbe60dbdafaf57fb4fff60f9cc484640226e39
|
4
|
+
data.tar.gz: fdfa7113d06c6605e5c9ce6936ab688b14364b9d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f50e755596d95d0063436bd0a65a045103d1e3fc89f3657b69c1204a6cb73f6172dcd90e27152c072b47f8a631bf9c526a56a1096444323bc4b93398852ba03c
|
7
|
+
data.tar.gz: eeddf72d17854248814a845470238695b36d2852555b7ba98a2cec79d88ce32786f525722fc5271518acb38d5987abefebc8c55763f42d64592b14f41ac6072f
|
data/lib/pg_party.rb
CHANGED
@@ -11,11 +11,15 @@ ActiveSupport.on_load(:active_record) do
|
|
11
11
|
ActiveRecord::ConnectionAdapters::AbstractAdapter.class_eval do
|
12
12
|
include PgParty::Adapter::AbstractMethods
|
13
13
|
end
|
14
|
+
|
15
|
+
begin
|
16
|
+
require "active_record/connection_adapters/postgresql_adapter"
|
17
|
+
require "pg_party/adapter/postgresql_methods"
|
14
18
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
19
|
+
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.class_eval do
|
20
|
+
include PgParty::Adapter::PostgreSQLMethods
|
21
|
+
end
|
22
|
+
rescue LoadError
|
23
|
+
# migration methods will not be available
|
20
24
|
end
|
21
25
|
end
|
@@ -75,15 +75,20 @@ module PgParty
|
|
75
75
|
modified_options[:id] = false
|
76
76
|
modified_options[:options] = "PARTITION BY #{type.to_s.upcase} ((#{quote_partition_key(partition_key)}))"
|
77
77
|
|
78
|
-
create_table(table_name, modified_options) do |td|
|
78
|
+
result = create_table(table_name, modified_options) do |td|
|
79
79
|
if id == :uuid
|
80
|
-
td.
|
80
|
+
td.column(primary_key, id, null: false, default: uuid_function)
|
81
81
|
elsif id
|
82
|
-
td.
|
82
|
+
td.column(primary_key, id, null: false)
|
83
83
|
end
|
84
84
|
|
85
85
|
yield(td) if block_given?
|
86
86
|
end
|
87
|
+
|
88
|
+
# Rails 4 has a bug where uuid columns are always nullable
|
89
|
+
change_column_null(table_name, primary_key, false) if id == :uuid
|
90
|
+
|
91
|
+
result
|
87
92
|
end
|
88
93
|
|
89
94
|
def create_partition_of(table_name, child_table_name, constraint_clause, **options)
|
@@ -93,13 +98,12 @@ module PgParty
|
|
93
98
|
|
94
99
|
raise ArgumentError, "composite primary key not supported" if primary_key.is_a?(Array)
|
95
100
|
|
96
|
-
|
101
|
+
execute(<<-SQL)
|
102
|
+
CREATE TABLE #{quote_table_name(child_table_name)}
|
97
103
|
PARTITION OF #{quote_table_name(table_name)}
|
98
104
|
FOR VALUES #{constraint_clause}
|
99
105
|
SQL
|
100
106
|
|
101
|
-
create_table(child_table_name, id: false, options: partition_clause)
|
102
|
-
|
103
107
|
if primary_key
|
104
108
|
execute(<<-SQL)
|
105
109
|
ALTER TABLE #{quote_table_name(child_table_name)}
|
@@ -108,7 +112,13 @@ module PgParty
|
|
108
112
|
end
|
109
113
|
|
110
114
|
if index && partition_key && primary_key != partition_key
|
111
|
-
|
115
|
+
index_name = index_name(child_table_name, partition_key)
|
116
|
+
|
117
|
+
execute(<<-SQL)
|
118
|
+
CREATE INDEX #{quote_table_name(index_name)}
|
119
|
+
ON #{quote_table_name(child_table_name)}
|
120
|
+
USING btree ((#{quote_partition_key(partition_key)}))
|
121
|
+
SQL
|
112
122
|
end
|
113
123
|
|
114
124
|
child_table_name
|
@@ -127,7 +137,11 @@ module PgParty
|
|
127
137
|
end
|
128
138
|
|
129
139
|
def supports_partitions?
|
130
|
-
postgresql_version >= 100000
|
140
|
+
__getobj__.send(:postgresql_version) >= 100000
|
141
|
+
end
|
142
|
+
|
143
|
+
def index_name(table_name, partition_key)
|
144
|
+
"index_#{table_name}_on_#{partition_key.to_s.split("::").join("_")}"
|
131
145
|
end
|
132
146
|
end
|
133
147
|
end
|
@@ -3,6 +3,16 @@ require "pg_party/model_decorator"
|
|
3
3
|
module PgParty
|
4
4
|
module Model
|
5
5
|
module SharedMethods
|
6
|
+
def reset_primary_key
|
7
|
+
PgParty::ModelDecorator.new(self).partition_primary_key
|
8
|
+
end
|
9
|
+
|
10
|
+
def table_exists?
|
11
|
+
return @table_exists if defined?(@table_exists)
|
12
|
+
|
13
|
+
@table_exists = PgParty::ModelDecorator.new(self).partition_table_exists?
|
14
|
+
end
|
15
|
+
|
6
16
|
def partitions
|
7
17
|
PgParty::ModelDecorator.new(self).partitions
|
8
18
|
end
|
@@ -1,5 +1,21 @@
|
|
1
1
|
module PgParty
|
2
2
|
class ModelDecorator < SimpleDelegator
|
3
|
+
def partition_primary_key
|
4
|
+
if self != base_class
|
5
|
+
base_class.primary_key
|
6
|
+
elsif partition = partitions.first
|
7
|
+
child_class(partition).get_primary_key(base_class.name)
|
8
|
+
else
|
9
|
+
get_primary_key(base_class.name)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def partition_table_exists?
|
14
|
+
target_table = partitions.first || table_name
|
15
|
+
|
16
|
+
connection.schema_cache.send(table_exists_method, target_table)
|
17
|
+
end
|
18
|
+
|
3
19
|
def in_partition(table_name)
|
4
20
|
child_class(table_name).all
|
5
21
|
end
|
@@ -51,6 +67,12 @@ module PgParty
|
|
51
67
|
|
52
68
|
private
|
53
69
|
|
70
|
+
def table_exists_method
|
71
|
+
[:data_source_exists?, :table_exists?].detect do |meth|
|
72
|
+
connection.schema_cache.respond_to?(meth)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
54
76
|
def child_class(table_name)
|
55
77
|
Class.new(__getobj__) do
|
56
78
|
self.table_name = table_name
|
data/lib/pg_party/version.rb
CHANGED
metadata
CHANGED
@@ -1,29 +1,35 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pg_party
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.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: 2017-10-
|
11
|
+
date: 2017-10-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.2'
|
20
|
+
- - "<"
|
18
21
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
22
|
+
version: '6'
|
20
23
|
type: :runtime
|
21
24
|
prerelease: false
|
22
25
|
version_requirements: !ruby/object:Gem::Requirement
|
23
26
|
requirements:
|
24
|
-
- - "
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '4.2'
|
30
|
+
- - "<"
|
25
31
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
32
|
+
version: '6'
|
27
33
|
- !ruby/object:Gem::Dependency
|
28
34
|
name: pg
|
29
35
|
requirement: !ruby/object:Gem::Requirement
|