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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 746fbf37c5142ff690f7641e15191b0d1ed6c1cc
4
- data.tar.gz: 1a1f1d4cced641aec4980dcbfb41616ea0212bc5
3
+ metadata.gz: a8f9aba599d3ef529a78c94fd5e94c2f204b7ccc
4
+ data.tar.gz: 5bf0f3c88787aec47ed93c41069cc7ad06054423
5
5
  SHA512:
6
- metadata.gz: def9f78de55963797ea0afdab709727d5a6bc88a4c11d9acc6de5bd076da4f5a53f6dfddff986b5a0bfaea1e7c358ac554fb5b82f5e54489b21d16333da60745
7
- data.tar.gz: 76e372029e0ae7586a37c8aa4808e02b9329fe0608be7be5fce93117ee8d63e092953bd5be6566d924b3cfd02d86f6fb2d0d25dd4e0851e36f24cbbc5a6ed349
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
- nil
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 @config[:use_qualified_names]
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 @config[:use_qualified_names]
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 && @config[:use_qualified_names]
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 @config[:use_qualified_names]
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 @config[:use_qualified_names]
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 && @config[:use_qualified_names] && shard.name
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 && @config[:use_qualified_names]
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
@@ -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
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Switchman
2
- VERSION = "1.3.7"
2
+ VERSION = "1.3.8"
3
3
  end
@@ -356775,3 +356775,41 @@ Migrating to AddDummyForeignKey (20150618035859)
356775
356775
   (0.3ms) BEGIN
356776
356776
  SQL (0.3ms) DELETE FROM "switchman_shards" WHERE "switchman_shards"."id" = $1 [["id", 1031]]
356777
356777
   (0.1ms) COMMIT
356778
+ Connecting to database specified by database.yml
356779
+  (0.9ms) SELECT * FROM unnest(current_schemas(false))
356780
+ Switchman::Shard Load (11.1ms) SELECT "switchman_shards".* FROM "switchman_shards" WHERE "switchman_shards"."default" = 't' LIMIT 1
356781
+ Connecting to database specified by database.yml
356782
+  (0.4ms) SELECT * FROM unnest(current_schemas(false))
356783
+ Switchman::Shard Load (0.4ms) SELECT "switchman_shards".* FROM "switchman_shards" WHERE "switchman_shards"."default" = 't' LIMIT 1
356784
+  (1.4ms) SELECT * FROM unnest(current_schemas(false))
356785
+ Switchman::Shard Load (0.9ms) SELECT "switchman_shards".* FROM "switchman_shards" WHERE "switchman_shards"."default" = 't' ORDER BY "switchman_shards"."id" ASC LIMIT 1
356786
+  (0.1ms) BEGIN
356787
+  (0.1ms) ROLLBACK
356788
+  (0.1ms) BEGIN
356789
+  (0.1ms) ROLLBACK
356790
+  (0.1ms) BEGIN
356791
+  (0.2ms) ROLLBACK
356792
+  (1.1ms) SELECT * FROM unnest(current_schemas(false))
356793
+ Switchman::Shard Load (0.7ms) SELECT "switchman_shards".* FROM "switchman_shards" WHERE "switchman_shards"."default" = 't' ORDER BY "switchman_shards"."id" ASC LIMIT 1
356794
+  (0.1ms) BEGIN
356795
+  (0.2ms) ROLLBACK
356796
+  (0.1ms) BEGIN
356797
+  (0.1ms) ROLLBACK
356798
+  (0.1ms) BEGIN
356799
+  (0.1ms) ROLLBACK
356800
+  (1.0ms) SELECT * FROM unnest(current_schemas(false))
356801
+ Switchman::Shard Load (1.1ms) SELECT "switchman_shards".* FROM "switchman_shards" WHERE "switchman_shards"."default" = 't' ORDER BY "switchman_shards"."id" ASC LIMIT 1
356802
+  (0.1ms) BEGIN
356803
+  (0.1ms) ROLLBACK
356804
+  (0.1ms) BEGIN
356805
+  (0.1ms) ROLLBACK
356806
+  (0.1ms) BEGIN
356807
+  (0.2ms) ROLLBACK
356808
+  (1.1ms) SELECT * FROM unnest(current_schemas(false))
356809
+ Switchman::Shard Load (0.6ms) SELECT "switchman_shards".* FROM "switchman_shards" WHERE "switchman_shards"."default" = 't' ORDER BY "switchman_shards"."id" ASC LIMIT 1
356810
+  (0.1ms) BEGIN
356811
+  (0.1ms) ROLLBACK
356812
+  (0.2ms) BEGIN
356813
+  (0.1ms) ROLLBACK
356814
+  (0.1ms) BEGIN
356815
+  (0.1ms) 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.7
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-23 00:00:00.000000000 Z
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