activerecord 4.2.8 → 6.0.0
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 +5 -5
- data/CHANGELOG.md +612 -1583
- data/MIT-LICENSE +4 -2
- data/README.rdoc +13 -12
- data/examples/performance.rb +33 -32
- data/examples/simple.rb +5 -4
- data/lib/active_record.rb +41 -22
- data/lib/active_record/aggregations.rb +267 -251
- data/lib/active_record/association_relation.rb +11 -6
- data/lib/active_record/associations.rb +1737 -1597
- data/lib/active_record/associations/alias_tracker.rb +29 -35
- data/lib/active_record/associations/association.rb +125 -58
- data/lib/active_record/associations/association_scope.rb +103 -132
- data/lib/active_record/associations/belongs_to_association.rb +65 -60
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +8 -12
- data/lib/active_record/associations/builder/association.rb +27 -40
- data/lib/active_record/associations/builder/belongs_to.rb +69 -55
- data/lib/active_record/associations/builder/collection_association.rb +10 -33
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +52 -66
- data/lib/active_record/associations/builder/has_many.rb +8 -4
- data/lib/active_record/associations/builder/has_one.rb +46 -5
- data/lib/active_record/associations/builder/singular_association.rb +16 -10
- data/lib/active_record/associations/collection_association.rb +134 -286
- data/lib/active_record/associations/collection_proxy.rb +241 -146
- data/lib/active_record/associations/foreign_association.rb +10 -1
- data/lib/active_record/associations/has_many_association.rb +34 -97
- data/lib/active_record/associations/has_many_through_association.rb +60 -87
- data/lib/active_record/associations/has_one_association.rb +61 -49
- data/lib/active_record/associations/has_one_through_association.rb +20 -11
- data/lib/active_record/associations/join_dependency.rb +137 -167
- data/lib/active_record/associations/join_dependency/join_association.rb +38 -88
- data/lib/active_record/associations/join_dependency/join_base.rb +10 -9
- data/lib/active_record/associations/join_dependency/join_part.rb +14 -14
- data/lib/active_record/associations/preloader.rb +90 -92
- data/lib/active_record/associations/preloader/association.rb +90 -123
- data/lib/active_record/associations/preloader/through_association.rb +85 -65
- data/lib/active_record/associations/singular_association.rb +18 -39
- data/lib/active_record/associations/through_association.rb +38 -18
- data/lib/active_record/attribute_assignment.rb +56 -183
- data/lib/active_record/attribute_decorators.rb +39 -15
- data/lib/active_record/attribute_methods.rb +120 -135
- data/lib/active_record/attribute_methods/before_type_cast.rb +13 -8
- data/lib/active_record/attribute_methods/dirty.rb +174 -144
- data/lib/active_record/attribute_methods/primary_key.rb +91 -83
- data/lib/active_record/attribute_methods/query.rb +6 -5
- data/lib/active_record/attribute_methods/read.rb +20 -76
- data/lib/active_record/attribute_methods/serialization.rb +40 -20
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +58 -36
- data/lib/active_record/attribute_methods/write.rb +32 -54
- data/lib/active_record/attributes.rb +214 -82
- data/lib/active_record/autosave_association.rb +91 -37
- data/lib/active_record/base.rb +57 -45
- data/lib/active_record/callbacks.rb +100 -74
- data/lib/active_record/coders/json.rb +3 -1
- data/lib/active_record/coders/yaml_column.rb +24 -12
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +796 -296
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +26 -8
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +234 -115
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +82 -23
- data/lib/active_record/connection_adapters/abstract/quoting.rb +170 -53
- data/lib/active_record/connection_adapters/abstract/savepoints.rb +5 -3
- data/lib/active_record/connection_adapters/abstract/schema_creation.rb +74 -46
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +356 -227
- data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +79 -36
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +664 -244
- data/lib/active_record/connection_adapters/abstract/transaction.rb +191 -83
- data/lib/active_record/connection_adapters/abstract_adapter.rb +460 -204
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +510 -627
- data/lib/active_record/connection_adapters/column.rb +56 -43
- data/lib/active_record/connection_adapters/connection_specification.rb +174 -152
- data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +29 -0
- data/lib/active_record/connection_adapters/mysql/column.rb +27 -0
- data/lib/active_record/connection_adapters/mysql/database_statements.rb +200 -0
- data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +72 -0
- data/lib/active_record/connection_adapters/mysql/quoting.rb +81 -0
- data/lib/active_record/connection_adapters/mysql/schema_creation.rb +72 -0
- data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +95 -0
- data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +88 -0
- data/lib/active_record/connection_adapters/mysql/schema_statements.rb +264 -0
- data/lib/active_record/connection_adapters/mysql/type_metadata.rb +31 -0
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +58 -188
- data/lib/active_record/connection_adapters/postgresql/column.rb +21 -11
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +64 -114
- data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +44 -0
- data/lib/active_record/connection_adapters/postgresql/oid.rb +23 -25
- data/lib/active_record/connection_adapters/postgresql/oid/array.rb +50 -58
- data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +9 -8
- data/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +4 -2
- data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +5 -1
- data/lib/active_record/connection_adapters/postgresql/oid/date.rb +13 -1
- data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +9 -22
- data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +3 -1
- data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +5 -3
- data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +31 -19
- data/lib/active_record/connection_adapters/postgresql/oid/inet.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +3 -11
- data/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb +45 -0
- data/lib/active_record/connection_adapters/postgresql/oid/money.rb +7 -9
- data/lib/active_record/connection_adapters/postgresql/oid/{integer.rb → oid.rb} +6 -2
- data/lib/active_record/connection_adapters/postgresql/oid/point.rb +33 -11
- data/lib/active_record/connection_adapters/postgresql/oid/range.rb +52 -34
- data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +4 -5
- data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +58 -54
- data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +10 -5
- data/lib/active_record/connection_adapters/postgresql/oid/vector.rb +3 -1
- data/lib/active_record/connection_adapters/postgresql/oid/xml.rb +3 -1
- data/lib/active_record/connection_adapters/postgresql/quoting.rb +144 -47
- data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +27 -14
- data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +76 -0
- data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +178 -108
- data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +50 -0
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +470 -290
- data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +36 -0
- data/lib/active_record/connection_adapters/postgresql/utils.rb +12 -8
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +551 -356
- data/lib/active_record/connection_adapters/schema_cache.rb +72 -25
- data/lib/active_record/connection_adapters/sql_type_metadata.rb +37 -0
- data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +118 -0
- data/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb +21 -0
- data/lib/active_record/connection_adapters/sqlite3/quoting.rb +103 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +17 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +19 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb +18 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +137 -0
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +290 -345
- data/lib/active_record/connection_adapters/statement_pool.rb +34 -13
- data/lib/active_record/connection_handling.rb +176 -41
- data/lib/active_record/core.rb +251 -231
- data/lib/active_record/counter_cache.rb +67 -49
- 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 +79 -0
- data/lib/active_record/define_callbacks.rb +22 -0
- data/lib/active_record/dynamic_matchers.rb +87 -105
- data/lib/active_record/enum.rb +163 -86
- data/lib/active_record/errors.rb +188 -53
- data/lib/active_record/explain.rb +23 -11
- data/lib/active_record/explain_registry.rb +4 -2
- data/lib/active_record/explain_subscriber.rb +10 -5
- data/lib/active_record/fixture_set/file.rb +35 -9
- 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 +153 -0
- data/lib/active_record/fixture_set/table_rows.rb +47 -0
- data/lib/active_record/fixtures.rb +228 -499
- data/lib/active_record/gem_version.rb +5 -3
- data/lib/active_record/inheritance.rb +158 -112
- data/lib/active_record/insert_all.rb +179 -0
- data/lib/active_record/integration.rb +123 -29
- data/lib/active_record/internal_metadata.rb +53 -0
- data/lib/active_record/legacy_yaml_adapter.rb +21 -3
- data/lib/active_record/locale/en.yml +3 -2
- data/lib/active_record/locking/optimistic.rb +87 -96
- data/lib/active_record/locking/pessimistic.rb +18 -6
- data/lib/active_record/log_subscriber.rb +76 -33
- data/lib/active_record/middleware/database_selector.rb +75 -0
- data/lib/active_record/middleware/database_selector/resolver.rb +92 -0
- data/lib/active_record/middleware/database_selector/resolver/session.rb +45 -0
- data/lib/active_record/migration.rb +626 -283
- data/lib/active_record/migration/command_recorder.rb +177 -90
- data/lib/active_record/migration/compatibility.rb +244 -0
- data/lib/active_record/migration/join_table.rb +8 -6
- data/lib/active_record/model_schema.rb +314 -112
- data/lib/active_record/nested_attributes.rb +264 -222
- data/lib/active_record/no_touching.rb +14 -1
- data/lib/active_record/null_relation.rb +24 -37
- data/lib/active_record/persistence.rb +557 -125
- data/lib/active_record/query_cache.rb +19 -23
- data/lib/active_record/querying.rb +43 -29
- data/lib/active_record/railtie.rb +147 -46
- data/lib/active_record/railties/collection_cache_association_loading.rb +34 -0
- data/lib/active_record/railties/console_sandbox.rb +2 -0
- data/lib/active_record/railties/controller_runtime.rb +34 -33
- data/lib/active_record/railties/databases.rake +330 -197
- data/lib/active_record/readonly_attributes.rb +5 -4
- data/lib/active_record/reflection.rb +428 -279
- data/lib/active_record/relation.rb +518 -341
- data/lib/active_record/relation/batches.rb +207 -55
- data/lib/active_record/relation/batches/batch_enumerator.rb +69 -0
- data/lib/active_record/relation/calculations.rb +267 -253
- data/lib/active_record/relation/delegation.rb +70 -80
- data/lib/active_record/relation/finder_methods.rb +277 -241
- data/lib/active_record/relation/from_clause.rb +26 -0
- data/lib/active_record/relation/merger.rb +78 -87
- data/lib/active_record/relation/predicate_builder.rb +114 -119
- data/lib/active_record/relation/predicate_builder/array_handler.rb +27 -26
- data/lib/active_record/relation/predicate_builder/association_query_value.rb +43 -0
- data/lib/active_record/relation/predicate_builder/base_handler.rb +18 -0
- data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +19 -0
- data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +53 -0
- data/lib/active_record/relation/predicate_builder/range_handler.rb +22 -0
- data/lib/active_record/relation/predicate_builder/relation_handler.rb +7 -1
- data/lib/active_record/relation/query_attribute.rb +50 -0
- data/lib/active_record/relation/query_methods.rb +575 -394
- data/lib/active_record/relation/record_fetch_warning.rb +51 -0
- data/lib/active_record/relation/spawn_methods.rb +11 -13
- data/lib/active_record/relation/where_clause.rb +190 -0
- data/lib/active_record/relation/where_clause_factory.rb +33 -0
- data/lib/active_record/result.rb +79 -42
- data/lib/active_record/runtime_registry.rb +6 -4
- data/lib/active_record/sanitization.rb +144 -121
- data/lib/active_record/schema.rb +21 -24
- data/lib/active_record/schema_dumper.rb +112 -93
- data/lib/active_record/schema_migration.rb +24 -17
- data/lib/active_record/scoping.rb +45 -26
- data/lib/active_record/scoping/default.rb +101 -85
- data/lib/active_record/scoping/named.rb +86 -33
- data/lib/active_record/secure_token.rb +40 -0
- data/lib/active_record/serialization.rb +5 -5
- data/lib/active_record/statement_cache.rb +73 -36
- data/lib/active_record/store.rb +127 -42
- data/lib/active_record/suppressor.rb +61 -0
- data/lib/active_record/table_metadata.rb +75 -0
- data/lib/active_record/tasks/database_tasks.rb +308 -99
- data/lib/active_record/tasks/mysql_database_tasks.rb +55 -99
- data/lib/active_record/tasks/postgresql_database_tasks.rb +81 -41
- data/lib/active_record/tasks/sqlite_database_tasks.rb +38 -16
- data/lib/active_record/test_databases.rb +23 -0
- data/lib/active_record/test_fixtures.rb +224 -0
- data/lib/active_record/timestamp.rb +86 -40
- data/lib/active_record/touch_later.rb +66 -0
- data/lib/active_record/transactions.rb +216 -150
- data/lib/active_record/translation.rb +3 -1
- data/lib/active_record/type.rb +78 -23
- data/lib/active_record/type/adapter_specific_registry.rb +129 -0
- data/lib/active_record/type/date.rb +4 -45
- data/lib/active_record/type/date_time.rb +4 -49
- data/lib/active_record/type/decimal_without_scale.rb +6 -2
- data/lib/active_record/type/hash_lookup_type_map.rb +5 -3
- data/lib/active_record/type/internal/timezone.rb +17 -0
- data/lib/active_record/type/json.rb +30 -0
- data/lib/active_record/type/serialized.rb +24 -15
- data/lib/active_record/type/text.rb +2 -2
- data/lib/active_record/type/time.rb +11 -16
- data/lib/active_record/type/type_map.rb +15 -17
- data/lib/active_record/type/unsigned_integer.rb +9 -7
- data/lib/active_record/type_caster.rb +9 -0
- data/lib/active_record/type_caster/connection.rb +34 -0
- data/lib/active_record/type_caster/map.rb +20 -0
- data/lib/active_record/validations.rb +39 -35
- data/lib/active_record/validations/absence.rb +25 -0
- data/lib/active_record/validations/associated.rb +13 -4
- data/lib/active_record/validations/length.rb +26 -0
- data/lib/active_record/validations/presence.rb +14 -13
- data/lib/active_record/validations/uniqueness.rb +42 -55
- data/lib/active_record/version.rb +3 -1
- data/lib/arel.rb +51 -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 +257 -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 +204 -0
- data/lib/arel/visitors/dot.rb +297 -0
- data/lib/arel/visitors/ibm_db.rb +34 -0
- data/lib/arel/visitors/informix.rb +62 -0
- data/lib/arel/visitors/mssql.rb +157 -0
- data/lib/arel/visitors/mysql.rb +83 -0
- data/lib/arel/visitors/oracle.rb +159 -0
- data/lib/arel/visitors/oracle12.rb +66 -0
- data/lib/arel/visitors/postgresql.rb +110 -0
- data/lib/arel/visitors/sqlite.rb +39 -0
- data/lib/arel/visitors/to_sql.rb +889 -0
- data/lib/arel/visitors/visitor.rb +46 -0
- data/lib/arel/visitors/where_sql.rb +23 -0
- data/lib/arel/window_predications.rb +9 -0
- data/lib/rails/generators/active_record.rb +7 -5
- data/lib/rails/generators/active_record/application_record/application_record_generator.rb +27 -0
- data/lib/rails/generators/active_record/application_record/templates/application_record.rb.tt +5 -0
- data/lib/rails/generators/active_record/migration.rb +31 -1
- data/lib/rails/generators/active_record/migration/migration_generator.rb +42 -37
- data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb.tt +24 -0
- data/lib/rails/generators/active_record/migration/templates/{migration.rb → migration.rb.tt} +11 -2
- data/lib/rails/generators/active_record/model/model_generator.rb +19 -22
- data/lib/rails/generators/active_record/model/templates/model.rb.tt +22 -0
- data/lib/rails/generators/active_record/model/templates/{module.rb → module.rb.tt} +0 -0
- metadata +164 -60
- data/lib/active_record/associations/preloader/belongs_to.rb +0 -17
- data/lib/active_record/associations/preloader/collection_association.rb +0 -24
- data/lib/active_record/associations/preloader/has_many.rb +0 -17
- data/lib/active_record/associations/preloader/has_many_through.rb +0 -19
- data/lib/active_record/associations/preloader/has_one.rb +0 -23
- data/lib/active_record/associations/preloader/has_one_through.rb +0 -9
- data/lib/active_record/associations/preloader/singular_association.rb +0 -21
- data/lib/active_record/attribute.rb +0 -163
- data/lib/active_record/attribute_set.rb +0 -81
- data/lib/active_record/attribute_set/builder.rb +0 -106
- data/lib/active_record/connection_adapters/mysql_adapter.rb +0 -491
- data/lib/active_record/connection_adapters/postgresql/array_parser.rb +0 -93
- data/lib/active_record/connection_adapters/postgresql/oid/float.rb +0 -21
- data/lib/active_record/connection_adapters/postgresql/oid/infinity.rb +0 -13
- data/lib/active_record/connection_adapters/postgresql/oid/json.rb +0 -35
- data/lib/active_record/connection_adapters/postgresql/oid/time.rb +0 -11
- data/lib/active_record/railties/jdbcmysql_error.rb +0 -16
- data/lib/active_record/serializers/xml_serializer.rb +0 -193
- data/lib/active_record/type/big_integer.rb +0 -13
- data/lib/active_record/type/binary.rb +0 -50
- data/lib/active_record/type/boolean.rb +0 -31
- data/lib/active_record/type/decimal.rb +0 -58
- data/lib/active_record/type/decorator.rb +0 -14
- data/lib/active_record/type/float.rb +0 -19
- data/lib/active_record/type/integer.rb +0 -59
- data/lib/active_record/type/mutable.rb +0 -16
- data/lib/active_record/type/numeric.rb +0 -36
- data/lib/active_record/type/string.rb +0 -40
- data/lib/active_record/type/time_value.rb +0 -38
- data/lib/active_record/type/value.rb +0 -110
- data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb +0 -19
- data/lib/rails/generators/active_record/model/templates/model.rb +0 -10
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module Railties # :nodoc:
|
5
|
+
module CollectionCacheAssociationLoading #:nodoc:
|
6
|
+
def setup(context, options, as, block)
|
7
|
+
@relation = relation_from_options(options)
|
8
|
+
|
9
|
+
super
|
10
|
+
end
|
11
|
+
|
12
|
+
def relation_from_options(cached: nil, partial: nil, collection: nil, **_)
|
13
|
+
return unless cached
|
14
|
+
|
15
|
+
relation = partial if partial.is_a?(ActiveRecord::Relation)
|
16
|
+
relation ||= collection if collection.is_a?(ActiveRecord::Relation)
|
17
|
+
|
18
|
+
if relation && !relation.loaded?
|
19
|
+
relation.skip_preloading!
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def collection_without_template(*)
|
24
|
+
@relation.preload_associations(@collection) if @relation
|
25
|
+
super
|
26
|
+
end
|
27
|
+
|
28
|
+
def collection_with_template(*)
|
29
|
+
@relation.preload_associations(@collection) if @relation
|
30
|
+
super
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -1,50 +1,51 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/core_ext/module/attr_internal"
|
4
|
+
require "active_record/log_subscriber"
|
3
5
|
|
4
6
|
module ActiveRecord
|
5
7
|
module Railties # :nodoc:
|
6
8
|
module ControllerRuntime #:nodoc:
|
7
9
|
extend ActiveSupport::Concern
|
8
10
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
# because of queries in middleware or in cases we are streaming
|
16
|
-
# and it won't be cleaned up by the method below.
|
17
|
-
ActiveRecord::LogSubscriber.reset_runtime
|
18
|
-
super
|
11
|
+
module ClassMethods # :nodoc:
|
12
|
+
def log_process_action(payload)
|
13
|
+
messages, db_runtime = super, payload[:db_runtime]
|
14
|
+
messages << ("ActiveRecord: %.1fms" % db_runtime.to_f) if db_runtime
|
15
|
+
messages
|
16
|
+
end
|
19
17
|
end
|
20
18
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
runtime
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
else
|
19
|
+
private
|
20
|
+
attr_internal :db_runtime
|
21
|
+
|
22
|
+
def process_action(action, *args)
|
23
|
+
# We also need to reset the runtime before each action
|
24
|
+
# because of queries in middleware or in cases we are streaming
|
25
|
+
# and it won't be cleaned up by the method below.
|
26
|
+
ActiveRecord::LogSubscriber.reset_runtime
|
30
27
|
super
|
31
28
|
end
|
32
|
-
end
|
33
29
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
30
|
+
def cleanup_view_runtime
|
31
|
+
if logger && logger.info? && ActiveRecord::Base.connected?
|
32
|
+
db_rt_before_render = ActiveRecord::LogSubscriber.reset_runtime
|
33
|
+
self.db_runtime = (db_runtime || 0) + db_rt_before_render
|
34
|
+
runtime = super
|
35
|
+
db_rt_after_render = ActiveRecord::LogSubscriber.reset_runtime
|
36
|
+
self.db_runtime += db_rt_after_render
|
37
|
+
runtime - db_rt_after_render
|
38
|
+
else
|
39
|
+
super
|
40
|
+
end
|
38
41
|
end
|
39
|
-
end
|
40
42
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
43
|
+
def append_info_to_payload(payload)
|
44
|
+
super
|
45
|
+
if ActiveRecord::Base.connected?
|
46
|
+
payload[:db_runtime] = (db_runtime || 0) + ActiveRecord::LogSubscriber.reset_runtime
|
47
|
+
end
|
46
48
|
end
|
47
|
-
end
|
48
49
|
end
|
49
50
|
end
|
50
51
|
end
|
@@ -1,48 +1,90 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_record"
|
4
|
+
|
5
|
+
databases = ActiveRecord::Tasks::DatabaseTasks.setup_initial_database_yaml
|
2
6
|
|
3
7
|
db_namespace = namespace :db do
|
4
|
-
|
8
|
+
desc "Set the environment value for the database"
|
9
|
+
task "environment:set" => :load_config do
|
10
|
+
ActiveRecord::InternalMetadata.create_table
|
11
|
+
ActiveRecord::InternalMetadata[:environment] = ActiveRecord::Base.connection.migration_context.current_environment
|
12
|
+
end
|
13
|
+
|
14
|
+
task check_protected_environments: :load_config do
|
15
|
+
ActiveRecord::Tasks::DatabaseTasks.check_protected_environments!
|
16
|
+
end
|
17
|
+
|
18
|
+
task load_config: :environment do
|
5
19
|
ActiveRecord::Base.configurations = ActiveRecord::Tasks::DatabaseTasks.database_configuration || {}
|
6
20
|
ActiveRecord::Migrator.migrations_paths = ActiveRecord::Tasks::DatabaseTasks.migrations_paths
|
7
21
|
end
|
8
22
|
|
9
23
|
namespace :create do
|
10
|
-
task :
|
24
|
+
task all: :load_config do
|
11
25
|
ActiveRecord::Tasks::DatabaseTasks.create_all
|
12
26
|
end
|
27
|
+
|
28
|
+
ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |spec_name|
|
29
|
+
desc "Create #{spec_name} database for current environment"
|
30
|
+
task spec_name => :load_config do
|
31
|
+
db_config = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, spec_name: spec_name)
|
32
|
+
ActiveRecord::Tasks::DatabaseTasks.create(db_config.config)
|
33
|
+
end
|
34
|
+
end
|
13
35
|
end
|
14
36
|
|
15
|
-
desc
|
16
|
-
task :
|
37
|
+
desc "Creates the database from DATABASE_URL or config/database.yml for the current RAILS_ENV (use db:create:all to create all databases in the config). Without RAILS_ENV or when RAILS_ENV is development, it defaults to creating the development and test databases."
|
38
|
+
task create: [:load_config] do
|
17
39
|
ActiveRecord::Tasks::DatabaseTasks.create_current
|
18
40
|
end
|
19
41
|
|
20
42
|
namespace :drop do
|
21
|
-
task :
|
43
|
+
task all: [:load_config, :check_protected_environments] do
|
22
44
|
ActiveRecord::Tasks::DatabaseTasks.drop_all
|
23
45
|
end
|
46
|
+
|
47
|
+
ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |spec_name|
|
48
|
+
desc "Drop #{spec_name} database for current environment"
|
49
|
+
task spec_name => [:load_config, :check_protected_environments] do
|
50
|
+
db_config = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, spec_name: spec_name)
|
51
|
+
ActiveRecord::Tasks::DatabaseTasks.drop(db_config.config)
|
52
|
+
end
|
53
|
+
end
|
24
54
|
end
|
25
55
|
|
26
|
-
desc
|
27
|
-
task :
|
56
|
+
desc "Drops the database from DATABASE_URL or config/database.yml for the current RAILS_ENV (use db:drop:all to drop all databases in the config). Without RAILS_ENV or when RAILS_ENV is development, it defaults to dropping the development and test databases."
|
57
|
+
task drop: [:load_config, :check_protected_environments] do
|
58
|
+
db_namespace["drop:_unsafe"].invoke
|
59
|
+
end
|
60
|
+
|
61
|
+
task "drop:_unsafe" => [:load_config] do
|
28
62
|
ActiveRecord::Tasks::DatabaseTasks.drop_current
|
29
63
|
end
|
30
64
|
|
31
65
|
namespace :purge do
|
32
|
-
task :
|
66
|
+
task all: [:load_config, :check_protected_environments] do
|
33
67
|
ActiveRecord::Tasks::DatabaseTasks.purge_all
|
34
68
|
end
|
35
69
|
end
|
36
70
|
|
37
|
-
# desc "
|
38
|
-
task :
|
71
|
+
# desc "Truncates tables of each database for current environment"
|
72
|
+
task truncate_all: [:load_config, :check_protected_environments] do
|
73
|
+
ActiveRecord::Tasks::DatabaseTasks.truncate_all
|
74
|
+
end
|
75
|
+
|
76
|
+
# desc "Empty the database from DATABASE_URL or config/database.yml for the current RAILS_ENV (use db:purge:all to purge all databases in the config). Without RAILS_ENV it defaults to purging the development and test databases."
|
77
|
+
task purge: [:load_config, :check_protected_environments] do
|
39
78
|
ActiveRecord::Tasks::DatabaseTasks.purge_current
|
40
79
|
end
|
41
80
|
|
42
81
|
desc "Migrate the database (options: VERSION=x, VERBOSE=false, SCOPE=blog)."
|
43
|
-
task
|
44
|
-
ActiveRecord::Tasks::DatabaseTasks.
|
45
|
-
|
82
|
+
task migrate: :load_config do
|
83
|
+
ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env).each do |db_config|
|
84
|
+
ActiveRecord::Base.establish_connection(db_config.config)
|
85
|
+
ActiveRecord::Tasks::DatabaseTasks.migrate
|
86
|
+
end
|
87
|
+
db_namespace["_dump"].invoke
|
46
88
|
end
|
47
89
|
|
48
90
|
# IMPORTANT: This task won't dump the schema if ActiveRecord::Base.dump_schema_after_migration is set to false
|
@@ -57,168 +99,276 @@ db_namespace = namespace :db do
|
|
57
99
|
end
|
58
100
|
# Allow this task to be called as many times as required. An example is the
|
59
101
|
# migrate:redo task, which calls other two internally that depend on this one.
|
60
|
-
db_namespace[
|
102
|
+
db_namespace["_dump"].reenable
|
61
103
|
end
|
62
104
|
|
63
105
|
namespace :migrate do
|
106
|
+
ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |spec_name|
|
107
|
+
desc "Migrate #{spec_name} database for current environment"
|
108
|
+
task spec_name => :load_config do
|
109
|
+
db_config = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, spec_name: spec_name)
|
110
|
+
ActiveRecord::Base.establish_connection(db_config.config)
|
111
|
+
ActiveRecord::Tasks::DatabaseTasks.migrate
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
64
115
|
# desc 'Rollbacks the database one migration and re migrate up (options: STEP=x, VERSION=x).'
|
65
|
-
task
|
66
|
-
if ENV[
|
67
|
-
|
68
|
-
|
116
|
+
task redo: :load_config do
|
117
|
+
raise "Empty VERSION provided" if ENV["VERSION"] && ENV["VERSION"].empty?
|
118
|
+
|
119
|
+
if ENV["VERSION"]
|
120
|
+
db_namespace["migrate:down"].invoke
|
121
|
+
db_namespace["migrate:up"].invoke
|
69
122
|
else
|
70
|
-
db_namespace[
|
71
|
-
db_namespace[
|
123
|
+
db_namespace["rollback"].invoke
|
124
|
+
db_namespace["migrate"].invoke
|
72
125
|
end
|
73
126
|
end
|
74
127
|
|
75
128
|
# desc 'Resets your database using your migrations for the current environment'
|
76
|
-
task :
|
129
|
+
task reset: ["db:drop", "db:create", "db:migrate"]
|
77
130
|
|
78
131
|
# desc 'Runs the "up" for a given migration VERSION.'
|
79
|
-
task
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
132
|
+
task up: :load_config do
|
133
|
+
ActiveRecord::Tasks::DatabaseTasks.raise_for_multi_db(command: "db:migrate:up")
|
134
|
+
|
135
|
+
raise "VERSION is required" if !ENV["VERSION"] || ENV["VERSION"].empty?
|
136
|
+
|
137
|
+
ActiveRecord::Tasks::DatabaseTasks.check_target_version
|
138
|
+
|
139
|
+
ActiveRecord::Base.connection.migration_context.run(
|
140
|
+
:up,
|
141
|
+
ActiveRecord::Tasks::DatabaseTasks.target_version
|
142
|
+
)
|
143
|
+
db_namespace["_dump"].invoke
|
144
|
+
end
|
145
|
+
|
146
|
+
namespace :up do
|
147
|
+
ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |spec_name|
|
148
|
+
task spec_name => :load_config do
|
149
|
+
raise "VERSION is required" if !ENV["VERSION"] || ENV["VERSION"].empty?
|
150
|
+
|
151
|
+
db_config = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, spec_name: spec_name)
|
152
|
+
|
153
|
+
ActiveRecord::Base.establish_connection(db_config.config)
|
154
|
+
ActiveRecord::Tasks::DatabaseTasks.check_target_version
|
155
|
+
ActiveRecord::Base.connection.migration_context.run(
|
156
|
+
:up,
|
157
|
+
ActiveRecord::Tasks::DatabaseTasks.target_version
|
158
|
+
)
|
159
|
+
|
160
|
+
db_namespace["_dump"].invoke
|
161
|
+
end
|
162
|
+
end
|
84
163
|
end
|
85
164
|
|
86
165
|
# desc 'Runs the "down" for a given migration VERSION.'
|
87
|
-
task
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
166
|
+
task down: :load_config do
|
167
|
+
ActiveRecord::Tasks::DatabaseTasks.raise_for_multi_db(command: "db:migrate:down")
|
168
|
+
|
169
|
+
raise "VERSION is required - To go down one migration, use db:rollback" if !ENV["VERSION"] || ENV["VERSION"].empty?
|
170
|
+
|
171
|
+
ActiveRecord::Tasks::DatabaseTasks.check_target_version
|
172
|
+
|
173
|
+
ActiveRecord::Base.connection.migration_context.run(
|
174
|
+
:down,
|
175
|
+
ActiveRecord::Tasks::DatabaseTasks.target_version
|
176
|
+
)
|
177
|
+
db_namespace["_dump"].invoke
|
92
178
|
end
|
93
179
|
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
180
|
+
namespace :down do
|
181
|
+
ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |spec_name|
|
182
|
+
task spec_name => :load_config do
|
183
|
+
raise "VERSION is required" if !ENV["VERSION"] || ENV["VERSION"].empty?
|
184
|
+
|
185
|
+
db_config = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, spec_name: spec_name)
|
186
|
+
|
187
|
+
ActiveRecord::Base.establish_connection(db_config.config)
|
188
|
+
ActiveRecord::Tasks::DatabaseTasks.check_target_version
|
189
|
+
ActiveRecord::Base.connection.migration_context.run(
|
190
|
+
:down,
|
191
|
+
ActiveRecord::Tasks::DatabaseTasks.target_version
|
192
|
+
)
|
193
|
+
|
194
|
+
db_namespace["_dump"].invoke
|
195
|
+
end
|
98
196
|
end
|
99
|
-
|
100
|
-
|
101
|
-
file_list =
|
102
|
-
ActiveRecord::Migrator.migrations_paths.flat_map do |path|
|
103
|
-
# match "20091231235959_some_name.rb" and "001_some_name.rb" pattern
|
104
|
-
Dir.foreach(path).grep(/^(\d{3,})_(.+)\.rb$/) do
|
105
|
-
version = ActiveRecord::SchemaMigration.normalize_migration_number($1)
|
106
|
-
status = db_list.delete(version) ? 'up' : 'down'
|
107
|
-
[status, version, $2.humanize]
|
108
|
-
end
|
109
|
-
end
|
197
|
+
end
|
110
198
|
|
111
|
-
|
112
|
-
|
199
|
+
desc "Display status of migrations"
|
200
|
+
task status: :load_config do
|
201
|
+
ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env).each do |db_config|
|
202
|
+
ActiveRecord::Base.establish_connection(db_config.config)
|
203
|
+
ActiveRecord::Tasks::DatabaseTasks.migrate_status
|
113
204
|
end
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
205
|
+
end
|
206
|
+
|
207
|
+
namespace :status do
|
208
|
+
ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |spec_name|
|
209
|
+
desc "Display status of migrations for #{spec_name} database"
|
210
|
+
task spec_name => :load_config do
|
211
|
+
db_config = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, spec_name: spec_name)
|
212
|
+
ActiveRecord::Base.establish_connection(db_config.config)
|
213
|
+
ActiveRecord::Tasks::DatabaseTasks.migrate_status
|
214
|
+
end
|
120
215
|
end
|
121
|
-
puts
|
122
216
|
end
|
123
217
|
end
|
124
218
|
|
125
|
-
desc
|
126
|
-
task
|
127
|
-
step = ENV[
|
128
|
-
ActiveRecord::
|
129
|
-
db_namespace[
|
219
|
+
desc "Rolls the schema back to the previous version (specify steps w/ STEP=n)."
|
220
|
+
task rollback: :load_config do
|
221
|
+
step = ENV["STEP"] ? ENV["STEP"].to_i : 1
|
222
|
+
ActiveRecord::Base.connection.migration_context.rollback(step)
|
223
|
+
db_namespace["_dump"].invoke
|
130
224
|
end
|
131
225
|
|
132
226
|
# desc 'Pushes the schema to the next version (specify steps w/ STEP=n).'
|
133
|
-
task
|
134
|
-
step = ENV[
|
135
|
-
ActiveRecord::
|
136
|
-
db_namespace[
|
227
|
+
task forward: :load_config do
|
228
|
+
step = ENV["STEP"] ? ENV["STEP"].to_i : 1
|
229
|
+
ActiveRecord::Base.connection.migration_context.forward(step)
|
230
|
+
db_namespace["_dump"].invoke
|
137
231
|
end
|
138
232
|
|
139
233
|
# desc 'Drops and recreates the database from db/schema.rb for the current environment and loads the seeds.'
|
140
|
-
task :
|
141
|
-
db_namespace["drop"].invoke
|
142
|
-
db_namespace["setup"].invoke
|
143
|
-
end
|
234
|
+
task reset: [ "db:drop", "db:setup" ]
|
144
235
|
|
145
236
|
# desc "Retrieves the charset for the current environment's database"
|
146
|
-
task
|
237
|
+
task charset: :load_config do
|
147
238
|
puts ActiveRecord::Tasks::DatabaseTasks.charset_current
|
148
239
|
end
|
149
240
|
|
150
241
|
# desc "Retrieves the collation for the current environment's database"
|
151
|
-
task
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
$stderr.puts 'Sorry, your database adapter is not supported yet. Feel free to submit a patch.'
|
156
|
-
end
|
242
|
+
task collation: :load_config do
|
243
|
+
puts ActiveRecord::Tasks::DatabaseTasks.collation_current
|
244
|
+
rescue NoMethodError
|
245
|
+
$stderr.puts "Sorry, your database adapter is not supported yet. Feel free to submit a patch."
|
157
246
|
end
|
158
247
|
|
159
|
-
desc
|
160
|
-
task
|
161
|
-
puts "Current version: #{ActiveRecord::
|
248
|
+
desc "Retrieves the current schema version number"
|
249
|
+
task version: :load_config do
|
250
|
+
puts "Current version: #{ActiveRecord::Base.connection.migration_context.current_version}"
|
162
251
|
end
|
163
252
|
|
164
253
|
# desc "Raises an error if there are pending migrations"
|
165
|
-
task :
|
166
|
-
pending_migrations = ActiveRecord::
|
254
|
+
task abort_if_pending_migrations: :load_config do
|
255
|
+
pending_migrations = ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env).flat_map do |db_config|
|
256
|
+
ActiveRecord::Base.establish_connection(db_config.config)
|
257
|
+
|
258
|
+
ActiveRecord::Base.connection.migration_context.open.pending_migrations
|
259
|
+
end
|
167
260
|
|
168
261
|
if pending_migrations.any?
|
169
262
|
puts "You have #{pending_migrations.size} pending #{pending_migrations.size > 1 ? 'migrations:' : 'migration:'}"
|
170
263
|
pending_migrations.each do |pending_migration|
|
171
|
-
puts
|
264
|
+
puts " %4d %s" % [pending_migration.version, pending_migration.name]
|
265
|
+
end
|
266
|
+
abort %{Run `rails db:migrate` to update your database then try again.}
|
267
|
+
end
|
268
|
+
ensure
|
269
|
+
ActiveRecord::Base.establish_connection(ActiveRecord::Tasks::DatabaseTasks.env.to_sym)
|
270
|
+
end
|
271
|
+
|
272
|
+
namespace :abort_if_pending_migrations do
|
273
|
+
ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |spec_name|
|
274
|
+
# desc "Raises an error if there are pending migrations for #{spec_name} database"
|
275
|
+
task spec_name => :load_config do
|
276
|
+
db_config = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, spec_name: spec_name)
|
277
|
+
ActiveRecord::Base.establish_connection(db_config.config)
|
278
|
+
|
279
|
+
pending_migrations = ActiveRecord::Base.connection.migration_context.open.pending_migrations
|
280
|
+
|
281
|
+
if pending_migrations.any?
|
282
|
+
puts "You have #{pending_migrations.size} pending #{pending_migrations.size > 1 ? 'migrations:' : 'migration:'}"
|
283
|
+
pending_migrations.each do |pending_migration|
|
284
|
+
puts " %4d %s" % [pending_migration.version, pending_migration.name]
|
285
|
+
end
|
286
|
+
abort %{Run `rails db:migrate:#{spec_name}` to update your database then try again.}
|
287
|
+
end
|
172
288
|
end
|
173
|
-
abort %{Run `rake db:migrate` to update your database then try again.}
|
174
289
|
end
|
175
290
|
end
|
176
291
|
|
177
|
-
desc
|
178
|
-
task :
|
292
|
+
desc "Creates the database, loads the schema, and initializes with the seed data (use db:reset to also drop the database first)"
|
293
|
+
task setup: ["db:schema:load_if_ruby", "db:structure:load_if_sql", :seed]
|
294
|
+
|
295
|
+
desc "Runs setup if database does not exist, or runs migrations if it does"
|
296
|
+
task prepare: :load_config do
|
297
|
+
seed = false
|
179
298
|
|
180
|
-
|
181
|
-
|
182
|
-
|
299
|
+
ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env).each do |db_config|
|
300
|
+
ActiveRecord::Base.establish_connection(db_config.config)
|
301
|
+
|
302
|
+
# Skipped when no database
|
303
|
+
ActiveRecord::Tasks::DatabaseTasks.migrate
|
304
|
+
if ActiveRecord::Base.dump_schema_after_migration
|
305
|
+
ActiveRecord::Tasks::DatabaseTasks.dump_schema(db_config.config, ActiveRecord::Base.schema_format, db_config.spec_name)
|
306
|
+
end
|
307
|
+
|
308
|
+
rescue ActiveRecord::NoDatabaseError
|
309
|
+
ActiveRecord::Tasks::DatabaseTasks.create_current(db_config.env_name, db_config.spec_name)
|
310
|
+
ActiveRecord::Tasks::DatabaseTasks.load_schema(
|
311
|
+
db_config.config,
|
312
|
+
ActiveRecord::Base.schema_format,
|
313
|
+
nil,
|
314
|
+
db_config.env_name,
|
315
|
+
db_config.spec_name
|
316
|
+
)
|
317
|
+
|
318
|
+
seed = true
|
319
|
+
end
|
320
|
+
|
321
|
+
ActiveRecord::Base.establish_connection
|
322
|
+
ActiveRecord::Tasks::DatabaseTasks.load_seed if seed
|
323
|
+
end
|
324
|
+
|
325
|
+
desc "Loads the seed data from db/seeds.rb"
|
326
|
+
task seed: :load_config do
|
327
|
+
db_namespace["abort_if_pending_migrations"].invoke
|
183
328
|
ActiveRecord::Tasks::DatabaseTasks.load_seed
|
184
329
|
end
|
185
330
|
|
331
|
+
namespace :seed do
|
332
|
+
desc "Truncates tables of each database for current environment and loads the seeds"
|
333
|
+
task replant: [:load_config, :truncate_all, :seed]
|
334
|
+
end
|
335
|
+
|
186
336
|
namespace :fixtures do
|
187
|
-
desc "
|
188
|
-
task
|
189
|
-
require
|
337
|
+
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."
|
338
|
+
task load: :load_config do
|
339
|
+
require "active_record/fixtures"
|
190
340
|
|
191
341
|
base_dir = ActiveRecord::Tasks::DatabaseTasks.fixtures_path
|
192
342
|
|
193
|
-
fixtures_dir = if ENV[
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
343
|
+
fixtures_dir = if ENV["FIXTURES_DIR"]
|
344
|
+
File.join base_dir, ENV["FIXTURES_DIR"]
|
345
|
+
else
|
346
|
+
base_dir
|
347
|
+
end
|
198
348
|
|
199
|
-
fixture_files = if ENV[
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
349
|
+
fixture_files = if ENV["FIXTURES"]
|
350
|
+
ENV["FIXTURES"].split(",")
|
351
|
+
else
|
352
|
+
# The use of String#[] here is to support namespaced fixtures.
|
353
|
+
Dir["#{fixtures_dir}/**/*.yml"].map { |f| f[(fixtures_dir.size + 1)..-5] }
|
354
|
+
end
|
205
355
|
|
206
356
|
ActiveRecord::FixtureSet.create_fixtures(fixtures_dir, fixture_files)
|
207
357
|
end
|
208
358
|
|
209
359
|
# desc "Search for a fixture given a LABEL or ID. Specify an alternative path (eg. spec/fixtures) using FIXTURES_PATH=spec/fixtures."
|
210
|
-
task
|
211
|
-
require
|
360
|
+
task identify: :load_config do
|
361
|
+
require "active_record/fixtures"
|
212
362
|
|
213
|
-
label, id = ENV[
|
214
|
-
raise
|
363
|
+
label, id = ENV["LABEL"], ENV["ID"]
|
364
|
+
raise "LABEL or ID required" if label.blank? && id.blank?
|
215
365
|
|
216
366
|
puts %Q(The fixture ID for "#{label}" is #{ActiveRecord::FixtureSet.identify(label)}.) if label
|
217
367
|
|
218
368
|
base_dir = ActiveRecord::Tasks::DatabaseTasks.fixtures_path
|
219
369
|
|
220
370
|
Dir["#{base_dir}/**/*.yml"].each do |file|
|
221
|
-
if data = YAML
|
371
|
+
if data = YAML.load(ERB.new(IO.read(file)).result)
|
222
372
|
data.each_key do |key|
|
223
373
|
key_id = ActiveRecord::FixtureSet.identify(key)
|
224
374
|
|
@@ -232,134 +382,113 @@ db_namespace = namespace :db do
|
|
232
382
|
end
|
233
383
|
|
234
384
|
namespace :schema do
|
235
|
-
desc
|
236
|
-
task
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
|
385
|
+
desc "Creates a db/schema.rb file that is portable against any DB supported by Active Record"
|
386
|
+
task dump: :load_config do
|
387
|
+
ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env).each do |db_config|
|
388
|
+
ActiveRecord::Base.establish_connection(db_config.config)
|
389
|
+
ActiveRecord::Tasks::DatabaseTasks.dump_schema(db_config.config, :ruby, db_config.spec_name)
|
241
390
|
end
|
242
|
-
|
391
|
+
|
392
|
+
db_namespace["schema:dump"].reenable
|
243
393
|
end
|
244
394
|
|
245
|
-
desc
|
246
|
-
task :
|
247
|
-
ActiveRecord::Tasks::DatabaseTasks.load_schema_current(:ruby, ENV[
|
395
|
+
desc "Loads a schema.rb file into the database"
|
396
|
+
task load: [:load_config, :check_protected_environments] do
|
397
|
+
ActiveRecord::Tasks::DatabaseTasks.load_schema_current(:ruby, ENV["SCHEMA"])
|
248
398
|
end
|
249
399
|
|
250
|
-
task :
|
400
|
+
task load_if_ruby: ["db:create", :environment] do
|
251
401
|
db_namespace["schema:load"].invoke if ActiveRecord::Base.schema_format == :ruby
|
252
402
|
end
|
253
403
|
|
254
404
|
namespace :cache do
|
255
|
-
desc
|
256
|
-
task
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
405
|
+
desc "Creates a db/schema_cache.yml file."
|
406
|
+
task dump: :load_config do
|
407
|
+
ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env).each do |db_config|
|
408
|
+
ActiveRecord::Base.establish_connection(db_config.config)
|
409
|
+
filename = ActiveRecord::Tasks::DatabaseTasks.cache_dump_filename(db_config.spec_name)
|
410
|
+
ActiveRecord::Tasks::DatabaseTasks.dump_schema_cache(
|
411
|
+
ActiveRecord::Base.connection,
|
412
|
+
filename,
|
413
|
+
)
|
414
|
+
end
|
263
415
|
end
|
264
416
|
|
265
|
-
desc
|
266
|
-
task
|
267
|
-
|
268
|
-
|
417
|
+
desc "Clears a db/schema_cache.yml file."
|
418
|
+
task clear: :load_config do
|
419
|
+
ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env).each do |db_config|
|
420
|
+
filename = ActiveRecord::Tasks::DatabaseTasks.cache_dump_filename(db_config.spec_name)
|
421
|
+
rm_f filename, verbose: false
|
422
|
+
end
|
269
423
|
end
|
270
424
|
end
|
271
|
-
|
272
425
|
end
|
273
426
|
|
274
427
|
namespace :structure do
|
275
|
-
desc
|
276
|
-
task
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
if ActiveRecord::Base.connection.supports_migrations? &&
|
282
|
-
ActiveRecord::SchemaMigration.table_exists?
|
283
|
-
File.open(filename, "a") do |f|
|
284
|
-
f.puts ActiveRecord::Base.connection.dump_schema_information
|
285
|
-
f.print "\n"
|
286
|
-
end
|
428
|
+
desc "Dumps the database structure to db/structure.sql. Specify another file with SCHEMA=db/my_structure.sql"
|
429
|
+
task dump: :load_config do
|
430
|
+
ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env).each do |db_config|
|
431
|
+
ActiveRecord::Base.establish_connection(db_config.config)
|
432
|
+
ActiveRecord::Tasks::DatabaseTasks.dump_schema(db_config.config, :sql, db_config.spec_name)
|
287
433
|
end
|
288
|
-
|
434
|
+
|
435
|
+
db_namespace["structure:dump"].reenable
|
289
436
|
end
|
290
437
|
|
291
|
-
desc "
|
292
|
-
task :
|
293
|
-
ActiveRecord::Tasks::DatabaseTasks.load_schema_current(:sql, ENV[
|
438
|
+
desc "Recreates the databases from the structure.sql file"
|
439
|
+
task load: [:load_config, :check_protected_environments] do
|
440
|
+
ActiveRecord::Tasks::DatabaseTasks.load_schema_current(:sql, ENV["SCHEMA"])
|
294
441
|
end
|
295
442
|
|
296
|
-
task :
|
443
|
+
task load_if_sql: ["db:create", :environment] do
|
297
444
|
db_namespace["structure:load"].invoke if ActiveRecord::Base.schema_format == :sql
|
298
445
|
end
|
299
446
|
end
|
300
447
|
|
301
448
|
namespace :test do
|
302
|
-
|
303
|
-
task :deprecated do
|
304
|
-
Rake.application.top_level_tasks.grep(/^db:test:/).each do |task|
|
305
|
-
$stderr.puts "WARNING: #{task} is deprecated. The Rails test helper now maintains " \
|
306
|
-
"your test schema automatically, see the release notes for details."
|
307
|
-
end
|
308
|
-
end
|
309
|
-
|
310
449
|
# desc "Recreate the test database from the current schema"
|
311
|
-
task :
|
450
|
+
task load: %w(db:test:purge) do
|
312
451
|
case ActiveRecord::Base.schema_format
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
452
|
+
when :ruby
|
453
|
+
db_namespace["test:load_schema"].invoke
|
454
|
+
when :sql
|
455
|
+
db_namespace["test:load_structure"].invoke
|
317
456
|
end
|
318
457
|
end
|
319
458
|
|
320
459
|
# desc "Recreate the test database from an existent schema.rb file"
|
321
|
-
task :
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
ActiveRecord::Tasks::DatabaseTasks.
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
460
|
+
task load_schema: %w(db:test:purge) do
|
461
|
+
should_reconnect = ActiveRecord::Base.connection_pool.active_connection?
|
462
|
+
ActiveRecord::Schema.verbose = false
|
463
|
+
ActiveRecord::Base.configurations.configs_for(env_name: "test").each do |db_config|
|
464
|
+
filename = ActiveRecord::Tasks::DatabaseTasks.dump_filename(db_config.spec_name, :ruby)
|
465
|
+
ActiveRecord::Tasks::DatabaseTasks.load_schema(db_config.config, :ruby, filename, "test")
|
466
|
+
end
|
467
|
+
ensure
|
468
|
+
if should_reconnect
|
469
|
+
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations.default_hash(ActiveRecord::Tasks::DatabaseTasks.env))
|
330
470
|
end
|
331
471
|
end
|
332
472
|
|
333
473
|
# desc "Recreate the test database from an existent structure.sql file"
|
334
|
-
task :
|
335
|
-
ActiveRecord::
|
336
|
-
|
337
|
-
|
338
|
-
# desc "Recreate the test database from a fresh schema"
|
339
|
-
task :clone => %w(db:test:deprecated environment) do
|
340
|
-
case ActiveRecord::Base.schema_format
|
341
|
-
when :ruby
|
342
|
-
db_namespace["test:clone_schema"].invoke
|
343
|
-
when :sql
|
344
|
-
db_namespace["test:clone_structure"].invoke
|
474
|
+
task load_structure: %w(db:test:purge) do
|
475
|
+
ActiveRecord::Base.configurations.configs_for(env_name: "test").each do |db_config|
|
476
|
+
filename = ActiveRecord::Tasks::DatabaseTasks.dump_filename(db_config.spec_name, :sql)
|
477
|
+
ActiveRecord::Tasks::DatabaseTasks.load_schema(db_config.config, :sql, filename, "test")
|
345
478
|
end
|
346
479
|
end
|
347
480
|
|
348
|
-
# desc "Recreate the test database from a fresh schema.rb file"
|
349
|
-
task :clone_schema => %w(db:test:deprecated db:schema:dump db:test:load_schema)
|
350
|
-
|
351
|
-
# desc "Recreate the test database from a fresh structure.sql file"
|
352
|
-
task :clone_structure => %w(db:test:deprecated db:structure:dump db:test:load_structure)
|
353
|
-
|
354
481
|
# desc "Empty the test database"
|
355
|
-
task :
|
356
|
-
ActiveRecord::
|
482
|
+
task purge: %w(load_config check_protected_environments) do
|
483
|
+
ActiveRecord::Base.configurations.configs_for(env_name: "test").each do |db_config|
|
484
|
+
ActiveRecord::Tasks::DatabaseTasks.purge(db_config.config)
|
485
|
+
end
|
357
486
|
end
|
358
487
|
|
359
|
-
# desc '
|
360
|
-
task :
|
488
|
+
# desc 'Load the test schema'
|
489
|
+
task prepare: :load_config do
|
361
490
|
unless ActiveRecord::Base.configurations.blank?
|
362
|
-
db_namespace[
|
491
|
+
db_namespace["test:load"].invoke
|
363
492
|
end
|
364
493
|
end
|
365
494
|
end
|
@@ -368,15 +497,19 @@ end
|
|
368
497
|
namespace :railties do
|
369
498
|
namespace :install do
|
370
499
|
# desc "Copies missing migrations from Railties (e.g. engines). You can specify Railties to use with FROM=railtie1,railtie2"
|
371
|
-
task :
|
372
|
-
to_load = ENV[
|
500
|
+
task migrations: :'db:load_config' do
|
501
|
+
to_load = ENV["FROM"].blank? ? :all : ENV["FROM"].split(",").map(&:strip)
|
373
502
|
railties = {}
|
374
503
|
Rails.application.migration_railties.each do |railtie|
|
375
504
|
next unless to_load == :all || to_load.include?(railtie.railtie_name)
|
376
505
|
|
377
|
-
if railtie.respond_to?(:paths) && (path = railtie.paths[
|
506
|
+
if railtie.respond_to?(:paths) && (path = railtie.paths["db/migrate"].first)
|
378
507
|
railties[railtie.railtie_name] = path
|
379
508
|
end
|
509
|
+
|
510
|
+
unless ENV["MIGRATIONS_PATH"].blank?
|
511
|
+
railties[railtie.railtie_name] = railtie.root + ENV["MIGRATIONS_PATH"]
|
512
|
+
end
|
380
513
|
end
|
381
514
|
|
382
515
|
on_skip = Proc.new do |name, migration|
|
@@ -387,8 +520,8 @@ namespace :railties do
|
|
387
520
|
puts "Copied migration #{migration.basename} from #{name}"
|
388
521
|
end
|
389
522
|
|
390
|
-
ActiveRecord::Migration.copy(ActiveRecord::
|
391
|
-
:
|
523
|
+
ActiveRecord::Migration.copy(ActiveRecord::Tasks::DatabaseTasks.migrations_paths.first, railties,
|
524
|
+
on_skip: on_skip, on_copy: on_copy)
|
392
525
|
end
|
393
526
|
end
|
394
527
|
end
|