activerecord 5.1.0.beta1 → 5.1.0.rc1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of activerecord might be problematic. Click here for more details.

Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +93 -6
  3. data/lib/active_record/associations.rb +4 -0
  4. data/lib/active_record/associations/association_scope.rb +8 -8
  5. data/lib/active_record/associations/belongs_to_association.rb +4 -0
  6. data/lib/active_record/associations/builder/belongs_to.rb +8 -1
  7. data/lib/active_record/associations/collection_proxy.rb +5 -4
  8. data/lib/active_record/associations/join_dependency.rb +1 -1
  9. data/lib/active_record/associations/join_dependency/join_association.rb +4 -23
  10. data/lib/active_record/attribute_methods/dirty.rb +3 -3
  11. data/lib/active_record/connection_adapters/abstract/quoting.rb +20 -3
  12. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +3 -7
  13. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +30 -16
  14. data/lib/active_record/connection_adapters/abstract_adapter.rb +8 -5
  15. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +16 -80
  16. data/lib/active_record/connection_adapters/mysql/schema_statements.rb +33 -0
  17. data/lib/active_record/connection_adapters/mysql/type_metadata.rb +2 -0
  18. data/lib/active_record/connection_adapters/postgresql/oid/array.rb +1 -1
  19. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +56 -96
  20. data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +2 -0
  21. data/lib/active_record/connection_adapters/postgresql_adapter.rb +4 -12
  22. data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +32 -0
  23. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +4 -51
  24. data/lib/active_record/core.rb +0 -1
  25. data/lib/active_record/gem_version.rb +1 -1
  26. data/lib/active_record/locking/optimistic.rb +2 -6
  27. data/lib/active_record/migration.rb +32 -17
  28. data/lib/active_record/null_relation.rb +1 -1
  29. data/lib/active_record/querying.rb +1 -1
  30. data/lib/active_record/railties/databases.rake +3 -19
  31. data/lib/active_record/reflection.rb +67 -16
  32. data/lib/active_record/relation.rb +0 -4
  33. data/lib/active_record/relation/calculations.rb +7 -10
  34. data/lib/active_record/relation/delegation.rb +2 -2
  35. data/lib/active_record/relation/finder_methods.rb +102 -100
  36. data/lib/active_record/relation/query_methods.rb +6 -1
  37. data/lib/active_record/result.rb +12 -1
  38. data/lib/active_record/sanitization.rb +1 -2
  39. data/lib/active_record/schema_dumper.rb +1 -1
  40. data/lib/active_record/schema_migration.rb +5 -1
  41. data/lib/active_record/tasks/postgresql_database_tasks.rb +20 -0
  42. data/lib/active_record/transactions.rb +1 -1
  43. data/lib/active_record/type/decimal_without_scale.rb +4 -0
  44. data/lib/active_record/type/serialized.rb +2 -0
  45. data/lib/rails/generators/active_record/migration.rb +1 -1
  46. metadata +8 -6
@@ -154,8 +154,8 @@ module ActiveRecord
154
154
  Arel::Visitors::ToSql.new(self)
155
155
  end
156
156
 
157
- def valid_type?(type)
158
- false
157
+ def valid_type?(type) # :nodoc:
158
+ !native_database_types[type].nil?
159
159
  end
160
160
 
161
161
  def schema_creation
@@ -232,10 +232,10 @@ module ActiveRecord
232
232
  self.class::ADAPTER_NAME
233
233
  end
234
234
 
235
- # Does this adapter support migrations?
236
- def supports_migrations?
237
- false
235
+ def supports_migrations? # :nodoc:
236
+ true
238
237
  end
238
+ deprecate :supports_migrations?
239
239
 
240
240
  def supports_primary_key? # :nodoc:
241
241
  true
@@ -439,6 +439,9 @@ module ActiveRecord
439
439
  # This is done under the hood by calling #active?. If the connection
