activerecord 5.1.7 → 5.2.0.beta1

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 (259) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +221 -900
  3. data/README.rdoc +3 -3
  4. data/examples/performance.rb +2 -0
  5. data/examples/simple.rb +2 -0
  6. data/lib/active_record.rb +10 -3
  7. data/lib/active_record/aggregations.rb +2 -0
  8. data/lib/active_record/association_relation.rb +2 -0
  9. data/lib/active_record/associations.rb +13 -42
  10. data/lib/active_record/associations/alias_tracker.rb +17 -17
  11. data/lib/active_record/associations/association.rb +11 -22
  12. data/lib/active_record/associations/association_scope.rb +32 -44
  13. data/lib/active_record/associations/belongs_to_association.rb +6 -4
  14. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +3 -1
  15. data/lib/active_record/associations/builder/association.rb +2 -5
  16. data/lib/active_record/associations/builder/belongs_to.rb +7 -12
  17. data/lib/active_record/associations/builder/collection_association.rb +1 -1
  18. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +3 -1
  19. data/lib/active_record/associations/builder/has_many.rb +2 -0
  20. data/lib/active_record/associations/builder/has_one.rb +2 -0
  21. data/lib/active_record/associations/builder/singular_association.rb +2 -0
  22. data/lib/active_record/associations/collection_association.rb +41 -33
  23. data/lib/active_record/associations/collection_proxy.rb +11 -14
  24. data/lib/active_record/associations/foreign_association.rb +2 -0
  25. data/lib/active_record/associations/has_many_association.rb +4 -2
  26. data/lib/active_record/associations/has_many_through_association.rb +4 -2
  27. data/lib/active_record/associations/has_one_association.rb +3 -1
  28. data/lib/active_record/associations/has_one_through_association.rb +3 -1
  29. data/lib/active_record/associations/join_dependency.rb +22 -40
  30. data/lib/active_record/associations/join_dependency/join_association.rb +17 -56
  31. data/lib/active_record/associations/join_dependency/join_base.rb +9 -8
  32. data/lib/active_record/associations/join_dependency/join_part.rb +2 -9
  33. data/lib/active_record/associations/preloader.rb +17 -37
  34. data/lib/active_record/associations/preloader/association.rb +42 -58
  35. data/lib/active_record/associations/preloader/through_association.rb +71 -79
  36. data/lib/active_record/associations/singular_association.rb +14 -10
  37. data/lib/active_record/associations/through_association.rb +3 -1
  38. data/lib/active_record/attribute_assignment.rb +2 -0
  39. data/lib/active_record/attribute_decorators.rb +3 -2
  40. data/lib/active_record/attribute_methods.rb +47 -7
  41. data/lib/active_record/attribute_methods/before_type_cast.rb +2 -0
  42. data/lib/active_record/attribute_methods/dirty.rb +25 -214
  43. data/lib/active_record/attribute_methods/primary_key.rb +7 -6
  44. data/lib/active_record/attribute_methods/query.rb +2 -0
  45. data/lib/active_record/attribute_methods/read.rb +8 -2
  46. data/lib/active_record/attribute_methods/serialization.rb +23 -0
  47. data/lib/active_record/attribute_methods/time_zone_conversion.rb +6 -8
  48. data/lib/active_record/attribute_methods/write.rb +21 -9
  49. data/lib/active_record/attributes.rb +7 -6
  50. data/lib/active_record/autosave_association.rb +5 -11
  51. data/lib/active_record/base.rb +2 -0
  52. data/lib/active_record/callbacks.rb +6 -8
  53. data/lib/active_record/coders/json.rb +2 -0
  54. data/lib/active_record/coders/yaml_column.rb +2 -0
  55. data/lib/active_record/collection_cache_key.rb +10 -5
  56. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +110 -35
  57. data/lib/active_record/connection_adapters/abstract/database_limits.rb +2 -0
  58. data/lib/active_record/connection_adapters/abstract/database_statements.rb +120 -28
  59. data/lib/active_record/connection_adapters/abstract/query_cache.rb +7 -2
  60. data/lib/active_record/connection_adapters/abstract/quoting.rb +14 -33
  61. data/lib/active_record/connection_adapters/abstract/savepoints.rb +2 -0
  62. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +13 -5
  63. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +40 -2
  64. data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +31 -53
  65. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +103 -63
  66. data/lib/active_record/connection_adapters/abstract/transaction.rb +45 -9
  67. data/lib/active_record/connection_adapters/abstract_adapter.rb +62 -90
  68. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +75 -138
  69. data/lib/active_record/connection_adapters/column.rb +3 -1
  70. data/lib/active_record/connection_adapters/connection_specification.rb +17 -3
  71. data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +2 -0
  72. data/lib/active_record/connection_adapters/mysql/column.rb +2 -0
  73. data/lib/active_record/connection_adapters/mysql/database_statements.rb +3 -1
  74. data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +2 -0
  75. data/lib/active_record/connection_adapters/mysql/quoting.rb +9 -10
  76. data/lib/active_record/connection_adapters/mysql/schema_creation.rb +5 -3
  77. data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +7 -6
  78. data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +30 -30
  79. data/lib/active_record/connection_adapters/mysql/schema_statements.rb +91 -1
  80. data/lib/active_record/connection_adapters/mysql/type_metadata.rb +2 -0
  81. data/lib/active_record/connection_adapters/mysql2_adapter.rb +8 -2
  82. data/lib/active_record/connection_adapters/postgresql/column.rb +2 -0
  83. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +6 -0
  84. data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +2 -0
  85. data/lib/active_record/connection_adapters/postgresql/oid.rb +2 -1
  86. data/lib/active_record/connection_adapters/postgresql/oid/array.rb +3 -11
  87. data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +2 -0
  88. data/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb +2 -0
  89. data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +2 -0
  90. data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +2 -0
  91. data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +2 -0
  92. data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +3 -1
  93. data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +2 -0
  94. data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +2 -0
  95. data/lib/active_record/connection_adapters/postgresql/oid/inet.rb +2 -0
  96. data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +3 -1
  97. data/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb +2 -0
  98. data/lib/active_record/connection_adapters/postgresql/oid/money.rb +3 -1
  99. data/lib/active_record/connection_adapters/postgresql/oid/oid.rb +2 -0
  100. data/lib/active_record/connection_adapters/postgresql/oid/point.rb +2 -0
  101. data/lib/active_record/connection_adapters/postgresql/oid/range.rb +3 -5
  102. data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +2 -0
  103. data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +4 -2
  104. data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +3 -1
  105. data/lib/active_record/connection_adapters/postgresql/oid/vector.rb +2 -0
  106. data/lib/active_record/connection_adapters/postgresql/oid/xml.rb +2 -0
  107. data/lib/active_record/connection_adapters/postgresql/quoting.rb +10 -0
  108. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +19 -25
  109. data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +2 -0
  110. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +11 -7
  111. data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +20 -13
  112. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +79 -65
  113. data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +2 -0
  114. data/lib/active_record/connection_adapters/postgresql/utils.rb +2 -0
  115. data/lib/active_record/connection_adapters/postgresql_adapter.rb +47 -82
  116. data/lib/active_record/connection_adapters/schema_cache.rb +2 -0
  117. data/lib/active_record/connection_adapters/sql_type_metadata.rb +2 -0
  118. data/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb +2 -0
  119. data/lib/active_record/connection_adapters/sqlite3/quoting.rb +19 -2
  120. data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +2 -0
  121. data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +6 -15
  122. data/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb +3 -2
  123. data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +71 -1
  124. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +34 -89
  125. data/lib/active_record/connection_adapters/statement_pool.rb +2 -0
  126. data/lib/active_record/connection_handling.rb +4 -2
  127. data/lib/active_record/core.rb +27 -57
  128. data/lib/active_record/counter_cache.rb +15 -12
  129. data/lib/active_record/define_callbacks.rb +5 -3
  130. data/lib/active_record/dynamic_matchers.rb +9 -9
  131. data/lib/active_record/enum.rb +15 -13
  132. data/lib/active_record/errors.rb +54 -21
  133. data/lib/active_record/explain.rb +3 -1
  134. data/lib/active_record/explain_registry.rb +2 -0
  135. data/lib/active_record/explain_subscriber.rb +2 -0
  136. data/lib/active_record/fixture_set/file.rb +2 -0
  137. data/lib/active_record/fixtures.rb +40 -24
  138. data/lib/active_record/gem_version.rb +5 -3
  139. data/lib/active_record/inheritance.rb +6 -5
  140. data/lib/active_record/integration.rb +58 -19
  141. data/lib/active_record/internal_metadata.rb +2 -0
  142. data/lib/active_record/legacy_yaml_adapter.rb +3 -1
  143. data/lib/active_record/locking/optimistic.rb +31 -20
  144. data/lib/active_record/locking/pessimistic.rb +10 -7
  145. data/lib/active_record/log_subscriber.rb +2 -0
  146. data/lib/active_record/migration.rb +47 -21
  147. data/lib/active_record/migration/command_recorder.rb +11 -9
  148. data/lib/active_record/migration/compatibility.rb +20 -2
  149. data/lib/active_record/migration/join_table.rb +2 -0
  150. data/lib/active_record/model_schema.rb +29 -38
  151. data/lib/active_record/nested_attributes.rb +18 -6
  152. data/lib/active_record/no_touching.rb +3 -1
  153. data/lib/active_record/null_relation.rb +2 -0
  154. data/lib/active_record/persistence.rb +184 -40
  155. data/lib/active_record/query_cache.rb +17 -12
  156. data/lib/active_record/querying.rb +3 -1
  157. data/lib/active_record/railtie.rb +54 -1
  158. data/lib/active_record/railties/console_sandbox.rb +2 -0
  159. data/lib/active_record/railties/controller_runtime.rb +2 -0
  160. data/lib/active_record/railties/databases.rake +41 -28
  161. data/lib/active_record/readonly_attributes.rb +3 -2
  162. data/lib/active_record/reflection.rb +100 -182
  163. data/lib/active_record/relation.rb +61 -193
  164. data/lib/active_record/relation/batches.rb +20 -5
  165. data/lib/active_record/relation/batches/batch_enumerator.rb +2 -0
  166. data/lib/active_record/relation/calculations.rb +40 -23
  167. data/lib/active_record/relation/delegation.rb +10 -27
  168. data/lib/active_record/relation/finder_methods.rb +53 -49
  169. data/lib/active_record/relation/from_clause.rb +2 -8
  170. data/lib/active_record/relation/merger.rb +22 -19
  171. data/lib/active_record/relation/predicate_builder.rb +42 -79
  172. data/lib/active_record/relation/predicate_builder/array_handler.rb +10 -7
  173. data/lib/active_record/relation/predicate_builder/association_query_value.rb +46 -0
  174. data/lib/active_record/relation/predicate_builder/base_handler.rb +2 -2
  175. data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +12 -1
  176. data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +54 -0
  177. data/lib/active_record/relation/predicate_builder/range_handler.rb +22 -6
  178. data/lib/active_record/relation/predicate_builder/relation_handler.rb +6 -0
  179. data/lib/active_record/relation/query_attribute.rb +9 -2
  180. data/lib/active_record/relation/query_methods.rb +80 -69
  181. data/lib/active_record/relation/record_fetch_warning.rb +2 -0
  182. data/lib/active_record/relation/spawn_methods.rb +2 -0
  183. data/lib/active_record/relation/where_clause.rb +50 -67
  184. data/lib/active_record/relation/where_clause_factory.rb +4 -46
  185. data/lib/active_record/result.rb +2 -0
  186. data/lib/active_record/runtime_registry.rb +2 -0
  187. data/lib/active_record/sanitization.rb +15 -9
  188. data/lib/active_record/schema.rb +3 -1
  189. data/lib/active_record/schema_dumper.rb +24 -23
  190. data/lib/active_record/schema_migration.rb +2 -0
  191. data/lib/active_record/scoping.rb +9 -8
  192. data/lib/active_record/scoping/default.rb +6 -7
  193. data/lib/active_record/scoping/named.rb +15 -7
  194. data/lib/active_record/secure_token.rb +2 -0
  195. data/lib/active_record/serialization.rb +2 -0
  196. data/lib/active_record/statement_cache.rb +22 -12
  197. data/lib/active_record/store.rb +2 -0
  198. data/lib/active_record/suppressor.rb +2 -0
  199. data/lib/active_record/table_metadata.rb +3 -1
  200. data/lib/active_record/tasks/database_tasks.rb +23 -12
  201. data/lib/active_record/tasks/mysql_database_tasks.rb +9 -48
  202. data/lib/active_record/tasks/postgresql_database_tasks.rb +10 -2
  203. data/lib/active_record/tasks/sqlite_database_tasks.rb +25 -3
  204. data/lib/active_record/timestamp.rb +5 -12
  205. data/lib/active_record/touch_later.rb +2 -0
  206. data/lib/active_record/transactions.rb +9 -7
  207. data/lib/active_record/translation.rb +2 -0
  208. data/lib/active_record/type.rb +4 -1
  209. data/lib/active_record/type/adapter_specific_registry.rb +2 -0
  210. data/lib/active_record/type/date.rb +2 -0
  211. data/lib/active_record/type/date_time.rb +2 -0
  212. data/lib/active_record/type/decimal_without_scale.rb +2 -0
  213. data/lib/active_record/type/hash_lookup_type_map.rb +2 -0
  214. data/lib/active_record/type/internal/timezone.rb +2 -0
  215. data/lib/active_record/type/json.rb +30 -0
  216. data/lib/active_record/type/serialized.rb +2 -4
  217. data/lib/active_record/type/text.rb +2 -0
  218. data/lib/active_record/type/time.rb +2 -0
  219. data/lib/active_record/type/type_map.rb +2 -0
  220. data/lib/active_record/type/unsigned_integer.rb +2 -0
  221. data/lib/active_record/type_caster.rb +2 -0
  222. data/lib/active_record/type_caster/connection.rb +2 -0
  223. data/lib/active_record/type_caster/map.rb +2 -0
  224. data/lib/active_record/validations.rb +2 -0
  225. data/lib/active_record/validations/absence.rb +2 -0
  226. data/lib/active_record/validations/associated.rb +2 -0
  227. data/lib/active_record/validations/length.rb +2 -0
  228. data/lib/active_record/validations/presence.rb +2 -0
  229. data/lib/active_record/validations/uniqueness.rb +36 -6
  230. data/lib/active_record/version.rb +2 -0
  231. data/lib/rails/generators/active_record.rb +3 -1
  232. data/lib/rails/generators/active_record/application_record/application_record_generator.rb +27 -0
  233. data/lib/rails/generators/active_record/{model/templates/application_record.rb → application_record/templates/application_record.rb.tt} +0 -0
  234. data/lib/rails/generators/active_record/migration.rb +2 -0
  235. data/lib/rails/generators/active_record/migration/migration_generator.rb +3 -1
  236. data/lib/rails/generators/active_record/migration/templates/{create_table_migration.rb → create_table_migration.rb.tt} +0 -0
  237. data/lib/rails/generators/active_record/migration/templates/{migration.rb → migration.rb.tt} +0 -0
  238. data/lib/rails/generators/active_record/model/model_generator.rb +2 -23
  239. data/lib/rails/generators/active_record/model/templates/{model.rb → model.rb.tt} +0 -0
  240. data/lib/rails/generators/active_record/model/templates/{module.rb → module.rb.tt} +0 -0
  241. metadata +25 -38
  242. data/lib/active_record/associations/preloader/belongs_to.rb +0 -15
  243. data/lib/active_record/associations/preloader/collection_association.rb +0 -17
  244. data/lib/active_record/associations/preloader/has_many.rb +0 -15
  245. data/lib/active_record/associations/preloader/has_many_through.rb +0 -19
  246. data/lib/active_record/associations/preloader/has_one.rb +0 -15
  247. data/lib/active_record/associations/preloader/has_one_through.rb +0 -9
  248. data/lib/active_record/associations/preloader/singular_association.rb +0 -18
  249. data/lib/active_record/attribute.rb +0 -240
  250. data/lib/active_record/attribute/user_provided_default.rb +0 -30
  251. data/lib/active_record/attribute_mutation_tracker.rb +0 -122
  252. data/lib/active_record/attribute_set.rb +0 -113
  253. data/lib/active_record/attribute_set/builder.rb +0 -126
  254. data/lib/active_record/attribute_set/yaml_encoder.rb +0 -41
  255. data/lib/active_record/connection_adapters/postgresql/oid/json.rb +0 -10
  256. data/lib/active_record/railties/jdbcmysql_error.rb +0 -16
  257. data/lib/active_record/relation/predicate_builder/association_query_handler.rb +0 -88
  258. data/lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb +0 -59
  259. data/lib/active_record/type/internal/abstract_json.rb +0 -37
