activerecord 5.2.4.4 → 6.0.3.4
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 +777 -552
- data/MIT-LICENSE +3 -1
- data/README.rdoc +5 -3
- data/examples/performance.rb +1 -1
- data/lib/active_record.rb +10 -2
- data/lib/active_record/advisory_lock_base.rb +18 -0
- data/lib/active_record/aggregations.rb +4 -3
- data/lib/active_record/association_relation.rb +10 -8
- data/lib/active_record/associations.rb +21 -16
- data/lib/active_record/associations/alias_tracker.rb +0 -1
- data/lib/active_record/associations/association.rb +56 -19
- data/lib/active_record/associations/association_scope.rb +4 -6
- data/lib/active_record/associations/belongs_to_association.rb +36 -42
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +0 -4
- data/lib/active_record/associations/builder/association.rb +14 -18
- data/lib/active_record/associations/builder/belongs_to.rb +19 -52
- data/lib/active_record/associations/builder/collection_association.rb +3 -13
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +17 -40
- data/lib/active_record/associations/builder/has_many.rb +2 -0
- data/lib/active_record/associations/builder/has_one.rb +35 -1
- data/lib/active_record/associations/builder/singular_association.rb +2 -0
- data/lib/active_record/associations/collection_association.rb +12 -23
- data/lib/active_record/associations/collection_proxy.rb +13 -17
- data/lib/active_record/associations/foreign_association.rb +7 -0
- data/lib/active_record/associations/has_many_association.rb +2 -11
- data/lib/active_record/associations/has_many_through_association.rb +14 -14
- data/lib/active_record/associations/has_one_association.rb +28 -30
- data/lib/active_record/associations/has_one_through_association.rb +5 -5
- data/lib/active_record/associations/join_dependency.rb +37 -28
- data/lib/active_record/associations/join_dependency/join_association.rb +9 -10
- data/lib/active_record/associations/join_dependency/join_part.rb +2 -2
- data/lib/active_record/associations/preloader.rb +39 -32
- data/lib/active_record/associations/preloader/association.rb +38 -36
- data/lib/active_record/associations/preloader/through_association.rb +48 -39
- data/lib/active_record/associations/singular_association.rb +2 -16
- data/lib/active_record/attribute_assignment.rb +7 -11
- data/lib/active_record/attribute_decorators.rb +0 -2
- data/lib/active_record/attribute_methods.rb +28 -100
- data/lib/active_record/attribute_methods/before_type_cast.rb +4 -2
- data/lib/active_record/attribute_methods/dirty.rb +111 -40
- data/lib/active_record/attribute_methods/primary_key.rb +15 -24
- data/lib/active_record/attribute_methods/query.rb +2 -3
- data/lib/active_record/attribute_methods/read.rb +15 -54
- data/lib/active_record/attribute_methods/serialization.rb +1 -2
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +1 -3
- data/lib/active_record/attribute_methods/write.rb +17 -25
- data/lib/active_record/attributes.rb +13 -1
- data/lib/active_record/autosave_association.rb +3 -5
- data/lib/active_record/base.rb +2 -3
- data/lib/active_record/callbacks.rb +6 -21
- data/lib/active_record/coders/yaml_column.rb +0 -1
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +103 -18
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +17 -4
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +102 -124
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +18 -9
- data/lib/active_record/connection_adapters/abstract/quoting.rb +68 -17
- data/lib/active_record/connection_adapters/abstract/schema_creation.rb +20 -14
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +100 -72
- data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +1 -3
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +175 -79
- data/lib/active_record/connection_adapters/abstract/transaction.rb +96 -57
- data/lib/active_record/connection_adapters/abstract_adapter.rb +191 -43
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +142 -215
- data/lib/active_record/connection_adapters/column.rb +17 -13
- data/lib/active_record/connection_adapters/connection_specification.rb +54 -45
- data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +6 -10
- data/lib/active_record/connection_adapters/mysql/column.rb +1 -1
- data/lib/active_record/connection_adapters/mysql/database_statements.rb +70 -14
- data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +0 -1
- data/lib/active_record/connection_adapters/mysql/quoting.rb +44 -7
- data/lib/active_record/connection_adapters/mysql/schema_creation.rb +4 -6
- data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +40 -32
- data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +14 -6
- data/lib/active_record/connection_adapters/mysql/schema_statements.rb +132 -16
- data/lib/active_record/connection_adapters/mysql/type_metadata.rb +6 -10
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +26 -10
- data/lib/active_record/connection_adapters/postgresql/column.rb +17 -31
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +26 -1
- data/lib/active_record/connection_adapters/postgresql/oid/array.rb +1 -2
- data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +1 -4
- data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +0 -1
- data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +1 -2
- data/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb +1 -2
- data/lib/active_record/connection_adapters/postgresql/oid/money.rb +2 -2
- data/lib/active_record/connection_adapters/postgresql/oid/oid.rb +1 -1
- data/lib/active_record/connection_adapters/postgresql/oid/point.rb +1 -2
- data/lib/active_record/connection_adapters/postgresql/oid/range.rb +1 -2
- data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +1 -1
- data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +9 -7
- data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +5 -3
- data/lib/active_record/connection_adapters/postgresql/quoting.rb +44 -7
- data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +1 -1
- data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +14 -3
- data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +107 -91
- data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +0 -1
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +63 -75
- data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +24 -27
- data/lib/active_record/connection_adapters/postgresql/utils.rb +0 -1
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +168 -75
- data/lib/active_record/connection_adapters/schema_cache.rb +37 -14
- data/lib/active_record/connection_adapters/sql_type_metadata.rb +11 -8
- data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +119 -0
- data/lib/active_record/connection_adapters/sqlite3/quoting.rb +42 -7
- data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +43 -12
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +135 -146
- data/lib/active_record/connection_adapters/statement_pool.rb +0 -1
- data/lib/active_record/connection_handling.rb +139 -26
- data/lib/active_record/core.rb +103 -61
- data/lib/active_record/counter_cache.rb +8 -30
- data/lib/active_record/database_configurations.rb +233 -0
- data/lib/active_record/database_configurations/database_config.rb +37 -0
- data/lib/active_record/database_configurations/hash_config.rb +50 -0
- data/lib/active_record/database_configurations/url_config.rb +78 -0
- data/lib/active_record/dynamic_matchers.rb +3 -4
- data/lib/active_record/enum.rb +37 -7
- data/lib/active_record/errors.rb +15 -7
- data/lib/active_record/explain.rb +1 -2
- data/lib/active_record/fixture_set/model_metadata.rb +33 -0
- data/lib/active_record/fixture_set/render_context.rb +17 -0
- data/lib/active_record/fixture_set/table_row.rb +152 -0
- data/lib/active_record/fixture_set/table_rows.rb +46 -0
- data/lib/active_record/fixtures.rb +144 -474
- data/lib/active_record/gem_version.rb +3 -3
- data/lib/active_record/inheritance.rb +13 -6
- data/lib/active_record/insert_all.rb +179 -0
- data/lib/active_record/integration.rb +68 -16
- data/lib/active_record/internal_metadata.rb +11 -3
- data/lib/active_record/locking/optimistic.rb +5 -7
- data/lib/active_record/locking/pessimistic.rb +3 -3
- data/lib/active_record/log_subscriber.rb +8 -27
- data/lib/active_record/middleware/database_selector.rb +74 -0
- data/lib/active_record/middleware/database_selector/resolver.rb +87 -0
- data/lib/active_record/middleware/database_selector/resolver/session.rb +45 -0
- data/lib/active_record/migration.rb +104 -85
- data/lib/active_record/migration/command_recorder.rb +54 -22
- data/lib/active_record/migration/compatibility.rb +79 -52
- data/lib/active_record/migration/join_table.rb +0 -1
- data/lib/active_record/model_schema.rb +33 -11
- data/lib/active_record/nested_attributes.rb +2 -4
- data/lib/active_record/no_touching.rb +9 -2
- data/lib/active_record/null_relation.rb +0 -1
- data/lib/active_record/persistence.rb +232 -29
- data/lib/active_record/query_cache.rb +11 -4
- data/lib/active_record/querying.rb +33 -21
- data/lib/active_record/railtie.rb +80 -43
- data/lib/active_record/railties/collection_cache_association_loading.rb +34 -0
- data/lib/active_record/railties/controller_runtime.rb +30 -35
- data/lib/active_record/railties/databases.rake +199 -46
- data/lib/active_record/reflection.rb +40 -38
- data/lib/active_record/relation.rb +322 -80
- data/lib/active_record/relation/batches.rb +13 -11
- data/lib/active_record/relation/calculations.rb +54 -48
- data/lib/active_record/relation/delegation.rb +33 -49
- data/lib/active_record/relation/finder_methods.rb +23 -28
- data/lib/active_record/relation/from_clause.rb +4 -0
- data/lib/active_record/relation/merger.rb +11 -21
- data/lib/active_record/relation/predicate_builder.rb +5 -11
- data/lib/active_record/relation/predicate_builder/array_handler.rb +5 -4
- data/lib/active_record/relation/predicate_builder/association_query_value.rb +1 -4
- data/lib/active_record/relation/predicate_builder/base_handler.rb +1 -2
- data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +1 -2
- data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +1 -4
- data/lib/active_record/relation/predicate_builder/range_handler.rb +3 -23
- data/lib/active_record/relation/query_attribute.rb +13 -8
- data/lib/active_record/relation/query_methods.rb +221 -70
- data/lib/active_record/relation/spawn_methods.rb +1 -2
- data/lib/active_record/relation/where_clause.rb +14 -11
- data/lib/active_record/relation/where_clause_factory.rb +1 -2
- data/lib/active_record/result.rb +30 -12
- data/lib/active_record/sanitization.rb +32 -40
- data/lib/active_record/schema.rb +2 -11
- data/lib/active_record/schema_dumper.rb +22 -7
- data/lib/active_record/schema_migration.rb +6 -2
- data/lib/active_record/scoping.rb +8 -9
- data/lib/active_record/scoping/default.rb +4 -6
- data/lib/active_record/scoping/named.rb +21 -17
- data/lib/active_record/statement_cache.rb +30 -3
- data/lib/active_record/store.rb +87 -8
- data/lib/active_record/suppressor.rb +2 -2
- data/lib/active_record/table_metadata.rb +23 -15
- data/lib/active_record/tasks/database_tasks.rb +194 -25
- data/lib/active_record/tasks/mysql_database_tasks.rb +5 -6
- data/lib/active_record/tasks/postgresql_database_tasks.rb +5 -8
- data/lib/active_record/tasks/sqlite_database_tasks.rb +2 -9
- data/lib/active_record/test_databases.rb +23 -0
- data/lib/active_record/test_fixtures.rb +225 -0
- data/lib/active_record/timestamp.rb +39 -26
- data/lib/active_record/touch_later.rb +5 -4
- data/lib/active_record/transactions.rb +64 -73
- data/lib/active_record/translation.rb +1 -1
- data/lib/active_record/type.rb +3 -5
- data/lib/active_record/type/adapter_specific_registry.rb +3 -13
- data/lib/active_record/type/hash_lookup_type_map.rb +0 -1
- data/lib/active_record/type/serialized.rb +0 -1
- data/lib/active_record/type/type_map.rb +0 -1
- data/lib/active_record/type/unsigned_integer.rb +0 -1
- data/lib/active_record/type_caster/connection.rb +15 -14
- data/lib/active_record/type_caster/map.rb +1 -4
- data/lib/active_record/validations.rb +3 -3
- data/lib/active_record/validations/associated.rb +1 -2
- data/lib/active_record/validations/uniqueness.rb +15 -27
- data/lib/arel.rb +62 -0
- data/lib/arel/alias_predication.rb +9 -0
- data/lib/arel/attributes.rb +22 -0
- data/lib/arel/attributes/attribute.rb +37 -0
- data/lib/arel/collectors/bind.rb +24 -0
- data/lib/arel/collectors/composite.rb +31 -0
- data/lib/arel/collectors/plain_string.rb +20 -0
- data/lib/arel/collectors/sql_string.rb +20 -0
- data/lib/arel/collectors/substitute_binds.rb +28 -0
- data/lib/arel/crud.rb +42 -0
- data/lib/arel/delete_manager.rb +18 -0
- data/lib/arel/errors.rb +9 -0
- data/lib/arel/expressions.rb +29 -0
- data/lib/arel/factory_methods.rb +49 -0
- data/lib/arel/insert_manager.rb +49 -0
- data/lib/arel/math.rb +45 -0
- data/lib/arel/nodes.rb +68 -0
- data/lib/arel/nodes/and.rb +32 -0
- data/lib/arel/nodes/ascending.rb +23 -0
- data/lib/arel/nodes/binary.rb +52 -0
- data/lib/arel/nodes/bind_param.rb +36 -0
- data/lib/arel/nodes/case.rb +55 -0
- data/lib/arel/nodes/casted.rb +50 -0
- data/lib/arel/nodes/comment.rb +29 -0
- data/lib/arel/nodes/count.rb +12 -0
- data/lib/arel/nodes/delete_statement.rb +45 -0
- data/lib/arel/nodes/descending.rb +23 -0
- data/lib/arel/nodes/equality.rb +18 -0
- data/lib/arel/nodes/extract.rb +24 -0
- data/lib/arel/nodes/false.rb +16 -0
- data/lib/arel/nodes/full_outer_join.rb +8 -0
- data/lib/arel/nodes/function.rb +44 -0
- data/lib/arel/nodes/grouping.rb +8 -0
- data/lib/arel/nodes/in.rb +8 -0
- data/lib/arel/nodes/infix_operation.rb +80 -0
- data/lib/arel/nodes/inner_join.rb +8 -0
- data/lib/arel/nodes/insert_statement.rb +37 -0
- data/lib/arel/nodes/join_source.rb +20 -0
- data/lib/arel/nodes/matches.rb +18 -0
- data/lib/arel/nodes/named_function.rb +23 -0
- data/lib/arel/nodes/node.rb +50 -0
- data/lib/arel/nodes/node_expression.rb +13 -0
- data/lib/arel/nodes/outer_join.rb +8 -0
- data/lib/arel/nodes/over.rb +15 -0
- data/lib/arel/nodes/regexp.rb +16 -0
- data/lib/arel/nodes/right_outer_join.rb +8 -0
- data/lib/arel/nodes/select_core.rb +67 -0
- data/lib/arel/nodes/select_statement.rb +41 -0
- data/lib/arel/nodes/sql_literal.rb +16 -0
- data/lib/arel/nodes/string_join.rb +11 -0
- data/lib/arel/nodes/table_alias.rb +27 -0
- data/lib/arel/nodes/terminal.rb +16 -0
- data/lib/arel/nodes/true.rb +16 -0
- data/lib/arel/nodes/unary.rb +45 -0
- data/lib/arel/nodes/unary_operation.rb +20 -0
- data/lib/arel/nodes/unqualified_column.rb +22 -0
- data/lib/arel/nodes/update_statement.rb +41 -0
- data/lib/arel/nodes/values_list.rb +9 -0
- data/lib/arel/nodes/window.rb +126 -0
- data/lib/arel/nodes/with.rb +11 -0
- data/lib/arel/order_predications.rb +13 -0
- data/lib/arel/predications.rb +256 -0
- data/lib/arel/select_manager.rb +271 -0
- data/lib/arel/table.rb +110 -0
- data/lib/arel/tree_manager.rb +72 -0
- data/lib/arel/update_manager.rb +34 -0
- data/lib/arel/visitors.rb +20 -0
- data/lib/arel/visitors/depth_first.rb +203 -0
- data/lib/arel/visitors/dot.rb +296 -0
- data/lib/arel/visitors/ibm_db.rb +34 -0
- data/lib/arel/visitors/informix.rb +62 -0
- data/lib/arel/visitors/mssql.rb +156 -0
- data/lib/arel/visitors/mysql.rb +83 -0
- data/lib/arel/visitors/oracle.rb +158 -0
- data/lib/arel/visitors/oracle12.rb +65 -0
- data/lib/arel/visitors/postgresql.rb +109 -0
- data/lib/arel/visitors/sqlite.rb +38 -0
- data/lib/arel/visitors/to_sql.rb +888 -0
- data/lib/arel/visitors/visitor.rb +45 -0
- data/lib/arel/visitors/where_sql.rb +22 -0
- data/lib/arel/window_predications.rb +9 -0
- data/lib/rails/generators/active_record/application_record/application_record_generator.rb +0 -1
- data/lib/rails/generators/active_record/migration.rb +14 -2
- data/lib/rails/generators/active_record/migration/migration_generator.rb +2 -5
- data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb.tt +1 -1
- data/lib/rails/generators/active_record/migration/templates/migration.rb.tt +4 -2
- data/lib/rails/generators/active_record/model/model_generator.rb +1 -1
- data/lib/rails/generators/active_record/model/templates/model.rb.tt +10 -1
- metadata +115 -29
- data/lib/active_record/collection_cache_key.rb +0 -53
@@ -24,13 +24,21 @@ module ActiveRecord
|
|
24
24
|
# You can define a scope that applies to all finders using
|
25
25
|
# {default_scope}[rdoc-ref:Scoping::Default::ClassMethods#default_scope].
|
26
26
|
def all
|
27
|
-
|
27
|
+
scope = current_scope
|
28
|
+
|
29
|
+
if scope
|
30
|
+
if scope._deprecated_scope_source
|
31
|
+
ActiveSupport::Deprecation.warn(<<~MSG.squish)
|
32
|
+
Class level methods will no longer inherit scoping from `#{scope._deprecated_scope_source}`
|
33
|
+
in Rails 6.1. To continue using the scoped relation, pass it into the block directly.
|
34
|
+
To instead access the full set of models, as Rails 6.1 will, use `#{name}.default_scoped`.
|
35
|
+
MSG
|
36
|
+
end
|
28
37
|
|
29
|
-
|
30
|
-
|
31
|
-
current_scope.clone
|
38
|
+
if self == scope.klass
|
39
|
+
scope.clone
|
32
40
|
else
|
33
|
-
relation.merge!(
|
41
|
+
relation.merge!(scope)
|
34
42
|
end
|
35
43
|
else
|
36
44
|
default_scoped
|
@@ -38,21 +46,20 @@ module ActiveRecord
|
|
38
46
|
end
|
39
47
|
|
40
48
|
def scope_for_association(scope = relation) # :nodoc:
|
41
|
-
|
42
|
-
|
43
|
-
if current_scope && current_scope.empty_scope?
|
49
|
+
if current_scope&.empty_scope?
|
44
50
|
scope
|
45
51
|
else
|
46
52
|
default_scoped(scope)
|
47
53
|
end
|
48
54
|
end
|
49
55
|
|
50
|
-
|
56
|
+
# Returns a scope for the model with default scopes.
|
57
|
+
def default_scoped(scope = relation)
|
51
58
|
build_default_scope(scope) || scope
|
52
59
|
end
|
53
60
|
|
54
61
|
def default_extensions # :nodoc:
|
55
|
-
if scope =
|
62
|
+
if scope = scope_for_association || build_default_scope
|
56
63
|
scope.extensions
|
57
64
|
else
|
58
65
|
[]
|
@@ -181,16 +188,14 @@ module ActiveRecord
|
|
181
188
|
extension = Module.new(&block) if block
|
182
189
|
|
183
190
|
if body.respond_to?(:to_proc)
|
184
|
-
singleton_class.
|
185
|
-
scope = all
|
186
|
-
scope = scope._exec_scope(*args, &body)
|
191
|
+
singleton_class.define_method(name) do |*args|
|
192
|
+
scope = all._exec_scope(name, *args, &body)
|
187
193
|
scope = scope.extending(extension) if extension
|
188
194
|
scope
|
189
195
|
end
|
190
196
|
else
|
191
|
-
singleton_class.
|
192
|
-
scope = all
|
193
|
-
scope = scope.scoping { body.call(*args) || scope }
|
197
|
+
singleton_class.define_method(name) do |*args|
|
198
|
+
scope = body.call(*args) || all
|
194
199
|
scope = scope.extending(extension) if extension
|
195
200
|
scope
|
196
201
|
end
|
@@ -200,7 +205,6 @@ module ActiveRecord
|
|
200
205
|
end
|
201
206
|
|
202
207
|
private
|
203
|
-
|
204
208
|
def valid_scope_name?(name)
|
205
209
|
if respond_to?(name, true) && logger
|
206
210
|
logger.warn "Creating scope :#{name}. " \
|
@@ -44,7 +44,7 @@ module ActiveRecord
|
|
44
44
|
def initialize(values)
|
45
45
|
@values = values
|
46
46
|
@indexes = values.each_with_index.find_all { |thing, i|
|
47
|
-
|
47
|
+
Substitute === thing
|
48
48
|
}.map(&:last)
|
49
49
|
end
|
50
50
|
|
@@ -56,6 +56,28 @@ module ActiveRecord
|
|
56
56
|
end
|
57
57
|
end
|
58
58
|
|
59
|
+
class PartialQueryCollector
|
60
|
+
def initialize
|
61
|
+
@parts = []
|
62
|
+
@binds = []
|
63
|
+
end
|
64
|
+
|
65
|
+
def <<(str)
|
66
|
+
@parts << str
|
67
|
+
self
|
68
|
+
end
|
69
|
+
|
70
|
+
def add_bind(obj)
|
71
|
+
@binds << obj
|
72
|
+
@parts << Substitute.new
|
73
|
+
self
|
74
|
+
end
|
75
|
+
|
76
|
+
def value
|
77
|
+
[@parts, @binds]
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
59
81
|
def self.query(sql)
|
60
82
|
Query.new(sql)
|
61
83
|
end
|
@@ -64,6 +86,10 @@ module ActiveRecord
|
|
64
86
|
PartialQuery.new(values)
|
65
87
|
end
|
66
88
|
|
89
|
+
def self.partial_query_collector
|
90
|
+
PartialQueryCollector.new
|
91
|
+
end
|
92
|
+
|
67
93
|
class Params # :nodoc:
|
68
94
|
def bind; Substitute.new; end
|
69
95
|
end
|
@@ -106,6 +132,8 @@ module ActiveRecord
|
|
106
132
|
sql = query_builder.sql_for bind_values, connection
|
107
133
|
|
108
134
|
klass.find_by_sql(sql, bind_values, preparable: true, &block)
|
135
|
+
rescue ::RangeError
|
136
|
+
nil
|
109
137
|
end
|
110
138
|
|
111
139
|
def self.unsupported_value?(value)
|
@@ -114,8 +142,7 @@ module ActiveRecord
|
|
114
142
|
end
|
115
143
|
end
|
116
144
|
|
117
|
-
|
118
|
-
|
145
|
+
private
|
119
146
|
attr_reader :query_builder, :bind_map, :klass
|
120
147
|
end
|
121
148
|
end
|
data/lib/active_record/store.rb
CHANGED
@@ -11,14 +11,20 @@ module ActiveRecord
|
|
11
11
|
# of the model. This is very helpful for easily exposing store keys to a form or elsewhere that's
|
12
12
|
# already built around just accessing attributes on the model.
|
13
13
|
#
|
14
|
+
# Every accessor comes with dirty tracking methods (+key_changed?+, +key_was+ and +key_change+) and
|
15
|
+
# methods to access the changes made during the last save (+saved_change_to_key?+, +saved_change_to_key+ and
|
16
|
+
# +key_before_last_save+).
|
17
|
+
#
|
18
|
+
# NOTE: There is no +key_will_change!+ method for accessors, use +store_will_change!+ instead.
|
19
|
+
#
|
14
20
|
# Make sure that you declare the database column used for the serialized store as a text, so there's
|
15
21
|
# plenty of room.
|
16
22
|
#
|
17
23
|
# You can set custom coder to encode/decode your serialized attributes to/from different formats.
|
18
24
|
# JSON, YAML, Marshal are supported out of the box. Generally it can be any wrapper that provides +load+ and +dump+.
|
19
25
|
#
|
20
|
-
# NOTE: If you are using
|
21
|
-
# the serialization provided by {.store}[rdoc-ref:rdoc-ref:ClassMethods#store].
|
26
|
+
# NOTE: If you are using structured database data types (eg. PostgreSQL +hstore+/+json+, or MySQL 5.7+
|
27
|
+
# +json+) there is no need for the serialization provided by {.store}[rdoc-ref:rdoc-ref:ClassMethods#store].
|
22
28
|
# Simply use {.store_accessor}[rdoc-ref:ClassMethods#store_accessor] instead to generate
|
23
29
|
# the accessor methods. Be aware that these columns use a string keyed hash and do not allow access
|
24
30
|
# using a symbol.
|
@@ -31,24 +37,40 @@ module ActiveRecord
|
|
31
37
|
#
|
32
38
|
# class User < ActiveRecord::Base
|
33
39
|
# store :settings, accessors: [ :color, :homepage ], coder: JSON
|
40
|
+
# store :parent, accessors: [ :name ], coder: JSON, prefix: true
|
41
|
+
# store :spouse, accessors: [ :name ], coder: JSON, prefix: :partner
|
42
|
+
# store :settings, accessors: [ :two_factor_auth ], suffix: true
|
43
|
+
# store :settings, accessors: [ :login_retry ], suffix: :config
|
34
44
|
# end
|
35
45
|
#
|
36
|
-
# u = User.new(color: 'black', homepage: '37signals.com')
|
46
|
+
# u = User.new(color: 'black', homepage: '37signals.com', parent_name: 'Mary', partner_name: 'Lily')
|
37
47
|
# u.color # Accessor stored attribute
|
48
|
+
# u.parent_name # Accessor stored attribute with prefix
|
49
|
+
# u.partner_name # Accessor stored attribute with custom prefix
|
50
|
+
# u.two_factor_auth_settings # Accessor stored attribute with suffix
|
51
|
+
# u.login_retry_config # Accessor stored attribute with custom suffix
|
38
52
|
# u.settings[:country] = 'Denmark' # Any attribute, even if not specified with an accessor
|
39
53
|
#
|
40
54
|
# # There is no difference between strings and symbols for accessing custom attributes
|
41
55
|
# u.settings[:country] # => 'Denmark'
|
42
56
|
# u.settings['country'] # => 'Denmark'
|
43
57
|
#
|
58
|
+
# # Dirty tracking
|
59
|
+
# u.color = 'green'
|
60
|
+
# u.color_changed? # => true
|
61
|
+
# u.color_was # => 'black'
|
62
|
+
# u.color_change # => ['black', 'red']
|
63
|
+
#
|
44
64
|
# # Add additional accessors to an existing store through store_accessor
|
45
65
|
# class SuperUser < User
|
46
66
|
# store_accessor :settings, :privileges, :servants
|
67
|
+
# store_accessor :parent, :birthday, prefix: true
|
68
|
+
# store_accessor :settings, :secret_question, suffix: :config
|
47
69
|
# end
|
48
70
|
#
|
49
71
|
# The stored attribute names can be retrieved using {.stored_attributes}[rdoc-ref:rdoc-ref:ClassMethods#stored_attributes].
|
50
72
|
#
|
51
|
-
# User.stored_attributes[:settings] # [:color, :homepage]
|
73
|
+
# User.stored_attributes[:settings] # [:color, :homepage, :two_factor_auth, :login_retry]
|
52
74
|
#
|
53
75
|
# == Overwriting default accessors
|
54
76
|
#
|
@@ -81,21 +103,78 @@ module ActiveRecord
|
|
81
103
|
module ClassMethods
|
82
104
|
def store(store_attribute, options = {})
|
83
105
|
serialize store_attribute, IndifferentCoder.new(store_attribute, options[:coder])
|
84
|
-
store_accessor(store_attribute, options[:accessors]) if options.has_key? :accessors
|
106
|
+
store_accessor(store_attribute, options[:accessors], **options.slice(:prefix, :suffix)) if options.has_key? :accessors
|
85
107
|
end
|
86
108
|
|
87
|
-
def store_accessor(store_attribute, *keys)
|
109
|
+
def store_accessor(store_attribute, *keys, prefix: nil, suffix: nil)
|
88
110
|
keys = keys.flatten
|
89
111
|
|
112
|
+
accessor_prefix =
|
113
|
+
case prefix
|
114
|
+
when String, Symbol
|
115
|
+
"#{prefix}_"
|
116
|
+
when TrueClass
|
117
|
+
"#{store_attribute}_"
|
118
|
+
else
|
119
|
+
""
|
120
|
+
end
|
121
|
+
accessor_suffix =
|
122
|
+
case suffix
|
123
|
+
when String, Symbol
|
124
|
+
"_#{suffix}"
|
125
|
+
when TrueClass
|
126
|
+
"_#{store_attribute}"
|
127
|
+
else
|
128
|
+
""
|
129
|
+
end
|
130
|
+
|
90
131
|
_store_accessors_module.module_eval do
|
91
132
|
keys.each do |key|
|
92
|
-
|
133
|
+
accessor_key = "#{accessor_prefix}#{key}#{accessor_suffix}"
|
134
|
+
|
135
|
+
define_method("#{accessor_key}=") do |value|
|
93
136
|
write_store_attribute(store_attribute, key, value)
|
94
137
|
end
|
95
138
|
|
96
|
-
define_method(
|
139
|
+
define_method(accessor_key) do
|
97
140
|
read_store_attribute(store_attribute, key)
|
98
141
|
end
|
142
|
+
|
143
|
+
define_method("#{accessor_key}_changed?") do
|
144
|
+
return false unless attribute_changed?(store_attribute)
|
145
|
+
prev_store, new_store = changes[store_attribute]
|
146
|
+
prev_store&.dig(key) != new_store&.dig(key)
|
147
|
+
end
|
148
|
+
|
149
|
+
define_method("#{accessor_key}_change") do
|
150
|
+
return unless attribute_changed?(store_attribute)
|
151
|
+
prev_store, new_store = changes[store_attribute]
|
152
|
+
[prev_store&.dig(key), new_store&.dig(key)]
|
153
|
+
end
|
154
|
+
|
155
|
+
define_method("#{accessor_key}_was") do
|
156
|
+
return unless attribute_changed?(store_attribute)
|
157
|
+
prev_store, _new_store = changes[store_attribute]
|
158
|
+
prev_store&.dig(key)
|
159
|
+
end
|
160
|
+
|
161
|
+
define_method("saved_change_to_#{accessor_key}?") do
|
162
|
+
return false unless saved_change_to_attribute?(store_attribute)
|
163
|
+
prev_store, new_store = saved_change_to_attribute(store_attribute)
|
164
|
+
prev_store&.dig(key) != new_store&.dig(key)
|
165
|
+
end
|
166
|
+
|
167
|
+
define_method("saved_change_to_#{accessor_key}") do
|
168
|
+
return unless saved_change_to_attribute?(store_attribute)
|
169
|
+
prev_store, new_store = saved_change_to_attribute(store_attribute)
|
170
|
+
[prev_store&.dig(key), new_store&.dig(key)]
|
171
|
+
end
|
172
|
+
|
173
|
+
define_method("#{accessor_key}_before_last_save") do
|
174
|
+
return unless saved_change_to_attribute?(store_attribute)
|
175
|
+
prev_store, _new_store = saved_change_to_attribute(store_attribute)
|
176
|
+
prev_store&.dig(key)
|
177
|
+
end
|
99
178
|
end
|
100
179
|
end
|
101
180
|
|
@@ -40,11 +40,11 @@ module ActiveRecord
|
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
43
|
-
def save(
|
43
|
+
def save(*, **) # :nodoc:
|
44
44
|
SuppressorRegistry.suppressed[self.class.name] ? true : super
|
45
45
|
end
|
46
46
|
|
47
|
-
def save!(
|
47
|
+
def save!(*, **) # :nodoc:
|
48
48
|
SuppressorRegistry.suppressed[self.class.name] ? true : super
|
49
49
|
end
|
50
50
|
end
|
@@ -4,17 +4,18 @@ module ActiveRecord
|
|
4
4
|
class TableMetadata # :nodoc:
|
5
5
|
delegate :foreign_type, :foreign_key, :join_primary_key, :join_foreign_key, to: :association, prefix: true
|
6
6
|
|
7
|
-
def initialize(klass, arel_table, association = nil)
|
7
|
+
def initialize(klass, arel_table, association = nil, types = klass)
|
8
8
|
@klass = klass
|
9
|
+
@types = types
|
9
10
|
@arel_table = arel_table
|
10
11
|
@association = association
|
11
12
|
end
|
12
13
|
|
13
14
|
def resolve_column_aliases(hash)
|
14
15
|
new_hash = hash.dup
|
15
|
-
hash.
|
16
|
-
if
|
17
|
-
new_hash[
|
16
|
+
hash.each_key do |key|
|
17
|
+
if key.is_a?(Symbol) && new_key = klass.attribute_aliases[key.to_s]
|
18
|
+
new_hash[new_key] = new_hash.delete(key)
|
18
19
|
end
|
19
20
|
end
|
20
21
|
new_hash
|
@@ -29,11 +30,7 @@ module ActiveRecord
|
|
29
30
|
end
|
30
31
|
|
31
32
|
def type(column_name)
|
32
|
-
|
33
|
-
klass.type_for_attribute(column_name)
|
34
|
-
else
|
35
|
-
Type.default_value
|
36
|
-
end
|
33
|
+
types.type_for_attribute(column_name)
|
37
34
|
end
|
38
35
|
|
39
36
|
def has_column?(column_name)
|
@@ -48,17 +45,20 @@ module ActiveRecord
|
|
48
45
|
association = klass._reflect_on_association(table_name) || klass._reflect_on_association(table_name.to_s.singularize)
|
49
46
|
|
50
47
|
if !association && table_name == arel_table.name
|
51
|
-
|
48
|
+
self
|
52
49
|
elsif association && !association.polymorphic?
|
53
50
|
association_klass = association.klass
|
54
51
|
arel_table = association_klass.arel_table.alias(table_name)
|
52
|
+
TableMetadata.new(association_klass, arel_table, association)
|
55
53
|
else
|
56
54
|
type_caster = TypeCaster::Connection.new(klass, table_name)
|
57
|
-
association_klass = nil
|
58
55
|
arel_table = Arel::Table.new(table_name, type_caster: type_caster)
|
56
|
+
TableMetadata.new(nil, arel_table, association, type_caster)
|
59
57
|
end
|
58
|
+
end
|
60
59
|
|
61
|
-
|
60
|
+
def associated_predicate_builder(table_name)
|
61
|
+
associated_table(table_name).predicate_builder
|
62
62
|
end
|
63
63
|
|
64
64
|
def polymorphic_association?
|
@@ -73,10 +73,18 @@ module ActiveRecord
|
|
73
73
|
klass.reflect_on_aggregation(aggregation_name)
|
74
74
|
end
|
75
75
|
|
76
|
-
# TODO Change this to private once we've dropped Ruby 2.2 support.
|
77
|
-
# Workaround for Ruby 2.2 "private attribute?" warning.
|
78
76
|
protected
|
77
|
+
def predicate_builder
|
78
|
+
if klass
|
79
|
+
predicate_builder = klass.predicate_builder.dup
|
80
|
+
predicate_builder.instance_variable_set(:@table, self)
|
81
|
+
predicate_builder
|
82
|
+
else
|
83
|
+
PredicateBuilder.new(self)
|
84
|
+
end
|
85
|
+
end
|
79
86
|
|
80
|
-
|
87
|
+
private
|
88
|
+
attr_reader :klass, :types, :arel_table, :association
|
81
89
|
end
|
82
90
|
end
|
@@ -1,5 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require "active_record/database_configurations"
|
4
|
+
|
3
5
|
module ActiveRecord
|
4
6
|
module Tasks # :nodoc:
|
5
7
|
class DatabaseAlreadyExists < StandardError; end # :nodoc:
|
@@ -8,7 +10,7 @@ module ActiveRecord
|
|
8
10
|
# ActiveRecord::Tasks::DatabaseTasks is a utility class, which encapsulates
|
9
11
|
# logic behind common tasks used to manage database and migrations.
|
10
12
|
#
|
11
|
-
# The tasks defined here are used with
|
13
|
+
# The tasks defined here are used with Rails commands provided by Active Record.
|
12
14
|
#
|
13
15
|
# In order to use DatabaseTasks, a few config values need to be set. All the needed
|
14
16
|
# config values are set by Rails already, so it's necessary to do it only if you
|
@@ -101,25 +103,30 @@ module ActiveRecord
|
|
101
103
|
@env ||= Rails.env
|
102
104
|
end
|
103
105
|
|
106
|
+
def spec
|
107
|
+
@spec ||= "primary"
|
108
|
+
end
|
109
|
+
|
104
110
|
def seed_loader
|
105
111
|
@seed_loader ||= Rails.application
|
106
112
|
end
|
107
113
|
|
108
114
|
def current_config(options = {})
|
109
115
|
options.reverse_merge! env: env
|
116
|
+
options[:spec] ||= "primary"
|
110
117
|
if options.has_key?(:config)
|
111
118
|
@current_config = options[:config]
|
112
119
|
else
|
113
|
-
@current_config ||= ActiveRecord::Base.configurations
|
120
|
+
@current_config ||= ActiveRecord::Base.configurations.configs_for(env_name: options[:env], spec_name: options[:spec]).config
|
114
121
|
end
|
115
122
|
end
|
116
123
|
|
117
124
|
def create(*arguments)
|
118
125
|
configuration = arguments.first
|
119
126
|
class_for_adapter(configuration["adapter"]).new(*arguments).create
|
120
|
-
$stdout.puts "Created database '#{configuration['database']}'"
|
127
|
+
$stdout.puts "Created database '#{configuration['database']}'" if verbose?
|
121
128
|
rescue DatabaseAlreadyExists
|
122
|
-
$stderr.puts "Database '#{configuration['database']}' already exists"
|
129
|
+
$stderr.puts "Database '#{configuration['database']}' already exists" if verbose?
|
123
130
|
rescue Exception => error
|
124
131
|
$stderr.puts error
|
125
132
|
$stderr.puts "Couldn't create '#{configuration['database']}' database. Please check your configuration."
|
@@ -134,8 +141,47 @@ module ActiveRecord
|
|
134
141
|
end
|
135
142
|
end
|
136
143
|
|
137
|
-
def
|
138
|
-
|
144
|
+
def setup_initial_database_yaml
|
145
|
+
return {} unless defined?(Rails)
|
146
|
+
|
147
|
+
begin
|
148
|
+
Rails.application.config.load_database_yaml
|
149
|
+
rescue
|
150
|
+
$stderr.puts "Rails couldn't infer whether you are using multiple databases from your database.yml and can't generate the tasks for the non-primary databases. If you'd like to use this feature, please simplify your ERB."
|
151
|
+
|
152
|
+
{}
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
def for_each(databases)
|
157
|
+
return {} unless defined?(Rails)
|
158
|
+
|
159
|
+
database_configs = ActiveRecord::DatabaseConfigurations.new(databases).configs_for(env_name: Rails.env)
|
160
|
+
|
161
|
+
# if this is a single database application we don't want tasks for each primary database
|
162
|
+
return if database_configs.count == 1
|
163
|
+
|
164
|
+
database_configs.each do |db_config|
|
165
|
+
yield db_config.spec_name
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
def raise_for_multi_db(environment = env, command:)
|
170
|
+
db_configs = ActiveRecord::Base.configurations.configs_for(env_name: environment)
|
171
|
+
|
172
|
+
if db_configs.count > 1
|
173
|
+
dbs_list = []
|
174
|
+
|
175
|
+
db_configs.each do |db|
|
176
|
+
dbs_list << "#{command}:#{db.spec_name}"
|
177
|
+
end
|
178
|
+
|
179
|
+
raise "You're using a multiple database application. To use `#{command}` you must run the namespaced task with a VERSION. Available tasks are #{dbs_list.to_sentence}."
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
def create_current(environment = env, spec_name = nil)
|
184
|
+
each_current_configuration(environment, spec_name) { |configuration|
|
139
185
|
create configuration
|
140
186
|
}
|
141
187
|
ActiveRecord::Base.establish_connection(environment.to_sym)
|
@@ -144,7 +190,7 @@ module ActiveRecord
|
|
144
190
|
def drop(*arguments)
|
145
191
|
configuration = arguments.first
|
146
192
|
class_for_adapter(configuration["adapter"]).new(*arguments).drop
|
147
|
-
$stdout.puts "Dropped database '#{configuration['database']}'"
|
193
|
+
$stdout.puts "Dropped database '#{configuration['database']}'" if verbose?
|
148
194
|
rescue ActiveRecord::NoDatabaseError
|
149
195
|
$stderr.puts "Database '#{configuration['database']}' does not exist"
|
150
196
|
rescue Exception => error
|
@@ -163,20 +209,56 @@ module ActiveRecord
|
|
163
209
|
}
|
164
210
|
end
|
165
211
|
|
212
|
+
def truncate_tables(configuration)
|
213
|
+
ActiveRecord::Base.connected_to(database: { truncation: configuration }) do
|
214
|
+
conn = ActiveRecord::Base.connection
|
215
|
+
table_names = conn.tables
|
216
|
+
table_names -= [
|
217
|
+
conn.schema_migration.table_name,
|
218
|
+
InternalMetadata.table_name
|
219
|
+
]
|
220
|
+
|
221
|
+
ActiveRecord::Base.connection.truncate_tables(*table_names)
|
222
|
+
end
|
223
|
+
end
|
224
|
+
private :truncate_tables
|
225
|
+
|
226
|
+
def truncate_all(environment = env)
|
227
|
+
ActiveRecord::Base.configurations.configs_for(env_name: environment).each do |db_config|
|
228
|
+
truncate_tables db_config.config
|
229
|
+
end
|
230
|
+
end
|
231
|
+
|
166
232
|
def migrate
|
167
233
|
check_target_version
|
168
234
|
|
169
|
-
verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] != "false" : true
|
170
235
|
scope = ENV["SCOPE"]
|
171
|
-
verbose_was, Migration.verbose = Migration.verbose, verbose
|
236
|
+
verbose_was, Migration.verbose = Migration.verbose, verbose?
|
237
|
+
|
172
238
|
Base.connection.migration_context.migrate(target_version) do |migration|
|
173
239
|
scope.blank? || scope == migration.scope
|
174
240
|
end
|
241
|
+
|
175
242
|
ActiveRecord::Base.clear_cache!
|
176
243
|
ensure
|
177
244
|
Migration.verbose = verbose_was
|
178
245
|
end
|
179
246
|
|
247
|
+
def migrate_status
|
248
|
+
unless ActiveRecord::Base.connection.schema_migration.table_exists?
|
249
|
+
Kernel.abort "Schema migrations table does not exist yet."
|
250
|
+
end
|
251
|
+
|
252
|
+
# output
|
253
|
+
puts "\ndatabase: #{ActiveRecord::Base.connection_config[:database]}\n\n"
|
254
|
+
puts "#{'Status'.center(8)} #{'Migration ID'.ljust(14)} Migration Name"
|
255
|
+
puts "-" * 50
|
256
|
+
ActiveRecord::Base.connection.migration_context.migrations_status.each do |status, version, name|
|
257
|
+
puts "#{status.center(8)} #{version.ljust(14)} #{name}"
|
258
|
+
end
|
259
|
+
puts
|
260
|
+
end
|
261
|
+
|
180
262
|
def check_target_version
|
181
263
|
if target_version && !(Migration::MigrationFilenameRegexp.match?(ENV["VERSION"]) || /\A\d+\z/.match?(ENV["VERSION"]))
|
182
264
|
raise "Invalid format of target version: `VERSION=#{ENV['VERSION']}`"
|
@@ -187,8 +269,8 @@ module ActiveRecord
|
|
187
269
|
ENV["VERSION"].to_i if ENV["VERSION"] && !ENV["VERSION"].empty?
|
188
270
|
end
|
189
271
|
|
190
|
-
def charset_current(environment = env)
|
191
|
-
charset ActiveRecord::Base.configurations
|
272
|
+
def charset_current(environment = env, specification_name = spec)
|
273
|
+
charset ActiveRecord::Base.configurations.configs_for(env_name: environment, spec_name: specification_name).config
|
192
274
|
end
|
193
275
|
|
194
276
|
def charset(*arguments)
|
@@ -196,8 +278,8 @@ module ActiveRecord
|
|
196
278
|
class_for_adapter(configuration["adapter"]).new(*arguments).charset
|
197
279
|
end
|
198
280
|
|
199
|
-
def collation_current(environment = env)
|
200
|
-
collation ActiveRecord::Base.configurations
|
281
|
+
def collation_current(environment = env, specification_name = spec)
|
282
|
+
collation ActiveRecord::Base.configurations.configs_for(env_name: environment, spec_name: specification_name).config
|
201
283
|
end
|
202
284
|
|
203
285
|
def collation(*arguments)
|
@@ -234,9 +316,10 @@ module ActiveRecord
|
|
234
316
|
class_for_adapter(configuration["adapter"]).new(*arguments).structure_load(filename, structure_load_flags)
|
235
317
|
end
|
236
318
|
|
237
|
-
def load_schema(configuration, format = ActiveRecord::Base.schema_format, file = nil, environment = env) # :nodoc:
|
238
|
-
file ||=
|
319
|
+
def load_schema(configuration, format = ActiveRecord::Base.schema_format, file = nil, environment = env, spec_name = "primary") # :nodoc:
|
320
|
+
file ||= dump_filename(spec_name, format)
|
239
321
|
|
322
|
+
verbose_was, Migration.verbose = Migration.verbose, verbose? && ENV["VERBOSE"]
|
240
323
|
check_schema_file(file)
|
241
324
|
ActiveRecord::Base.establish_connection(configuration)
|
242
325
|
|
@@ -250,27 +333,103 @@ module ActiveRecord
|
|
250
333
|
end
|
251
334
|
ActiveRecord::InternalMetadata.create_table
|
252
335
|
ActiveRecord::InternalMetadata[:environment] = environment
|
336
|
+
ActiveRecord::InternalMetadata[:schema_sha1] = schema_sha1(file)
|
337
|
+
ensure
|
338
|
+
Migration.verbose = verbose_was
|
339
|
+
end
|
340
|
+
|
341
|
+
def schema_up_to_date?(configuration, format = ActiveRecord::Base.schema_format, file = nil, environment = env, spec_name = "primary")
|
342
|
+
file ||= dump_filename(spec_name, format)
|
343
|
+
|
344
|
+
return true unless File.exist?(file)
|
345
|
+
|
346
|
+
ActiveRecord::Base.establish_connection(configuration)
|
347
|
+
return false unless ActiveRecord::InternalMetadata.table_exists?
|
348
|
+
ActiveRecord::InternalMetadata[:schema_sha1] == schema_sha1(file)
|
349
|
+
end
|
350
|
+
|
351
|
+
def reconstruct_from_schema(configuration, format = ActiveRecord::Base.schema_format, file = nil, environment = env, spec_name = "primary") # :nodoc:
|
352
|
+
file ||= dump_filename(spec_name, format)
|
353
|
+
|
354
|
+
check_schema_file(file)
|
355
|
+
|
356
|
+
ActiveRecord::Base.establish_connection(configuration)
|
357
|
+
|
358
|
+
if schema_up_to_date?(configuration, format, file, environment, spec_name)
|
359
|
+
truncate_tables(configuration)
|
360
|
+
else
|
361
|
+
purge(configuration)
|
362
|
+
load_schema(configuration, format, file, environment, spec_name)
|
363
|
+
end
|
364
|
+
rescue ActiveRecord::NoDatabaseError
|
365
|
+
create(configuration)
|
366
|
+
load_schema(configuration, format, file, environment, spec_name)
|
367
|
+
end
|
368
|
+
|
369
|
+
def dump_schema(configuration, format = ActiveRecord::Base.schema_format, spec_name = "primary") # :nodoc:
|
370
|
+
require "active_record/schema_dumper"
|
371
|
+
filename = dump_filename(spec_name, format)
|
372
|
+
connection = ActiveRecord::Base.connection
|
373
|
+
|
374
|
+
case format
|
375
|
+
when :ruby
|
376
|
+
File.open(filename, "w:utf-8") do |file|
|
377
|
+
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
|
378
|
+
end
|
379
|
+
when :sql
|
380
|
+
structure_dump(configuration, filename)
|
381
|
+
if connection.schema_migration.table_exists?
|
382
|
+
File.open(filename, "a") do |f|
|
383
|
+
f.puts connection.dump_schema_information
|
384
|
+
f.print "\n"
|
385
|
+
end
|
386
|
+
end
|
387
|
+
end
|
253
388
|
end
|
254
389
|
|
255
390
|
def schema_file(format = ActiveRecord::Base.schema_format)
|
391
|
+
File.join(db_dir, schema_file_type(format))
|
392
|
+
end
|
393
|
+
|
394
|
+
def schema_file_type(format = ActiveRecord::Base.schema_format)
|
256
395
|
case format
|
257
396
|
when :ruby
|
258
|
-
|
397
|
+
"schema.rb"
|
259
398
|
when :sql
|
260
|
-
|
399
|
+
"structure.sql"
|
400
|
+
end
|
401
|
+
end
|
402
|
+
|
403
|
+
def dump_filename(namespace, format = ActiveRecord::Base.schema_format)
|
404
|
+
filename = if namespace == "primary"
|
405
|
+
schema_file_type(format)
|
406
|
+
else
|
407
|
+
"#{namespace}_#{schema_file_type(format)}"
|
408
|
+
end
|
409
|
+
|
410
|
+
ENV["SCHEMA"] || File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, filename)
|
411
|
+
end
|
412
|
+
|
413
|
+
def cache_dump_filename(namespace)
|
414
|
+
filename = if namespace == "primary"
|
415
|
+
"schema_cache.yml"
|
416
|
+
else
|
417
|
+
"#{namespace}_schema_cache.yml"
|
261
418
|
end
|
419
|
+
|
420
|
+
ENV["SCHEMA_CACHE"] || File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, filename)
|
262
421
|
end
|
263
422
|
|
264
423
|
def load_schema_current(format = ActiveRecord::Base.schema_format, file = nil, environment = env)
|
265
|
-
each_current_configuration(environment) { |configuration,
|
266
|
-
load_schema
|
424
|
+
each_current_configuration(environment) { |configuration, spec_name, env|
|
425
|
+
load_schema(configuration, format, file, env, spec_name)
|
267
426
|
}
|
268
427
|
ActiveRecord::Base.establish_connection(environment.to_sym)
|
269
428
|
end
|
270
429
|
|
271
430
|
def check_schema_file(filename)
|
272
431
|
unless File.exist?(filename)
|
273
|
-
message =
|
432
|
+
message = +%{#{filename} doesn't exist yet. Run `rails db:migrate` to create it, then try again.}
|
274
433
|
message << %{ If you do not intend to use a database, you should instead alter #{Rails.root}/config/application.rb to limit the frameworks that will be loaded.} if defined?(::Rails.root)
|
275
434
|
Kernel.abort message
|
276
435
|
end
|
@@ -297,6 +456,9 @@ module ActiveRecord
|
|
297
456
|
end
|
298
457
|
|
299
458
|
private
|
459
|
+
def verbose?
|
460
|
+
ENV["VERBOSE"] ? ENV["VERBOSE"] != "false" : true
|
461
|
+
end
|
300
462
|
|
301
463
|
def class_for_adapter(adapter)
|
302
464
|
_key, task = @tasks.each_pair.detect { |pattern, _task| adapter[pattern] }
|
@@ -306,19 +468,22 @@ module ActiveRecord
|
|
306
468
|
task.is_a?(String) ? task.constantize : task
|
307
469
|
end
|
308
470
|
|
309
|
-
def each_current_configuration(environment)
|
471
|
+
def each_current_configuration(environment, spec_name = nil)
|
310
472
|
environments = [environment]
|
311
473
|
environments << "test" if environment == "development"
|
312
474
|
|
313
|
-
|
314
|
-
|
475
|
+
environments.each do |env|
|
476
|
+
ActiveRecord::Base.configurations.configs_for(env_name: env).each do |db_config|
|
477
|
+
next if spec_name && spec_name != db_config.spec_name
|
315
478
|
|
316
|
-
|
479
|
+
yield db_config.config, db_config.spec_name, env
|
480
|
+
end
|
317
481
|
end
|
318
482
|
end
|
319
483
|
|
320
484
|
def each_local_configuration
|
321
|
-
ActiveRecord::Base.configurations.
|
485
|
+
ActiveRecord::Base.configurations.configs_for.each do |db_config|
|
486
|
+
configuration = db_config.config
|
322
487
|
next unless configuration["database"]
|
323
488
|
|
324
489
|
if local_database?(configuration)
|
@@ -332,6 +497,10 @@ module ActiveRecord
|
|
332
497
|
def local_database?(configuration)
|
333
498
|
configuration["host"].blank? || LOCAL_HOSTS.include?(configuration["host"])
|
334
499
|
end
|
500
|
+
|
501
|
+
def schema_sha1(file)
|
502
|
+
Digest::SHA1.hexdigest(File.read(file))
|
503
|
+
end
|
335
504
|
end
|
336
505
|
end
|
337
506
|
end
|