440
440
  # is no longer active, then this method will reconnect to the database.
441
441
  def verify!(*ignored)
442
+ if ignored.size > 0
443
+ ActiveSupport::Deprecation.warn("Passing arguments to #verify method of the connection has no effect and has been deprecated. Please remove all arguments from the #verify method call.")
444
+ end
442
445
  reconnect! unless active?
443
446
  end
444
447
 
@@ -6,6 +6,7 @@ require "active_record/connection_adapters/mysql/quoting"
6
6
  require "active_record/connection_adapters/mysql/schema_creation"
7
7
  require "active_record/connection_adapters/mysql/schema_definitions"
8
8
  require "active_record/connection_adapters/mysql/schema_dumper"
9
+ require "active_record/connection_adapters/mysql/schema_statements"
9
10
  require "active_record/connection_adapters/mysql/type_metadata"
10
11
 
11
12
  require "active_support/core_ext/string/strip"
@@ -15,6 +16,7 @@ module ActiveRecord
15
16
  class AbstractMysqlAdapter < AbstractAdapter
16
17
  include MySQL::Quoting
17
18
  include MySQL::ColumnDumper
19
+ include MySQL::SchemaStatements
18
20
 
19
21
  def update_table_definition(table_name, base) # :nodoc:
20
22
  MySQL::Table.new(table_name, base)
@@ -89,11 +91,6 @@ module ActiveRecord
89
91
  /mariadb/i.match?(full_version)
90
92
  end
91
93
 
92
- # Returns true, since this connection adapter supports migrations.
93
- def supports_migrations?
94
- true
95
- end
96
-
97
94
  def supports_bulk_alter? #:nodoc:
98
95
  true
99
96
  end
@@ -315,57 +312,6 @@ module ActiveRecord
315
312
  show_variable "collation_database"
316
313
  end
317
314
 
318
- def tables # :nodoc:
319
- sql = "SELECT table_name FROM information_schema.tables WHERE table_type = 'BASE TABLE'"
320
- sql << " AND table_schema = #{quote(@config[:database])}"
321
-
322
- select_values(sql, "SCHEMA")
323
- end
324
-
325
- def views # :nodoc:
326
- select_values("SHOW FULL TABLES WHERE table_type = 'VIEW'", "SCHEMA")
327
- end
328
-
329
- def data_sources # :nodoc:
330
- sql = "SELECT table_name FROM information_schema.tables "
331
- sql << "WHERE table_schema = #{quote(@config[:database])}"
332
-
333
- select_values(sql, "SCHEMA")
334
- end
335
-
336
- def table_exists?(table_name) # :nodoc:
337
- return false unless table_name.present?
338
-
339
- schema, name = extract_schema_qualified_name(table_name)
340
-
341
- sql = "SELECT table_name FROM information_schema.tables WHERE table_type = 'BASE TABLE'"
342
- sql << " AND table_schema = #{quote(schema)} AND table_name = #{quote(name)}"
343
-
344
- select_values(sql, "SCHEMA").any?
345
- end
346
-
347
- def data_source_exists?(table_name) # :nodoc:
348
- return false unless table_name.present?
349
-
350
- schema, name = extract_schema_qualified_name(table_name)
351
-
352
- sql = "SELECT table_name FROM information_schema.tables "
353
- sql << "WHERE table_schema = #{quote(schema)} AND table_name = #{quote(name)}"
354
-
355
- select_values(sql, "SCHEMA").any?
356
- end
357
-
358
- def view_exists?(view_name) # :nodoc:
359
- return false unless view_name.present?
360
-
361
- schema, name = extract_schema_qualified_name(view_name)
362
-
363
- sql = "SELECT table_name FROM information_schema.tables WHERE table_type = 'VIEW'"
364
- sql << " AND table_schema = #{quote(schema)} AND table_name = #{quote(name)}"
365
-
366
- select_values(sql, "SCHEMA").any?
367
- end
368
-
369
315
  def truncate(table_name, name = nil)