@@ -1,63 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/hash/compact"
4
+
1
5
  module ActiveRecord
2
6
  module ConnectionAdapters # :nodoc:
3
- # The goal of this module is to move Adapter specific column
4
- # definitions to the Adapter instead of having it in the schema
5
- # dumper itself. This code represents the normal case.
6
- # We can then redefine how certain data types may be handled in the schema dumper on the
7
- # Adapter level by over-writing this code inside the database specific adapters
8
- module ColumnDumper
9
- def column_spec(column)
10
- [schema_type_with_virtual(column), prepare_column_options(column)]
7
+ class SchemaDumper < SchemaDumper # :nodoc:
8
+ def self.create(connection, options)
9
+ new(connection, options)
11
10
  end
12
11
 
13
- def column_spec_for_primary_key(column)
14
- return {} if default_primary_key?(column)
15
- spec = { id: schema_type(column).inspect }
16
- spec.merge!(prepare_column_options(column).except!(:null))
17
- spec[:default] ||= "nil" if explicit_primary_key_default?(column)
18
- spec
19
- end
20
-
21
- # This can be overridden on an Adapter level basis to support other
22
- # extended datatypes (Example: Adding an array option in the
23
- # PostgreSQL::ColumnDumper)
24
- def prepare_column_options(column)
25
- spec = {}
26
-
27
- if limit = schema_limit(column)
28
- spec[:limit] = limit
12
+ private
13
+ def column_spec(column)
14
+ [schema_type_with_virtual(column), prepare_column_options(column)]
29
15
  end
