switchman 1.12.16 → 1.13.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/models/switchman/shard_internal.rb +0 -2
- data/lib/switchman/active_record/base.rb +5 -0
- data/lib/switchman/active_record/relation.rb +28 -0
- data/lib/switchman/database_server.rb +1 -7
- data/lib/switchman/r_spec_helper.rb +1 -1
- data/lib/switchman/version.rb +1 -1
- metadata +2 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e2f68441314d99fc739f03cd1af70e938efa0d0803ff6f5c405aed15afea0798
|
4
|
+
data.tar.gz: 553846718daea03d833792d3ff079ee0ce2bddce63231f2e40262fe901c84949
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4d05aea36873a2d809b414ad9cceeb8e1ee3c0966acd7e67e3f941e3b971c685a3a7ad666d19b5a74edc47ba59025782a486c70c596d918998ef01a01c5b9f40
|
7
|
+
data.tar.gz: 02bb3d231398cae6966ec6d8f1ca5ce4101f529d55dc119ebcbd512077ab3c5887c3eb9309fc5a8aba2df7fcd5eb21fae9911c1c2b4c29cfc328653a52fa3d1f
|
@@ -4,6 +4,11 @@ module Switchman
|
|
4
4
|
module ClassMethods
|
5
5
|
delegate :shard, to: :all
|
6
6
|
|
7
|
+
def find_ids_in_ranges(opts={}, &block)
|
8
|
+
opts.reverse_merge!(:loose => true)
|
9
|
+
all.find_ids_in_ranges(opts, &block)
|
10
|
+
end
|
11
|
+
|
7
12
|
def shard_category
|
8
13
|
connection_specification_name.to_sym
|
9
14
|
end
|
@@ -67,6 +67,34 @@ module Switchman
|
|
67
67
|
RUBY
|
68
68
|
end
|
69
69
|
|
70
|
+
def find_ids_in_ranges(options = {})
|
71
|
+
is_integer = columns_hash[primary_key.to_s].type == :integer
|
72
|
+
loose_mode = options[:loose] && is_integer
|
73
|
+
# loose_mode: if we don't care about getting exactly batch_size ids in between
|
74
|
+
# don't get the max - just get the min and add batch_size so we get that many _at most_
|
75
|
+
values = loose_mode ? "MIN(id)" : "MIN(id), MAX(id)"
|
76
|
+
|
77
|
+
batch_size = options[:batch_size].try(:to_i) || 1000
|
78
|
+
quoted_primary_key = "#{klass.connection.quote_local_table_name(table_name)}.#{klass.connection.quote_column_name(primary_key)}"
|
79
|
+
as_id = " AS id" unless primary_key == 'id'
|
80
|
+
subquery_scope = except(:select).select("#{quoted_primary_key}#{as_id}").reorder(primary_key.to_sym).limit(loose_mode ? 1 : batch_size)
|
81
|
+
subquery_scope = subquery_scope.where("#{quoted_primary_key} <= ?", options[:end_at]) if options[:end_at]
|
82
|
+
|
83
|
+
first_subquery_scope = options[:start_at] ? subquery_scope.where("#{quoted_primary_key} >= ?", options[:start_at]) : subquery_scope
|
84
|
+
|
85
|
+
ids = connection.select_rows("SELECT #{values} FROM (#{first_subquery_scope.to_sql}) AS subquery").first
|
86
|
+
|
87
|
+
while ids.first.present?
|
88
|
+
ids.map!(&:to_i) if is_integer
|
89
|
+
ids << ids.first + batch_size if loose_mode
|
90
|
+
|
91
|
+
yield(*ids)
|
92
|
+
last_value = ids.last
|
93
|
+
next_subquery_scope = subquery_scope.where(["#{quoted_primary_key}>?", last_value])
|
94
|
+
ids = connection.select_rows("SELECT #{values} FROM (#{next_subquery_scope.to_sql}) AS subquery").first
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
70
98
|
def activate(&block)
|
71
99
|
shards = all_shards
|
72
100
|
if (Array === shards && shards.length == 1)
|
@@ -116,7 +116,7 @@ module Switchman
|
|
116
116
|
config = config.first if config.is_a?(Array)
|
117
117
|
username = config[:username]
|
118
118
|
end
|
119
|
-
@shareable =
|
119
|
+
@shareable = username !~ /%?\{[a-zA-Z0-9_]+\}/
|
120
120
|
end
|
121
121
|
|
122
122
|
def shards
|
@@ -143,8 +143,6 @@ module Switchman
|
|
143
143
|
when 'postgresql'
|
144
144
|
create_statement = lambda { "CREATE SCHEMA #{name}" }
|
145
145
|
password = " PASSWORD #{::ActiveRecord::Base.connection.quote(config[:password])}" if config[:password]
|
146
|
-
when 'sqlite3'
|
147
|
-
# no create_statement
|
148
146
|
else
|
149
147
|
create_statement = lambda { "CREATE DATABASE #{name}" }
|
150
148
|
end
|
@@ -172,14 +170,10 @@ module Switchman
|
|
172
170
|
|
173
171
|
if name.nil?
|
174
172
|
base_name = self.config[:database].to_s % self.config
|
175
|
-
base_name = $1 if base_name =~ /(?:.*\/)(.+)_shard_\d+(?:\.sqlite3)?$/
|
176
173
|
base_name = nil if base_name == ':memory:'
|
177
174
|
base_name << '_' if base_name
|
178
175
|
base_id = shard_id || SecureRandom.uuid
|
179
176
|
name = "#{base_name}shard_#{base_id}"
|
180
|
-
if config[:adapter] == 'sqlite3'
|
181
|
-
name = File.join('db', "#{name}.sqlite3")
|
182
|
-
end
|
183
177
|
end
|
184
178
|
|
185
179
|
shard = Shard.create!(:id => shard_id,
|
data/lib/switchman/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: switchman
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.13.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cody Cutrer
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2019-
|
13
|
+
date: 2019-02-27 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: railties
|
@@ -150,20 +150,6 @@ dependencies:
|
|
150
150
|
- - "~>"
|
151
151
|
- !ruby/object:Gem::Version
|
152
152
|
version: '0.15'
|
153
|
-
- !ruby/object:Gem::Dependency
|
154
|
-
name: sqlite3
|
155
|
-
requirement: !ruby/object:Gem::Requirement
|
156
|
-
requirements:
|
157
|
-
- - "~>"
|
158
|
-
- !ruby/object:Gem::Version
|
159
|
-
version: '1.3'
|
160
|
-
type: :development
|
161
|
-
prerelease: false
|
162
|
-
version_requirements: !ruby/object:Gem::Requirement
|
163
|
-
requirements:
|
164
|
-
- - "~>"
|
165
|
-
- !ruby/object:Gem::Version
|
166
|
-
version: '1.3'
|
167
153
|
- !ruby/object:Gem::Dependency
|
168
154
|
name: rake
|
169
155
|
requirement: !ruby/object:Gem::Requirement
|