370
316
  execute "TRUNCATE TABLE #{quote_table_name(table_name)}", name
371
317
  end
@@ -411,13 +357,13 @@ module ActiveRecord
411
357
  end
412
358
 
413
359
  def table_comment(table_name) # :nodoc:
414
- schema, name = extract_schema_qualified_name(table_name)
360
+ scope = quoted_scope(table_name)
415
361
 
416
362
  select_value(<<-SQL.strip_heredoc, "SCHEMA")
417
363
  SELECT table_comment
418
364
  FROM information_schema.tables
419
- WHERE table_schema = #{quote(schema)}
420
- AND table_name = #{quote(name)}
365
+ WHERE table_schema = #{scope[:schema]}
366
+ AND table_name = #{scope[:name]}
421
367
  SQL
422
368
  end
423
369
 
@@ -517,7 +463,7 @@ module ActiveRecord
517
463
  def foreign_keys(table_name)
518
464
  raise ArgumentError unless table_name.present?
519
465
 
520
- schema, name = extract_schema_qualified_name(table_name)
466
+ scope = quoted_scope(table_name)
521
467
 
522
468
  fk_info = select_all(<<-SQL.strip_heredoc, "SCHEMA")
523
469
  SELECT fk.referenced_table_name AS 'to_table',
@@ -530,9 +476,9 @@ module ActiveRecord
530
476
  JOIN information_schema.referential_constraints rc
531
477
  USING (constraint_schema, constraint_name)
532
478
  WHERE fk.referenced_column_name IS NOT NULL
533
- AND fk.table_schema = #{quote(schema)}
534
- AND fk.table_name = #{quote(name)}
535
- AND rc.table_name = #{quote(name)}
479
+ AND fk.table_schema = #{scope[:schema]}
480
+ AND fk.table_name = #{scope[:name]}
481
+ AND rc.table_name = #{scope[:name]}
536
482
  SQL
537
483
 
538
484
  fk_info.map do |row|
@@ -604,14 +550,14 @@ module ActiveRecord
604
550
  def primary_keys(table_name) # :nodoc:
605
551
  raise ArgumentError unless table_name.present?
606
552
 
607
- schema, name = extract_schema_qualified_name(table_name)
553
+ scope = quoted_scope(table_name)
608
554
 
609
555
  select_values(<<-SQL.strip_heredoc, "SCHEMA")
610
556
  SELECT column_name
611
557
  FROM information_schema.key_column_usage
612
558
  WHERE constraint_name = 'PRIMARY'
613
- AND table_schema = #{quote(schema)}
614
- AND table_name = #{quote(name)}
559
+ AND table_schema = #{scope[:schema]}
560
+ AND table_name = #{scope[:name]}
615
561
  ORDER BY ordinal_position
616
562
  SQL
617
563
  end
@@ -648,10 +594,6 @@ module ActiveRecord
648
594
  self.class.type_cast_config_to_boolean(@config.fetch(:strict, true))
649
595
  end
650
596
 
651
- def valid_type?(type)
652
- !native_database_types[type].nil?
653
- end
654
-
655
597
  def default_index_type?(index) # :nodoc:
656
598
  index.using == :btree || super
657
599
  end
@@ -786,11 +728,11 @@ module ActiveRecord
786
728
  def change_column_sql(table_name, column_name, type, options = {})
787
729
  column = column_for(table_name, column_name)
788
730
 
789
- unless options_include_default?(options)
731
+ unless options.key?(:default)
790
732
  options[:default] = column.default
791
733
  end
792
734
 
793
- unless options.has_key?(:null)
735
+ unless options.key?(:null)
794
736
  options[:null] = column.null
795
737
  end
796
738
 
@@ -870,9 +812,9 @@ module ActiveRecord
870
812
  variables["sql_auto_is_null"] = 0