30
16
 
31
- if precision = schema_precision(column)
32
- spec[:precision] = precision
17
+ def column_spec_for_primary_key(column)
18
+ return {} if default_primary_key?(column)
19
+ spec = { id: schema_type(column).inspect }
20
+ spec.merge!(prepare_column_options(column).except!(:null))
21
+ spec[:default] ||= "nil" if explicit_primary_key_default?(column)
22
+ spec
33
23
  end
34
24
 
35
- if scale = schema_scale(column)
36
- spec[:scale] = scale
25
+ def prepare_column_options(column)
26
+ spec = {}
27
+ spec[:limit] = schema_limit(column)
28
+ spec[:precision] = schema_precision(column)
29
+ spec[:scale] = schema_scale(column)
30
+ spec[:default] = schema_default(column)
31
+ spec[:null] = "false" unless column.null
32
+ spec[:collation] = schema_collation(column)
33
+ spec[:comment] = column.comment.inspect if column.comment.present?
34
+ spec.compact!
35
+ spec
37
36
  end
38
37
 
39
- default = schema_default(column) if column.has_default?
40
- spec[:default] = default unless default.nil?
41
-
42
- spec[:null] = "false" unless column.null
43
-
44
- if collation = schema_collation(column)
45
- spec[:collation] = collation
46
- end
47
-
48
- spec[:comment] = column.comment.inspect if column.comment.present?
49
-
50
- spec
51
- end
52
-
53
- # Lists the valid migration options
54
- def migration_keys # :nodoc:
55
- column_options_keys
56
- end
57
- deprecate :migration_keys
58
-
59
- private
60
-
61
38
  def default_primary_key?(column)
