activerecord 5.1.7 → 5.2.6
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +583 -673
- data/MIT-LICENSE +1 -1
- data/README.rdoc +5 -5
- data/examples/performance.rb +2 -0
- data/examples/simple.rb +2 -0
- data/lib/active_record/aggregations.rb +6 -5
- data/lib/active_record/association_relation.rb +7 -5
- data/lib/active_record/associations/alias_tracker.rb +19 -27
- data/lib/active_record/associations/association.rb +41 -37
- data/lib/active_record/associations/association_scope.rb +38 -50
- data/lib/active_record/associations/belongs_to_association.rb +27 -8
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +8 -8
- data/lib/active_record/associations/builder/association.rb +4 -7
- data/lib/active_record/associations/builder/belongs_to.rb +12 -4
- data/lib/active_record/associations/builder/collection_association.rb +3 -3
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +3 -1
- data/lib/active_record/associations/builder/has_many.rb +2 -0
- data/lib/active_record/associations/builder/has_one.rb +2 -0
- data/lib/active_record/associations/builder/singular_association.rb +2 -0
- data/lib/active_record/associations/collection_association.rb +59 -47
- data/lib/active_record/associations/collection_proxy.rb +20 -49
- data/lib/active_record/associations/foreign_association.rb +2 -0
- data/lib/active_record/associations/has_many_association.rb +12 -1
- data/lib/active_record/associations/has_many_through_association.rb +36 -30
- data/lib/active_record/associations/has_one_association.rb +12 -1
- data/lib/active_record/associations/has_one_through_association.rb +13 -8
- data/lib/active_record/associations/join_dependency/join_association.rb +39 -63
- data/lib/active_record/associations/join_dependency/join_base.rb +9 -8
- data/lib/active_record/associations/join_dependency/join_part.rb +9 -9
- data/lib/active_record/associations/join_dependency.rb +48 -93
- data/lib/active_record/associations/preloader/association.rb +45 -61
- data/lib/active_record/associations/preloader/through_association.rb +71 -79
- data/lib/active_record/associations/preloader.rb +18 -38
- data/lib/active_record/associations/singular_association.rb +14 -16
- data/lib/active_record/associations/through_association.rb +26 -11
- data/lib/active_record/associations.rb +40 -63
- data/lib/active_record/attribute_assignment.rb +2 -5
- data/lib/active_record/attribute_decorators.rb +3 -2
- data/lib/active_record/attribute_methods/before_type_cast.rb +2 -0
- data/lib/active_record/attribute_methods/dirty.rb +30 -214
- data/lib/active_record/attribute_methods/primary_key.rb +7 -6
- data/lib/active_record/attribute_methods/query.rb +2 -0
- data/lib/active_record/attribute_methods/read.rb +9 -3
- data/lib/active_record/attribute_methods/serialization.rb +23 -0
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +6 -8
- data/lib/active_record/attribute_methods/write.rb +21 -9
- data/lib/active_record/attribute_methods.rb +65 -24
- data/lib/active_record/attributes.rb +6 -5
- data/lib/active_record/autosave_association.rb +35 -19
- data/lib/active_record/base.rb +2 -0
- data/lib/active_record/callbacks.rb +8 -6
- data/lib/active_record/coders/json.rb +2 -0
- data/lib/active_record/coders/yaml_column.rb +2 -0
- data/lib/active_record/collection_cache_key.rb +12 -8
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +139 -41
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +7 -0
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +174 -33
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +15 -5
- data/lib/active_record/connection_adapters/abstract/quoting.rb +13 -31
- data/lib/active_record/connection_adapters/abstract/savepoints.rb +2 -0
- data/lib/active_record/connection_adapters/abstract/schema_creation.rb +14 -5
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +64 -6
- data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +31 -53
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +152 -81
- data/lib/active_record/connection_adapters/abstract/transaction.rb +66 -21
- data/lib/active_record/connection_adapters/abstract_adapter.rb +84 -97
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +92 -165
- data/lib/active_record/connection_adapters/column.rb +3 -1
- data/lib/active_record/connection_adapters/connection_specification.rb +17 -3
- data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +13 -2
- data/lib/active_record/connection_adapters/mysql/column.rb +2 -0
- data/lib/active_record/connection_adapters/mysql/database_statements.rb +47 -2
- data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +2 -0
- data/lib/active_record/connection_adapters/mysql/quoting.rb +9 -10
- data/lib/active_record/connection_adapters/mysql/schema_creation.rb +5 -3
- data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +7 -10
- data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +30 -30
- data/lib/active_record/connection_adapters/mysql/schema_statements.rb +106 -1
- data/lib/active_record/connection_adapters/mysql/type_metadata.rb +2 -0
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +8 -2
- data/lib/active_record/connection_adapters/postgresql/column.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +6 -0
- data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/array.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/date.rb +23 -0
- data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/inet.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +3 -1
- data/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/money.rb +5 -3
- data/lib/active_record/connection_adapters/postgresql/oid/oid.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/point.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/range.rb +4 -2
- data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +4 -2
- data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +3 -1
- data/lib/active_record/connection_adapters/postgresql/oid/vector.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/xml.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid.rb +3 -1
- data/lib/active_record/connection_adapters/postgresql/quoting.rb +18 -0
- data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +19 -25
- data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +50 -0
- data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +24 -11
- data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +20 -13
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +233 -111
- data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/utils.rb +3 -1
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +57 -73
- data/lib/active_record/connection_adapters/schema_cache.rb +4 -2
- data/lib/active_record/connection_adapters/sql_type_metadata.rb +2 -0
- data/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb +2 -0
- data/lib/active_record/connection_adapters/sqlite3/quoting.rb +22 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +2 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +6 -15
- data/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb +3 -2
- data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +75 -1
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +81 -94
- data/lib/active_record/connection_adapters/statement_pool.rb +2 -0
- data/lib/active_record/connection_handling.rb +4 -2
- data/lib/active_record/core.rb +41 -61
- data/lib/active_record/counter_cache.rb +10 -3
- data/lib/active_record/define_callbacks.rb +5 -3
- data/lib/active_record/dynamic_matchers.rb +9 -9
- data/lib/active_record/enum.rb +18 -13
- data/lib/active_record/errors.rb +42 -3
- data/lib/active_record/explain.rb +3 -1
- data/lib/active_record/explain_registry.rb +2 -0
- data/lib/active_record/explain_subscriber.rb +2 -0
- data/lib/active_record/fixture_set/file.rb +2 -0
- data/lib/active_record/fixtures.rb +67 -60
- data/lib/active_record/gem_version.rb +4 -2
- data/lib/active_record/inheritance.rb +49 -19
- data/lib/active_record/integration.rb +58 -19
- data/lib/active_record/internal_metadata.rb +2 -0
- data/lib/active_record/legacy_yaml_adapter.rb +3 -1
- data/lib/active_record/locking/optimistic.rb +14 -17
- data/lib/active_record/locking/pessimistic.rb +9 -6
- data/lib/active_record/log_subscriber.rb +43 -0
- data/lib/active_record/migration/command_recorder.rb +11 -9
- data/lib/active_record/migration/compatibility.rb +47 -9
- data/lib/active_record/migration/join_table.rb +2 -0
- data/lib/active_record/migration.rb +189 -139
- data/lib/active_record/model_schema.rb +16 -21
- data/lib/active_record/nested_attributes.rb +18 -6
- data/lib/active_record/no_touching.rb +3 -1
- data/lib/active_record/null_relation.rb +2 -0
- data/lib/active_record/persistence.rb +167 -16
- data/lib/active_record/query_cache.rb +6 -8
- data/lib/active_record/querying.rb +4 -2
- data/lib/active_record/railtie.rb +62 -6
- data/lib/active_record/railties/console_sandbox.rb +2 -0
- data/lib/active_record/railties/controller_runtime.rb +2 -0
- data/lib/active_record/railties/databases.rake +46 -36
- data/lib/active_record/readonly_attributes.rb +3 -2
- data/lib/active_record/reflection.rb +108 -194
- data/lib/active_record/relation/batches/batch_enumerator.rb +2 -0
- data/lib/active_record/relation/batches.rb +20 -5
- data/lib/active_record/relation/calculations.rb +45 -19
- data/lib/active_record/relation/delegation.rb +45 -27
- data/lib/active_record/relation/finder_methods.rb +75 -76
- data/lib/active_record/relation/from_clause.rb +2 -8
- data/lib/active_record/relation/merger.rb +53 -23
- data/lib/active_record/relation/predicate_builder/array_handler.rb +10 -7
- data/lib/active_record/relation/predicate_builder/association_query_value.rb +46 -0
- data/lib/active_record/relation/predicate_builder/base_handler.rb +2 -2
- data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +12 -1
- data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +56 -0
- data/lib/active_record/relation/predicate_builder/range_handler.rb +26 -9
- data/lib/active_record/relation/predicate_builder/relation_handler.rb +6 -0
- data/lib/active_record/relation/predicate_builder.rb +60 -79
- data/lib/active_record/relation/query_attribute.rb +28 -2
- data/lib/active_record/relation/query_methods.rb +128 -99
- data/lib/active_record/relation/record_fetch_warning.rb +2 -0
- data/lib/active_record/relation/spawn_methods.rb +4 -2
- data/lib/active_record/relation/where_clause.rb +65 -68
- data/lib/active_record/relation/where_clause_factory.rb +5 -48
- data/lib/active_record/relation.rb +120 -214
- data/lib/active_record/result.rb +2 -0
- data/lib/active_record/runtime_registry.rb +2 -0
- data/lib/active_record/sanitization.rb +129 -121
- data/lib/active_record/schema.rb +4 -2
- data/lib/active_record/schema_dumper.rb +36 -26
- data/lib/active_record/schema_migration.rb +2 -0
- data/lib/active_record/scoping/default.rb +8 -9
- data/lib/active_record/scoping/named.rb +23 -7
- data/lib/active_record/scoping.rb +9 -8
- data/lib/active_record/secure_token.rb +2 -0
- data/lib/active_record/serialization.rb +2 -0
- data/lib/active_record/statement_cache.rb +23 -13
- data/lib/active_record/store.rb +3 -1
- data/lib/active_record/suppressor.rb +2 -0
- data/lib/active_record/table_metadata.rb +12 -3
- data/lib/active_record/tasks/database_tasks.rb +25 -14
- data/lib/active_record/tasks/mysql_database_tasks.rb +9 -48
- data/lib/active_record/tasks/postgresql_database_tasks.rb +10 -2
- data/lib/active_record/tasks/sqlite_database_tasks.rb +25 -3
- data/lib/active_record/timestamp.rb +6 -6
- data/lib/active_record/touch_later.rb +2 -0
- data/lib/active_record/transactions.rb +33 -28
- data/lib/active_record/translation.rb +2 -0
- data/lib/active_record/type/adapter_specific_registry.rb +2 -0
- data/lib/active_record/type/date.rb +2 -0
- data/lib/active_record/type/date_time.rb +2 -0
- data/lib/active_record/type/decimal_without_scale.rb +2 -0
- data/lib/active_record/type/hash_lookup_type_map.rb +2 -0
- data/lib/active_record/type/internal/timezone.rb +2 -0
- data/lib/active_record/type/json.rb +30 -0
- data/lib/active_record/type/serialized.rb +2 -0
- data/lib/active_record/type/text.rb +2 -0
- data/lib/active_record/type/time.rb +2 -0
- data/lib/active_record/type/type_map.rb +2 -0
- data/lib/active_record/type/unsigned_integer.rb +2 -0
- data/lib/active_record/type.rb +4 -1
- data/lib/active_record/type_caster/connection.rb +2 -0
- data/lib/active_record/type_caster/map.rb +3 -1
- data/lib/active_record/type_caster.rb +2 -0
- data/lib/active_record/validations/absence.rb +2 -0
- data/lib/active_record/validations/associated.rb +2 -0
- data/lib/active_record/validations/length.rb +2 -0
- data/lib/active_record/validations/presence.rb +2 -0
- data/lib/active_record/validations/uniqueness.rb +35 -5
- data/lib/active_record/validations.rb +2 -0
- data/lib/active_record/version.rb +2 -0
- data/lib/active_record.rb +11 -4
- data/lib/rails/generators/active_record/application_record/application_record_generator.rb +27 -0
- data/lib/rails/generators/active_record/{model/templates/application_record.rb → application_record/templates/application_record.rb.tt} +0 -0
- data/lib/rails/generators/active_record/migration/migration_generator.rb +3 -1
- data/lib/rails/generators/active_record/migration/templates/{create_table_migration.rb → create_table_migration.rb.tt} +0 -0
- data/lib/rails/generators/active_record/migration/templates/{migration.rb → migration.rb.tt} +0 -0
- data/lib/rails/generators/active_record/migration.rb +2 -0
- data/lib/rails/generators/active_record/model/model_generator.rb +2 -23
- data/lib/rails/generators/active_record/model/templates/{model.rb → model.rb.tt} +0 -0
- data/lib/rails/generators/active_record/model/templates/{module.rb → module.rb.tt} +0 -0
- data/lib/rails/generators/active_record.rb +3 -1
- metadata +23 -36
- data/lib/active_record/associations/preloader/belongs_to.rb +0 -15
- data/lib/active_record/associations/preloader/collection_association.rb +0 -17
- data/lib/active_record/associations/preloader/has_many.rb +0 -15
- data/lib/active_record/associations/preloader/has_many_through.rb +0 -19
- data/lib/active_record/associations/preloader/has_one.rb +0 -15
- data/lib/active_record/associations/preloader/has_one_through.rb +0 -9
- data/lib/active_record/associations/preloader/singular_association.rb +0 -18
- data/lib/active_record/attribute/user_provided_default.rb +0 -30
- data/lib/active_record/attribute.rb +0 -240
- data/lib/active_record/attribute_mutation_tracker.rb +0 -122
- data/lib/active_record/attribute_set/builder.rb +0 -126
- data/lib/active_record/attribute_set/yaml_encoder.rb +0 -41
- data/lib/active_record/attribute_set.rb +0 -113
- data/lib/active_record/connection_adapters/postgresql/oid/json.rb +0 -10
- data/lib/active_record/railties/jdbcmysql_error.rb +0 -16
- data/lib/active_record/relation/predicate_builder/association_query_handler.rb +0 -88
- data/lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb +0 -59
- data/lib/active_record/type/internal/abstract_json.rb +0 -37
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActiveRecord
|
2
4
|
# = Active Record \Persistence
|
3
5
|
module Persistence
|
@@ -69,6 +71,142 @@ module ActiveRecord
|
|
69
71
|
klass.allocate.init_with("attributes" => attributes, "new_record" => false, &block)
|
70
72
|
end
|
71
73
|
|
74
|
+
# Updates an object (or multiple objects) and saves it to the database, if validations pass.
|
75
|
+
# The resulting object is returned whether the object was saved successfully to the database or not.
|
76
|
+
#
|
77
|
+
# ==== Parameters
|
78
|
+
#
|
79
|
+
# * +id+ - This should be the id or an array of ids to be updated.
|
80
|
+
# * +attributes+ - This should be a hash of attributes or an array of hashes.
|
81
|
+
#
|
82
|
+
# ==== Examples
|
83
|
+
#
|
84
|
+
# # Updates one record
|
85
|
+
# Person.update(15, user_name: "Samuel", group: "expert")
|
86
|
+
#
|
87
|
+
# # Updates multiple records
|
88
|
+
# people = { 1 => { "first_name" => "David" }, 2 => { "first_name" => "Jeremy" } }
|
89
|
+
# Person.update(people.keys, people.values)
|
90
|
+
#
|
91
|
+
# # Updates multiple records from the result of a relation
|
92
|
+
# people = Person.where(group: "expert")
|
93
|
+
# people.update(group: "masters")
|
94
|
+
#
|
95
|
+
# Note: Updating a large number of records will run an UPDATE
|
96
|
+
# query for each record, which may cause a performance issue.
|
97
|
+
# When running callbacks is not needed for each record update,
|
98
|
+
# it is preferred to use {update_all}[rdoc-ref:Relation#update_all]
|
99
|
+
# for updating all records in a single query.
|
100
|
+
def update(id = :all, attributes)
|
101
|
+
if id.is_a?(Array)
|
102
|
+
id.map { |one_id| find(one_id) }.each_with_index { |object, idx|
|
103
|
+
object.update(attributes[idx])
|
104
|
+
}
|
105
|
+
elsif id == :all
|
106
|
+
all.each { |record| record.update(attributes) }
|
107
|
+
else
|
108
|
+
if ActiveRecord::Base === id
|
109
|
+
raise ArgumentError,
|
110
|
+
"You are passing an instance of ActiveRecord::Base to `update`. " \
|
111
|
+
"Please pass the id of the object by calling `.id`."
|
112
|
+
end
|
113
|
+
object = find(id)
|
114
|
+
object.update(attributes)
|
115
|
+
object
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
# Destroy an object (or multiple objects) that has the given id. The object is instantiated first,
|
120
|
+
# therefore all callbacks and filters are fired off before the object is deleted. This method is
|
121
|
+
# less efficient than #delete but allows cleanup methods and other actions to be run.
|
122
|
+
#
|
123
|
+
# This essentially finds the object (or multiple objects) with the given id, creates a new object
|
124
|
+
# from the attributes, and then calls destroy on it.
|
125
|
+
#
|
126
|
+
# ==== Parameters
|
127
|
+
#
|
128
|
+
# * +id+ - This should be the id or an array of ids to be destroyed.
|
129
|
+
#
|
130
|
+
# ==== Examples
|
131
|
+
#
|
132
|
+
# # Destroy a single object
|
133
|
+
# Todo.destroy(1)
|
134
|
+
#
|
135
|
+
# # Destroy multiple objects
|
136
|
+
# todos = [1,2,3]
|
137
|
+
# Todo.destroy(todos)
|
138
|
+
def destroy(id)
|
139
|
+
if id.is_a?(Array)
|
140
|
+
find(id).each(&:destroy)
|
141
|
+
else
|
142
|
+
find(id).destroy
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
# Deletes the row with a primary key matching the +id+ argument, using a
|
147
|
+
# SQL +DELETE+ statement, and returns the number of rows deleted. Active
|
148
|
+
# Record objects are not instantiated, so the object's callbacks are not
|
149
|
+
# executed, including any <tt>:dependent</tt> association options.
|
150
|
+
#
|
151
|
+
# You can delete multiple rows at once by passing an Array of <tt>id</tt>s.
|
152
|
+
#
|
153
|
+
# Note: Although it is often much faster than the alternative, #destroy,
|
154
|
+
# skipping callbacks might bypass business logic in your application
|
155
|
+
# that ensures referential integrity or performs other essential jobs.
|
156
|
+
#
|
157
|
+
# ==== Examples
|
158
|
+
#
|
159
|
+
# # Delete a single row
|
160
|
+
# Todo.delete(1)
|
161
|
+
#
|
162
|
+
# # Delete multiple rows
|
163
|
+
# Todo.delete([2,3,4])
|
164
|
+
def delete(id_or_array)
|
165
|
+
where(primary_key => id_or_array).delete_all
|
166
|
+
end
|
167
|
+
|
168
|
+
def _insert_record(values) # :nodoc:
|
169
|
+
primary_key_value = nil
|
170
|
+
|
171
|
+
if primary_key && Hash === values
|
172
|
+
primary_key_value = values[primary_key]
|
173
|
+
|
174
|
+
if !primary_key_value && prefetch_primary_key?
|
175
|
+
primary_key_value = next_sequence_value
|
176
|
+
values[primary_key] = primary_key_value
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
if values.empty?
|
181
|
+
im = arel_table.compile_insert(connection.empty_insert_statement_value)
|
182
|
+
im.into arel_table
|
183
|
+
else
|
184
|
+
im = arel_table.compile_insert(_substitute_values(values))
|
185
|
+
end
|
186
|
+
|
187
|
+
connection.insert(im, "#{self} Create", primary_key || false, primary_key_value)
|
188
|
+
end
|
189
|
+
|
190
|
+
def _update_record(values, constraints) # :nodoc:
|
191
|
+
constraints = _substitute_values(constraints).map { |attr, bind| attr.eq(bind) }
|
192
|
+
|
193
|
+
um = arel_table.where(
|
194
|
+
constraints.reduce(&:and)
|
195
|
+
).compile_update(_substitute_values(values), primary_key)
|
196
|
+
|
197
|
+
connection.update(um, "#{self} Update")
|
198
|
+
end
|
199
|
+
|
200
|
+
def _delete_record(constraints) # :nodoc:
|
201
|
+
constraints = _substitute_values(constraints).map { |attr, bind| attr.eq(bind) }
|
202
|
+
|
203
|
+
dm = Arel::DeleteManager.new
|
204
|
+
dm.from(arel_table)
|
205
|
+
dm.wheres = constraints
|
206
|
+
|
207
|
+
connection.delete(dm, "#{self} Destroy")
|
208
|
+
end
|
209
|
+
|
72
210
|
private
|
73
211
|
# Called by +instantiate+ to decide which class to use for a new
|
74
212
|
# record instance.
|
@@ -78,6 +216,14 @@ module ActiveRecord
|
|
78
216
|
def discriminate_class_for_record(record)
|
79
217
|
self
|
80
218
|
end
|
219
|
+
|
220
|
+
def _substitute_values(values)
|
221
|
+
values.map do |name, value|
|
222
|
+
attr = arel_attribute(name)
|
223
|
+
bind = predicate_builder.build_bind_attribute(name, value)
|
224
|
+
[attr, bind]
|
225
|
+
end
|
226
|
+
end
|
81
227
|
end
|
82
228
|
|
83
229
|
# Returns true if this object hasn't been saved yet -- that is, a record
|
@@ -175,7 +321,7 @@ module ActiveRecord
|
|
175
321
|
# callbacks or any <tt>:dependent</tt> association
|
176
322
|
# options, use <tt>#destroy</tt>.
|
177
323
|
def delete
|
178
|
-
|
324
|
+
_delete_row if persisted?
|
179
325
|
@destroyed = true
|
180
326
|
freeze
|
181
327
|
end
|
@@ -224,9 +370,9 @@ module ActiveRecord
|
|
224
370
|
# Any change to the attributes on either instance will affect both instances.
|
225
371
|
# If you want to change the sti column as well, use #becomes! instead.
|
226
372
|
def becomes(klass)
|
227
|
-
became = klass.
|
373
|
+
became = klass.allocate
|
374
|
+
became.send(:initialize)
|
228
375
|
became.instance_variable_set("@attributes", @attributes)
|
229
|
-
became.instance_variable_set("@mutation_tracker", @mutation_tracker ||= nil)
|
230
376
|
became.instance_variable_set("@mutations_from_database", @mutations_from_database ||= nil)
|
231
377
|
became.instance_variable_set("@changed_attributes", attributes_changed_by_setter)
|
232
378
|
became.instance_variable_set("@new_record", new_record?)
|
@@ -327,13 +473,17 @@ module ActiveRecord
|
|
327
473
|
verify_readonly_attribute(key.to_s)
|
328
474
|
end
|
329
475
|
|
330
|
-
|
331
|
-
|
476
|
+
id_in_database = self.id_in_database
|
332
477
|
attributes.each do |k, v|
|
333
|
-
|
478
|
+
write_attribute_without_type_cast(k, v)
|
334
479
|
end
|
335
480
|
|
336
|
-
|
481
|
+
affected_rows = self.class._update_record(
|
482
|
+
attributes,
|
483
|
+
self.class.primary_key => id_in_database
|
484
|
+
)
|
485
|
+
|
486
|
+
affected_rows == 1
|
337
487
|
end
|
338
488
|
|
339
489
|
# Initializes +attribute+ to zero if +nil+ and adds the value passed as +by+ (default is 1).
|
@@ -348,7 +498,7 @@ module ActiveRecord
|
|
348
498
|
# Wrapper around #increment that writes the update to the database.
|
349
499
|
# Only +attribute+ is updated; the record itself is not saved.
|
350
500
|
# This means that any other modified attributes will still be dirty.
|
351
|
-
# Validations and callbacks are skipped. Supports the
|
501
|
+
# Validations and callbacks are skipped. Supports the +touch+ option from
|
352
502
|
# +update_counters+, see that for more.
|
353
503
|
# Returns +self+.
|
354
504
|
def increment!(attribute, by = 1, touch: nil)
|
@@ -369,7 +519,7 @@ module ActiveRecord
|
|
369
519
|
# Wrapper around #decrement that writes the update to the database.
|
370
520
|
# Only +attribute+ is updated; the record itself is not saved.
|
371
521
|
# This means that any other modified attributes will still be dirty.
|
372
|
-
# Validations and callbacks are skipped. Supports the
|
522
|
+
# Validations and callbacks are skipped. Supports the +touch+ option from
|
373
523
|
# +update_counters+, see that for more.
|
374
524
|
# Returns +self+.
|
375
525
|
def decrement!(attribute, by = 1, touch: nil)
|
@@ -524,11 +674,11 @@ module ActiveRecord
|
|
524
674
|
end
|
525
675
|
|
526
676
|
def destroy_row
|
527
|
-
|
677
|
+
_delete_row
|
528
678
|
end
|
529
679
|
|
530
|
-
def
|
531
|
-
self.class.
|
680
|
+
def _delete_row
|
681
|
+
self.class._delete_record(self.class.primary_key => id_in_database)
|
532
682
|
end
|
533
683
|
|
534
684
|
def _touch_row(attribute_names, time)
|
@@ -543,14 +693,15 @@ module ActiveRecord
|
|
543
693
|
end
|
544
694
|
|
545
695
|
def _update_row(attribute_names, attempted_action = "update")
|
546
|
-
self.class.
|
547
|
-
|
696
|
+
self.class._update_record(
|
697
|
+
attributes_with_values(attribute_names),
|
548
698
|
self.class.primary_key => id_in_database
|
549
699
|
)
|
550
700
|
end
|
551
701
|
|
552
702
|
def create_or_update(*args, &block)
|
553
703
|
_raise_readonly_record_error if readonly?
|
704
|
+
return false if destroyed?
|
554
705
|
result = new_record? ? _create_record(&block) : _update_record(*args, &block)
|
555
706
|
result != false
|
556
707
|
end
|
@@ -578,9 +729,9 @@ module ActiveRecord
|
|
578
729
|
# and returns its id.
|
579
730
|
def _create_record(attribute_names = self.attribute_names)
|
580
731
|
attribute_names &= self.class.column_names
|
581
|
-
attributes_values =
|
732
|
+
attributes_values = attributes_with_values_for_create(attribute_names)
|
582
733
|
|
583
|
-
new_id = self.class.
|
734
|
+
new_id = self.class._insert_record(attributes_values)
|
584
735
|
self.id ||= new_id if self.class.primary_key
|
585
736
|
|
586
737
|
@new_record = false
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActiveRecord
|
2
4
|
# = Active Record Query Cache
|
3
5
|
class QueryCache
|
@@ -24,16 +26,12 @@ module ActiveRecord
|
|
24
26
|
end
|
25
27
|
|
26
28
|
def self.run
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
caching_pool.enable_query_cache!
|
31
|
-
|
32
|
-
[caching_pool, caching_was_enabled]
|
29
|
+
ActiveRecord::Base.connection_handler.connection_pool_list.
|
30
|
+
reject { |p| p.query_cache_enabled }.each { |p| p.enable_query_cache! }
|
33
31
|
end
|
34
32
|
|
35
|
-
def self.complete(
|
36
|
-
|
33
|
+
def self.complete(pools)
|
34
|
+
pools.each { |pool| pool.disable_query_cache! }
|
37
35
|
|
38
36
|
ActiveRecord::Base.connection_handler.connection_pool_list.each do |pool|
|
39
37
|
pool.release_connection if pool.active_connection? && !pool.connection.transaction_open?
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActiveRecord
|
2
4
|
module Querying
|
3
5
|
delegate :find, :take, :take!, :first, :first!, :last, :last!, :exists?, :any?, :many?, :none?, :one?, to: :all
|
@@ -5,7 +7,7 @@ module ActiveRecord
|
|
5
7
|
delegate :first_or_create, :first_or_create!, :first_or_initialize, to: :all
|
6
8
|
delegate :find_or_create_by, :find_or_create_by!, :find_or_initialize_by, to: :all
|
7
9
|
delegate :find_by, :find_by!, to: :all
|
8
|
-
delegate :
|
10
|
+
delegate :destroy_all, :delete_all, :update_all, to: :all
|
9
11
|
delegate :find_each, :find_in_batches, :in_batches, to: :all
|
10
12
|
delegate :select, :group, :order, :except, :reorder, :limit, :offset, :joins, :left_joins, :left_outer_joins, :or,
|
11
13
|
:where, :rewhere, :preload, :eager_load, :includes, :from, :lock, :readonly, :extending,
|
@@ -38,7 +40,7 @@ module ActiveRecord
|
|
38
40
|
def find_by_sql(sql, binds = [], preparable: nil, &block)
|
39
41
|
result_set = connection.select_all(sanitize_sql(sql), "#{name} Load", binds, preparable: preparable)
|
40
42
|
column_types = result_set.column_types.dup
|
41
|
-
|
43
|
+
attribute_types.each_key { |k| column_types.delete k }
|
42
44
|
message_bus = ActiveSupport::Notifications.instrumenter
|
43
45
|
|
44
46
|
payload = {
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "active_record"
|
2
4
|
require "rails"
|
3
5
|
require "active_model/railtie"
|
@@ -26,6 +28,9 @@ module ActiveRecord
|
|
26
28
|
config.active_record.use_schema_cache_dump = true
|
27
29
|
config.active_record.maintain_test_schema = true
|
28
30
|
|
31
|
+
config.active_record.sqlite3 = ActiveSupport::OrderedOptions.new
|
32
|
+
config.active_record.sqlite3.represent_boolean_as_integer = nil
|
33
|
+
|
29
34
|
config.eager_load_namespaces << ActiveRecord
|
30
35
|
|
31
36
|
rake_tasks do
|
@@ -54,6 +59,7 @@ module ActiveRecord
|
|
54
59
|
console = ActiveSupport::Logger.new(STDERR)
|
55
60
|
Rails.logger.extend ActiveSupport::Logger.broadcast console
|
56
61
|
end
|
62
|
+
ActiveRecord::Base.verbose_query_logs = false
|
57
63
|
end
|
58
64
|
|
59
65
|
runner do
|
@@ -85,12 +91,16 @@ module ActiveRecord
|
|
85
91
|
filename = File.join(app.config.paths["db"].first, "schema_cache.yml")
|
86
92
|
|
87
93
|
if File.file?(filename)
|
94
|
+
current_version = ActiveRecord::Migrator.current_version
|
95
|
+
|
96
|
+
next if current_version.nil?
|
97
|
+
|
88
98
|
cache = YAML.load(File.read(filename))
|
89
|
-
if cache.version ==
|
99
|
+
if cache.version == current_version
|
90
100
|
connection.schema_cache = cache
|
91
101
|
connection_pool.schema_cache = cache.dup
|
92
102
|
else
|
93
|
-
warn "Ignoring db/schema_cache.yml because it has expired. The current schema version is #{
|
103
|
+
warn "Ignoring db/schema_cache.yml because it has expired. The current schema version is #{current_version}, but the one in the cache is #{cache.version}."
|
94
104
|
end
|
95
105
|
end
|
96
106
|
end
|
@@ -108,7 +118,9 @@ module ActiveRecord
|
|
108
118
|
|
109
119
|
initializer "active_record.set_configs" do |app|
|
110
120
|
ActiveSupport.on_load(:active_record) do
|
111
|
-
app.config.active_record.
|
121
|
+
configs = app.config.active_record.dup
|
122
|
+
configs.delete(:sqlite3)
|
123
|
+
configs.each do |k, v|
|
112
124
|
send "#{k}=", v
|
113
125
|
end
|
114
126
|
end
|
@@ -157,14 +169,58 @@ end_warning
|
|
157
169
|
end
|
158
170
|
|
159
171
|
initializer "active_record.set_executor_hooks" do
|
160
|
-
|
161
|
-
ActiveRecord::QueryCache.install_executor_hooks
|
162
|
-
end
|
172
|
+
ActiveRecord::QueryCache.install_executor_hooks
|
163
173
|
end
|
164
174
|
|
165
175
|
initializer "active_record.add_watchable_files" do |app|
|
166
176
|
path = app.paths["db"].first
|
167
177
|
config.watchable_files.concat ["#{path}/schema.rb", "#{path}/structure.sql"]
|
168
178
|
end
|
179
|
+
|
180
|
+
initializer "active_record.clear_active_connections" do
|
181
|
+
config.after_initialize do
|
182
|
+
ActiveSupport.on_load(:active_record) do
|
183
|
+
# Ideally the application doesn't connect to the database during boot,
|
184
|
+
# but sometimes it does. In case it did, we want to empty out the
|
185
|
+
# connection pools so that a non-database-using process (e.g. a master
|
186
|
+
# process in a forking server model) doesn't retain a needless
|
187
|
+
# connection. If it was needed, the incremental cost of reestablishing
|
188
|
+
# this connection is trivial: the rest of the pool would need to be
|
189
|
+
# populated anyway.
|
190
|
+
|
191
|
+
clear_active_connections!
|
192
|
+
flush_idle_connections!
|
193
|
+
end
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
initializer "active_record.check_represent_sqlite3_boolean_as_integer" do
|
198
|
+
config.after_initialize do
|
199
|
+
ActiveSupport.on_load(:active_record_sqlite3adapter) do
|
200
|
+
represent_boolean_as_integer = Rails.application.config.active_record.sqlite3.delete(:represent_boolean_as_integer)
|
201
|
+
unless represent_boolean_as_integer.nil?
|
202
|
+
ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer = represent_boolean_as_integer
|
203
|
+
end
|
204
|
+
|
205
|
+
unless ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer
|
206
|
+
ActiveSupport::Deprecation.warn <<-MSG
|
207
|
+
Leaving `ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer`
|
208
|
+
set to false is deprecated. SQLite databases have used 't' and 'f' to serialize
|
209
|
+
boolean values and must have old data converted to 1 and 0 (its native boolean
|
210
|
+
serialization) before setting this flag to true. Conversion can be accomplished
|
211
|
+
by setting up a rake task which runs
|
212
|
+
|
213
|
+
ExampleModel.where("boolean_column = 't'").update_all(boolean_column: 1)
|
214
|
+
ExampleModel.where("boolean_column = 'f'").update_all(boolean_column: 0)
|
215
|
+
|
216
|
+
for all models and all boolean columns, after which the flag must be set to
|
217
|
+
true by adding the following to your application.rb file:
|
218
|
+
|
219
|
+
Rails.application.config.active_record.sqlite3.represent_boolean_as_integer = true
|
220
|
+
MSG
|
221
|
+
end
|
222
|
+
end
|
223
|
+
end
|
224
|
+
end
|
169
225
|
end
|
170
226
|
end
|
@@ -1,17 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "active_record"
|
2
4
|
|
3
5
|
db_namespace = namespace :db do
|
4
6
|
desc "Set the environment value for the database"
|
5
|
-
task "environment:set" =>
|
7
|
+
task "environment:set" => :load_config do
|
6
8
|
ActiveRecord::InternalMetadata.create_table
|
7
|
-
ActiveRecord::InternalMetadata[:environment] = ActiveRecord::
|
9
|
+
ActiveRecord::InternalMetadata[:environment] = ActiveRecord::Base.connection.migration_context.current_environment
|
8
10
|
end
|
9
11
|
|
10
|
-
task check_protected_environments:
|
12
|
+
task check_protected_environments: :load_config do
|
11
13
|
ActiveRecord::Tasks::DatabaseTasks.check_protected_environments!
|
12
14
|
end
|
13
15
|
|
14
|
-
task :
|
16
|
+
task load_config: :environment do
|
15
17
|
ActiveRecord::Base.configurations = ActiveRecord::Tasks::DatabaseTasks.database_configuration || {}
|
16
18
|
ActiveRecord::Migrator.migrations_paths = ActiveRecord::Tasks::DatabaseTasks.migrations_paths
|
17
19
|
end
|
@@ -54,7 +56,7 @@ db_namespace = namespace :db do
|
|
54
56
|
end
|
55
57
|
|
56
58
|
desc "Migrate the database (options: VERSION=x, VERBOSE=false, SCOPE=blog)."
|
57
|
-
task migrate:
|
59
|
+
task migrate: :load_config do
|
58
60
|
ActiveRecord::Tasks::DatabaseTasks.migrate
|
59
61
|
db_namespace["_dump"].invoke
|
60
62
|
end
|
@@ -76,7 +78,7 @@ db_namespace = namespace :db do
|
|
76
78
|
|
77
79
|
namespace :migrate do
|
78
80
|
# desc 'Rollbacks the database one migration and re migrate up (options: STEP=x, VERSION=x).'
|
79
|
-
task redo:
|
81
|
+
task redo: :load_config do
|
80
82
|
raise "Empty VERSION provided" if ENV["VERSION"] && ENV["VERSION"].empty?
|
81
83
|
|
82
84
|
if ENV["VERSION"]
|
@@ -92,24 +94,33 @@ db_namespace = namespace :db do
|
|
92
94
|
task reset: ["db:drop", "db:create", "db:migrate"]
|
93
95
|
|
94
96
|
# desc 'Runs the "up" for a given migration VERSION.'
|
95
|
-
task up:
|
97
|
+
task up: :load_config do
|
96
98
|
raise "VERSION is required" if !ENV["VERSION"] || ENV["VERSION"].empty?
|
97
99
|
|
98
|
-
|
99
|
-
|
100
|
+
ActiveRecord::Tasks::DatabaseTasks.check_target_version
|
101
|
+
|
102
|
+
ActiveRecord::Base.connection.migration_context.run(
|
103
|
+
:up,
|
104
|
+
ActiveRecord::Tasks::DatabaseTasks.target_version
|
105
|
+
)
|
100
106
|
db_namespace["_dump"].invoke
|
101
107
|
end
|
102
108
|
|
103
109
|
# desc 'Runs the "down" for a given migration VERSION.'
|
104
|
-
task down:
|
110
|
+
task down: :load_config do
|
105
111
|
raise "VERSION is required - To go down one migration, use db:rollback" if !ENV["VERSION"] || ENV["VERSION"].empty?
|
106
|
-
|
107
|
-
ActiveRecord::
|
112
|
+
|
113
|
+
ActiveRecord::Tasks::DatabaseTasks.check_target_version
|
114
|
+
|
115
|
+
ActiveRecord::Base.connection.migration_context.run(
|
116
|
+
:down,
|
117
|
+
ActiveRecord::Tasks::DatabaseTasks.target_version
|
118
|
+
)
|
108
119
|
db_namespace["_dump"].invoke
|
109
120
|
end
|
110
121
|
|
111
122
|
desc "Display status of migrations"
|
112
|
-
task status:
|
123
|
+
task status: :load_config do
|
113
124
|
unless ActiveRecord::SchemaMigration.table_exists?
|
114
125
|
abort "Schema migrations table does not exist yet."
|
115
126
|
end
|
@@ -118,8 +129,7 @@ db_namespace = namespace :db do
|
|
118
129
|
puts "\ndatabase: #{ActiveRecord::Base.connection_config[:database]}\n\n"
|
119
130
|
puts "#{'Status'.center(8)} #{'Migration ID'.ljust(14)} Migration Name"
|
120
131
|
puts "-" * 50
|
121
|
-
|
122
|
-
ActiveRecord::Migrator.migrations_status(paths).each do |status, version, name|
|
132
|
+
ActiveRecord::Base.connection.migration_context.migrations_status.each do |status, version, name|
|
123
133
|
puts "#{status.center(8)} #{version.ljust(14)} #{name}"
|
124
134
|
end
|
125
135
|
puts
|
@@ -127,16 +137,16 @@ db_namespace = namespace :db do
|
|
127
137
|
end
|
128
138
|
|
129
139
|
desc "Rolls the schema back to the previous version (specify steps w/ STEP=n)."
|
130
|
-
task rollback:
|
140
|
+
task rollback: :load_config do
|
131
141
|
step = ENV["STEP"] ? ENV["STEP"].to_i : 1
|
132
|
-
ActiveRecord::
|
142
|
+
ActiveRecord::Base.connection.migration_context.rollback(step)
|
133
143
|
db_namespace["_dump"].invoke
|
134
144
|
end
|
135
145
|
|
136
146
|
# desc 'Pushes the schema to the next version (specify steps w/ STEP=n).'
|
137
|
-
task forward:
|
147
|
+
task forward: :load_config do
|
138
148
|
step = ENV["STEP"] ? ENV["STEP"].to_i : 1
|
139
|
-
ActiveRecord::
|
149
|
+
ActiveRecord::Base.connection.migration_context.forward(step)
|
140
150
|
db_namespace["_dump"].invoke
|
141
151
|
end
|
142
152
|
|
@@ -144,12 +154,12 @@ db_namespace = namespace :db do
|
|
144
154
|
task reset: [ "db:drop", "db:setup" ]
|
145
155
|
|
146
156
|
# desc "Retrieves the charset for the current environment's database"
|
147
|
-
task charset:
|
157
|
+
task charset: :load_config do
|
148
158
|
puts ActiveRecord::Tasks::DatabaseTasks.charset_current
|
149
159
|
end
|
150
160
|
|
151
161
|
# desc "Retrieves the collation for the current environment's database"
|
152
|
-
task collation:
|
162
|
+
task collation: :load_config do
|
153
163
|
begin
|
154
164
|
puts ActiveRecord::Tasks::DatabaseTasks.collation_current
|
155
165
|
rescue NoMethodError
|
@@ -158,13 +168,13 @@ db_namespace = namespace :db do
|
|
158
168
|
end
|
159
169
|
|
160
170
|
desc "Retrieves the current schema version number"
|
161
|
-
task version:
|
162
|
-
puts "Current version: #{ActiveRecord::
|
171
|
+
task version: :load_config do
|
172
|
+
puts "Current version: #{ActiveRecord::Base.connection.migration_context.current_version}"
|
163
173
|
end
|
164
174
|
|
165
175
|
# desc "Raises an error if there are pending migrations"
|
166
|
-
task abort_if_pending_migrations:
|
167
|
-
pending_migrations = ActiveRecord::
|
176
|
+
task abort_if_pending_migrations: :load_config do
|
177
|
+
pending_migrations = ActiveRecord::Base.connection.migration_context.open.pending_migrations
|
168
178
|
|
169
179
|
if pending_migrations.any?
|
170
180
|
puts "You have #{pending_migrations.size} pending #{pending_migrations.size > 1 ? 'migrations:' : 'migration:'}"
|
@@ -186,7 +196,7 @@ db_namespace = namespace :db do
|
|
186
196
|
|
187
197
|
namespace :fixtures do
|
188
198
|
desc "Loads fixtures into the current environment's database. Load specific fixtures using FIXTURES=x,y. Load from subdirectory in test/fixtures using FIXTURES_DIR=z. Specify an alternative path (eg. spec/fixtures) using FIXTURES_PATH=spec/fixtures."
|
189
|
-
task load:
|
199
|
+
task load: :load_config do
|
190
200
|
require "active_record/fixtures"
|
191
201
|
|
192
202
|
base_dir = ActiveRecord::Tasks::DatabaseTasks.fixtures_path
|
@@ -208,7 +218,7 @@ db_namespace = namespace :db do
|
|
208
218
|
end
|
209
219
|
|
210
220
|
# desc "Search for a fixture given a LABEL or ID. Specify an alternative path (eg. spec/fixtures) using FIXTURES_PATH=spec/fixtures."
|
211
|
-
task identify:
|
221
|
+
task identify: :load_config do
|
212
222
|
require "active_record/fixtures"
|
213
223
|
|
214
224
|
label, id = ENV["LABEL"], ENV["ID"]
|
@@ -219,7 +229,7 @@ db_namespace = namespace :db do
|
|
219
229
|
base_dir = ActiveRecord::Tasks::DatabaseTasks.fixtures_path
|
220
230
|
|
221
231
|
Dir["#{base_dir}/**/*.yml"].each do |file|
|
222
|
-
if data = YAML
|
232
|
+
if data = YAML.load(ERB.new(IO.read(file)).result)
|
223
233
|
data.each_key do |key|
|
224
234
|
key_id = ActiveRecord::FixtureSet.identify(key)
|
225
235
|
|
@@ -234,7 +244,7 @@ db_namespace = namespace :db do
|
|
234
244
|
|
235
245
|
namespace :schema do
|
236
246
|
desc "Creates a db/schema.rb file that is portable against any DB supported by Active Record"
|
237
|
-
task dump:
|
247
|
+
task dump: :load_config do
|
238
248
|
require "active_record/schema_dumper"
|
239
249
|
filename = ENV["SCHEMA"] || File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, "schema.rb")
|
240
250
|
File.open(filename, "w:utf-8") do |file|
|
@@ -244,7 +254,7 @@ db_namespace = namespace :db do
|
|
244
254
|
end
|
245
255
|
|
246
256
|
desc "Loads a schema.rb file into the database"
|
247
|
-
task load: [:
|
257
|
+
task load: [:load_config, :check_protected_environments] do
|
248
258
|
ActiveRecord::Tasks::DatabaseTasks.load_schema_current(:ruby, ENV["SCHEMA"])
|
249
259
|
end
|
250
260
|
|
@@ -254,14 +264,14 @@ db_namespace = namespace :db do
|
|
254
264
|
|
255
265
|
namespace :cache do
|
256
266
|
desc "Creates a db/schema_cache.yml file."
|
257
|
-
task dump:
|
267
|
+
task dump: :load_config do
|
258
268
|
conn = ActiveRecord::Base.connection
|
259
269
|
filename = File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, "schema_cache.yml")
|
260
270
|
ActiveRecord::Tasks::DatabaseTasks.dump_schema_cache(conn, filename)
|
261
271
|
end
|
262
272
|
|
263
273
|
desc "Clears a db/schema_cache.yml file."
|
264
|
-
task clear:
|
274
|
+
task clear: :load_config do
|
265
275
|
filename = File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, "schema_cache.yml")
|
266
276
|
rm_f filename, verbose: false
|
267
277
|
end
|
@@ -271,7 +281,7 @@ db_namespace = namespace :db do
|
|
271
281
|
|
272
282
|
namespace :structure do
|
273
283
|
desc "Dumps the database structure to db/structure.sql. Specify another file with SCHEMA=db/my_structure.sql"
|
274
|
-
task dump:
|
284
|
+
task dump: :load_config do
|
275
285
|
filename = ENV["SCHEMA"] || File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, "structure.sql")
|
276
286
|
current_config = ActiveRecord::Tasks::DatabaseTasks.current_config
|
277
287
|
ActiveRecord::Tasks::DatabaseTasks.structure_dump(current_config, filename)
|
@@ -286,7 +296,7 @@ db_namespace = namespace :db do
|
|
286
296
|
end
|
287
297
|
|
288
298
|
desc "Recreates the databases from the structure.sql file"
|
289
|
-
task load: [:
|
299
|
+
task load: [:load_config, :check_protected_environments] do
|
290
300
|
ActiveRecord::Tasks::DatabaseTasks.load_schema_current(:sql, ENV["SCHEMA"])
|
291
301
|
end
|
292
302
|
|
@@ -325,12 +335,12 @@ db_namespace = namespace :db do
|
|
325
335
|
end
|
326
336
|
|
327
337
|
# desc "Empty the test database"
|
328
|
-
task purge: %w(
|
338
|
+
task purge: %w(load_config check_protected_environments) do
|
329
339
|
ActiveRecord::Tasks::DatabaseTasks.purge ActiveRecord::Base.configurations["test"]
|
330
340
|
end
|
331
341
|
|
332
342
|
# desc 'Load the test schema'
|
333
|
-
task prepare:
|
343
|
+
task prepare: :load_config do
|
334
344
|
unless ActiveRecord::Base.configurations.blank?
|
335
345
|
db_namespace["test:load"].invoke
|
336
346
|
end
|