871
813
 
872
814
  # Increase timeout so the server doesn't disconnect us.
873
- wait_timeout = @config[:wait_timeout]
815
+ wait_timeout = self.class.type_cast_config_to_integer(@config[:wait_timeout])
874
816
  wait_timeout = 2147483 unless wait_timeout.is_a?(Integer)
875
- variables["wait_timeout"] = self.class.type_cast_config_to_integer(wait_timeout)
817
+ variables["wait_timeout"] = wait_timeout
876
818
 
877
819
  defaults = [":default", :default].to_set
878
820
 
@@ -949,12 +891,6 @@ module ActiveRecord
949
891
  )
950
892
  end
951
893
 
952
- def extract_schema_qualified_name(string) # :nodoc:
953
- schema, name = string.to_s.scan(/[^`.\s]+|`[^`]*`/)
954
- schema, name = @config[:database], schema unless name
955
- [schema, name]
956
- end
957
-
958
894
  def integer_to_sql(limit) # :nodoc:
959
895
  case limit
960
896
  when 1; "tinyint"
@@ -0,0 +1,33 @@
1
+ module ActiveRecord
2
+ module ConnectionAdapters
3
+ module MySQL
4
+ module SchemaStatements # :nodoc:
5
+ private
6
+ def data_source_sql(name = nil, type: nil)
7
+ scope = quoted_scope(name, type: type)
8
+
9
+ sql = "SELECT table_name FROM information_schema.tables"
10
+ sql << " WHERE table_schema = #{scope[:schema]}"
11
+ sql << " AND table_name = #{scope[:name]}" if scope[:name]
12
+ sql << " AND table_type = #{scope[:type]}" if scope[:type]
13
+ sql
14
+ end
15
+
16
+ def quoted_scope(name = nil, type: nil)
17
+ schema, name = extract_schema_qualified_name(name)
18
+ scope = {}
19
+ scope[:schema] = schema ? quote(schema) : "database()"
20
+ scope[:name] = quote(name) if name
21
+ scope[:type] = quote(type) if type
22
+ scope
23
+ end
24
+
25
+ def extract_schema_qualified_name(string)
26
+ schema, name = string.to_s.scan(/[^`.\s]+|`[^`]*`/)
27
+ schema, name = nil, schema unless name
28
+ [schema, name]
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -2,6 +2,8 @@ module ActiveRecord
2
2
  module ConnectionAdapters
3
3
  module MySQL
4
4
  class TypeMetadata < DelegateClass(SqlTypeMetadata) # :nodoc:
5
+ undef to_yaml if method_defined?(:to_yaml)
6
+
5
7
  attr_reader :extra
6
8
 
7
9
  def initialize(type_metadata, extra: "")
@@ -23,7 +23,7 @@ module ActiveRecord
23
23
  when ::String
24
24
  type_cast_array(@pg_decoder.decode(value), :deserialize)
25
25
  when Data
26
- deserialize(value.values)
26
+ type_cast_array(value.values, :deserialize)
27
27
  else
28
28
  super
29
29
  end
@@ -54,81 +54,13 @@ module ActiveRecord
54
54
  execute "DROP DATABASE IF EXISTS #{quote_table_name(name)}"
55
55
  end
56
56
 
57
- # Returns the list of all tables in the schema search path.
58
- def tables
59
- select_values("SELECT tablename FROM pg_tables WHERE schemaname = ANY(current_schemas(false))", "SCHEMA")
60
- end
61
-
62
- def data_sources # :nodoc
63
- select_values(<<-SQL, "SCHEMA")
64
- SELECT c.relname
65
- FROM pg_class c
66
- LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
67
- WHERE c.relkind IN ('r','v','m') -- (r)elation/table, (v)iew, (m)aterialized view
68
- AND n.nspname = ANY (current_schemas(false))
69
- SQL
70
- end
71
-
72
- def views # :nodoc:
73
- select_values(<<-SQL, "SCHEMA")
74
- SELECT c.relname
75
- FROM pg_class c
76
- LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
77
- WHERE c.relkind IN ('v','m') -- (v)iew, (m)aterialized view
78
- AND n.nspname = ANY (current_schemas(false))
79
- SQL
80
- end
81
-
82
- # Returns true if table exists.
83
- # If the schema is not specified as part of +name+ then it will only find tables within
84
- # the current schema search path (regardless of permissions to access tables in other schemas)
85
- def table_exists?(name)
86
- name = Utils.extract_schema_qualified_name(name.to_s)
87
- return false unless name.identifier
88
-
89
- select_values(<<-SQL, "SCHEMA").any?
90
- SELECT tablename
91
- FROM pg_tables
92
- WHERE tablename = #{quote(name.identifier)}
93
- AND schemaname = #{name.schema ? quote(name.schema) : "ANY (current_schemas(false))"}
94
- SQL
95
- end
96
-
97
- def data_source_exists?(name) # :nodoc:
98
- name = Utils.extract_schema_qualified_name(name.to_s)
99
- return false unless name.identifier
100
-
101
- select_values(<<-SQL, "SCHEMA").any?
102
- SELECT c.relname
103
- FROM pg_class c
104
- LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
105
- WHERE c.relkind IN ('r','v','m') -- (r)elation/table, (v)iew, (m)aterialized view
106
- AND c.relname = #{quote(name.identifier)}
107
- AND n.nspname = #{name.schema ? quote(name.schema) : "ANY (current_schemas(false))"}
108
- SQL
109
- end
110
-
111
- def view_exists?(view_name) # :nodoc:
112
- name = Utils.extract_schema_qualified_name(view_name.to_s)
113
- return false unless name.identifier
114
-
115
- select_values(<<-SQL, "SCHEMA").any?
116
- SELECT c.relname
117
- FROM pg_class c
118
- LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
119
- WHERE c.relkind IN ('v','m') -- (v)iew, (m)aterialized view
120
- AND c.relname = #{quote(name.identifier)}
121
- AND n.nspname = #{name.schema ? quote(name.schema) : "ANY (current_schemas(false))"}
122
- SQL
123
- end
124
-
125
57
  def drop_table(table_name, options = {}) # :nodoc:
126
58
  execute "DROP TABLE#{' IF EXISTS' if options[:if_exists]} #{quote_table_name(table_name)}#{' CASCADE' if options[:force] == :cascade}"
127
59
  end
128
60
 
129
61
  # Returns true if schema exists.
130
62
  def schema_exists?(name)
131
- select_value("SELECT COUNT(*) FROM pg_namespace WHERE nspname = '#{name}'", "SCHEMA").to_i > 0
63
+ select_value("SELECT COUNT(*) FROM pg_namespace WHERE nspname = #{quote(name)}", "SCHEMA").to_i > 0
132
64
  end
133
65
 
134
66
  # Verifies existence of an index with a given name.
@@ -138,8 +70,8 @@ module ActiveRecord
138
70
  Passing default to #index_name_exists? is deprecated without replacement.
139
71
  MSG
140
72
  end
141
- table = Utils.extract_schema_qualified_name(table_name.to_s)
142
- index = Utils.extract_schema_qualified_name(index_name.to_s)
73
+ table = quoted_scope(table_name)
74
+ index = quoted_scope(index_name)
143
75
 
144
76
  select_value(<<-SQL, "SCHEMA").to_i > 0
145
77
  SELECT COUNT(*)
@@ -148,9 +80,9 @@ module ActiveRecord
148
80
  INNER JOIN pg_class i ON d.indexrelid = i.oid
149
81
  LEFT JOIN pg_namespace n ON n.oid = i.relnamespace
150
82
  WHERE i.relkind = 'i'
151
- AND i.relname = '#{index.identifier}'
152
- AND t.relname = '#{table.identifier}'
153
- AND n.nspname = #{index.schema ? "'#{index.schema}'" : 'ANY (current_schemas(false))'}
83
+ AND i.relname = #{index[:name]}
84
+ AND t.relname = #{table[:name]}
85
+ AND n.nspname = #{index[:schema]}
154
86
  SQL
155
87
  end
156
88
 
@@ -162,7 +94,7 @@ module ActiveRecord
162
94
  MSG
163
95
  end
164
96
 
165
- table = Utils.extract_schema_qualified_name(table_name.to_s)
97
+ scope = quoted_scope(table_name)
166
98
 
167
99
  result = query(<<-SQL, "SCHEMA")
168
100
  SELECT distinct i.relname, d.indisunique, d.indkey, pg_get_indexdef(d.indexrelid), t.oid,
@@ -176,8 +108,8 @@ module ActiveRecord
176
108
  LEFT JOIN pg_namespace n ON n.oid = i.relnamespace
177
109
  WHERE i.relkind = 'i'
178
110
  AND d.indisprimary = 'f'
179
- AND t.relname = '#{table.identifier}'
180
- AND n.nspname = #{table.schema ? "'#{table.schema}'" : 'ANY (current_schemas(false))'}
111
+ AND t.relname = #{scope[:name]}
112
+ AND n.nspname = #{scope[:schema]}
181
113
  ORDER BY i.relname
182
114
  SQL
183
115
 
@@ -239,22 +171,22 @@ module ActiveRecord
239
171
 
240
172
  # Returns a comment stored in database for given table
241
173
  def table_comment(table_name) # :nodoc:
242
- name = Utils.extract_schema_qualified_name(table_name.to_s)
243
- if name.identifier
174
+ scope = quoted_scope(table_name, type: "BASE TABLE")
175
+ if scope[:name]
244
176
  select_value(<<-SQL.strip_heredoc, "SCHEMA")
245
177
  SELECT pg_catalog.obj_description(c.oid, 'pg_class')
246
178
  FROM pg_catalog.pg_class c
247
179
  LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
248
- WHERE c.relname = #{quote(name.identifier)}
249
- AND c.relkind IN ('r') -- (r)elation/table
250
- AND n.nspname = #{name.schema ? quote(name.schema) : 'ANY (current_schemas(false))'}
180
+ WHERE c.relname = #{scope[:name]}
181
+ AND c.relkind IN (#{scope[:type]})
182
+ AND n.nspname = #{scope[:schema]}
251
183
  SQL
252
184
  end
253
185
  end
254
186
 
255
187
  # Returns the current database name.
256
188
  def current_database
257
- select_value("select current_database()", "SCHEMA")
189
+ select_value("SELECT current_database()", "SCHEMA")
258
190
  end
259
191
 
260
192
  # Returns the current schema name.
@@ -430,17 +362,15 @@ module ActiveRecord
430
362
  end
431
363
 
432
364
  def primary_keys(table_name) # :nodoc:
433
- name = Utils.extract_schema_qualified_name(table_name.to_s)
365
+ scope = quoted_scope(table_name)
434
366
  select_values(<<-SQL.strip_heredoc, "SCHEMA")
435
367
  SELECT column_name
436
368
  FROM information_schema.key_column_usage kcu
437
369
  JOIN information_schema.table_constraints tc
438
- ON kcu.table_name = tc.table_name
439
- AND kcu.table_schema = tc.table_schema
440
- AND kcu.constraint_name = tc.constraint_name
370
+ USING (table_schema, table_name, constraint_name)
441
371
  WHERE constraint_type = 'PRIMARY KEY'
442
- AND kcu.table_name = #{quote(name.identifier)}
443
- AND kcu.table_schema = #{name.schema ? quote(name.schema) : "ANY (current_schemas(false))"}
372
+ AND kcu.table_name = #{scope[:name]}
373
+ AND kcu.table_schema = #{scope[:schema]}
444
374
  ORDER BY kcu.ordinal_position
445
375
  SQL
446
376
  end
@@ -489,7 +419,7 @@ module ActiveRecord
489
419
  end
490
420
  execute sql
491
421
 
492
- change_column_default(table_name, column_name, options[:default]) if options_include_default?(options)
422
+ change_column_default(table_name, column_name, options[:default]) if options.key?(:default)
493
423
  change_column_null(table_name, column_name, options[:null], options[:default]) if options.key?(:null)
494
424
  change_column_comment(table_name, column_name, options[:comment]) if options.key?(:comment)
495
425
  end
@@ -579,6 +509,7 @@ module ActiveRecord
579
509
  end
580
510
 
581
511
  def foreign_keys(table_name)
512
+ scope = quoted_scope(table_name)
582
513
  fk_info = select_all(<<-SQL.strip_heredoc, "SCHEMA")
583
514
  SELECT t2.oid::regclass::text AS to_table, a1.attname AS column, a2.attname AS primary_key, c.conname AS name, c.confupdtype AS on_update, c.confdeltype AS on_delete
584
515
  FROM pg_constraint c
@@ -588,8 +519,8 @@ module ActiveRecord
588
519
  JOIN pg_attribute a2 ON a2.attnum = c.confkey[1] AND a2.attrelid = t2.oid
589
520
  JOIN pg_namespace t3 ON c.connamespace = t3.oid
590
521
  WHERE c.contype = 'f'
591
- AND t1.relname = #{quote(table_name)}
592
- AND t3.nspname = ANY (current_schemas(false))
522
+ AND t1.relname = #{scope[:name]}
523
+ AND t3.nspname = #{scope[:schema]}
593
524
  ORDER BY c.conname
594
525
  SQL
595
526
 
@@ -615,10 +546,6 @@ module ActiveRecord
615
546
  end
616
547
  end
617
548
 
618
- def index_name_length
619
- 63
620
- end
621
-
622
549
  # Maps logical Rails types to PostgreSQL-specific data types.
623
550
  def type_to_sql(type, limit: nil, precision: nil, scale: nil, array: nil, **) # :nodoc:
624
551
  sql = \
@@ -677,6 +604,39 @@ module ActiveRecord
677
604
  )
678
605
  PostgreSQLTypeMetadata.new(simple_type, oid: oid, fmod: fmod)
679
606
  end
607
+
608
+ private
609
+ def data_source_sql(name = nil, type: nil)
610
+ scope = quoted_scope(name, type: type)
611
+ scope[:type] ||= "'r','v','m'" # (r)elation/table, (v)iew, (m)aterialized view
612
+
613
+ sql = "SELECT c.relname FROM pg_class c LEFT JOIN pg_namespace n ON n.oid = c.relnamespace"
614
+ sql << " WHERE n.nspname = #{scope[:schema]}"
615
+ sql << " AND c.relname = #{scope[:name]}" if scope[:name]
616
+ sql << " AND c.relkind IN (#{scope[:type]})"
617
+ sql
618
+ end
619
+
620
+ def quoted_scope(name = nil, type: nil)
621
+ schema, name = extract_schema_qualified_name(name)
622
+ type = \
623
+ case type
624
+ when "BASE TABLE"
625
+ "'r'"
626
+ when "VIEW"
627
+ "'v','m'"
628
+ end
629
+ scope = {}
630
+ scope[:schema] = schema ? quote(schema) : "ANY (current_schemas(false))"
631
+ scope[:name] = quote(name) if name
632
+ scope[:type] = type if type
633
+ scope
634
+ end
635
+
636
+ def extract_schema_qualified_name(string)
637
+ name = Utils.extract_schema_qualified_name(string.to_s)
638
+ [name.schema, name.identifier]
639
+ end
680
640
  end
681
641
  end
682
642
  end