62
39
  schema_type(column) == :bigint
63
40
  end
@@ -67,7 +44,7 @@ module ActiveRecord
67
44
  end
68
45
 
69
46
  def schema_type_with_virtual(column)
70
- if supports_virtual_columns? && column.virtual?
47
+ if @connection.supports_virtual_columns? && column.virtual?
71
48
  :virtual
72
49
  else
73
50
  schema_type(column)
@@ -84,7 +61,7 @@ module ActiveRecord
84
61
 
85
62
  def schema_limit(column)
86
63
  limit = column.limit unless column.bigint?
87
- limit.inspect if limit && limit != native_database_types[column.type][:limit]
64
+ limit.inspect if limit && limit != @connection.native_database_types[column.type][:limit]
88
65
  end
89
66
 
90
67
  def schema_precision(column)
@@ -96,7 +73,8 @@ module ActiveRecord
96
73
  end
97
74
 
98
75
  def schema_default(column)
99
- type = lookup_cast_type_from_column(column)
76
+ return unless column.has_default?
77
+ type = @connection.lookup_cast_type_from_column(column)
100
78
  default = type.deserialize(column.default)
101
79
  if default.nil?
102
80
  schema_expression(column)
@@ -1,6 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_record/migration/join_table"
2
4
  require "active_support/core_ext/string/access"
