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