switchman 1.3.7 → 1.3.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/switchman/active_record/abstract_adapter.rb +4 -0
- data/lib/switchman/active_record/attribute_methods.rb +1 -1
- data/lib/switchman/active_record/postgresql_adapter.rb +18 -7
- data/lib/switchman/active_record/reflection.rb +15 -0
- data/lib/switchman/arel.rb +30 -0
- data/lib/switchman/engine.rb +3 -0
- data/lib/switchman/version.rb +1 -1
- data/spec/dummy/log/test.log +38 -0
- data/spec/lib/active_record/postgresql_adapter_spec.rb +16 -8
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a8f9aba599d3ef529a78c94fd5e94c2f204b7ccc
|
4
|
+
data.tar.gz: 5bf0f3c88787aec47ed93c41069cc7ad06054423
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b44289cf27c475ffacc928e691c6e0698b771ebc840d742ffab6e7e926bf728ebff2c574bb403df221cbf51bfc46eeea18b2a62bde62e1a847972228eefb9b65
|
7
|
+
data.tar.gz: bb774752360b5b84cfa665b8c13f1dd3bba7fe5a4bd09007d453309d8003699c8e0917e17e3452222410746aaee6088d95b1d9cbc3aa2e5bcdf76ffd260afafd
|
@@ -22,6 +22,10 @@ module Switchman
|
|
22
22
|
@last_query_at = Time.now
|
23
23
|
end
|
24
24
|
|
25
|
+
def quote_local_table_name(name)
|
26
|
+
quote_table_name(name)
|
27
|
+
end
|
28
|
+
|
25
29
|
if ::Rails.version < '4'
|
26
30
|
def dump_schema_information #:nodoc:
|
27
31
|
sm_table = ::ActiveRecord::Migrator.schema_migrations_table_name
|
@@ -30,7 +30,7 @@ module Switchman
|
|
30
30
|
reflections.find { |_, r| r.belongs_to? && r.foreign_key.to_s == attr_name }.try(:last)
|
31
31
|
rescue ::ActiveRecord::StatementInvalid
|
32
32
|
# this is for when models are referenced in initializers before migrations have been run
|
33
|
-
|
33
|
+
raise if connection.open_transactions > 0
|
34
34
|
end
|
35
35
|
|
36
36
|
def define_method_global_attribute(attr_name)
|
@@ -9,8 +9,12 @@ module Switchman
|
|
9
9
|
select_values("SELECT * FROM unnest(current_schemas(false))")
|
10
10
|
end
|
11
11
|
|
12
|
+
def use_qualified_names?
|
13
|
+
@config[:use_qualified_names]
|
14
|
+
end
|
15
|
+
|
12
16
|
def tables(name = nil)
|
13
|
-
schema = shard.name if
|
17
|
+
schema = shard.name if use_qualified_names?
|
14
18
|
|
15
19
|
query(<<-SQL, 'SCHEMA').map { |row| row[0] }
|
16
20
|
SELECT tablename
|
@@ -23,7 +27,7 @@ module Switchman
|
|
23
27
|
if ::Rails.version < '4.2'
|
24
28
|
schema, table = ::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::Utils.extract_schema_and_table(name.to_s)
|
25
29
|
return false unless table
|
26
|
-
schema ||= shard.name if
|
30
|
+
schema ||= shard.name if use_qualified_names?
|
27
31
|
|
28
32
|
binds = [[nil, table]]
|
29
33
|
binds << [nil, schema] if schema
|
@@ -39,7 +43,7 @@ module Switchman
|
|
39
43
|
else
|
40
44
|
name = Utils.extract_schema_qualified_name(name.to_s)
|
41
45
|
return false unless name.identifier
|
42
|
-
if !name.schema &&
|
46
|
+
if !name.schema && use_qualified_names?
|
43
47
|
name.instance_variable_set(:@schema, shard.name)
|
44
48
|
end
|
45
49
|
|
@@ -55,7 +59,7 @@ module Switchman
|
|
55
59
|
end
|
56
60
|
|
57
61
|
def indexes(table_name)
|
58
|
-
schema = shard.name if
|
62
|
+
schema = shard.name if use_qualified_names?
|
59
63
|
|
60
64
|
result = query(<<-SQL, 'SCHEMA')
|
61
65
|
SELECT distinct i.relname, d.indisunique, d.indkey, pg_get_indexdef(d.indexrelid), t.oid
|
@@ -107,7 +111,7 @@ module Switchman
|
|
107
111
|
end
|
108
112
|
|
109
113
|
def index_name_exists?(table_name, index_name, default)
|
110
|
-
schema = shard.name if
|
114
|
+
schema = shard.name if use_qualified_names?
|
111
115
|
|
112
116
|
exec_query(<<-SQL, 'SCHEMA').rows.first[0].to_i > 0
|
113
117
|
SELECT COUNT(*)
|
@@ -121,11 +125,18 @@ module Switchman
|
|
121
125
|
SQL
|
122
126
|
end
|
123
127
|
|
128
|
+
def quote_local_table_name(name)
|
129
|
+
# postgres quotes tables and columns the same; just pass through
|
130
|
+
# (differs from quote_table_name below by no logic to explicitly
|
131
|
+
# qualify the table)
|
132
|
+
quote_column_name(name)
|
133
|
+
end
|
134
|
+
|
124
135
|
def quote_table_name name
|
125
136
|
if ::Rails.version < '4.2'
|
126
137
|
schema, name_part = extract_pg_identifier_from_name(name.to_s)
|
127
138
|
|
128
|
-
if !name_part &&
|
139
|
+
if !name_part && use_qualified_names? && shard.name
|
129
140
|
schema, name_part = shard.name, schema
|
130
141
|
end
|
131
142
|
|
@@ -137,7 +148,7 @@ module Switchman
|
|
137
148
|
end
|
138
149
|
else
|
139
150
|
name = Utils.extract_schema_qualified_name(name.to_s)
|
140
|
-
if !name.schema &&
|
151
|
+
if !name.schema && use_qualified_names?
|
141
152
|
name.instance_variable_set(:@schema, shard.name)
|
142
153
|
end
|
143
154
|
name.quoted
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Switchman
|
2
|
+
module ActiveRecord
|
3
|
+
module Reflection
|
4
|
+
module AssociationReflection
|
5
|
+
# removes memoization - ActiveRecord::ModelSchema does that anyway;
|
6
|
+
# and in fact this is the exact change AR makes in 4.2+
|
7
|
+
if ::Rails.version < '4.2'
|
8
|
+
def quoted_table_name
|
9
|
+
klass.quoted_table_name
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/lib/switchman/arel.rb
CHANGED
@@ -1,6 +1,36 @@
|
|
1
1
|
module Switchman
|
2
2
|
module Arel
|
3
3
|
module Visitors
|
4
|
+
module ToSql
|
5
|
+
def visit_Arel_Nodes_TableAlias *args
|
6
|
+
if ::Rails.version < '4.2'
|
7
|
+
o = args.shift
|
8
|
+
"#{visit o.relation, *args} #{quote_local_table_name o.name}"
|
9
|
+
else
|
10
|
+
o, collector = args
|
11
|
+
collector = visit o.relation, collector
|
12
|
+
collector << " "
|
13
|
+
collector << quote_local_table_name(o.name)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def visit_Arel_Attributes_Attribute *args
|
18
|
+
o = args.first
|
19
|
+
self.last_column = column_for(o) if ::Rails.version < '4.0'
|
20
|
+
join_name = o.relation.table_alias || o.relation.name
|
21
|
+
result = "#{quote_local_table_name join_name}.#{quote_column_name o.name}"
|
22
|
+
unless ::Rails.version < '4.2'
|
23
|
+
result = args.last << result
|
24
|
+
end
|
25
|
+
result
|
26
|
+
end
|
27
|
+
|
28
|
+
def quote_local_table_name name
|
29
|
+
return name if ::Arel::Nodes::SqlLiteral === name
|
30
|
+
@connection.quote_local_table_name(name)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
4
34
|
module PostgreSQL
|
5
35
|
# the only difference is to remove caching (which only applies to Arel < 6.0/AR < 4.2)
|
6
36
|
def quote_table_name name
|
data/lib/switchman/engine.rb
CHANGED
@@ -80,6 +80,7 @@ module Switchman
|
|
80
80
|
require "switchman/active_record/persistence"
|
81
81
|
require "switchman/active_record/query_cache"
|
82
82
|
require "switchman/active_record/query_methods"
|
83
|
+
require "switchman/active_record/reflection"
|
83
84
|
require "switchman/active_record/relation"
|
84
85
|
require "switchman/active_record/spawn_methods"
|
85
86
|
require "switchman/arel"
|
@@ -105,12 +106,14 @@ module Switchman
|
|
105
106
|
::ActiveRecord::ConnectionAdapters::QueryCache.send(:remove_method, :select_all)
|
106
107
|
|
107
108
|
::ActiveRecord::LogSubscriber.send(:include, ActiveRecord::LogSubscriber)
|
109
|
+
::ActiveRecord::Reflection::AssociationReflection.prepend(ActiveRecord::Reflection::AssociationReflection)
|
108
110
|
::ActiveRecord::Relation.send(:include, ActiveRecord::Calculations)
|
109
111
|
::ActiveRecord::Relation.send(:include, ActiveRecord::FinderMethods)
|
110
112
|
::ActiveRecord::Relation.send(:include, ActiveRecord::QueryMethods)
|
111
113
|
::ActiveRecord::Relation.send(:include, ActiveRecord::Relation)
|
112
114
|
::ActiveRecord::Relation.send(:include, ActiveRecord::SpawnMethods)
|
113
115
|
|
116
|
+
::Arel::Visitors::ToSql.prepend(Arel::Visitors::ToSql)
|
114
117
|
::Arel::Visitors::PostgreSQL.send(:include, Arel::Visitors::PostgreSQL) if ::Rails.version < '4.2'
|
115
118
|
end
|
116
119
|
end
|
data/lib/switchman/version.rb
CHANGED
data/spec/dummy/log/test.log
CHANGED
@@ -356775,3 +356775,41 @@ Migrating to AddDummyForeignKey (20150618035859)
|
|
356775
356775
|
[1m[36m (0.3ms)[0m [1mBEGIN[0m
|
356776
356776
|
[1m[35mSQL (0.3ms)[0m DELETE FROM "switchman_shards" WHERE "switchman_shards"."id" = $1 [["id", 1031]]
|
356777
356777
|
[1m[36m (0.1ms)[0m [1mCOMMIT[0m
|
356778
|
+
Connecting to database specified by database.yml
|
356779
|
+
[1m[36m (0.9ms)[0m [1mSELECT * FROM unnest(current_schemas(false))[0m
|
356780
|
+
[1m[35mSwitchman::Shard Load (11.1ms)[0m SELECT "switchman_shards".* FROM "switchman_shards" WHERE "switchman_shards"."default" = 't' LIMIT 1
|
356781
|
+
Connecting to database specified by database.yml
|
356782
|
+
[1m[36m (0.4ms)[0m [1mSELECT * FROM unnest(current_schemas(false))[0m
|
356783
|
+
[1m[35mSwitchman::Shard Load (0.4ms)[0m SELECT "switchman_shards".* FROM "switchman_shards" WHERE "switchman_shards"."default" = 't' LIMIT 1
|
356784
|
+
[1m[36m (1.4ms)[0m [1mSELECT * FROM unnest(current_schemas(false))[0m
|
356785
|
+
[1m[35mSwitchman::Shard Load (0.9ms)[0m SELECT "switchman_shards".* FROM "switchman_shards" WHERE "switchman_shards"."default" = 't' ORDER BY "switchman_shards"."id" ASC LIMIT 1
|
356786
|
+
[1m[36m (0.1ms)[0m [1mBEGIN[0m
|
356787
|
+
[1m[35m (0.1ms)[0m ROLLBACK
|
356788
|
+
[1m[36m (0.1ms)[0m [1mBEGIN[0m
|
356789
|
+
[1m[35m (0.1ms)[0m ROLLBACK
|
356790
|
+
[1m[36m (0.1ms)[0m [1mBEGIN[0m
|
356791
|
+
[1m[35m (0.2ms)[0m ROLLBACK
|
356792
|
+
[1m[36m (1.1ms)[0m [1mSELECT * FROM unnest(current_schemas(false))[0m
|
356793
|
+
[1m[35mSwitchman::Shard Load (0.7ms)[0m SELECT "switchman_shards".* FROM "switchman_shards" WHERE "switchman_shards"."default" = 't' ORDER BY "switchman_shards"."id" ASC LIMIT 1
|
356794
|
+
[1m[36m (0.1ms)[0m [1mBEGIN[0m
|
356795
|
+
[1m[35m (0.2ms)[0m ROLLBACK
|
356796
|
+
[1m[36m (0.1ms)[0m [1mBEGIN[0m
|
356797
|
+
[1m[35m (0.1ms)[0m ROLLBACK
|
356798
|
+
[1m[36m (0.1ms)[0m [1mBEGIN[0m
|
356799
|
+
[1m[35m (0.1ms)[0m ROLLBACK
|
356800
|
+
[1m[36m (1.0ms)[0m [1mSELECT * FROM unnest(current_schemas(false))[0m
|
356801
|
+
[1m[35mSwitchman::Shard Load (1.1ms)[0m SELECT "switchman_shards".* FROM "switchman_shards" WHERE "switchman_shards"."default" = 't' ORDER BY "switchman_shards"."id" ASC LIMIT 1
|
356802
|
+
[1m[36m (0.1ms)[0m [1mBEGIN[0m
|
356803
|
+
[1m[35m (0.1ms)[0m ROLLBACK
|
356804
|
+
[1m[36m (0.1ms)[0m [1mBEGIN[0m
|
356805
|
+
[1m[35m (0.1ms)[0m ROLLBACK
|
356806
|
+
[1m[36m (0.1ms)[0m [1mBEGIN[0m
|
356807
|
+
[1m[35m (0.2ms)[0m ROLLBACK
|
356808
|
+
[1m[36m (1.1ms)[0m [1mSELECT * FROM unnest(current_schemas(false))[0m
|
356809
|
+
[1m[35mSwitchman::Shard Load (0.6ms)[0m SELECT "switchman_shards".* FROM "switchman_shards" WHERE "switchman_shards"."default" = 't' ORDER BY "switchman_shards"."id" ASC LIMIT 1
|
356810
|
+
[1m[36m (0.1ms)[0m [1mBEGIN[0m
|
356811
|
+
[1m[35m (0.1ms)[0m ROLLBACK
|
356812
|
+
[1m[36m (0.2ms)[0m [1mBEGIN[0m
|
356813
|
+
[1m[35m (0.1ms)[0m ROLLBACK
|
356814
|
+
[1m[36m (0.1ms)[0m [1mBEGIN[0m
|
356815
|
+
[1m[35m (0.1ms)[0m ROLLBACK
|
@@ -3,21 +3,18 @@ require "spec_helper"
|
|
3
3
|
module Switchman
|
4
4
|
module ActiveRecord
|
5
5
|
describe PostgreSQLAdapter do
|
6
|
+
before do
|
7
|
+
skip "requires PostgreSQL" unless ::ActiveRecord::Base.connection.adapter_name == 'PostgreSQL'
|
8
|
+
end
|
9
|
+
|
6
10
|
describe '#quote_table_name' do
|
7
11
|
before do
|
8
|
-
skip "requires PostgreSQL" unless ::ActiveRecord::Base.connection.adapter_name == 'PostgreSQL'
|
9
|
-
@config = ::ActiveRecord::Base.connection.instance_variable_get(:@config)
|
10
|
-
@prior_use_qualified_names = @config[:use_qualified_names]
|
11
|
-
@config[:use_qualified_names] = true
|
12
12
|
shard = mock()
|
13
13
|
shard.stubs(:name).returns('bob')
|
14
|
+
::ActiveRecord::Base.connection.stubs(:use_qualified_names?).returns(true)
|
14
15
|
::ActiveRecord::Base.connection.stubs(:shard).returns(shard)
|
15
16
|
end
|
16
17
|
|
17
|
-
after do
|
18
|
-
@config[:use_qualified_names] = @prior_use_qualified_names
|
19
|
-
end
|
20
|
-
|
21
18
|
it 'should add schema if not included' do
|
22
19
|
expect(::ActiveRecord::Base.connection.quote_table_name('table')).to eq '"bob"."table"'
|
23
20
|
end
|
@@ -26,6 +23,17 @@ module Switchman
|
|
26
23
|
expect(::ActiveRecord::Base.connection.quote_table_name('schema.table')).to eq '"schema"."table"'
|
27
24
|
end
|
28
25
|
end
|
26
|
+
|
27
|
+
context "table aliases" do
|
28
|
+
it "qualifies tables, but not aliases or columns" do
|
29
|
+
shard = mock()
|
30
|
+
shard.stubs(:name).returns('bob')
|
31
|
+
::ActiveRecord::Base.connection.stubs(:use_qualified_names?).returns(true)
|
32
|
+
::ActiveRecord::Base.connection.stubs(:shard).returns(shard)
|
33
|
+
|
34
|
+
expect(User.joins(:parent).where(id: 1).to_sql).to be_include %{* FROM "bob"."users" INNER JOIN "bob"."users" "parents_users" ON "parents_users"."id" = "users"."parent_id" WHERE "users"."id" = 1}
|
35
|
+
end
|
36
|
+
end
|
29
37
|
end
|
30
38
|
end
|
31
39
|
end
|
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.3.
|
4
|
+
version: 1.3.8
|
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: 2015-10-
|
13
|
+
date: 2015-10-26 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: railties
|
@@ -163,6 +163,7 @@ files:
|
|
163
163
|
- lib/switchman/active_record/postgresql_adapter.rb
|
164
164
|
- lib/switchman/active_record/query_cache.rb
|
165
165
|
- lib/switchman/active_record/query_methods.rb
|
166
|
+
- lib/switchman/active_record/reflection.rb
|
166
167
|
- lib/switchman/active_record/relation.rb
|
167
168
|
- lib/switchman/active_record/spawn_methods.rb
|
168
169
|
- lib/switchman/active_support/cache.rb
|