3
- require "digest"
5
+ require "digest/sha2"
4
6
 
5
7
  module ActiveRecord
6
8
  module ConnectionAdapters # :nodoc:
@@ -77,7 +79,7 @@ module ActiveRecord
77
79
  end
78
80
 
79
81
  # Returns an array of indexes for the given table.
80
- def indexes(table_name, name = nil)
82
+ def indexes(table_name)
81
83
  raise NotImplementedError, "#indexes is not implemented"
82
84
  end
83
85
 
@@ -105,10 +107,12 @@ module ActiveRecord
105
107
  indexes(table_name).any? { |i| checks.all? { |check| check[i] } }
106
108
  end
107
109
 
108
- # Returns an array of Column objects for the table specified by +table_name+.
109
- # See the concrete implementation for details on the expected parameter values.
110
+ # Returns an array of +Column+ objects for the table specified by +table_name+.
110
111
  def columns(table_name)
111
- raise NotImplementedError, "#columns is not implemented"
112
+ table_name = table_name.to_s
113
+ column_definitions(table_name).map do |field|
114
+ new_column_from_field(table_name, field)
115
+ end
112
116
  end
113
117
 
114
118
  # Checks to see if a column exists in a given table.
@@ -186,6 +190,8 @@ module ActiveRecord
186
190
  # The name of the primary key, if one is to be added automatically.
187
191
  # Defaults to +id+. If <tt>:id</tt> is false, then this option is ignored.
188
192
  #
193
+ # If an array is passed, a composite primary key will be created.
194
+ #
189
195
  # Note that Active Record models will automatically detect their
190
196
  # primary key. This can be avoided by using
191
197
  # {self.primary_key=}[rdoc-ref:AttributeMethods::PrimaryKey::ClassMethods#primary_key=] on the model
@@ -210,7 +216,7 @@ module ActiveRecord
210
216
  # generates:
