activerecord 3.2.6 → 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 +7 -0
- data/CHANGELOG.md +611 -6417
- data/MIT-LICENSE +4 -2
- data/README.rdoc +44 -47
- data/examples/performance.rb +79 -71
- data/examples/simple.rb +6 -5
- data/lib/active_record/aggregations.rb +268 -238
- data/lib/active_record/association_relation.rb +40 -0
- data/lib/active_record/associations/alias_tracker.rb +47 -42
- data/lib/active_record/associations/association.rb +173 -81
- data/lib/active_record/associations/association_scope.rb +124 -92
- data/lib/active_record/associations/belongs_to_association.rb +83 -38
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +11 -9
- data/lib/active_record/associations/builder/association.rb +113 -32
- data/lib/active_record/associations/builder/belongs_to.rb +105 -60
- data/lib/active_record/associations/builder/collection_association.rb +53 -56
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +98 -41
- data/lib/active_record/associations/builder/has_many.rb +11 -63
- data/lib/active_record/associations/builder/has_one.rb +47 -45
- data/lib/active_record/associations/builder/singular_association.rb +30 -18
- data/lib/active_record/associations/collection_association.rb +217 -295
- data/lib/active_record/associations/collection_proxy.rb +1074 -77
- data/lib/active_record/associations/foreign_association.rb +20 -0
- data/lib/active_record/associations/has_many_association.rb +78 -50
- data/lib/active_record/associations/has_many_through_association.rb +99 -61
- data/lib/active_record/associations/has_one_association.rb +75 -30
- data/lib/active_record/associations/has_one_through_association.rb +20 -11
- data/lib/active_record/associations/join_dependency/join_association.rb +45 -119
- data/lib/active_record/associations/join_dependency/join_base.rb +11 -12
- data/lib/active_record/associations/join_dependency/join_part.rb +35 -42
- data/lib/active_record/associations/join_dependency.rb +208 -164
- data/lib/active_record/associations/preloader/association.rb +93 -87
- data/lib/active_record/associations/preloader/through_association.rb +87 -38
- data/lib/active_record/associations/preloader.rb +134 -110
- data/lib/active_record/associations/singular_association.rb +19 -24
- data/lib/active_record/associations/through_association.rb +61 -27
- data/lib/active_record/associations.rb +1766 -1505
- data/lib/active_record/attribute_assignment.rb +57 -193
- data/lib/active_record/attribute_decorators.rb +90 -0
- data/lib/active_record/attribute_methods/before_type_cast.rb +58 -8
- data/lib/active_record/attribute_methods/dirty.rb +187 -67
- data/lib/active_record/attribute_methods/primary_key.rb +100 -78
- data/lib/active_record/attribute_methods/query.rb +10 -8
- data/lib/active_record/attribute_methods/read.rb +29 -118
- data/lib/active_record/attribute_methods/serialization.rb +60 -72
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +69 -42
- data/lib/active_record/attribute_methods/write.rb +36 -44
- data/lib/active_record/attribute_methods.rb +306 -161
- data/lib/active_record/attributes.rb +279 -0
- data/lib/active_record/autosave_association.rb +324 -238
- data/lib/active_record/base.rb +114 -507
- data/lib/active_record/callbacks.rb +147 -83
- data/lib/active_record/coders/json.rb +15 -0
- data/lib/active_record/coders/yaml_column.rb +32 -23
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +962 -279
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +32 -5
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +331 -209
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +95 -23
- data/lib/active_record/connection_adapters/abstract/quoting.rb +201 -65
- data/lib/active_record/connection_adapters/abstract/savepoints.rb +23 -0
- data/lib/active_record/connection_adapters/abstract/schema_creation.rb +153 -0
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +510 -289
- data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +93 -0
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +1182 -313
- data/lib/active_record/connection_adapters/abstract/transaction.rb +323 -0
- data/lib/active_record/connection_adapters/abstract_adapter.rb +585 -120
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +610 -463
- data/lib/active_record/connection_adapters/column.rb +58 -233
- data/lib/active_record/connection_adapters/connection_specification.rb +297 -0
- 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 +75 -207
- data/lib/active_record/connection_adapters/postgresql/column.rb +30 -0
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +182 -0
- data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +44 -0
- data/lib/active_record/connection_adapters/postgresql/oid/array.rb +92 -0
- data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +53 -0
- data/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb +15 -0
- data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +17 -0
- data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +50 -0
- data/lib/active_record/connection_adapters/postgresql/oid/date.rb +23 -0
- data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +23 -0
- data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +15 -0
- data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +21 -0
- data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +71 -0
- data/lib/active_record/connection_adapters/postgresql/oid/inet.rb +15 -0
- data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +15 -0
- data/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb +45 -0
- data/lib/active_record/connection_adapters/postgresql/oid/money.rb +41 -0
- data/lib/active_record/connection_adapters/postgresql/oid/oid.rb +15 -0
- data/lib/active_record/connection_adapters/postgresql/oid/point.rb +65 -0
- data/lib/active_record/connection_adapters/postgresql/oid/range.rb +97 -0
- data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +18 -0
- data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +113 -0
- data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +26 -0
- data/lib/active_record/connection_adapters/postgresql/oid/vector.rb +28 -0
- data/lib/active_record/connection_adapters/postgresql/oid/xml.rb +30 -0
- data/lib/active_record/connection_adapters/postgresql/oid.rb +34 -0
- data/lib/active_record/connection_adapters/postgresql/quoting.rb +205 -0
- data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +43 -0
- data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +76 -0
- data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +222 -0
- data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +50 -0
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +776 -0
- data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +36 -0
- data/lib/active_record/connection_adapters/postgresql/utils.rb +81 -0
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +695 -1052
- data/lib/active_record/connection_adapters/schema_cache.rb +115 -24
- 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 +528 -26
- data/lib/active_record/connection_adapters/statement_pool.rb +34 -13
- data/lib/active_record/connection_handling.rb +267 -0
- data/lib/active_record/core.rb +599 -0
- data/lib/active_record/counter_cache.rb +177 -103
- 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/database_configurations.rb +233 -0
- data/lib/active_record/define_callbacks.rb +22 -0
- data/lib/active_record/dynamic_matchers.rb +107 -64
- data/lib/active_record/enum.rb +274 -0
- data/lib/active_record/errors.rb +254 -61
- data/lib/active_record/explain.rb +35 -70
- data/lib/active_record/explain_registry.rb +32 -0
- data/lib/active_record/explain_subscriber.rb +18 -8
- data/lib/active_record/fixture_set/file.rb +82 -0
- 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 +291 -475
- data/lib/active_record/gem_version.rb +17 -0
- data/lib/active_record/inheritance.rb +219 -100
- data/lib/active_record/insert_all.rb +179 -0
- data/lib/active_record/integration.rb +175 -17
- 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 +9 -1
- data/lib/active_record/locking/optimistic.rb +106 -92
- data/lib/active_record/locking/pessimistic.rb +23 -11
- data/lib/active_record/log_subscriber.rb +80 -30
- data/lib/active_record/middleware/database_selector/resolver/session.rb +45 -0
- data/lib/active_record/middleware/database_selector/resolver.rb +92 -0
- data/lib/active_record/middleware/database_selector.rb +75 -0
- data/lib/active_record/migration/command_recorder.rb +235 -56
- data/lib/active_record/migration/compatibility.rb +244 -0
- data/lib/active_record/migration/join_table.rb +17 -0
- data/lib/active_record/migration.rb +917 -301
- data/lib/active_record/model_schema.rb +351 -175
- data/lib/active_record/nested_attributes.rb +366 -235
- data/lib/active_record/no_touching.rb +65 -0
- data/lib/active_record/null_relation.rb +68 -0
- data/lib/active_record/persistence.rb +761 -166
- data/lib/active_record/query_cache.rb +22 -44
- data/lib/active_record/querying.rb +55 -31
- data/lib/active_record/railtie.rb +185 -47
- data/lib/active_record/railties/collection_cache_association_loading.rb +34 -0
- data/lib/active_record/railties/console_sandbox.rb +5 -4
- data/lib/active_record/railties/controller_runtime.rb +35 -33
- data/lib/active_record/railties/databases.rake +366 -463
- data/lib/active_record/readonly_attributes.rb +4 -6
- data/lib/active_record/reflection.rb +736 -228
- data/lib/active_record/relation/batches/batch_enumerator.rb +69 -0
- data/lib/active_record/relation/batches.rb +252 -52
- data/lib/active_record/relation/calculations.rb +340 -270
- data/lib/active_record/relation/delegation.rb +117 -36
- data/lib/active_record/relation/finder_methods.rb +439 -286
- data/lib/active_record/relation/from_clause.rb +26 -0
- data/lib/active_record/relation/merger.rb +184 -0
- data/lib/active_record/relation/predicate_builder/array_handler.rb +49 -0
- 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 +19 -0
- data/lib/active_record/relation/predicate_builder.rb +131 -39
- data/lib/active_record/relation/query_attribute.rb +50 -0
- data/lib/active_record/relation/query_methods.rb +1163 -221
- data/lib/active_record/relation/record_fetch_warning.rb +51 -0
- data/lib/active_record/relation/spawn_methods.rb +49 -120
- 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/relation.rb +671 -349
- data/lib/active_record/result.rb +149 -15
- data/lib/active_record/runtime_registry.rb +24 -0
- data/lib/active_record/sanitization.rb +153 -133
- data/lib/active_record/schema.rb +22 -19
- data/lib/active_record/schema_dumper.rb +178 -112
- data/lib/active_record/schema_migration.rb +60 -0
- data/lib/active_record/scoping/default.rb +107 -98
- data/lib/active_record/scoping/named.rb +130 -115
- data/lib/active_record/scoping.rb +77 -123
- data/lib/active_record/secure_token.rb +40 -0
- data/lib/active_record/serialization.rb +10 -6
- data/lib/active_record/statement_cache.rb +148 -0
- data/lib/active_record/store.rb +256 -16
- 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 +506 -0
- data/lib/active_record/tasks/mysql_database_tasks.rb +115 -0
- data/lib/active_record/tasks/postgresql_database_tasks.rb +141 -0
- data/lib/active_record/tasks/sqlite_database_tasks.rb +77 -0
- data/lib/active_record/test_databases.rb +23 -0
- data/lib/active_record/test_fixtures.rb +224 -0
- data/lib/active_record/timestamp.rb +93 -39
- data/lib/active_record/touch_later.rb +66 -0
- data/lib/active_record/transactions.rb +260 -129
- data/lib/active_record/translation.rb +3 -1
- data/lib/active_record/type/adapter_specific_registry.rb +129 -0
- data/lib/active_record/type/date.rb +9 -0
- data/lib/active_record/type/date_time.rb +9 -0
- data/lib/active_record/type/decimal_without_scale.rb +15 -0
- data/lib/active_record/type/hash_lookup_type_map.rb +25 -0
- 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 +71 -0
- data/lib/active_record/type/text.rb +11 -0
- data/lib/active_record/type/time.rb +21 -0
- data/lib/active_record/type/type_map.rb +62 -0
- data/lib/active_record/type/unsigned_integer.rb +17 -0
- data/lib/active_record/type.rb +78 -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/type_caster.rb +9 -0
- data/lib/active_record/validations/absence.rb +25 -0
- data/lib/active_record/validations/associated.rb +35 -18
- data/lib/active_record/validations/length.rb +26 -0
- data/lib/active_record/validations/presence.rb +68 -0
- data/lib/active_record/validations/uniqueness.rb +123 -77
- data/lib/active_record/validations.rb +54 -43
- data/lib/active_record/version.rb +7 -7
- data/lib/active_record.rb +97 -49
- 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 +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/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/visitors.rb +20 -0
- data/lib/arel/window_predications.rb +9 -0
- data/lib/arel.rb +51 -0
- 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/migration_generator.rb +59 -9
- 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.tt +48 -0
- data/lib/rails/generators/active_record/migration.rb +41 -8
- data/lib/rails/generators/active_record/model/model_generator.rb +24 -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} +1 -1
- data/lib/rails/generators/active_record.rb +10 -16
- metadata +285 -149
- data/examples/associations.png +0 -0
- data/lib/active_record/associations/has_and_belongs_to_many_association.rb +0 -63
- data/lib/active_record/associations/join_helper.rb +0 -55
- 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_and_belongs_to_many.rb +0 -60
- data/lib/active_record/associations/preloader/has_many.rb +0 -17
- data/lib/active_record/associations/preloader/has_many_through.rb +0 -15
- 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_methods/deprecated_underscore_read.rb +0 -32
- data/lib/active_record/connection_adapters/abstract/connection_specification.rb +0 -188
- data/lib/active_record/connection_adapters/mysql_adapter.rb +0 -426
- data/lib/active_record/connection_adapters/sqlite_adapter.rb +0 -579
- data/lib/active_record/dynamic_finder_match.rb +0 -68
- data/lib/active_record/dynamic_scope_match.rb +0 -23
- data/lib/active_record/fixtures/file.rb +0 -65
- data/lib/active_record/identity_map.rb +0 -162
- data/lib/active_record/observer.rb +0 -121
- data/lib/active_record/railties/jdbcmysql_error.rb +0 -16
- data/lib/active_record/serializers/xml_serializer.rb +0 -203
- data/lib/active_record/session_store.rb +0 -358
- data/lib/active_record/test_case.rb +0 -73
- data/lib/rails/generators/active_record/migration/templates/migration.rb +0 -34
- data/lib/rails/generators/active_record/model/templates/migration.rb +0 -15
- data/lib/rails/generators/active_record/model/templates/model.rb +0 -12
- data/lib/rails/generators/active_record/observer/observer_generator.rb +0 -15
- data/lib/rails/generators/active_record/observer/templates/observer.rb +0 -4
- data/lib/rails/generators/active_record/session_migration/session_migration_generator.rb +0 -25
- data/lib/rails/generators/active_record/session_migration/templates/migration.rb +0 -12
@@ -0,0 +1,506 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_record/database_configurations"
|
4
|
+
|
5
|
+
module ActiveRecord
|
6
|
+
module Tasks # :nodoc:
|
7
|
+
class DatabaseAlreadyExists < StandardError; end # :nodoc:
|
8
|
+
class DatabaseNotSupported < StandardError; end # :nodoc:
|
9
|
+
|
10
|
+
# ActiveRecord::Tasks::DatabaseTasks is a utility class, which encapsulates
|
11
|
+
# logic behind common tasks used to manage database and migrations.
|
12
|
+
#
|
13
|
+
# The tasks defined here are used with Rails commands provided by Active Record.
|
14
|
+
#
|
15
|
+
# In order to use DatabaseTasks, a few config values need to be set. All the needed
|
16
|
+
# config values are set by Rails already, so it's necessary to do it only if you
|
17
|
+
# want to change the defaults or when you want to use Active Record outside of Rails
|
18
|
+
# (in such case after configuring the database tasks, you can also use the rake tasks
|
19
|
+
# defined in Active Record).
|
20
|
+
#
|
21
|
+
# The possible config values are:
|
22
|
+
#
|
23
|
+
# * +env+: current environment (like Rails.env).
|
24
|
+
# * +database_configuration+: configuration of your databases (as in +config/database.yml+).
|
25
|
+
# * +db_dir+: your +db+ directory.
|
26
|
+
# * +fixtures_path+: a path to fixtures directory.
|
27
|
+
# * +migrations_paths+: a list of paths to directories with migrations.
|
28
|
+
# * +seed_loader+: an object which will load seeds, it needs to respond to the +load_seed+ method.
|
29
|
+
# * +root+: a path to the root of the application.
|
30
|
+
#
|
31
|
+
# Example usage of DatabaseTasks outside Rails could look as such:
|
32
|
+
#
|
33
|
+
# include ActiveRecord::Tasks
|
34
|
+
# DatabaseTasks.database_configuration = YAML.load_file('my_database_config.yml')
|
35
|
+
# DatabaseTasks.db_dir = 'db'
|
36
|
+
# # other settings...
|
37
|
+
#
|
38
|
+
# DatabaseTasks.create_current('production')
|
39
|
+
module DatabaseTasks
|
40
|
+
##
|
41
|
+
# :singleton-method:
|
42
|
+
# Extra flags passed to database CLI tool (mysqldump/pg_dump) when calling db:structure:dump
|
43
|
+
mattr_accessor :structure_dump_flags, instance_accessor: false
|
44
|
+
|
45
|
+
##
|
46
|
+
# :singleton-method:
|
47
|
+
# Extra flags passed to database CLI tool when calling db:structure:load
|
48
|
+
mattr_accessor :structure_load_flags, instance_accessor: false
|
49
|
+
|
50
|
+
extend self
|
51
|
+
|
52
|
+
attr_writer :current_config, :db_dir, :migrations_paths, :fixtures_path, :root, :env, :seed_loader
|
53
|
+
attr_accessor :database_configuration
|
54
|
+
|
55
|
+
LOCAL_HOSTS = ["127.0.0.1", "localhost"]
|
56
|
+
|
57
|
+
def check_protected_environments!
|
58
|
+
unless ENV["DISABLE_DATABASE_ENVIRONMENT_CHECK"]
|
59
|
+
current = ActiveRecord::Base.connection.migration_context.current_environment
|
60
|
+
stored = ActiveRecord::Base.connection.migration_context.last_stored_environment
|
61
|
+
|
62
|
+
if ActiveRecord::Base.connection.migration_context.protected_environment?
|
63
|
+
raise ActiveRecord::ProtectedEnvironmentError.new(stored)
|
64
|
+
end
|
65
|
+
|
66
|
+
if stored && stored != current
|
67
|
+
raise ActiveRecord::EnvironmentMismatchError.new(current: current, stored: stored)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
rescue ActiveRecord::NoDatabaseError
|
71
|
+
end
|
72
|
+
|
73
|
+
def register_task(pattern, task)
|
74
|
+
@tasks ||= {}
|
75
|
+
@tasks[pattern] = task
|
76
|
+
end
|
77
|
+
|
78
|
+
register_task(/mysql/, "ActiveRecord::Tasks::MySQLDatabaseTasks")
|
79
|
+
register_task(/postgresql/, "ActiveRecord::Tasks::PostgreSQLDatabaseTasks")
|
80
|
+
register_task(/sqlite/, "ActiveRecord::Tasks::SQLiteDatabaseTasks")
|
81
|
+
|
82
|
+
def db_dir
|
83
|
+
@db_dir ||= Rails.application.config.paths["db"].first
|
84
|
+
end
|
85
|
+
|
86
|
+
def migrations_paths
|
87
|
+
@migrations_paths ||= Rails.application.paths["db/migrate"].to_a
|
88
|
+
end
|
89
|
+
|
90
|
+
def fixtures_path
|
91
|
+
@fixtures_path ||= if ENV["FIXTURES_PATH"]
|
92
|
+
File.join(root, ENV["FIXTURES_PATH"])
|
93
|
+
else
|
94
|
+
File.join(root, "test", "fixtures")
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
def root
|
99
|
+
@root ||= Rails.root
|
100
|
+
end
|
101
|
+
|
102
|
+
def env
|
103
|
+
@env ||= Rails.env
|
104
|
+
end
|
105
|
+
|
106
|
+
def spec
|
107
|
+
@spec ||= "primary"
|
108
|
+
end
|
109
|
+
|
110
|
+
def seed_loader
|
111
|
+
@seed_loader ||= Rails.application
|
112
|
+
end
|
113
|
+
|
114
|
+
def current_config(options = {})
|
115
|
+
options.reverse_merge! env: env
|
116
|
+
options[:spec] ||= "primary"
|
117
|
+
if options.has_key?(:config)
|
118
|
+
@current_config = options[:config]
|
119
|
+
else
|
120
|
+
@current_config ||= ActiveRecord::Base.configurations.configs_for(env_name: options[:env], spec_name: options[:spec]).config
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
def create(*arguments)
|
125
|
+
configuration = arguments.first
|
126
|
+
class_for_adapter(configuration["adapter"]).new(*arguments).create
|
127
|
+
$stdout.puts "Created database '#{configuration['database']}'" if verbose?
|
128
|
+
rescue DatabaseAlreadyExists
|
129
|
+
$stderr.puts "Database '#{configuration['database']}' already exists" if verbose?
|
130
|
+
rescue Exception => error
|
131
|
+
$stderr.puts error
|
132
|
+
$stderr.puts "Couldn't create '#{configuration['database']}' database. Please check your configuration."
|
133
|
+
raise
|
134
|
+
end
|
135
|
+
|
136
|
+
def create_all
|
137
|
+
old_pool = ActiveRecord::Base.connection_handler.retrieve_connection_pool(ActiveRecord::Base.connection_specification_name)
|
138
|
+
each_local_configuration { |configuration| create configuration }
|
139
|
+
if old_pool
|
140
|
+
ActiveRecord::Base.connection_handler.establish_connection(old_pool.spec.to_hash)
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
def setup_initial_database_yaml
|
145
|
+
return {} unless defined?(Rails)
|
146
|
+
|
147
|
+
begin
|
148
|
+
Rails.application.config.load_database_yaml
|
149
|
+
rescue
|
150
|
+
$stderr.puts "Rails couldn't infer whether you are using multiple databases from your database.yml and can't generate the tasks for the non-primary databases. If you'd like to use this feature, please simplify your ERB."
|
151
|
+
|
152
|
+
{}
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
def for_each(databases)
|
157
|
+
return {} unless defined?(Rails)
|
158
|
+
|
159
|
+
database_configs = ActiveRecord::DatabaseConfigurations.new(databases).configs_for(env_name: Rails.env)
|
160
|
+
|
161
|
+
# if this is a single database application we don't want tasks for each primary database
|
162
|
+
return if database_configs.count == 1
|
163
|
+
|
164
|
+
database_configs.each do |db_config|
|
165
|
+
yield db_config.spec_name
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
def raise_for_multi_db(environment = env, command:)
|
170
|
+
db_configs = ActiveRecord::Base.configurations.configs_for(env_name: environment)
|
171
|
+
|
172
|
+
if db_configs.count > 1
|
173
|
+
dbs_list = []
|
174
|
+
|
175
|
+
db_configs.each do |db|
|
176
|
+
dbs_list << "#{command}:#{db.spec_name}"
|
177
|
+
end
|
178
|
+
|
179
|
+
raise "You're using a multiple database application. To use `#{command}` you must run the namespaced task with a VERSION. Available tasks are #{dbs_list.to_sentence}."
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
def create_current(environment = env, spec_name = nil)
|
184
|
+
each_current_configuration(environment, spec_name) { |configuration|
|
185
|
+
create configuration
|
186
|
+
}
|
187
|
+
ActiveRecord::Base.establish_connection(environment.to_sym)
|
188
|
+
end
|
189
|
+
|
190
|
+
def drop(*arguments)
|
191
|
+
configuration = arguments.first
|
192
|
+
class_for_adapter(configuration["adapter"]).new(*arguments).drop
|
193
|
+
$stdout.puts "Dropped database '#{configuration['database']}'" if verbose?
|
194
|
+
rescue ActiveRecord::NoDatabaseError
|
195
|
+
$stderr.puts "Database '#{configuration['database']}' does not exist"
|
196
|
+
rescue Exception => error
|
197
|
+
$stderr.puts error
|
198
|
+
$stderr.puts "Couldn't drop database '#{configuration['database']}'"
|
199
|
+
raise
|
200
|
+
end
|
201
|
+
|
202
|
+
def drop_all
|
203
|
+
each_local_configuration { |configuration| drop configuration }
|
204
|
+
end
|
205
|
+
|
206
|
+
def drop_current(environment = env)
|
207
|
+
each_current_configuration(environment) { |configuration|
|
208
|
+
drop configuration
|
209
|
+
}
|
210
|
+
end
|
211
|
+
|
212
|
+
def truncate_tables(configuration)
|
213
|
+
ActiveRecord::Base.connected_to(database: { truncation: configuration }) do
|
214
|
+
conn = ActiveRecord::Base.connection
|
215
|
+
table_names = conn.tables
|
216
|
+
table_names -= [
|
217
|
+
conn.schema_migration.table_name,
|
218
|
+
InternalMetadata.table_name
|
219
|
+
]
|
220
|
+
|
221
|
+
ActiveRecord::Base.connection.truncate_tables(*table_names)
|
222
|
+
end
|
223
|
+
end
|
224
|
+
private :truncate_tables
|
225
|
+
|
226
|
+
def truncate_all(environment = env)
|
227
|
+
ActiveRecord::Base.configurations.configs_for(env_name: environment).each do |db_config|
|
228
|
+
truncate_tables db_config.config
|
229
|
+
end
|
230
|
+
end
|
231
|
+
|
232
|
+
def migrate
|
233
|
+
check_target_version
|
234
|
+
|
235
|
+
scope = ENV["SCOPE"]
|
236
|
+
verbose_was, Migration.verbose = Migration.verbose, verbose?
|
237
|
+
|
238
|
+
Base.connection.migration_context.migrate(target_version) do |migration|
|
239
|
+
scope.blank? || scope == migration.scope
|
240
|
+
end
|
241
|
+
|
242
|
+
ActiveRecord::Base.clear_cache!
|
243
|
+
ensure
|
244
|
+
Migration.verbose = verbose_was
|
245
|
+
end
|
246
|
+
|
247
|
+
def migrate_status
|
248
|
+
unless ActiveRecord::Base.connection.schema_migration.table_exists?
|
249
|
+
Kernel.abort "Schema migrations table does not exist yet."
|
250
|
+
end
|
251
|
+
|
252
|
+
# output
|
253
|
+
puts "\ndatabase: #{ActiveRecord::Base.connection_config[:database]}\n\n"
|
254
|
+
puts "#{'Status'.center(8)} #{'Migration ID'.ljust(14)} Migration Name"
|
255
|
+
puts "-" * 50
|
256
|
+
ActiveRecord::Base.connection.migration_context.migrations_status.each do |status, version, name|
|
257
|
+
puts "#{status.center(8)} #{version.ljust(14)} #{name}"
|
258
|
+
end
|
259
|
+
puts
|
260
|
+
end
|
261
|
+
|
262
|
+
def check_target_version
|
263
|
+
if target_version && !(Migration::MigrationFilenameRegexp.match?(ENV["VERSION"]) || /\A\d+\z/.match?(ENV["VERSION"]))
|
264
|
+
raise "Invalid format of target version: `VERSION=#{ENV['VERSION']}`"
|
265
|
+
end
|
266
|
+
end
|
267
|
+
|
268
|
+
def target_version
|
269
|
+
ENV["VERSION"].to_i if ENV["VERSION"] && !ENV["VERSION"].empty?
|
270
|
+
end
|
271
|
+
|
272
|
+
def charset_current(environment = env, specification_name = spec)
|
273
|
+
charset ActiveRecord::Base.configurations.configs_for(env_name: environment, spec_name: specification_name).config
|
274
|
+
end
|
275
|
+
|
276
|
+
def charset(*arguments)
|
277
|
+
configuration = arguments.first
|
278
|
+
class_for_adapter(configuration["adapter"]).new(*arguments).charset
|
279
|
+
end
|
280
|
+
|
281
|
+
def collation_current(environment = env, specification_name = spec)
|
282
|
+
collation ActiveRecord::Base.configurations.configs_for(env_name: environment, spec_name: specification_name).config
|
283
|
+
end
|
284
|
+
|
285
|
+
def collation(*arguments)
|
286
|
+
configuration = arguments.first
|
287
|
+
class_for_adapter(configuration["adapter"]).new(*arguments).collation
|
288
|
+
end
|
289
|
+
|
290
|
+
def purge(configuration)
|
291
|
+
class_for_adapter(configuration["adapter"]).new(configuration).purge
|
292
|
+
end
|
293
|
+
|
294
|
+
def purge_all
|
295
|
+
each_local_configuration { |configuration|
|
296
|
+
purge configuration
|
297
|
+
}
|
298
|
+
end
|
299
|
+
|
300
|
+
def purge_current(environment = env)
|
301
|
+
each_current_configuration(environment) { |configuration|
|
302
|
+
purge configuration
|
303
|
+
}
|
304
|
+
ActiveRecord::Base.establish_connection(environment.to_sym)
|
305
|
+
end
|
306
|
+
|
307
|
+
def structure_dump(*arguments)
|
308
|
+
configuration = arguments.first
|
309
|
+
filename = arguments.delete_at 1
|
310
|
+
class_for_adapter(configuration["adapter"]).new(*arguments).structure_dump(filename, structure_dump_flags)
|
311
|
+
end
|
312
|
+
|
313
|
+
def structure_load(*arguments)
|
314
|
+
configuration = arguments.first
|
315
|
+
filename = arguments.delete_at 1
|
316
|
+
class_for_adapter(configuration["adapter"]).new(*arguments).structure_load(filename, structure_load_flags)
|
317
|
+
end
|
318
|
+
|
319
|
+
def load_schema(configuration, format = ActiveRecord::Base.schema_format, file = nil, environment = env, spec_name = "primary") # :nodoc:
|
320
|
+
file ||= dump_filename(spec_name, format)
|
321
|
+
|
322
|
+
verbose_was, Migration.verbose = Migration.verbose, verbose? && ENV["VERBOSE"]
|
323
|
+
check_schema_file(file)
|
324
|
+
ActiveRecord::Base.establish_connection(configuration)
|
325
|
+
|
326
|
+
case format
|
327
|
+
when :ruby
|
328
|
+
load(file)
|
329
|
+
when :sql
|
330
|
+
structure_load(configuration, file)
|
331
|
+
else
|
332
|
+
raise ArgumentError, "unknown format #{format.inspect}"
|
333
|
+
end
|
334
|
+
ActiveRecord::InternalMetadata.create_table
|
335
|
+
ActiveRecord::InternalMetadata[:environment] = environment
|
336
|
+
ActiveRecord::InternalMetadata[:schema_sha1] = schema_sha1(file)
|
337
|
+
ensure
|
338
|
+
Migration.verbose = verbose_was
|
339
|
+
end
|
340
|
+
|
341
|
+
def schema_up_to_date?(configuration, format = ActiveRecord::Base.schema_format, file = nil, environment = env, spec_name = "primary")
|
342
|
+
file ||= dump_filename(spec_name, format)
|
343
|
+
|
344
|
+
return true unless File.exist?(file)
|
345
|
+
|
346
|
+
ActiveRecord::Base.establish_connection(configuration)
|
347
|
+
return false unless ActiveRecord::InternalMetadata.table_exists?
|
348
|
+
ActiveRecord::InternalMetadata[:schema_sha1] == schema_sha1(file)
|
349
|
+
end
|
350
|
+
|
351
|
+
def reconstruct_from_schema(configuration, format = ActiveRecord::Base.schema_format, file = nil, environment = env, spec_name = "primary") # :nodoc:
|
352
|
+
file ||= dump_filename(spec_name, format)
|
353
|
+
|
354
|
+
check_schema_file(file)
|
355
|
+
|
356
|
+
ActiveRecord::Base.establish_connection(configuration)
|
357
|
+
|
358
|
+
if schema_up_to_date?(configuration, format, file, environment, spec_name)
|
359
|
+
truncate_tables(configuration)
|
360
|
+
else
|
361
|
+
purge(configuration)
|
362
|
+
load_schema(configuration, format, file, environment, spec_name)
|
363
|
+
end
|
364
|
+
rescue ActiveRecord::NoDatabaseError
|
365
|
+
create(configuration)
|
366
|
+
load_schema(configuration, format, file, environment, spec_name)
|
367
|
+
end
|
368
|
+
|
369
|
+
def dump_schema(configuration, format = ActiveRecord::Base.schema_format, spec_name = "primary") # :nodoc:
|
370
|
+
require "active_record/schema_dumper"
|
371
|
+
filename = dump_filename(spec_name, format)
|
372
|
+
connection = ActiveRecord::Base.connection
|
373
|
+
|
374
|
+
case format
|
375
|
+
when :ruby
|
376
|
+
File.open(filename, "w:utf-8") do |file|
|
377
|
+
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
|
378
|
+
end
|
379
|
+
when :sql
|
380
|
+
structure_dump(configuration, filename)
|
381
|
+
if connection.schema_migration.table_exists?
|
382
|
+
File.open(filename, "a") do |f|
|
383
|
+
f.puts connection.dump_schema_information
|
384
|
+
f.print "\n"
|
385
|
+
end
|
386
|
+
end
|
387
|
+
end
|
388
|
+
end
|
389
|
+
|
390
|
+
def schema_file(format = ActiveRecord::Base.schema_format)
|
391
|
+
File.join(db_dir, schema_file_type(format))
|
392
|
+
end
|
393
|
+
|
394
|
+
def schema_file_type(format = ActiveRecord::Base.schema_format)
|
395
|
+
case format
|
396
|
+
when :ruby
|
397
|
+
"schema.rb"
|
398
|
+
when :sql
|
399
|
+
"structure.sql"
|
400
|
+
end
|
401
|
+
end
|
402
|
+
|
403
|
+
def dump_filename(namespace, format = ActiveRecord::Base.schema_format)
|
404
|
+
filename = if namespace == "primary"
|
405
|
+
schema_file_type(format)
|
406
|
+
else
|
407
|
+
"#{namespace}_#{schema_file_type(format)}"
|
408
|
+
end
|
409
|
+
|
410
|
+
ENV["SCHEMA"] || File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, filename)
|
411
|
+
end
|
412
|
+
|
413
|
+
def cache_dump_filename(namespace)
|
414
|
+
filename = if namespace == "primary"
|
415
|
+
"schema_cache.yml"
|
416
|
+
else
|
417
|
+
"#{namespace}_schema_cache.yml"
|
418
|
+
end
|
419
|
+
|
420
|
+
ENV["SCHEMA_CACHE"] || File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, filename)
|
421
|
+
end
|
422
|
+
|
423
|
+
def load_schema_current(format = ActiveRecord::Base.schema_format, file = nil, environment = env)
|
424
|
+
each_current_configuration(environment) { |configuration, spec_name, env|
|
425
|
+
load_schema(configuration, format, file, env, spec_name)
|
426
|
+
}
|
427
|
+
ActiveRecord::Base.establish_connection(environment.to_sym)
|
428
|
+
end
|
429
|
+
|
430
|
+
def check_schema_file(filename)
|
431
|
+
unless File.exist?(filename)
|
432
|
+
message = +%{#{filename} doesn't exist yet. Run `rails db:migrate` to create it, then try again.}
|
433
|
+
message << %{ If you do not intend to use a database, you should instead alter #{Rails.root}/config/application.rb to limit the frameworks that will be loaded.} if defined?(::Rails.root)
|
434
|
+
Kernel.abort message
|
435
|
+
end
|
436
|
+
end
|
437
|
+
|
438
|
+
def load_seed
|
439
|
+
if seed_loader
|
440
|
+
seed_loader.load_seed
|
441
|
+
else
|
442
|
+
raise "You tried to load seed data, but no seed loader is specified. Please specify seed " \
|
443
|
+
"loader with ActiveRecord::Tasks::DatabaseTasks.seed_loader = your_seed_loader\n" \
|
444
|
+
"Seed loader should respond to load_seed method"
|
445
|
+
end
|
446
|
+
end
|
447
|
+
|
448
|
+
# Dumps the schema cache in YAML format for the connection into the file
|
449
|
+
#
|
450
|
+
# ==== Examples:
|
451
|
+
# ActiveRecord::Tasks::DatabaseTasks.dump_schema_cache(ActiveRecord::Base.connection, "tmp/schema_dump.yaml")
|
452
|
+
def dump_schema_cache(conn, filename)
|
453
|
+
conn.schema_cache.clear!
|
454
|
+
conn.data_sources.each { |table| conn.schema_cache.add(table) }
|
455
|
+
open(filename, "wb") { |f| f.write(YAML.dump(conn.schema_cache)) }
|
456
|
+
end
|
457
|
+
|
458
|
+
private
|
459
|
+
def verbose?
|
460
|
+
ENV["VERBOSE"] ? ENV["VERBOSE"] != "false" : true
|
461
|
+
end
|
462
|
+
|
463
|
+
def class_for_adapter(adapter)
|
464
|
+
_key, task = @tasks.each_pair.detect { |pattern, _task| adapter[pattern] }
|
465
|
+
unless task
|
466
|
+
raise DatabaseNotSupported, "Rake tasks not supported by '#{adapter}' adapter"
|
467
|
+
end
|
468
|
+
task.is_a?(String) ? task.constantize : task
|
469
|
+
end
|
470
|
+
|
471
|
+
def each_current_configuration(environment, spec_name = nil)
|
472
|
+
environments = [environment]
|
473
|
+
environments << "test" if environment == "development"
|
474
|
+
|
475
|
+
environments.each do |env|
|
476
|
+
ActiveRecord::Base.configurations.configs_for(env_name: env).each do |db_config|
|
477
|
+
next if spec_name && spec_name != db_config.spec_name
|
478
|
+
|
479
|
+
yield db_config.config, db_config.spec_name, env
|
480
|
+
end
|
481
|
+
end
|
482
|
+
end
|
483
|
+
|
484
|
+
def each_local_configuration
|
485
|
+
ActiveRecord::Base.configurations.configs_for.each do |db_config|
|
486
|
+
configuration = db_config.config
|
487
|
+
next unless configuration["database"]
|
488
|
+
|
489
|
+
if local_database?(configuration)
|
490
|
+
yield configuration
|
491
|
+
else
|
492
|
+
$stderr.puts "This task only modifies local databases. #{configuration['database']} is on a remote host."
|
493
|
+
end
|
494
|
+
end
|
495
|
+
end
|
496
|
+
|
497
|
+
def local_database?(configuration)
|
498
|
+
configuration["host"].blank? || LOCAL_HOSTS.include?(configuration["host"])
|
499
|
+
end
|
500
|
+
|
501
|
+
def schema_sha1(file)
|
502
|
+
Digest::SHA1.hexdigest(File.read(file))
|
503
|
+
end
|
504
|
+
end
|
505
|
+
end
|
506
|
+
end
|
@@ -0,0 +1,115 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module Tasks # :nodoc:
|
5
|
+
class MySQLDatabaseTasks # :nodoc:
|
6
|
+
ER_DB_CREATE_EXISTS = 1007
|
7
|
+
|
8
|
+
delegate :connection, :establish_connection, to: ActiveRecord::Base
|
9
|
+
|
10
|
+
def initialize(configuration)
|
11
|
+
@configuration = configuration
|
12
|
+
end
|
13
|
+
|
14
|
+
def create
|
15
|
+
establish_connection configuration_without_database
|
16
|
+
connection.create_database configuration["database"], creation_options
|
17
|
+
establish_connection configuration
|
18
|
+
rescue ActiveRecord::StatementInvalid => error
|
19
|
+
if connection.error_number(error.cause) == ER_DB_CREATE_EXISTS
|
20
|
+
raise DatabaseAlreadyExists
|
21
|
+
else
|
22
|
+
raise
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def drop
|
27
|
+
establish_connection configuration
|
28
|
+
connection.drop_database configuration["database"]
|
29
|
+
end
|
30
|
+
|
31
|
+
def purge
|
32
|
+
establish_connection configuration
|
33
|
+
connection.recreate_database configuration["database"], creation_options
|
34
|
+
end
|
35
|
+
|
36
|
+
def charset
|
37
|
+
connection.charset
|
38
|
+
end
|
39
|
+
|
40
|
+
def collation
|
41
|
+
connection.collation
|
42
|
+
end
|
43
|
+
|
44
|
+
def structure_dump(filename, extra_flags)
|
45
|
+
args = prepare_command_options
|
46
|
+
args.concat(["--result-file", "#{filename}"])
|
47
|
+
args.concat(["--no-data"])
|
48
|
+
args.concat(["--routines"])
|
49
|
+
args.concat(["--skip-comments"])
|
50
|
+
|
51
|
+
ignore_tables = ActiveRecord::SchemaDumper.ignore_tables
|
52
|
+
if ignore_tables.any?
|
53
|
+
args += ignore_tables.map { |table| "--ignore-table=#{configuration['database']}.#{table}" }
|
54
|
+
end
|
55
|
+
|
56
|
+
args.concat(["#{configuration['database']}"])
|
57
|
+
args.unshift(*extra_flags) if extra_flags
|
58
|
+
|
59
|
+
run_cmd("mysqldump", args, "dumping")
|
60
|
+
end
|
61
|
+
|
62
|
+
def structure_load(filename, extra_flags)
|
63
|
+
args = prepare_command_options
|
64
|
+
args.concat(["--execute", %{SET FOREIGN_KEY_CHECKS = 0; SOURCE #{filename}; SET FOREIGN_KEY_CHECKS = 1}])
|
65
|
+
args.concat(["--database", "#{configuration['database']}"])
|
66
|
+
args.unshift(*extra_flags) if extra_flags
|
67
|
+
|
68
|
+
run_cmd("mysql", args, "loading")
|
69
|
+
end
|
70
|
+
|
71
|
+
private
|
72
|
+
|
73
|
+
attr_reader :configuration
|
74
|
+
|
75
|
+
def configuration_without_database
|
76
|
+
configuration.merge("database" => nil)
|
77
|
+
end
|
78
|
+
|
79
|
+
def creation_options
|
80
|
+
Hash.new.tap do |options|
|
81
|
+
options[:charset] = configuration["encoding"] if configuration.include? "encoding"
|
82
|
+
options[:collation] = configuration["collation"] if configuration.include? "collation"
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def prepare_command_options
|
87
|
+
args = {
|
88
|
+
"host" => "--host",
|
89
|
+
"port" => "--port",
|
90
|
+
"socket" => "--socket",
|
91
|
+
"username" => "--user",
|
92
|
+
"password" => "--password",
|
93
|
+
"encoding" => "--default-character-set",
|
94
|
+
"sslca" => "--ssl-ca",
|
95
|
+
"sslcert" => "--ssl-cert",
|
96
|
+
"sslcapath" => "--ssl-capath",
|
97
|
+
"sslcipher" => "--ssl-cipher",
|
98
|
+
"sslkey" => "--ssl-key"
|
99
|
+
}.map { |opt, arg| "#{arg}=#{configuration[opt]}" if configuration[opt] }.compact
|
100
|
+
|
101
|
+
args
|
102
|
+
end
|
103
|
+
|
104
|
+
def run_cmd(cmd, args, action)
|
105
|
+
fail run_cmd_error(cmd, args, action) unless Kernel.system(cmd, *args)
|
106
|
+
end
|
107
|
+
|
108
|
+
def run_cmd_error(cmd, args, action)
|
109
|
+
msg = +"failed to execute: `#{cmd}`\n"
|
110
|
+
msg << "Please check the output above for any errors and make sure that `#{cmd}` is installed in your PATH and has proper permissions.\n\n"
|
111
|
+
msg
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|