activerecord 7.0.5 → 7.1.3.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +1624 -1338
- data/MIT-LICENSE +1 -1
- data/README.rdoc +18 -18
- data/lib/active_record/aggregations.rb +16 -13
- data/lib/active_record/association_relation.rb +1 -1
- data/lib/active_record/associations/association.rb +20 -4
- data/lib/active_record/associations/association_scope.rb +16 -9
- data/lib/active_record/associations/belongs_to_association.rb +14 -6
- data/lib/active_record/associations/builder/association.rb +3 -3
- data/lib/active_record/associations/builder/belongs_to.rb +21 -8
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +1 -5
- data/lib/active_record/associations/builder/singular_association.rb +4 -0
- data/lib/active_record/associations/collection_association.rb +16 -10
- data/lib/active_record/associations/collection_proxy.rb +20 -10
- data/lib/active_record/associations/foreign_association.rb +10 -3
- data/lib/active_record/associations/has_many_association.rb +20 -13
- data/lib/active_record/associations/has_many_through_association.rb +10 -6
- data/lib/active_record/associations/has_one_association.rb +10 -7
- data/lib/active_record/associations/join_dependency.rb +10 -8
- data/lib/active_record/associations/preloader/association.rb +31 -7
- data/lib/active_record/associations/preloader.rb +13 -10
- data/lib/active_record/associations/singular_association.rb +6 -8
- data/lib/active_record/associations/through_association.rb +22 -11
- data/lib/active_record/associations.rb +313 -217
- data/lib/active_record/attribute_assignment.rb +0 -2
- data/lib/active_record/attribute_methods/before_type_cast.rb +17 -0
- data/lib/active_record/attribute_methods/dirty.rb +52 -34
- data/lib/active_record/attribute_methods/primary_key.rb +76 -24
- data/lib/active_record/attribute_methods/query.rb +28 -16
- data/lib/active_record/attribute_methods/read.rb +18 -5
- data/lib/active_record/attribute_methods/serialization.rb +150 -31
- data/lib/active_record/attribute_methods/write.rb +3 -3
- data/lib/active_record/attribute_methods.rb +105 -21
- data/lib/active_record/attributes.rb +3 -3
- data/lib/active_record/autosave_association.rb +55 -9
- data/lib/active_record/base.rb +7 -2
- data/lib/active_record/callbacks.rb +10 -24
- data/lib/active_record/coders/column_serializer.rb +61 -0
- data/lib/active_record/coders/json.rb +1 -1
- data/lib/active_record/coders/yaml_column.rb +70 -42
- data/lib/active_record/connection_adapters/abstract/connection_handler.rb +163 -88
- data/lib/active_record/connection_adapters/abstract/connection_pool/queue.rb +2 -0
- data/lib/active_record/connection_adapters/abstract/connection_pool/reaper.rb +3 -1
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +74 -51
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +5 -0
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +129 -31
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +62 -23
- data/lib/active_record/connection_adapters/abstract/quoting.rb +41 -6
- data/lib/active_record/connection_adapters/abstract/savepoints.rb +4 -3
- data/lib/active_record/connection_adapters/abstract/schema_creation.rb +18 -4
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +137 -11
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +290 -125
- data/lib/active_record/connection_adapters/abstract/transaction.rb +287 -58
- data/lib/active_record/connection_adapters/abstract_adapter.rb +505 -102
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +214 -113
- data/lib/active_record/connection_adapters/column.rb +9 -0
- data/lib/active_record/connection_adapters/mysql/column.rb +1 -0
- data/lib/active_record/connection_adapters/mysql/database_statements.rb +23 -144
- data/lib/active_record/connection_adapters/mysql/quoting.rb +21 -14
- data/lib/active_record/connection_adapters/mysql/schema_creation.rb +9 -0
- data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +6 -0
- data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +1 -1
- data/lib/active_record/connection_adapters/mysql/schema_statements.rb +18 -13
- data/lib/active_record/connection_adapters/mysql2/database_statements.rb +151 -0
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +98 -53
- data/lib/active_record/connection_adapters/pool_config.rb +14 -5
- data/lib/active_record/connection_adapters/pool_manager.rb +19 -9
- data/lib/active_record/connection_adapters/postgresql/column.rb +14 -3
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +75 -41
- data/lib/active_record/connection_adapters/postgresql/oid/money.rb +3 -2
- data/lib/active_record/connection_adapters/postgresql/oid/range.rb +11 -2
- data/lib/active_record/connection_adapters/postgresql/oid/timestamp_with_time_zone.rb +2 -2
- data/lib/active_record/connection_adapters/postgresql/quoting.rb +15 -8
- data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +3 -9
- data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +76 -6
- data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +131 -2
- data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +53 -0
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +361 -60
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +353 -192
- data/lib/active_record/connection_adapters/schema_cache.rb +287 -59
- data/lib/active_record/connection_adapters/sqlite3/column.rb +49 -0
- data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +52 -39
- data/lib/active_record/connection_adapters/sqlite3/quoting.rb +9 -5
- data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +7 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +28 -9
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +211 -83
- data/lib/active_record/connection_adapters/statement_pool.rb +7 -0
- data/lib/active_record/connection_adapters/trilogy/database_statements.rb +99 -0
- data/lib/active_record/connection_adapters/trilogy_adapter.rb +262 -0
- data/lib/active_record/connection_adapters.rb +3 -1
- data/lib/active_record/connection_handling.rb +72 -95
- data/lib/active_record/core.rb +175 -153
- data/lib/active_record/counter_cache.rb +46 -25
- data/lib/active_record/database_configurations/database_config.rb +9 -3
- data/lib/active_record/database_configurations/hash_config.rb +22 -12
- data/lib/active_record/database_configurations/url_config.rb +17 -11
- data/lib/active_record/database_configurations.rb +86 -33
- data/lib/active_record/delegated_type.rb +9 -4
- data/lib/active_record/deprecator.rb +7 -0
- data/lib/active_record/destroy_association_async_job.rb +2 -0
- data/lib/active_record/encryption/auto_filtered_parameters.rb +66 -0
- data/lib/active_record/encryption/cipher/aes256_gcm.rb +4 -1
- data/lib/active_record/encryption/config.rb +25 -1
- data/lib/active_record/encryption/configurable.rb +12 -19
- data/lib/active_record/encryption/context.rb +10 -3
- data/lib/active_record/encryption/contexts.rb +5 -1
- data/lib/active_record/encryption/derived_secret_key_provider.rb +8 -2
- data/lib/active_record/encryption/encryptable_record.rb +42 -18
- data/lib/active_record/encryption/encrypted_attribute_type.rb +21 -6
- data/lib/active_record/encryption/extended_deterministic_queries.rb +66 -69
- data/lib/active_record/encryption/extended_deterministic_uniqueness_validator.rb +3 -3
- data/lib/active_record/encryption/key_generator.rb +12 -1
- data/lib/active_record/encryption/message_serializer.rb +2 -0
- data/lib/active_record/encryption/properties.rb +3 -3
- data/lib/active_record/encryption/scheme.rb +19 -22
- data/lib/active_record/encryption.rb +1 -0
- data/lib/active_record/enum.rb +112 -28
- data/lib/active_record/errors.rb +112 -18
- data/lib/active_record/explain.rb +23 -3
- data/lib/active_record/fixture_set/model_metadata.rb +14 -4
- data/lib/active_record/fixture_set/render_context.rb +2 -0
- data/lib/active_record/fixture_set/table_row.rb +29 -8
- data/lib/active_record/fixtures.rb +135 -71
- data/lib/active_record/future_result.rb +31 -5
- data/lib/active_record/gem_version.rb +4 -4
- data/lib/active_record/inheritance.rb +30 -16
- data/lib/active_record/insert_all.rb +57 -10
- data/lib/active_record/integration.rb +8 -8
- data/lib/active_record/internal_metadata.rb +120 -30
- data/lib/active_record/locking/optimistic.rb +32 -18
- data/lib/active_record/locking/pessimistic.rb +5 -2
- data/lib/active_record/log_subscriber.rb +29 -12
- data/lib/active_record/marshalling.rb +56 -0
- data/lib/active_record/message_pack.rb +124 -0
- data/lib/active_record/middleware/database_selector/resolver.rb +4 -0
- data/lib/active_record/middleware/database_selector.rb +6 -8
- data/lib/active_record/middleware/shard_selector.rb +3 -1
- data/lib/active_record/migration/command_recorder.rb +104 -5
- data/lib/active_record/migration/compatibility.rb +150 -58
- data/lib/active_record/migration/default_strategy.rb +23 -0
- data/lib/active_record/migration/execution_strategy.rb +19 -0
- data/lib/active_record/migration/pending_migration_connection.rb +21 -0
- data/lib/active_record/migration.rb +271 -114
- data/lib/active_record/model_schema.rb +64 -44
- data/lib/active_record/nested_attributes.rb +24 -6
- data/lib/active_record/normalization.rb +167 -0
- data/lib/active_record/persistence.rb +195 -42
- data/lib/active_record/promise.rb +84 -0
- data/lib/active_record/query_cache.rb +3 -21
- data/lib/active_record/query_logs.rb +77 -52
- data/lib/active_record/query_logs_formatter.rb +41 -0
- data/lib/active_record/querying.rb +15 -2
- data/lib/active_record/railtie.rb +109 -47
- data/lib/active_record/railties/controller_runtime.rb +14 -9
- data/lib/active_record/railties/databases.rake +142 -148
- data/lib/active_record/railties/job_runtime.rb +23 -0
- data/lib/active_record/readonly_attributes.rb +32 -5
- data/lib/active_record/reflection.rb +182 -44
- data/lib/active_record/relation/batches/batch_enumerator.rb +5 -3
- data/lib/active_record/relation/batches.rb +190 -61
- data/lib/active_record/relation/calculations.rb +187 -63
- data/lib/active_record/relation/delegation.rb +23 -9
- data/lib/active_record/relation/finder_methods.rb +77 -16
- data/lib/active_record/relation/merger.rb +2 -0
- data/lib/active_record/relation/predicate_builder/association_query_value.rb +31 -3
- data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +4 -6
- data/lib/active_record/relation/predicate_builder/relation_handler.rb +5 -1
- data/lib/active_record/relation/predicate_builder.rb +27 -16
- data/lib/active_record/relation/query_attribute.rb +25 -1
- data/lib/active_record/relation/query_methods.rb +386 -70
- data/lib/active_record/relation/spawn_methods.rb +18 -1
- data/lib/active_record/relation.rb +91 -35
- data/lib/active_record/result.rb +25 -9
- data/lib/active_record/runtime_registry.rb +24 -1
- data/lib/active_record/sanitization.rb +51 -11
- data/lib/active_record/schema.rb +2 -3
- data/lib/active_record/schema_dumper.rb +46 -7
- data/lib/active_record/schema_migration.rb +68 -33
- data/lib/active_record/scoping/default.rb +15 -5
- data/lib/active_record/scoping/named.rb +2 -2
- data/lib/active_record/scoping.rb +2 -1
- data/lib/active_record/secure_password.rb +60 -0
- data/lib/active_record/secure_token.rb +21 -3
- data/lib/active_record/signed_id.rb +7 -5
- data/lib/active_record/store.rb +8 -8
- data/lib/active_record/suppressor.rb +3 -1
- data/lib/active_record/table_metadata.rb +16 -3
- data/lib/active_record/tasks/database_tasks.rb +127 -105
- data/lib/active_record/tasks/mysql_database_tasks.rb +15 -6
- data/lib/active_record/tasks/postgresql_database_tasks.rb +16 -13
- data/lib/active_record/tasks/sqlite_database_tasks.rb +15 -7
- data/lib/active_record/test_fixtures.rb +113 -96
- data/lib/active_record/timestamp.rb +27 -15
- data/lib/active_record/token_for.rb +113 -0
- data/lib/active_record/touch_later.rb +11 -6
- data/lib/active_record/transactions.rb +39 -13
- data/lib/active_record/type/adapter_specific_registry.rb +1 -8
- data/lib/active_record/type/internal/timezone.rb +7 -2
- data/lib/active_record/type/serialized.rb +4 -0
- data/lib/active_record/type/time.rb +4 -0
- data/lib/active_record/validations/absence.rb +1 -1
- data/lib/active_record/validations/numericality.rb +5 -4
- data/lib/active_record/validations/presence.rb +5 -28
- data/lib/active_record/validations/uniqueness.rb +47 -2
- data/lib/active_record/validations.rb +8 -4
- data/lib/active_record/version.rb +1 -1
- data/lib/active_record.rb +121 -16
- data/lib/arel/errors.rb +10 -0
- data/lib/arel/factory_methods.rb +4 -0
- data/lib/arel/nodes/and.rb +4 -0
- data/lib/arel/nodes/binary.rb +6 -1
- data/lib/arel/nodes/bound_sql_literal.rb +61 -0
- data/lib/arel/nodes/cte.rb +36 -0
- data/lib/arel/nodes/fragments.rb +35 -0
- data/lib/arel/nodes/homogeneous_in.rb +1 -9
- data/lib/arel/nodes/leading_join.rb +8 -0
- data/lib/arel/nodes/node.rb +111 -2
- data/lib/arel/nodes/sql_literal.rb +6 -0
- data/lib/arel/nodes/table_alias.rb +4 -0
- data/lib/arel/nodes.rb +4 -0
- data/lib/arel/predications.rb +2 -0
- data/lib/arel/table.rb +9 -5
- data/lib/arel/visitors/mysql.rb +8 -1
- data/lib/arel/visitors/to_sql.rb +81 -17
- data/lib/arel/visitors/visitor.rb +2 -2
- data/lib/arel.rb +16 -2
- data/lib/rails/generators/active_record/application_record/USAGE +8 -0
- data/lib/rails/generators/active_record/migration.rb +3 -1
- data/lib/rails/generators/active_record/model/USAGE +113 -0
- data/lib/rails/generators/active_record/model/model_generator.rb +15 -6
- metadata +51 -15
- data/lib/active_record/connection_adapters/legacy_pool_manager.rb +0 -35
- data/lib/active_record/null_relation.rb +0 -63
@@ -13,8 +13,7 @@ module ActiveRecord
|
|
13
13
|
##
|
14
14
|
# :singleton-method:
|
15
15
|
# A list of tables which should not be dumped to the schema.
|
16
|
-
# Acceptable values are strings
|
17
|
-
# Only strings are accepted if ActiveRecord.schema_format == :sql.
|
16
|
+
# Acceptable values are strings and regexps.
|
18
17
|
cattr_accessor :ignore_tables, default: []
|
19
18
|
|
20
19
|
##
|
@@ -29,6 +28,18 @@ module ActiveRecord
|
|
29
28
|
# should not be dumped to db/schema.rb.
|
30
29
|
cattr_accessor :chk_ignore_pattern, default: /^chk_rails_[0-9a-f]{10}$/
|
31
30
|
|
31
|
+
##
|
32
|
+
# :singleton-method:
|
33
|
+
# Specify a custom regular expression matching exclusion constraints which name
|
34
|
+
# should not be dumped to db/schema.rb.
|
35
|
+
cattr_accessor :excl_ignore_pattern, default: /^excl_rails_[0-9a-f]{10}$/
|
36
|
+
|
37
|
+
##
|
38
|
+
# :singleton-method:
|
39
|
+
# Specify a custom regular expression matching unique constraints which name
|
40
|
+
# should not be dumped to db/schema.rb.
|
41
|
+
cattr_accessor :unique_ignore_pattern, default: /^uniq_rails_[0-9a-f]{10}$/
|
42
|
+
|
32
43
|
class << self
|
33
44
|
def dump(connection = ActiveRecord::Base.connection, stream = STDOUT, config = ActiveRecord::Base)
|
34
45
|
connection.create_schema_dumper(generate_options(config)).dump(stream)
|
@@ -46,6 +57,7 @@ module ActiveRecord
|
|
46
57
|
|
47
58
|
def dump(stream)
|
48
59
|
header(stream)
|
60
|
+
schemas(stream)
|
49
61
|
extensions(stream)
|
50
62
|
types(stream)
|
51
63
|
tables(stream)
|
@@ -60,6 +72,11 @@ module ActiveRecord
|
|
60
72
|
@connection = connection
|
61
73
|
@version = connection.migration_context.current_version rescue nil
|
62
74
|
@options = options
|
75
|
+
@ignore_tables = [
|
76
|
+
ActiveRecord::Base.schema_migrations_table_name,
|
77
|
+
ActiveRecord::Base.internal_metadata_table_name,
|
78
|
+
self.class.ignore_tables
|
79
|
+
].flatten
|
63
80
|
end
|
64
81
|
|
65
82
|
# turns 20170404131909 into "2017_04_04_131909"
|
@@ -103,6 +120,10 @@ module ActiveRecord
|
|
103
120
|
def types(stream)
|
104
121
|
end
|
105
122
|
|
123
|
+
# schemas are only supported by PostgreSQL
|
124
|
+
def schemas(stream)
|
125
|
+
end
|
126
|
+
|
106
127
|
def tables(stream)
|
107
128
|
sorted_tables = @connection.tables.sort
|
108
129
|
|
@@ -111,7 +132,7 @@ module ActiveRecord
|
|
111
132
|
end
|
112
133
|
|
113
134
|
# dump foreign keys at the end to make sure all dependent tables exist.
|
114
|
-
if @connection.
|
135
|
+
if @connection.use_foreign_keys?
|
115
136
|
sorted_tables.each do |tbl|
|
116
137
|
foreign_keys(tbl, stream) unless ignored?(tbl)
|
117
138
|
end
|
@@ -171,12 +192,13 @@ module ActiveRecord
|
|
171
192
|
|
172
193
|
indexes_in_create(table, tbl)
|
173
194
|
check_constraints_in_create(table, tbl) if @connection.supports_check_constraints?
|
195
|
+
exclusion_constraints_in_create(table, tbl) if @connection.supports_exclusion_constraints?
|
196
|
+
unique_constraints_in_create(table, tbl) if @connection.supports_unique_constraints?
|
174
197
|
|
175
198
|
tbl.puts " end"
|
176
199
|
tbl.puts
|
177
200
|
|
178
|
-
tbl.
|
179
|
-
stream.print tbl.read
|
201
|
+
stream.print tbl.string
|
180
202
|
rescue => e
|
181
203
|
stream.puts "# Could not dump table #{table.inspect} because of following #{e.class}"
|
182
204
|
stream.puts "# #{e.message}"
|
@@ -201,6 +223,18 @@ module ActiveRecord
|
|
201
223
|
|
202
224
|
def indexes_in_create(table, stream)
|
203
225
|
if (indexes = @connection.indexes(table)).any?
|
226
|
+
if @connection.supports_exclusion_constraints? && (exclusion_constraints = @connection.exclusion_constraints(table)).any?
|
227
|
+
exclusion_constraint_names = exclusion_constraints.collect(&:name)
|
228
|
+
|
229
|
+
indexes = indexes.reject { |index| exclusion_constraint_names.include?(index.name) }
|
230
|
+
end
|
231
|
+
|
232
|
+
if @connection.supports_unique_constraints? && (unique_constraints = @connection.unique_constraints(table)).any?
|
233
|
+
unique_constraint_names = unique_constraints.collect(&:name)
|
234
|
+
|
235
|
+
indexes = indexes.reject { |index| unique_constraint_names.include?(index.name) }
|
236
|
+
end
|
237
|
+
|
204
238
|
index_statements = indexes.map do |index|
|
205
239
|
" t.index #{index_parts(index).join(', ')}"
|
206
240
|
end
|
@@ -219,6 +253,8 @@ module ActiveRecord
|
|
219
253
|
index_parts << "opclass: #{format_index_parts(index.opclasses)}" if index.opclasses.present?
|
220
254
|
index_parts << "where: #{index.where.inspect}" if index.where
|
221
255
|
index_parts << "using: #{index.using.inspect}" if !@connection.default_index_type?(index)
|
256
|
+
index_parts << "include: #{index.include.inspect}" if index.include
|
257
|
+
index_parts << "nulls_not_distinct: #{index.nulls_not_distinct.inspect}" if index.nulls_not_distinct
|
222
258
|
index_parts << "type: #{index.type.inspect}" if index.type
|
223
259
|
index_parts << "comment: #{index.comment.inspect}" if index.comment
|
224
260
|
index_parts
|
@@ -235,6 +271,8 @@ module ActiveRecord
|
|
235
271
|
parts << "name: #{check_constraint.name.inspect}"
|
236
272
|
end
|
237
273
|
|
274
|
+
parts << "validate: #{check_constraint.validate?.inspect}" unless check_constraint.validate?
|
275
|
+
|
238
276
|
" #{parts.join(', ')}"
|
239
277
|
end
|
240
278
|
|
@@ -250,7 +288,7 @@ module ActiveRecord
|
|
250
288
|
remove_prefix_and_suffix(foreign_key.to_table).inspect,
|
251
289
|
]
|
252
290
|
|
253
|
-
if foreign_key.column != @connection.foreign_key_column_for(foreign_key.to_table)
|
291
|
+
if foreign_key.column != @connection.foreign_key_column_for(foreign_key.to_table, "id")
|
254
292
|
parts << "column: #{foreign_key.column.inspect}"
|
255
293
|
end
|
256
294
|
|
@@ -265,6 +303,7 @@ module ActiveRecord
|
|
265
303
|
parts << "on_update: #{foreign_key.on_update.inspect}" if foreign_key.on_update
|
266
304
|
parts << "on_delete: #{foreign_key.on_delete.inspect}" if foreign_key.on_delete
|
267
305
|
parts << "deferrable: #{foreign_key.deferrable.inspect}" if foreign_key.deferrable
|
306
|
+
parts << "validate: #{foreign_key.validate?.inspect}" unless foreign_key.validate?
|
268
307
|
|
269
308
|
" #{parts.join(', ')}"
|
270
309
|
end
|
@@ -302,7 +341,7 @@ module ActiveRecord
|
|
302
341
|
end
|
303
342
|
|
304
343
|
def ignored?(table_name)
|
305
|
-
|
344
|
+
@ignore_tables.any? do |ignored|
|
306
345
|
ignored === remove_prefix_and_suffix(table_name)
|
307
346
|
end
|
308
347
|
end
|
@@ -1,54 +1,89 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require "active_record/scoping/default"
|
4
|
-
require "active_record/scoping/named"
|
5
|
-
|
6
3
|
module ActiveRecord
|
7
4
|
# This class is used to create a table that keeps track of which migrations
|
8
5
|
# have been applied to a given database. When a migration is run, its schema
|
9
|
-
# number is inserted in to the
|
6
|
+
# number is inserted in to the schema migrations table so it doesn't need
|
10
7
|
# to be executed the next time.
|
11
|
-
class SchemaMigration
|
12
|
-
class
|
13
|
-
|
14
|
-
|
15
|
-
|
8
|
+
class SchemaMigration # :nodoc:
|
9
|
+
class NullSchemaMigration # :nodoc:
|
10
|
+
end
|
11
|
+
|
12
|
+
attr_reader :connection, :arel_table
|
13
|
+
|
14
|
+
def initialize(connection)
|
15
|
+
@connection = connection
|
16
|
+
@arel_table = Arel::Table.new(table_name)
|
17
|
+
end
|
18
|
+
|
19
|
+
def create_version(version)
|
20
|
+
im = Arel::InsertManager.new(arel_table)
|
21
|
+
im.insert(arel_table[primary_key] => version)
|
22
|
+
connection.insert(im, "#{self.class} Create", primary_key, version)
|
23
|
+
end
|
16
24
|
|
17
|
-
|
18
|
-
|
25
|
+
def delete_version(version)
|
26
|
+
dm = Arel::DeleteManager.new(arel_table)
|
27
|
+
dm.wheres = [arel_table[primary_key].eq(version)]
|
28
|
+
|
29
|
+
connection.delete(dm, "#{self.class} Destroy")
|
30
|
+
end
|
31
|
+
|
32
|
+
def delete_all_versions
|
33
|
+
versions.each do |version|
|
34
|
+
delete_version(version)
|
19
35
|
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def primary_key
|
39
|
+
"version"
|
40
|
+
end
|
41
|
+
|
42
|
+
def table_name
|
43
|
+
"#{ActiveRecord::Base.table_name_prefix}#{ActiveRecord::Base.schema_migrations_table_name}#{ActiveRecord::Base.table_name_suffix}"
|
44
|
+
end
|
20
45
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
end
|
46
|
+
def create_table
|
47
|
+
unless connection.table_exists?(table_name)
|
48
|
+
connection.create_table(table_name, id: false) do |t|
|
49
|
+
t.string :version, **connection.internal_string_options_for_primary_key
|
26
50
|
end
|
27
51
|
end
|
52
|
+
end
|
28
53
|
|
29
|
-
|
30
|
-
|
31
|
-
|
54
|
+
def drop_table
|
55
|
+
connection.drop_table table_name, if_exists: true
|
56
|
+
end
|
32
57
|
|
33
|
-
|
34
|
-
|
35
|
-
|
58
|
+
def normalize_migration_number(number)
|
59
|
+
"%.3d" % number.to_i
|
60
|
+
end
|
36
61
|
|
37
|
-
|
38
|
-
|
39
|
-
|
62
|
+
def normalized_versions
|
63
|
+
versions.map { |v| normalize_migration_number v }
|
64
|
+
end
|
40
65
|
|
41
|
-
|
42
|
-
|
43
|
-
|
66
|
+
def versions
|
67
|
+
sm = Arel::SelectManager.new(arel_table)
|
68
|
+
sm.project(arel_table[primary_key])
|
69
|
+
sm.order(arel_table[primary_key].asc)
|
44
70
|
|
45
|
-
|
46
|
-
|
47
|
-
|
71
|
+
connection.select_values(sm, "#{self.class} Load")
|
72
|
+
end
|
73
|
+
|
74
|
+
def integer_versions
|
75
|
+
versions.map(&:to_i)
|
76
|
+
end
|
77
|
+
|
78
|
+
def count
|
79
|
+
sm = Arel::SelectManager.new(arel_table)
|
80
|
+
sm.project(*Arel::Nodes::Count.new([Arel.star]))
|
81
|
+
|
82
|
+
connection.select_values(sm, "#{self.class} Count").first
|
48
83
|
end
|
49
84
|
|
50
|
-
def
|
51
|
-
|
85
|
+
def table_exists?
|
86
|
+
connection.data_source_exists?(table_name)
|
52
87
|
end
|
53
88
|
end
|
54
89
|
end
|
@@ -24,14 +24,22 @@ module ActiveRecord
|
|
24
24
|
# Returns a scope for the model without the previously set scopes.
|
25
25
|
#
|
26
26
|
# class Post < ActiveRecord::Base
|
27
|
+
# belongs_to :user
|
28
|
+
#
|
27
29
|
# def self.default_scope
|
28
30
|
# where(published: true)
|
29
31
|
# end
|
30
32
|
# end
|
31
33
|
#
|
34
|
+
# class User < ActiveRecord::Base
|
35
|
+
# has_many :posts
|
36
|
+
# end
|
37
|
+
#
|
32
38
|
# Post.all # Fires "SELECT * FROM posts WHERE published = true"
|
33
39
|
# Post.unscoped.all # Fires "SELECT * FROM posts"
|
34
40
|
# Post.where(published: false).unscoped.all # Fires "SELECT * FROM posts"
|
41
|
+
# User.find(1).posts # Fires "SELECT * FROM posts WHERE published = true AND posts.user_id = 1"
|
42
|
+
# User.find(1).posts.unscoped # Fires "SELECT * FROM posts"
|
35
43
|
#
|
36
44
|
# This method also accepts a block. All queries inside the block will
|
37
45
|
# not use the previously set scopes.
|
@@ -67,7 +75,8 @@ module ActiveRecord
|
|
67
75
|
# default_scope { where(published: true) }
|
68
76
|
# end
|
69
77
|
#
|
70
|
-
# Article.all
|
78
|
+
# Article.all
|
79
|
+
# # SELECT * FROM articles WHERE published = true
|
71
80
|
#
|
72
81
|
# The #default_scope is also applied while creating/building a record.
|
73
82
|
# It is not applied while updating or deleting a record.
|
@@ -88,7 +97,7 @@ module ActiveRecord
|
|
88
97
|
# queries that return a single object by primary key.
|
89
98
|
#
|
90
99
|
# Article.find(1).destroy
|
91
|
-
#
|
100
|
+
# # DELETE ... FROM `articles` where ID = 1 AND blog_id = 1;
|
92
101
|
#
|
93
102
|
# (You can also pass any object which responds to +call+ to the
|
94
103
|
# +default_scope+ macro, and it will be called when building the
|
@@ -102,7 +111,8 @@ module ActiveRecord
|
|
102
111
|
# default_scope { where(rating: 'G') }
|
103
112
|
# end
|
104
113
|
#
|
105
|
-
# Article.all
|
114
|
+
# Article.all
|
115
|
+
# # SELECT * FROM articles WHERE published = true AND rating = 'G'
|
106
116
|
#
|
107
117
|
# This is also the case with inheritance and module includes where the
|
108
118
|
# parent or module defines a #default_scope and the child or including
|
@@ -162,8 +172,8 @@ module ActiveRecord
|
|
162
172
|
# If all_queries is nil, only execute on select and insert queries.
|
163
173
|
#
|
164
174
|
# If all_queries is true, check if the default_scope object has
|
165
|
-
# all_queries set, then execute on all queries; select, insert, update
|
166
|
-
# and
|
175
|
+
# all_queries set, then execute on all queries; select, insert, update,
|
176
|
+
# delete, and reload.
|
167
177
|
def execute_scope?(all_queries, default_scope_obj)
|
168
178
|
all_queries.nil? || all_queries && default_scope_obj.all_queries
|
169
179
|
end
|
@@ -19,7 +19,7 @@ module ActiveRecord
|
|
19
19
|
#
|
20
20
|
# You can define a scope that applies to all finders using
|
21
21
|
# {default_scope}[rdoc-ref:Scoping::Default::ClassMethods#default_scope].
|
22
|
-
def all
|
22
|
+
def all(all_queries: nil)
|
23
23
|
scope = current_scope
|
24
24
|
|
25
25
|
if scope
|
@@ -29,7 +29,7 @@ module ActiveRecord
|
|
29
29
|
relation.merge!(scope)
|
30
30
|
end
|
31
31
|
else
|
32
|
-
default_scoped
|
32
|
+
default_scoped(all_queries: all_queries)
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
@@ -119,11 +119,12 @@ module ActiveRecord
|
|
119
119
|
return scope_type[model.name] if skip_inherited_scope
|
120
120
|
klass = model
|
121
121
|
base = model.base_class
|
122
|
-
while klass
|
122
|
+
while klass != base
|
123
123
|
value = scope_type[klass.name]
|
124
124
|
return value if value
|
125
125
|
klass = klass.superclass
|
126
126
|
end
|
127
|
+
scope_type[klass.name]
|
127
128
|
end
|
128
129
|
|
129
130
|
# Sets the +value+ for a given +scope_type+ and +model+.
|
@@ -0,0 +1,60 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module SecurePassword
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
include ActiveModel::SecurePassword
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
# Given a set of attributes, finds a record using the non-password
|
11
|
+
# attributes, and then authenticates that record using the password
|
12
|
+
# attributes. Returns the record if authentication succeeds; otherwise,
|
13
|
+
# returns +nil+.
|
14
|
+
#
|
15
|
+
# Regardless of whether a record is found, +authenticate_by+ will
|
16
|
+
# cryptographically digest the given password attributes. This behavior
|
17
|
+
# helps mitigate timing-based enumeration attacks, wherein an attacker can
|
18
|
+
# determine if a passworded record exists even without knowing the
|
19
|
+
# password.
|
20
|
+
#
|
21
|
+
# Raises an ArgumentError if the set of attributes doesn't contain at
|
22
|
+
# least one password and one non-password attribute.
|
23
|
+
#
|
24
|
+
# ==== Examples
|
25
|
+
#
|
26
|
+
# class User < ActiveRecord::Base
|
27
|
+
# has_secure_password
|
28
|
+
# end
|
29
|
+
#
|
30
|
+
# User.create(name: "John Doe", email: "jdoe@example.com", password: "abc123")
|
31
|
+
#
|
32
|
+
# User.authenticate_by(email: "jdoe@example.com", password: "abc123").name # => "John Doe" (in 373.4ms)
|
33
|
+
# User.authenticate_by(email: "jdoe@example.com", password: "wrong") # => nil (in 373.9ms)
|
34
|
+
# User.authenticate_by(email: "wrong@example.com", password: "abc123") # => nil (in 373.6ms)
|
35
|
+
#
|
36
|
+
# User.authenticate_by(email: "jdoe@example.com", password: nil) # => nil (no queries executed)
|
37
|
+
# User.authenticate_by(email: "jdoe@example.com", password: "") # => nil (no queries executed)
|
38
|
+
#
|
39
|
+
# User.authenticate_by(email: "jdoe@example.com") # => ArgumentError
|
40
|
+
# User.authenticate_by(password: "abc123") # => ArgumentError
|
41
|
+
def authenticate_by(attributes)
|
42
|
+
passwords, identifiers = attributes.to_h.partition do |name, value|
|
43
|
+
!has_attribute?(name) && has_attribute?("#{name}_digest")
|
44
|
+
end.map(&:to_h)
|
45
|
+
|
46
|
+
raise ArgumentError, "One or more password arguments are required" if passwords.empty?
|
47
|
+
raise ArgumentError, "One or more finder arguments are required" if identifiers.empty?
|
48
|
+
|
49
|
+
return if passwords.any? { |name, value| value.nil? || value.empty? }
|
50
|
+
|
51
|
+
if record = find_by(identifiers)
|
52
|
+
record if passwords.count { |name, value| record.public_send(:"authenticate_#{name}", value) } == passwords.size
|
53
|
+
else
|
54
|
+
new(passwords)
|
55
|
+
nil
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -24,12 +24,26 @@ module ActiveRecord
|
|
24
24
|
# user.regenerate_token # => true
|
25
25
|
# user.regenerate_auth_token # => true
|
26
26
|
#
|
27
|
-
#
|
27
|
+
# +SecureRandom::base58+ is used to generate at minimum a 24-character unique token, so collisions are highly unlikely.
|
28
28
|
#
|
29
29
|
# Note that it's still possible to generate a race condition in the database in the same way that
|
30
30
|
# {validates_uniqueness_of}[rdoc-ref:Validations::ClassMethods#validates_uniqueness_of] can.
|
31
31
|
# You're encouraged to add a unique index in the database to deal with this even more unlikely scenario.
|
32
|
-
|
32
|
+
#
|
33
|
+
# === Options
|
34
|
+
#
|
35
|
+
# [:length]
|
36
|
+
# Length of the Secure Random, with a minimum of 24 characters. It will
|
37
|
+
# default to 24.
|
38
|
+
#
|
39
|
+
# [:on]
|
40
|
+
# The callback when the value is generated. When called with <tt>on:
|
41
|
+
# :initialize</tt>, the value is generated in an
|
42
|
+
# <tt>after_initialize</tt> callback, otherwise the value will be used
|
43
|
+
# in a <tt>before_</tt> callback. When not specified, +:on+ will use the value of
|
44
|
+
# <tt>config.active_record.generate_secure_token_on</tt>, which defaults to +:initialize+
|
45
|
+
# starting in \Rails 7.1.
|
46
|
+
def has_secure_token(attribute = :token, length: MINIMUM_TOKEN_LENGTH, on: ActiveRecord.generate_secure_token_on)
|
33
47
|
if length < MINIMUM_TOKEN_LENGTH
|
34
48
|
raise MinimumLengthError, "Token requires a minimum length of #{MINIMUM_TOKEN_LENGTH} characters."
|
35
49
|
end
|
@@ -37,7 +51,11 @@ module ActiveRecord
|
|
37
51
|
# Load securerandom only when has_secure_token is used.
|
38
52
|
require "active_support/core_ext/securerandom"
|
39
53
|
define_method("regenerate_#{attribute}") { update! attribute => self.class.generate_unique_secure_token(length: length) }
|
40
|
-
|
54
|
+
set_callback on, on == :initialize ? :after : :before do
|
55
|
+
if new_record? && !query_attribute(attribute)
|
56
|
+
send("#{attribute}=", self.class.generate_unique_secure_token(length: length))
|
57
|
+
end
|
58
|
+
end
|
41
59
|
end
|
42
60
|
|
43
61
|
def generate_unique_secure_token(length: MINIMUM_TOKEN_LENGTH)
|
@@ -8,8 +8,8 @@ module ActiveRecord
|
|
8
8
|
included do
|
9
9
|
##
|
10
10
|
# :singleton-method:
|
11
|
-
# Set the secret used for the signed id verifier instance when using Active Record outside of Rails.
|
12
|
-
# Within Rails, this is automatically set using the Rails application key generator.
|
11
|
+
# Set the secret used for the signed id verifier instance when using Active Record outside of \Rails.
|
12
|
+
# Within \Rails, this is automatically set using the \Rails application key generator.
|
13
13
|
class_attribute :signed_id_verifier_secret, instance_writer: false
|
14
14
|
end
|
15
15
|
|
@@ -66,7 +66,7 @@ module ActiveRecord
|
|
66
66
|
end
|
67
67
|
|
68
68
|
# The verifier instance that all signed ids are generated and verified from. By default, it'll be initialized
|
69
|
-
# with the class-level +signed_id_verifier_secret+, which within Rails comes from the
|
69
|
+
# with the class-level +signed_id_verifier_secret+, which within \Rails comes from the
|
70
70
|
# Rails.application.key_generator. By default, it's SHA256 for the digest and JSON for the serialization.
|
71
71
|
def signed_id_verifier
|
72
72
|
@signed_id_verifier ||= begin
|
@@ -109,8 +109,10 @@ module ActiveRecord
|
|
109
109
|
#
|
110
110
|
# And you then change your +find_signed+ calls to require this new purpose. Any old signed ids that were not
|
111
111
|
# created with the purpose will no longer find the record.
|
112
|
-
def signed_id(expires_in: nil, purpose: nil)
|
113
|
-
|
112
|
+
def signed_id(expires_in: nil, expires_at: nil, purpose: nil)
|
113
|
+
raise ArgumentError, "Cannot get a signed_id for a new record" if new_record?
|
114
|
+
|
115
|
+
self.class.signed_id_verifier.generate id, expires_in: expires_in, expires_at: expires_at, purpose: self.class.combine_signed_id_purposes(purpose)
|
114
116
|
end
|
115
117
|
end
|
116
118
|
end
|
data/lib/active_record/store.rb
CHANGED
@@ -3,6 +3,8 @@
|
|
3
3
|
require "active_support/core_ext/hash/indifferent_access"
|
4
4
|
|
5
5
|
module ActiveRecord
|
6
|
+
# = Active Record \Store
|
7
|
+
#
|
6
8
|
# Store gives you a thin wrapper around serialize for the purpose of storing hashes in a single column.
|
7
9
|
# It's like a simple key/value store baked into your record when you don't care about being able to
|
8
10
|
# query that store outside the context of a single record.
|
@@ -102,7 +104,8 @@ module ActiveRecord
|
|
102
104
|
|
103
105
|
module ClassMethods
|
104
106
|
def store(store_attribute, options = {})
|
105
|
-
|
107
|
+
coder = build_column_serializer(store_attribute, options[:coder], Object, options[:yaml])
|
108
|
+
serialize store_attribute, coder: IndifferentCoder.new(store_attribute, coder)
|
106
109
|
store_accessor(store_attribute, options[:accessors], **options.slice(:prefix, :suffix)) if options.has_key? :accessors
|
107
110
|
end
|
108
111
|
|
@@ -160,19 +163,19 @@ module ActiveRecord
|
|
160
163
|
|
161
164
|
define_method("saved_change_to_#{accessor_key}?") do
|
162
165
|
return false unless saved_change_to_attribute?(store_attribute)
|
163
|
-
prev_store, new_store =
|
166
|
+
prev_store, new_store = saved_changes[store_attribute]
|
164
167
|
prev_store&.dig(key) != new_store&.dig(key)
|
165
168
|
end
|
166
169
|
|
167
170
|
define_method("saved_change_to_#{accessor_key}") do
|
168
171
|
return unless saved_change_to_attribute?(store_attribute)
|
169
|
-
prev_store, new_store =
|
172
|
+
prev_store, new_store = saved_changes[store_attribute]
|
170
173
|
[prev_store&.dig(key), new_store&.dig(key)]
|
171
174
|
end
|
172
175
|
|
173
176
|
define_method("#{accessor_key}_before_last_save") do
|
174
177
|
return unless saved_change_to_attribute?(store_attribute)
|
175
|
-
prev_store, _new_store =
|
178
|
+
prev_store, _new_store = saved_changes[store_attribute]
|
176
179
|
prev_store&.dig(key)
|
177
180
|
end
|
178
181
|
end
|
@@ -225,10 +228,7 @@ module ActiveRecord
|
|
225
228
|
|
226
229
|
def self.write(object, attribute, key, value)
|
227
230
|
prepare(object, attribute)
|
228
|
-
if value != read(object, attribute, key)
|
229
|
-
object.public_send :"#{attribute}_will_change!"
|
230
|
-
object.public_send(attribute)[key] = value
|
231
|
-
end
|
231
|
+
object.public_send(attribute)[key] = value if value != read(object, attribute, key)
|
232
232
|
end
|
233
233
|
|
234
234
|
def self.prepare(object, attribute)
|
@@ -1,6 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module ActiveRecord
|
4
|
+
# = Active Record \Suppressor
|
5
|
+
#
|
4
6
|
# ActiveRecord::Suppressor prevents the receiver from being saved during
|
5
7
|
# a given block.
|
6
8
|
#
|
@@ -32,7 +34,7 @@ module ActiveRecord
|
|
32
34
|
|
33
35
|
class << self
|
34
36
|
def registry # :nodoc:
|
35
|
-
ActiveSupport::IsolatedExecutionState[:
|
37
|
+
ActiveSupport::IsolatedExecutionState[:active_record_suppressor_registry] ||= {}
|
36
38
|
end
|
37
39
|
end
|
38
40
|
|
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
module ActiveRecord
|
4
4
|
class TableMetadata # :nodoc:
|
5
|
-
delegate :join_primary_key, :join_foreign_key, :join_foreign_type, to: :reflection
|
5
|
+
delegate :join_primary_key, :join_primary_type, :join_foreign_key, :join_foreign_type, to: :reflection
|
6
6
|
|
7
7
|
def initialize(klass, arel_table, reflection = nil)
|
8
8
|
@klass = klass
|
@@ -19,11 +19,20 @@ module ActiveRecord
|
|
19
19
|
end
|
20
20
|
|
21
21
|
def has_column?(column_name)
|
22
|
-
klass&.columns_hash
|
22
|
+
klass&.columns_hash&.key?(column_name)
|
23
23
|
end
|
24
24
|
|
25
25
|
def associated_with?(table_name)
|
26
|
-
|
26
|
+
if reflection = klass&._reflect_on_association(table_name)
|
27
|
+
reflection
|
28
|
+
elsif ActiveRecord.allow_deprecated_singular_associations_name && reflection = klass&._reflect_on_association(table_name.singularize)
|
29
|
+
ActiveRecord.deprecator.warn(<<~MSG)
|
30
|
+
Referring to a singular association (e.g. `#{reflection.name}`) by its plural name (e.g. `#{reflection.plural_name}`) is deprecated.
|
31
|
+
|
32
|
+
To convert this deprecation warning to an error and enable more performant behavior, set config.active_record.allow_deprecated_singular_associations_name = false.
|
33
|
+
MSG
|
34
|
+
reflection
|
35
|
+
end
|
27
36
|
end
|
28
37
|
|
29
38
|
def associated_table(table_name)
|
@@ -54,6 +63,10 @@ module ActiveRecord
|
|
54
63
|
reflection&.polymorphic?
|
55
64
|
end
|
56
65
|
|
66
|
+
def polymorphic_name_association
|
67
|
+
reflection&.polymorphic_name
|
68
|
+
end
|
69
|
+
|
57
70
|
def through_association?
|
58
71
|
reflection&.through_reflection?
|
59
72
|
end
|