211
217
  #
212
218
  # CREATE TABLE suppliers (
213
- # id int auto_increment PRIMARY KEY
219
+ # id bigint auto_increment PRIMARY KEY
214
220
  # ) ENGINE=InnoDB DEFAULT CHARSET=utf8
215
221
  #
216
222
  # ====== Rename the primary key column
@@ -222,7 +228,7 @@ module ActiveRecord
222
228
  # generates:
223
229
  #
224
230
  # CREATE TABLE objects (
225
- # guid int auto_increment PRIMARY KEY,
231
+ # guid bigint auto_increment PRIMARY KEY,
226
232
  # name varchar(80)
227
233
  # )
228
234
  #
@@ -239,18 +245,35 @@ module ActiveRecord
239
245
  # label varchar
240
246
  # )
241
247
  #
248
+ # ====== Create a composite primary key
249
+ #
250
+ # create_table(:orders, primary_key: [:product_id, :client_id]) do |t|
251
+ # t.belongs_to :product
252
+ # t.belongs_to :client
253
+ # end
254
+ #
255
+ # generates:
256
+ #
257
+ # CREATE TABLE order (
258
+ # product_id bigint NOT NULL,
259
+ # client_id bigint NOT NULL
260
+ # );
261
+ #
262
+ # ALTER TABLE ONLY "orders"
263
+ # ADD CONSTRAINT orders_pkey PRIMARY KEY (product_id, client_id);
264
+ #
242
265
  # ====== Do not add a primary key column
243
266
  #
244
267
  # create_table(:categories_suppliers, id: false) do |t|
245
- # t.column :category_id, :integer
246
- # t.column :supplier_id, :integer
268
+ # t.column :category_id, :bigint
269
+ # t.column :supplier_id, :bigint
247
270
  # end
248
271
  #
249
272
  # generates:
250
273
  #
251
274
  # CREATE TABLE categories_suppliers (
252
- # category_id int,
253
- # supplier_id int
275
+ # category_id bigint,
276
+ # supplier_id bigint
254
277
  # )
255
278
  #
256
279
  # ====== Create a temporary table based on a query
@@ -338,8 +361,8 @@ module ActiveRecord
338
361
  # generates:
339
362
  #
340
363
  # CREATE TABLE assemblies_parts (
341
- # assembly_id int NOT NULL,
342
- # part_id int NOT NULL,
364
+ # assembly_id bigint NOT NULL,
365
+ # part_id bigint NOT NULL,
343
366
  # ) ENGINE=InnoDB DEFAULT CHARSET=utf8
344
367
  #
345
368
  def create_join_table(table_1, table_2, column_options: {}, **options)
@@ -383,6 +406,8 @@ module ActiveRecord
383
406
  #
384
407
  # Defaults to false.
385
408
  #
409
+ # Only supported on the MySQL adapter, ignored elsewhere.
410
+ #
386
411
  # ====== Add a column
387
412
  #
388
413
  # change_table(:suppliers) do |t|
@@ -407,7 +432,7 @@ module ActiveRecord
407
432
  # t.references :company
408
433
  # end
409
434
  #
410
- # Creates a <tt>company_id(integer)</tt> column.
435
+ # Creates a <tt>company_id(bigint)</tt> column.
411
436
  #
412
437
  # ====== Add a polymorphic foreign key column
413
438
  #
@@ -415,7 +440,7 @@ module ActiveRecord
415
440
  # t.belongs_to :company, polymorphic: true
416
441
  # end
417
442
  #
418
- # Creates <tt>company_type(varchar)</tt> and <tt>company_id(integer)</tt> columns.
443
+ # Creates <tt>company_type(varchar)</tt> and <tt>company_id(bigint)</tt> columns.
419
444
  #
420
445
  # ====== Remove a column
421
446
  #
@@ -488,15 +513,17 @@ module ActiveRecord
488
513
  # * <tt>:limit</tt> -
489
514
  # Requests a maximum column length. This is the number of characters for a <tt>:string</tt> column
490
515
  # and number of bytes for <tt>:text</tt>, <tt>:binary</tt> and <tt>:integer</tt> columns.
516
+ # This option is ignored by some backends.
491
517
  # * <tt>:default</tt> -
492
518
  # The column's default value. Use +nil+ for +NULL+.
493
519
  # * <tt>:null</tt> -
494
- # Allows or disallows +NULL+ values in the column. This option could
495
- # have been named <tt>:null_allowed</tt>.
520
+ # Allows or disallows +NULL+ values in the column.
496
521
  # * <tt>:precision</tt> -
497
522
  # Specifies the precision for the <tt>:decimal</tt> and <tt>:numeric</tt> columns.
498
523
  # * <tt>:scale</tt> -
499
524
  # Specifies the scale for the <tt>:decimal</tt> and <tt>:numeric</tt> columns.
525
+ # * <tt>:comment</tt> -
526
+ # Specifies the comment for the column. This option is ignored by some backends.
500
527
  #
501
528
  # Note: The precision is the total number of significant digits,
502
529
  # and the scale is the number of digits that can be stored following
@@ -757,7 +784,7 @@ module ActiveRecord
757
784
  def rename_index(table_name, old_name, new_name)
758
785
  validate_index_length!(table_name, new_name)
759
786
 
760
- # this is a naive implementation; some DBs may support this more efficiently (Postgres, for instance)
787
+ # this is a naive implementation; some DBs may support this more efficiently (PostgreSQL, for instance)
761
788
  old_index_def = indexes(table_name).detect { |i| i.name == old_name }
762
789
  return unless old_index_def
763
790
  add_index(table_name, old_index_def.columns, name: new_name, unique: old_index_def.unique)
@@ -779,24 +806,19 @@ module ActiveRecord
779
806
  end
780
807
 
781
808
  # Verifies the existence of an index with a given name.
782
- def index_name_exists?(table_name, index_name, default = nil)
783
- unless default.nil?
784
- ActiveSupport::Deprecation.warn(<<-MSG.squish)
785
- Passing default to #index_name_exists? is deprecated without replacement.
786
- MSG
787
- end
809
+ def index_name_exists?(table_name, index_name)
788
810
  index_name = index_name.to_s
789
811
  indexes(table_name).detect { |i| i.name == index_name }
790
812
  end
791
813
 
792
- # Adds a reference. The reference column is an integer by default,
814
+ # Adds a reference. The reference column is a bigint by default,
793
815
  # the <tt>:type</tt> option can be used to specify a different type.
794
816
  # Optionally adds a +_type+ column, if <tt>:polymorphic</tt> option is provided.
795
817
  # #add_reference and #add_belongs_to are acceptable.
796
818
  #
797
819
  # The +options+ hash can include the following keys:
798
820
  # [<tt>:type</tt>]
799
- # The reference column type. Defaults to +:integer+.
821
+ # The reference column type. Defaults to +:bigint+.
800
822
  # [<tt>:index</tt>]
801
823
  # Add an appropriate index. Defaults to true.
802
824
  # See #add_index for usage of this option.
@@ -807,7 +829,7 @@ module ActiveRecord
807
829
  # [<tt>:null</tt>]
808
830
  # Whether the column allows nulls. Defaults to true.
809
831
  #
810
- # ====== Create a user_id integer column
832
+ # ====== Create a user_id bigint column
811
833
  #
812
834
  # add_reference(:products, :user)
813
835
  #
@@ -974,16 +996,6 @@ module ActiveRecord
974
996
  foreign_key_for(from_table, options_or_to_table).present?
975
997
  end
976
998
 
977
- def foreign_key_for(from_table, options_or_to_table = {}) # :nodoc:
978
- return unless supports_foreign_keys?
979
- foreign_keys(from_table).detect { |fk| fk.defined_for? options_or_to_table }
980
- end
981
-
982
- def foreign_key_for!(from_table, options_or_to_table = {}) # :nodoc:
983
- foreign_key_for(from_table, options_or_to_table) || \
984
- raise(ArgumentError, "Table '#{from_table}' has no foreign key for #{options_or_to_table}")
985
- end
986
-
987
999
  def foreign_key_column_for(table_name) # :nodoc:
988
1000
  prefix = Base.table_name_prefix
989
1001
  suffix = Base.table_name_suffix
@@ -1003,29 +1015,6 @@ module ActiveRecord
1003
1015
  insert_versions_sql(versions) if versions.any?
1004
1016
  end
1005
1017
 
1006
- def insert_versions_sql(versions) # :nodoc:
1007
- sm_table = quote_table_name(ActiveRecord::SchemaMigration.table_name)
1008
-
1009
- if versions.is_a?(Array)
1010
- sql = "INSERT INTO #{sm_table} (version) VALUES\n"
1011
- sql << versions.map { |v| "(#{quote(v)})" }.join(",\n")
1012
- sql << ";\n\n"
1013
- sql
1014
- else
1015
- "INSERT INTO #{sm_table} (version) VALUES (#{quote(versions)});"
1016
- end
1017
- end
1018
-
1019
- def initialize_schema_migrations_table # :nodoc:
1020
- ActiveRecord::SchemaMigration.create_table
1021
- end
1022
- deprecate :initialize_schema_migrations_table
1023
-
1024
- def initialize_internal_metadata_table # :nodoc:
1025
- ActiveRecord::InternalMetadata.create_table
1026
- end
1027
- deprecate :initialize_internal_metadata_table
1028
-
1029
1018
  def internal_string_options_for_primary_key # :nodoc:
1030
1019
  { primary_key: true }
1031
1020
  end
@@ -1170,10 +1159,14 @@ module ActiveRecord
1170
1159
  end
1171
1160
 
1172
1161
  # Changes the comment for a column or removes it if +nil+.
1173
- def change_column_comment(table_name, column_name, comment) #:nodoc:
1162
+ def change_column_comment(table_name, column_name, comment)
1174
1163
  raise NotImplementedError, "#{self.class} does not support changing column comments"
1175
1164
  end
1176
1165
 
1166
+ def create_schema_dumper(options) # :nodoc:
1167
+ SchemaDumper.create(self, options)
1168
+ end
1169
+
1177
1170
  private
1178
1171
  def column_options_keys
1179
1172
  [:limit, :precision, :scale, :default, :null, :collation, :comment]
@@ -1261,6 +1254,10 @@ module ActiveRecord
1261
1254
  end
1262
1255
  end
1263
1256
 
1257
+ def schema_creation
1258
+ SchemaCreation.new(self)
1259
+ end
1260
+
1264
1261
  def create_table_definition(*args)
1265
1262
  TableDefinition.new(*args)
1266
1263
  end
@@ -1269,6 +1266,17 @@ module ActiveRecord
1269
1266
  AlterTable.new create_table_definition(name)
1270
1267
  end
1271
1268
 
1269
+ def fetch_type_metadata(sql_type)
1270
+ cast_type = lookup_cast_type(sql_type)
1271
+ SqlTypeMetadata.new(
1272
+ sql_type: sql_type,
1273
+ type: cast_type.type,
1274
+ limit: cast_type.limit,
1275
+ precision: cast_type.precision,
1276
+ scale: cast_type.scale,
1277
+ )
1278
+ end
1279
+
1272
1280
  def index_column_names(column_names)
1273
1281
  if column_names.is_a?(String) && /\W/.match?(column_names)
1274
1282
  column_names
@@ -1286,13 +1294,32 @@ module ActiveRecord
1286
1294
  end
1287
1295
 
1288
1296
  def foreign_key_name(table_name, options)
1289
- identifier = "#{table_name}_#{options.fetch(:column)}_fk"
1290
- hashed_identifier = Digest::SHA256.hexdigest(identifier).first(10)
1291
1297
  options.fetch(:name) do
1298
+ identifier = "#{table_name}_#{options.fetch(:column)}_fk"
1299
+ hashed_identifier = Digest::SHA256.hexdigest(identifier).first(10)
1300
+
1292
1301
  "fk_rails_#{hashed_identifier}"
1293
1302
  end
1294
1303
  end
1295
1304
 
1305
+ def foreign_key_for(from_table, options_or_to_table = {})
1306
+ return unless supports_foreign_keys?
1307
+ foreign_keys(from_table).detect { |fk| fk.defined_for? options_or_to_table }
1308
+ end
1309
+
1310
+ def foreign_key_for!(from_table, options_or_to_table = {})
1311
+ foreign_key_for(from_table, options_or_to_table) || \
1312
+ raise(ArgumentError, "Table '#{from_table}' has no foreign key for #{options_or_to_table}")
1313
+ end
1314
+
1315
+ def extract_foreign_key_action(specifier)
1316
+ case specifier
1317
+ when "CASCADE"; :cascade
1318
+ when "SET NULL"; :nullify
1319
+ when "RESTRICT"; :restrict
1320
+ end
1321
+ end
1322
+
1296
1323
  def validate_index_length!(table_name, new_name, internal = false)
1297
1324
  max_index_length = internal ? index_name_length : allowed_index_name_length
1298
1325
 
@@ -1313,6 +1340,19 @@ module ActiveRecord
1313
1340
  options.is_a?(Hash) && options.key?(:name) && options.except(:name, :algorithm).empty?
1314
1341
  end
1315
1342
 
1343
+ def insert_versions_sql(versions)
1344
+ sm_table = quote_table_name(ActiveRecord::SchemaMigration.table_name)
1345
+
1346
+ if versions.is_a?(Array)
1347
+ sql = "INSERT INTO #{sm_table} (version) VALUES\n".dup
1348
+ sql << versions.map { |v| "(#{quote(v)})" }.join(",\n")
1349
+ sql << ";\n\n"
1350
+ sql
1351
+ else
1352
+ "INSERT INTO #{sm_table} (version) VALUES (#{quote(versions)});"
1353
+ end
1354
+ end
1355
+
1316
1356
  def data_source_sql(name = nil, type: nil)
1317
1357
  raise NotImplementedError
1318
1358
  end