activerecord 5.2.8.1 → 6.1.7.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +1347 -624
- data/MIT-LICENSE +3 -1
- data/README.rdoc +7 -5
- data/examples/performance.rb +1 -1
- data/lib/active_record/aggregations.rb +9 -8
- data/lib/active_record/association_relation.rb +30 -10
- data/lib/active_record/associations/alias_tracker.rb +19 -16
- data/lib/active_record/associations/association.rb +100 -41
- data/lib/active_record/associations/association_scope.rb +23 -21
- data/lib/active_record/associations/belongs_to_association.rb +55 -48
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +7 -6
- data/lib/active_record/associations/builder/association.rb +45 -22
- data/lib/active_record/associations/builder/belongs_to.rb +29 -59
- data/lib/active_record/associations/builder/collection_association.rb +8 -17
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +17 -41
- data/lib/active_record/associations/builder/has_many.rb +8 -2
- data/lib/active_record/associations/builder/has_one.rb +33 -2
- data/lib/active_record/associations/builder/singular_association.rb +3 -1
- data/lib/active_record/associations/collection_association.rb +44 -34
- data/lib/active_record/associations/collection_proxy.rb +25 -21
- data/lib/active_record/associations/foreign_association.rb +20 -0
- data/lib/active_record/associations/has_many_association.rb +26 -13
- data/lib/active_record/associations/has_many_through_association.rb +24 -18
- data/lib/active_record/associations/has_one_association.rb +43 -31
- data/lib/active_record/associations/has_one_through_association.rb +5 -5
- data/lib/active_record/associations/join_dependency/join_association.rb +44 -22
- data/lib/active_record/associations/join_dependency/join_part.rb +5 -5
- data/lib/active_record/associations/join_dependency.rb +91 -60
- data/lib/active_record/associations/preloader/association.rb +69 -43
- data/lib/active_record/associations/preloader/through_association.rb +49 -40
- data/lib/active_record/associations/preloader.rb +47 -34
- data/lib/active_record/associations/singular_association.rb +3 -17
- data/lib/active_record/associations/through_association.rb +1 -1
- data/lib/active_record/associations.rb +137 -25
- data/lib/active_record/attribute_assignment.rb +17 -19
- data/lib/active_record/attribute_methods/before_type_cast.rb +13 -7
- data/lib/active_record/attribute_methods/dirty.rb +101 -40
- data/lib/active_record/attribute_methods/primary_key.rb +20 -25
- data/lib/active_record/attribute_methods/query.rb +4 -8
- data/lib/active_record/attribute_methods/read.rb +14 -56
- data/lib/active_record/attribute_methods/serialization.rb +12 -7
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +12 -15
- data/lib/active_record/attribute_methods/write.rb +18 -34
- data/lib/active_record/attribute_methods.rb +81 -143
- data/lib/active_record/attributes.rb +46 -9
- data/lib/active_record/autosave_association.rb +57 -42
- data/lib/active_record/base.rb +4 -17
- data/lib/active_record/callbacks.rb +158 -43
- data/lib/active_record/coders/yaml_column.rb +16 -7
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +272 -130
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +7 -36
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +167 -146
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +18 -14
- data/lib/active_record/connection_adapters/abstract/quoting.rb +107 -47
- data/lib/active_record/connection_adapters/abstract/savepoints.rb +3 -3
- data/lib/active_record/connection_adapters/abstract/schema_creation.rb +153 -110
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +211 -90
- data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +2 -4
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +385 -144
- data/lib/active_record/connection_adapters/abstract/transaction.rb +167 -69
- data/lib/active_record/connection_adapters/abstract_adapter.rb +229 -99
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +243 -275
- data/lib/active_record/connection_adapters/column.rb +30 -12
- data/lib/active_record/connection_adapters/deduplicable.rb +29 -0
- data/lib/active_record/connection_adapters/legacy_pool_manager.rb +35 -0
- data/lib/active_record/connection_adapters/mysql/column.rb +1 -1
- data/lib/active_record/connection_adapters/mysql/database_statements.rb +88 -32
- data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +1 -2
- data/lib/active_record/connection_adapters/mysql/quoting.rb +59 -7
- data/lib/active_record/connection_adapters/mysql/schema_creation.rb +34 -10
- data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +48 -32
- data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +18 -7
- data/lib/active_record/connection_adapters/mysql/schema_statements.rb +142 -19
- data/lib/active_record/connection_adapters/mysql/type_metadata.rb +14 -9
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +53 -18
- data/lib/active_record/connection_adapters/pool_config.rb +73 -0
- data/lib/active_record/connection_adapters/pool_manager.rb +47 -0
- data/lib/active_record/connection_adapters/postgresql/column.rb +37 -28
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +40 -54
- data/lib/active_record/connection_adapters/postgresql/oid/array.rb +1 -2
- data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +1 -4
- data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +3 -5
- data/lib/active_record/connection_adapters/postgresql/oid/date.rb +2 -2
- data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +10 -2
- data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +0 -1
- data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +1 -2
- data/lib/active_record/connection_adapters/postgresql/oid/interval.rb +49 -0
- data/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb +3 -4
- data/lib/active_record/connection_adapters/postgresql/oid/macaddr.rb +25 -0
- data/lib/active_record/connection_adapters/postgresql/oid/oid.rb +1 -1
- data/lib/active_record/connection_adapters/postgresql/oid/point.rb +3 -4
- data/lib/active_record/connection_adapters/postgresql/oid/range.rb +25 -7
- data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +1 -1
- data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +9 -7
- data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +15 -3
- data/lib/active_record/connection_adapters/postgresql/oid.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/quoting.rb +73 -10
- data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +2 -2
- data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +19 -4
- data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +107 -91
- data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +0 -1
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +120 -100
- data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +31 -26
- data/lib/active_record/connection_adapters/postgresql/utils.rb +0 -1
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +225 -121
- data/lib/active_record/connection_adapters/schema_cache.rb +159 -21
- data/lib/active_record/connection_adapters/sql_type_metadata.rb +17 -6
- data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +146 -0
- data/lib/active_record/connection_adapters/sqlite3/quoting.rb +42 -7
- data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +5 -1
- data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +77 -13
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +174 -186
- data/lib/active_record/connection_adapters/statement_pool.rb +0 -1
- data/lib/active_record/connection_adapters.rb +52 -0
- data/lib/active_record/connection_handling.rb +293 -33
- data/lib/active_record/core.rb +341 -99
- data/lib/active_record/counter_cache.rb +8 -30
- data/lib/active_record/database_configurations/connection_url_resolver.rb +99 -0
- data/lib/active_record/database_configurations/database_config.rb +80 -0
- data/lib/active_record/database_configurations/hash_config.rb +96 -0
- data/lib/active_record/database_configurations/url_config.rb +53 -0
- data/lib/active_record/database_configurations.rb +273 -0
- data/lib/active_record/delegated_type.rb +209 -0
- data/lib/active_record/destroy_association_async_job.rb +36 -0
- data/lib/active_record/dynamic_matchers.rb +3 -4
- data/lib/active_record/enum.rb +108 -36
- data/lib/active_record/errors.rb +62 -19
- data/lib/active_record/explain.rb +10 -6
- data/lib/active_record/explain_subscriber.rb +1 -1
- data/lib/active_record/fixture_set/file.rb +10 -17
- data/lib/active_record/fixture_set/model_metadata.rb +32 -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 +200 -481
- data/lib/active_record/gem_version.rb +4 -4
- data/lib/active_record/inheritance.rb +53 -24
- data/lib/active_record/insert_all.rb +212 -0
- data/lib/active_record/integration.rb +67 -17
- data/lib/active_record/internal_metadata.rb +28 -9
- data/lib/active_record/legacy_yaml_adapter.rb +7 -3
- data/lib/active_record/locking/optimistic.rb +37 -23
- data/lib/active_record/locking/pessimistic.rb +9 -5
- data/lib/active_record/log_subscriber.rb +35 -35
- data/lib/active_record/middleware/database_selector/resolver/session.rb +48 -0
- data/lib/active_record/middleware/database_selector/resolver.rb +92 -0
- data/lib/active_record/middleware/database_selector.rb +77 -0
- data/lib/active_record/migration/command_recorder.rb +96 -44
- data/lib/active_record/migration/compatibility.rb +145 -64
- data/lib/active_record/migration/join_table.rb +0 -1
- data/lib/active_record/migration.rb +206 -157
- data/lib/active_record/model_schema.rb +148 -22
- data/lib/active_record/nested_attributes.rb +4 -7
- data/lib/active_record/no_touching.rb +8 -1
- data/lib/active_record/null_relation.rb +0 -1
- data/lib/active_record/persistence.rb +267 -59
- data/lib/active_record/query_cache.rb +21 -4
- data/lib/active_record/querying.rb +40 -23
- data/lib/active_record/railtie.rb +113 -74
- data/lib/active_record/railties/console_sandbox.rb +2 -4
- data/lib/active_record/railties/controller_runtime.rb +30 -35
- data/lib/active_record/railties/databases.rake +411 -80
- data/lib/active_record/readonly_attributes.rb +4 -0
- data/lib/active_record/reflection.rb +109 -93
- data/lib/active_record/relation/batches/batch_enumerator.rb +25 -9
- data/lib/active_record/relation/batches.rb +44 -35
- data/lib/active_record/relation/calculations.rb +157 -90
- data/lib/active_record/relation/delegation.rb +35 -50
- data/lib/active_record/relation/finder_methods.rb +64 -39
- data/lib/active_record/relation/from_clause.rb +5 -1
- data/lib/active_record/relation/merger.rb +32 -40
- data/lib/active_record/relation/predicate_builder/array_handler.rb +13 -13
- data/lib/active_record/relation/predicate_builder/association_query_value.rb +5 -9
- data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +1 -2
- data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +11 -10
- data/lib/active_record/relation/predicate_builder/range_handler.rb +3 -23
- data/lib/active_record/relation/predicate_builder/relation_handler.rb +1 -1
- data/lib/active_record/relation/predicate_builder.rb +62 -45
- data/lib/active_record/relation/query_attribute.rb +13 -8
- data/lib/active_record/relation/query_methods.rb +478 -187
- data/lib/active_record/relation/record_fetch_warning.rb +3 -3
- data/lib/active_record/relation/spawn_methods.rb +9 -9
- data/lib/active_record/relation/where_clause.rb +115 -62
- data/lib/active_record/relation.rb +379 -115
- data/lib/active_record/result.rb +64 -38
- data/lib/active_record/runtime_registry.rb +2 -2
- data/lib/active_record/sanitization.rb +22 -41
- data/lib/active_record/schema.rb +2 -11
- data/lib/active_record/schema_dumper.rb +54 -9
- data/lib/active_record/schema_migration.rb +7 -9
- data/lib/active_record/scoping/default.rb +4 -8
- data/lib/active_record/scoping/named.rb +17 -24
- data/lib/active_record/scoping.rb +8 -9
- data/lib/active_record/secure_token.rb +16 -8
- data/lib/active_record/serialization.rb +5 -3
- data/lib/active_record/signed_id.rb +116 -0
- data/lib/active_record/statement_cache.rb +49 -6
- data/lib/active_record/store.rb +94 -10
- data/lib/active_record/suppressor.rb +2 -2
- data/lib/active_record/table_metadata.rb +42 -43
- data/lib/active_record/tasks/database_tasks.rb +277 -81
- data/lib/active_record/tasks/mysql_database_tasks.rb +37 -39
- data/lib/active_record/tasks/postgresql_database_tasks.rb +27 -32
- data/lib/active_record/tasks/sqlite_database_tasks.rb +14 -17
- data/lib/active_record/test_databases.rb +24 -0
- data/lib/active_record/test_fixtures.rb +291 -0
- data/lib/active_record/timestamp.rb +43 -32
- data/lib/active_record/touch_later.rb +23 -22
- data/lib/active_record/transactions.rb +62 -118
- data/lib/active_record/translation.rb +1 -1
- data/lib/active_record/type/adapter_specific_registry.rb +3 -13
- data/lib/active_record/type/hash_lookup_type_map.rb +0 -1
- data/lib/active_record/type/serialized.rb +6 -3
- data/lib/active_record/type/time.rb +10 -0
- data/lib/active_record/type/type_map.rb +0 -1
- data/lib/active_record/type/unsigned_integer.rb +0 -1
- data/lib/active_record/type.rb +10 -5
- data/lib/active_record/type_caster/connection.rb +15 -15
- data/lib/active_record/type_caster/map.rb +8 -8
- data/lib/active_record/validations/associated.rb +1 -2
- data/lib/active_record/validations/numericality.rb +35 -0
- data/lib/active_record/validations/uniqueness.rb +38 -30
- data/lib/active_record/validations.rb +4 -3
- data/lib/active_record.rb +13 -12
- data/lib/arel/alias_predication.rb +9 -0
- data/lib/arel/attributes/attribute.rb +41 -0
- data/lib/arel/collectors/bind.rb +29 -0
- data/lib/arel/collectors/composite.rb +39 -0
- data/lib/arel/collectors/plain_string.rb +20 -0
- data/lib/arel/collectors/sql_string.rb +27 -0
- data/lib/arel/collectors/substitute_binds.rb +35 -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 +126 -0
- data/lib/arel/nodes/bind_param.rb +44 -0
- data/lib/arel/nodes/case.rb +55 -0
- data/lib/arel/nodes/casted.rb +62 -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 +15 -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 +11 -0
- data/lib/arel/nodes/homogeneous_in.rb +76 -0
- data/lib/arel/nodes/in.rb +15 -0
- data/lib/arel/nodes/infix_operation.rb +92 -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 +51 -0
- data/lib/arel/nodes/node_expression.rb +13 -0
- data/lib/arel/nodes/ordering.rb +27 -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 +19 -0
- data/lib/arel/nodes/string_join.rb +11 -0
- data/lib/arel/nodes/table_alias.rb +31 -0
- data/lib/arel/nodes/terminal.rb +16 -0
- data/lib/arel/nodes/true.rb +16 -0
- data/lib/arel/nodes/unary.rb +44 -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 +70 -0
- data/lib/arel/order_predications.rb +13 -0
- data/lib/arel/predications.rb +250 -0
- data/lib/arel/select_manager.rb +270 -0
- data/lib/arel/table.rb +118 -0
- data/lib/arel/tree_manager.rb +72 -0
- data/lib/arel/update_manager.rb +34 -0
- data/lib/arel/visitors/dot.rb +308 -0
- data/lib/arel/visitors/mysql.rb +93 -0
- data/lib/arel/visitors/postgresql.rb +120 -0
- data/lib/arel/visitors/sqlite.rb +38 -0
- data/lib/arel/visitors/to_sql.rb +899 -0
- data/lib/arel/visitors/visitor.rb +45 -0
- data/lib/arel/visitors.rb +13 -0
- data/lib/arel/window_predications.rb +9 -0
- data/lib/arel.rb +54 -0
- data/lib/rails/generators/active_record/application_record/application_record_generator.rb +0 -1
- data/lib/rails/generators/active_record/migration/migration_generator.rb +3 -5
- data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb.tt +3 -1
- data/lib/rails/generators/active_record/migration/templates/migration.rb.tt +7 -5
- data/lib/rails/generators/active_record/migration.rb +19 -2
- data/lib/rails/generators/active_record/model/model_generator.rb +39 -2
- data/lib/rails/generators/active_record/model/templates/abstract_base_class.rb.tt +7 -0
- data/lib/rails/generators/active_record/model/templates/model.rb.tt +10 -1
- metadata +118 -32
- data/lib/active_record/attribute_decorators.rb +0 -90
- data/lib/active_record/collection_cache_key.rb +0 -53
- data/lib/active_record/connection_adapters/connection_specification.rb +0 -287
- data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +0 -33
- data/lib/active_record/define_callbacks.rb +0 -22
- data/lib/active_record/relation/predicate_builder/base_handler.rb +0 -19
- data/lib/active_record/relation/where_clause_factory.rb +0 -34
@@ -1,14 +1,15 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require "active_record/database_configurations"
|
4
|
+
|
3
5
|
module ActiveRecord
|
4
6
|
module Tasks # :nodoc:
|
5
|
-
class DatabaseAlreadyExists < StandardError; end # :nodoc:
|
6
7
|
class DatabaseNotSupported < StandardError; end # :nodoc:
|
7
8
|
|
8
9
|
# ActiveRecord::Tasks::DatabaseTasks is a utility class, which encapsulates
|
9
10
|
# logic behind common tasks used to manage database and migrations.
|
10
11
|
#
|
11
|
-
# The tasks defined here are used with
|
12
|
+
# The tasks defined here are used with Rails commands provided by Active Record.
|
12
13
|
#
|
13
14
|
# In order to use DatabaseTasks, a few config values need to be set. All the needed
|
14
15
|
# config values are set by Rails already, so it's necessary to do it only if you
|
@@ -37,17 +38,18 @@ module ActiveRecord
|
|
37
38
|
module DatabaseTasks
|
38
39
|
##
|
39
40
|
# :singleton-method:
|
40
|
-
# Extra flags passed to database CLI tool (mysqldump/pg_dump) when calling db:
|
41
|
+
# Extra flags passed to database CLI tool (mysqldump/pg_dump) when calling db:schema:dump
|
41
42
|
mattr_accessor :structure_dump_flags, instance_accessor: false
|
42
43
|
|
43
44
|
##
|
44
45
|
# :singleton-method:
|
45
|
-
# Extra flags passed to database CLI tool when calling db:
|
46
|
+
# Extra flags passed to database CLI tool when calling db:schema:load
|
46
47
|
mattr_accessor :structure_load_flags, instance_accessor: false
|
47
48
|
|
48
49
|
extend self
|
49
50
|
|
50
51
|
attr_writer :current_config, :db_dir, :migrations_paths, :fixtures_path, :root, :env, :seed_loader
|
52
|
+
deprecate :current_config=
|
51
53
|
attr_accessor :database_configuration
|
52
54
|
|
53
55
|
LOCAL_HOSTS = ["127.0.0.1", "localhost"]
|
@@ -101,82 +103,161 @@ module ActiveRecord
|
|
101
103
|
@env ||= Rails.env
|
102
104
|
end
|
103
105
|
|
106
|
+
def spec
|
107
|
+
@spec ||= "primary"
|
108
|
+
end
|
109
|
+
deprecate spec: "please use name instead"
|
110
|
+
|
111
|
+
def name
|
112
|
+
@name ||= "primary"
|
113
|
+
end
|
114
|
+
|
104
115
|
def seed_loader
|
105
116
|
@seed_loader ||= Rails.application
|
106
117
|
end
|
107
118
|
|
108
119
|
def current_config(options = {})
|
109
|
-
options.reverse_merge! env: env
|
110
120
|
if options.has_key?(:config)
|
111
121
|
@current_config = options[:config]
|
112
122
|
else
|
113
|
-
|
123
|
+
env_name = options[:env] || env
|
124
|
+
name = options[:spec] || "primary"
|
125
|
+
|
126
|
+
@current_config ||= ActiveRecord::Base.configurations.configs_for(env_name: env_name, name: name)&.configuration_hash
|
114
127
|
end
|
115
128
|
end
|
129
|
+
deprecate :current_config
|
116
130
|
|
117
|
-
def create(*arguments)
|
118
|
-
|
119
|
-
|
120
|
-
$stdout.puts "Created database '#{
|
131
|
+
def create(configuration, *arguments)
|
132
|
+
db_config = resolve_configuration(configuration)
|
133
|
+
database_adapter_for(db_config, *arguments).create
|
134
|
+
$stdout.puts "Created database '#{db_config.database}'" if verbose?
|
121
135
|
rescue DatabaseAlreadyExists
|
122
|
-
$stderr.puts "Database '#{
|
136
|
+
$stderr.puts "Database '#{db_config.database}' already exists" if verbose?
|
123
137
|
rescue Exception => error
|
124
138
|
$stderr.puts error
|
125
|
-
$stderr.puts "Couldn't create '#{
|
139
|
+
$stderr.puts "Couldn't create '#{db_config.database}' database. Please check your configuration."
|
126
140
|
raise
|
127
141
|
end
|
128
142
|
|
129
143
|
def create_all
|
130
144
|
old_pool = ActiveRecord::Base.connection_handler.retrieve_connection_pool(ActiveRecord::Base.connection_specification_name)
|
131
|
-
each_local_configuration { |
|
145
|
+
each_local_configuration { |db_config| create(db_config) }
|
132
146
|
if old_pool
|
133
|
-
ActiveRecord::Base.connection_handler.establish_connection(old_pool.
|
147
|
+
ActiveRecord::Base.connection_handler.establish_connection(old_pool.db_config)
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
def setup_initial_database_yaml
|
152
|
+
return {} unless defined?(Rails)
|
153
|
+
|
154
|
+
begin
|
155
|
+
Rails.application.config.load_database_yaml
|
156
|
+
rescue
|
157
|
+
unless ActiveRecord::Base.suppress_multiple_database_warning
|
158
|
+
$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."
|
159
|
+
end
|
160
|
+
|
161
|
+
{}
|
134
162
|
end
|
135
163
|
end
|
136
164
|
|
137
|
-
def
|
138
|
-
|
139
|
-
|
140
|
-
|
165
|
+
def for_each(databases)
|
166
|
+
return {} unless defined?(Rails)
|
167
|
+
|
168
|
+
database_configs = ActiveRecord::DatabaseConfigurations.new(databases).configs_for(env_name: Rails.env)
|
169
|
+
|
170
|
+
# if this is a single database application we don't want tasks for each primary database
|
171
|
+
return if database_configs.count == 1
|
172
|
+
|
173
|
+
database_configs.each do |db_config|
|
174
|
+
yield db_config.name
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
def raise_for_multi_db(environment = env, command:)
|
179
|
+
db_configs = ActiveRecord::Base.configurations.configs_for(env_name: environment)
|
180
|
+
|
181
|
+
if db_configs.count > 1
|
182
|
+
dbs_list = []
|
183
|
+
|
184
|
+
db_configs.each do |db|
|
185
|
+
dbs_list << "#{command}:#{db.name}"
|
186
|
+
end
|
187
|
+
|
188
|
+
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}."
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
def create_current(environment = env, name = nil)
|
193
|
+
each_current_configuration(environment, name) { |db_config| create(db_config) }
|
141
194
|
ActiveRecord::Base.establish_connection(environment.to_sym)
|
142
195
|
end
|
143
196
|
|
144
|
-
def drop(*arguments)
|
145
|
-
|
146
|
-
|
147
|
-
$stdout.puts "Dropped database '#{
|
197
|
+
def drop(configuration, *arguments)
|
198
|
+
db_config = resolve_configuration(configuration)
|
199
|
+
database_adapter_for(db_config, *arguments).drop
|
200
|
+
$stdout.puts "Dropped database '#{db_config.database}'" if verbose?
|
148
201
|
rescue ActiveRecord::NoDatabaseError
|
149
|
-
$stderr.puts "Database '#{
|
202
|
+
$stderr.puts "Database '#{db_config.database}' does not exist"
|
150
203
|
rescue Exception => error
|
151
204
|
$stderr.puts error
|
152
|
-
$stderr.puts "Couldn't drop database '#{
|
205
|
+
$stderr.puts "Couldn't drop database '#{db_config.database}'"
|
153
206
|
raise
|
154
207
|
end
|
155
208
|
|
156
209
|
def drop_all
|
157
|
-
each_local_configuration { |
|
210
|
+
each_local_configuration { |db_config| drop(db_config) }
|
158
211
|
end
|
159
212
|
|
160
213
|
def drop_current(environment = env)
|
161
|
-
each_current_configuration(environment) { |
|
162
|
-
|
163
|
-
|
214
|
+
each_current_configuration(environment) { |db_config| drop(db_config) }
|
215
|
+
end
|
216
|
+
|
217
|
+
def truncate_tables(db_config)
|
218
|
+
ActiveRecord::Base.establish_connection(db_config)
|
219
|
+
|
220
|
+
connection = ActiveRecord::Base.connection
|
221
|
+
connection.truncate_tables(*connection.tables)
|
222
|
+
end
|
223
|
+
private :truncate_tables
|
224
|
+
|
225
|
+
def truncate_all(environment = env)
|
226
|
+
ActiveRecord::Base.configurations.configs_for(env_name: environment).each do |db_config|
|
227
|
+
truncate_tables(db_config)
|
228
|
+
end
|
164
229
|
end
|
165
230
|
|
166
231
|
def migrate
|
167
232
|
check_target_version
|
168
233
|
|
169
|
-
verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] != "false" : true
|
170
234
|
scope = ENV["SCOPE"]
|
171
|
-
verbose_was, Migration.verbose = Migration.verbose, verbose
|
235
|
+
verbose_was, Migration.verbose = Migration.verbose, verbose?
|
236
|
+
|
172
237
|
Base.connection.migration_context.migrate(target_version) do |migration|
|
173
238
|
scope.blank? || scope == migration.scope
|
174
239
|
end
|
240
|
+
|
175
241
|
ActiveRecord::Base.clear_cache!
|
176
242
|
ensure
|
177
243
|
Migration.verbose = verbose_was
|
178
244
|
end
|
179
245
|
|
246
|
+
def migrate_status
|
247
|
+
unless ActiveRecord::Base.connection.schema_migration.table_exists?
|
248
|
+
Kernel.abort "Schema migrations table does not exist yet."
|
249
|
+
end
|
250
|
+
|
251
|
+
# output
|
252
|
+
puts "\ndatabase: #{ActiveRecord::Base.connection_db_config.database}\n\n"
|
253
|
+
puts "#{'Status'.center(8)} #{'Migration ID'.ljust(14)} Migration Name"
|
254
|
+
puts "-" * 50
|
255
|
+
ActiveRecord::Base.connection.migration_context.migrations_status.each do |status, version, name|
|
256
|
+
puts "#{status.center(8)} #{version.ljust(14)} #{name}"
|
257
|
+
end
|
258
|
+
puts
|
259
|
+
end
|
260
|
+
|
180
261
|
def check_target_version
|
181
262
|
if target_version && !(Migration::MigrationFilenameRegexp.match?(ENV["VERSION"]) || /\A\d+\z/.match?(ENV["VERSION"]))
|
182
263
|
raise "Invalid format of target version: `VERSION=#{ENV['VERSION']}`"
|
@@ -187,90 +268,178 @@ module ActiveRecord
|
|
187
268
|
ENV["VERSION"].to_i if ENV["VERSION"] && !ENV["VERSION"].empty?
|
188
269
|
end
|
189
270
|
|
190
|
-
def charset_current(
|
191
|
-
|
271
|
+
def charset_current(env_name = env, db_name = name)
|
272
|
+
db_config = ActiveRecord::Base.configurations.configs_for(env_name: env_name, name: db_name)
|
273
|
+
charset(db_config)
|
192
274
|
end
|
193
275
|
|
194
|
-
def charset(*arguments)
|
195
|
-
|
196
|
-
|
276
|
+
def charset(configuration, *arguments)
|
277
|
+
db_config = resolve_configuration(configuration)
|
278
|
+
database_adapter_for(db_config, *arguments).charset
|
197
279
|
end
|
198
280
|
|
199
|
-
def collation_current(
|
200
|
-
|
281
|
+
def collation_current(env_name = env, db_name = name)
|
282
|
+
db_config = ActiveRecord::Base.configurations.configs_for(env_name: env_name, name: db_name)
|
283
|
+
collation(db_config)
|
201
284
|
end
|
202
285
|
|
203
|
-
def collation(*arguments)
|
204
|
-
|
205
|
-
|
286
|
+
def collation(configuration, *arguments)
|
287
|
+
db_config = resolve_configuration(configuration)
|
288
|
+
database_adapter_for(db_config, *arguments).collation
|
206
289
|
end
|
207
290
|
|
208
291
|
def purge(configuration)
|
209
|
-
|
292
|
+
db_config = resolve_configuration(configuration)
|
293
|
+
database_adapter_for(db_config).purge
|
210
294
|
end
|
211
295
|
|
212
296
|
def purge_all
|
213
|
-
each_local_configuration { |
|
214
|
-
purge configuration
|
215
|
-
}
|
297
|
+
each_local_configuration { |db_config| purge(db_config) }
|
216
298
|
end
|
217
299
|
|
218
300
|
def purge_current(environment = env)
|
219
|
-
each_current_configuration(environment) { |
|
220
|
-
purge configuration
|
221
|
-
}
|
301
|
+
each_current_configuration(environment) { |db_config| purge(db_config) }
|
222
302
|
ActiveRecord::Base.establish_connection(environment.to_sym)
|
223
303
|
end
|
224
304
|
|
225
|
-
def structure_dump(*arguments)
|
226
|
-
|
227
|
-
filename = arguments.delete_at
|
228
|
-
|
305
|
+
def structure_dump(configuration, *arguments)
|
306
|
+
db_config = resolve_configuration(configuration)
|
307
|
+
filename = arguments.delete_at(0)
|
308
|
+
database_adapter_for(db_config, *arguments).structure_dump(filename, structure_dump_flags)
|
229
309
|
end
|
230
310
|
|
231
|
-
def structure_load(*arguments)
|
232
|
-
|
233
|
-
filename = arguments.delete_at
|
234
|
-
|
311
|
+
def structure_load(configuration, *arguments)
|
312
|
+
db_config = resolve_configuration(configuration)
|
313
|
+
filename = arguments.delete_at(0)
|
314
|
+
database_adapter_for(db_config, *arguments).structure_load(filename, structure_load_flags)
|
235
315
|
end
|
236
316
|
|
237
|
-
def load_schema(
|
238
|
-
file ||=
|
317
|
+
def load_schema(db_config, format = ActiveRecord::Base.schema_format, file = nil) # :nodoc:
|
318
|
+
file ||= dump_filename(db_config.name, format)
|
239
319
|
|
320
|
+
verbose_was, Migration.verbose = Migration.verbose, verbose? && ENV["VERBOSE"]
|
240
321
|
check_schema_file(file)
|
241
|
-
ActiveRecord::Base.establish_connection(
|
322
|
+
ActiveRecord::Base.establish_connection(db_config)
|
242
323
|
|
243
324
|
case format
|
244
325
|
when :ruby
|
245
326
|
load(file)
|
246
327
|
when :sql
|
247
|
-
structure_load(
|
328
|
+
structure_load(db_config, file)
|
248
329
|
else
|
249
330
|
raise ArgumentError, "unknown format #{format.inspect}"
|
250
331
|
end
|
251
332
|
ActiveRecord::InternalMetadata.create_table
|
252
|
-
ActiveRecord::InternalMetadata[:environment] =
|
333
|
+
ActiveRecord::InternalMetadata[:environment] = db_config.env_name
|
334
|
+
ActiveRecord::InternalMetadata[:schema_sha1] = schema_sha1(file)
|
335
|
+
ensure
|
336
|
+
Migration.verbose = verbose_was
|
337
|
+
end
|
338
|
+
|
339
|
+
def schema_up_to_date?(configuration, format = ActiveRecord::Base.schema_format, file = nil, environment = nil, name = nil)
|
340
|
+
db_config = resolve_configuration(configuration)
|
341
|
+
|
342
|
+
if environment || name
|
343
|
+
ActiveSupport::Deprecation.warn("`environment` and `name` will be removed as parameters in 7.0.0, you may now pass an ActiveRecord::DatabaseConfigurations::DatabaseConfig as `configuration` instead.")
|
344
|
+
end
|
345
|
+
|
346
|
+
name ||= db_config.name
|
347
|
+
|
348
|
+
file ||= dump_filename(name, format)
|
349
|
+
|
350
|
+
return true unless File.exist?(file)
|
351
|
+
|
352
|
+
ActiveRecord::Base.establish_connection(db_config)
|
353
|
+
|
354
|
+
return false unless ActiveRecord::InternalMetadata.enabled?
|
355
|
+
return false unless ActiveRecord::InternalMetadata.table_exists?
|
356
|
+
|
357
|
+
ActiveRecord::InternalMetadata[:schema_sha1] == schema_sha1(file)
|
358
|
+
end
|
359
|
+
|
360
|
+
def reconstruct_from_schema(db_config, format = ActiveRecord::Base.schema_format, file = nil) # :nodoc:
|
361
|
+
file ||= dump_filename(db_config.name, format)
|
362
|
+
|
363
|
+
check_schema_file(file)
|
364
|
+
|
365
|
+
ActiveRecord::Base.establish_connection(db_config)
|
366
|
+
|
367
|
+
if schema_up_to_date?(db_config, format, file)
|
368
|
+
truncate_tables(db_config)
|
369
|
+
else
|
370
|
+
purge(db_config)
|
371
|
+
load_schema(db_config, format, file)
|
372
|
+
end
|
373
|
+
rescue ActiveRecord::NoDatabaseError
|
374
|
+
create(db_config)
|
375
|
+
load_schema(db_config, format, file)
|
376
|
+
end
|
377
|
+
|
378
|
+
def dump_schema(db_config, format = ActiveRecord::Base.schema_format) # :nodoc:
|
379
|
+
require "active_record/schema_dumper"
|
380
|
+
filename = dump_filename(db_config.name, format)
|
381
|
+
connection = ActiveRecord::Base.connection
|
382
|
+
|
383
|
+
FileUtils.mkdir_p(db_dir)
|
384
|
+
case format
|
385
|
+
when :ruby
|
386
|
+
File.open(filename, "w:utf-8") do |file|
|
387
|
+
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
|
388
|
+
end
|
389
|
+
when :sql
|
390
|
+
structure_dump(db_config, filename)
|
391
|
+
if connection.schema_migration.table_exists?
|
392
|
+
File.open(filename, "a") do |f|
|
393
|
+
f.puts connection.dump_schema_information
|
394
|
+
f.print "\n"
|
395
|
+
end
|
396
|
+
end
|
397
|
+
end
|
253
398
|
end
|
254
399
|
|
255
400
|
def schema_file(format = ActiveRecord::Base.schema_format)
|
401
|
+
File.join(db_dir, schema_file_type(format))
|
402
|
+
end
|
403
|
+
|
404
|
+
def schema_file_type(format = ActiveRecord::Base.schema_format)
|
256
405
|
case format
|
257
406
|
when :ruby
|
258
|
-
|
407
|
+
"schema.rb"
|
259
408
|
when :sql
|
260
|
-
|
409
|
+
"structure.sql"
|
410
|
+
end
|
411
|
+
end
|
412
|
+
|
413
|
+
def dump_filename(db_config_name, format = ActiveRecord::Base.schema_format)
|
414
|
+
filename = if ActiveRecord::Base.configurations.primary?(db_config_name)
|
415
|
+
schema_file_type(format)
|
416
|
+
else
|
417
|
+
"#{db_config_name}_#{schema_file_type(format)}"
|
418
|
+
end
|
419
|
+
|
420
|
+
ENV["SCHEMA"] || File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, filename)
|
421
|
+
end
|
422
|
+
|
423
|
+
def cache_dump_filename(db_config_name, schema_cache_path: nil)
|
424
|
+
filename = if ActiveRecord::Base.configurations.primary?(db_config_name)
|
425
|
+
"schema_cache.yml"
|
426
|
+
else
|
427
|
+
"#{db_config_name}_schema_cache.yml"
|
261
428
|
end
|
429
|
+
|
430
|
+
schema_cache_path || ENV["SCHEMA_CACHE"] || File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, filename)
|
262
431
|
end
|
263
432
|
|
264
433
|
def load_schema_current(format = ActiveRecord::Base.schema_format, file = nil, environment = env)
|
265
|
-
each_current_configuration(environment)
|
266
|
-
load_schema
|
267
|
-
|
434
|
+
each_current_configuration(environment) do |db_config|
|
435
|
+
load_schema(db_config, format, file)
|
436
|
+
end
|
268
437
|
ActiveRecord::Base.establish_connection(environment.to_sym)
|
269
438
|
end
|
270
439
|
|
271
440
|
def check_schema_file(filename)
|
272
441
|
unless File.exist?(filename)
|
273
|
-
message =
|
442
|
+
message = +%{#{filename} doesn't exist yet. Run `bin/rails db:migrate` to create it, then try again.}
|
274
443
|
message << %{ If you do not intend to use a database, you should instead alter #{Rails.root}/config/application.rb to limit the frameworks that will be loaded.} if defined?(::Rails.root)
|
275
444
|
Kernel.abort message
|
276
445
|
end
|
@@ -291,12 +460,32 @@ module ActiveRecord
|
|
291
460
|
# ==== Examples:
|
292
461
|
# ActiveRecord::Tasks::DatabaseTasks.dump_schema_cache(ActiveRecord::Base.connection, "tmp/schema_dump.yaml")
|
293
462
|
def dump_schema_cache(conn, filename)
|
294
|
-
conn.schema_cache.
|
295
|
-
|
296
|
-
|
463
|
+
conn.schema_cache.dump_to(filename)
|
464
|
+
end
|
465
|
+
|
466
|
+
def clear_schema_cache(filename)
|
467
|
+
FileUtils.rm_f filename, verbose: false
|
297
468
|
end
|
298
469
|
|
299
470
|
private
|
471
|
+
def resolve_configuration(configuration)
|
472
|
+
Base.configurations.resolve(configuration)
|
473
|
+
end
|
474
|
+
|
475
|
+
def verbose?
|
476
|
+
ENV["VERBOSE"] ? ENV["VERBOSE"] != "false" : true
|
477
|
+
end
|
478
|
+
|
479
|
+
# Create a new instance for the specified db configuration object
|
480
|
+
# For classes that have been converted to use db_config objects, pass a
|
481
|
+
# `DatabaseConfig`, otherwise pass a `Hash`
|
482
|
+
def database_adapter_for(db_config, *arguments)
|
483
|
+
klass = class_for_adapter(db_config.adapter)
|
484
|
+
converted = klass.respond_to?(:using_database_configurations?) && klass.using_database_configurations?
|
485
|
+
|
486
|
+
config = converted ? db_config : db_config.configuration_hash
|
487
|
+
klass.new(config, *arguments)
|
488
|
+
end
|
300
489
|
|
301
490
|
def class_for_adapter(adapter)
|
302
491
|
_key, task = @tasks.each_pair.detect { |pattern, _task| adapter[pattern] }
|
@@ -306,31 +495,38 @@ module ActiveRecord
|
|
306
495
|
task.is_a?(String) ? task.constantize : task
|
307
496
|
end
|
308
497
|
|
309
|
-
def each_current_configuration(environment)
|
498
|
+
def each_current_configuration(environment, name = nil)
|
310
499
|
environments = [environment]
|
311
|
-
environments << "test" if environment == "development"
|
500
|
+
environments << "test" if environment == "development" && !ENV["SKIP_TEST_DATABASE"] && !ENV["DATABASE_URL"]
|
312
501
|
|
313
|
-
|
314
|
-
|
502
|
+
environments.each do |env|
|
503
|
+
ActiveRecord::Base.configurations.configs_for(env_name: env).each do |db_config|
|
504
|
+
next if name && name != db_config.name
|
315
505
|
|
316
|
-
|
506
|
+
yield db_config
|
507
|
+
end
|
317
508
|
end
|
318
509
|
end
|
319
510
|
|
320
511
|
def each_local_configuration
|
321
|
-
ActiveRecord::Base.configurations.
|
322
|
-
next unless
|
512
|
+
ActiveRecord::Base.configurations.configs_for.each do |db_config|
|
513
|
+
next unless db_config.database
|
323
514
|
|
324
|
-
if local_database?(
|
325
|
-
yield
|
515
|
+
if local_database?(db_config)
|
516
|
+
yield db_config
|
326
517
|
else
|
327
|
-
$stderr.puts "This task only modifies local databases. #{
|
518
|
+
$stderr.puts "This task only modifies local databases. #{db_config.database} is on a remote host."
|
328
519
|
end
|
329
520
|
end
|
330
521
|
end
|
331
522
|
|
332
|
-
def local_database?(
|
333
|
-
|
523
|
+
def local_database?(db_config)
|
524
|
+
host = db_config.host
|
525
|
+
host.blank? || LOCAL_HOSTS.include?(host)
|
526
|
+
end
|
527
|
+
|
528
|
+
def schema_sha1(file)
|
529
|
+
Digest::SHA1.hexdigest(File.read(file))
|
334
530
|
end
|
335
531
|
end
|
336
532
|
end
|
@@ -3,32 +3,33 @@
|
|
3
3
|
module ActiveRecord
|
4
4
|
module Tasks # :nodoc:
|
5
5
|
class MySQLDatabaseTasks # :nodoc:
|
6
|
+
ER_DB_CREATE_EXISTS = 1007
|
7
|
+
|
6
8
|
delegate :connection, :establish_connection, to: ActiveRecord::Base
|
7
9
|
|
8
|
-
def
|
9
|
-
|
10
|
+
def self.using_database_configurations?
|
11
|
+
true
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize(db_config)
|
15
|
+
@db_config = db_config
|
16
|
+
@configuration_hash = db_config.configuration_hash
|
10
17
|
end
|
11
18
|
|
12
19
|
def create
|
13
|
-
establish_connection
|
14
|
-
connection.create_database
|
15
|
-
establish_connection
|
16
|
-
rescue ActiveRecord::StatementInvalid => error
|
17
|
-
if error.message.include?("database exists")
|
18
|
-
raise DatabaseAlreadyExists
|
19
|
-
else
|
20
|
-
raise
|
21
|
-
end
|
20
|
+
establish_connection(configuration_hash_without_database)
|
21
|
+
connection.create_database(db_config.database, creation_options)
|
22
|
+
establish_connection(db_config)
|
22
23
|
end
|
23
24
|
|
24
25
|
def drop
|
25
|
-
establish_connection
|
26
|
-
connection.drop_database
|
26
|
+
establish_connection(db_config)
|
27
|
+
connection.drop_database(db_config.database)
|
27
28
|
end
|
28
29
|
|
29
30
|
def purge
|
30
|
-
establish_connection
|
31
|
-
connection.recreate_database
|
31
|
+
establish_connection(db_config)
|
32
|
+
connection.recreate_database(db_config.database, creation_options)
|
32
33
|
end
|
33
34
|
|
34
35
|
def charset
|
@@ -48,10 +49,10 @@ module ActiveRecord
|
|
48
49
|
|
49
50
|
ignore_tables = ActiveRecord::SchemaDumper.ignore_tables
|
50
51
|
if ignore_tables.any?
|
51
|
-
args += ignore_tables.map { |table| "--ignore-table=#{
|
52
|
+
args += ignore_tables.map { |table| "--ignore-table=#{db_config.database}.#{table}" }
|
52
53
|
end
|
53
54
|
|
54
|
-
args.concat([
|
55
|
+
args.concat([db_config.database.to_s])
|
55
56
|
args.unshift(*extra_flags) if extra_flags
|
56
57
|
|
57
58
|
run_cmd("mysqldump", args, "dumping")
|
@@ -60,43 +61,40 @@ module ActiveRecord
|
|
60
61
|
def structure_load(filename, extra_flags)
|
61
62
|
args = prepare_command_options
|
62
63
|
args.concat(["--execute", %{SET FOREIGN_KEY_CHECKS = 0; SOURCE #{filename}; SET FOREIGN_KEY_CHECKS = 1}])
|
63
|
-
args.concat(["--database",
|
64
|
+
args.concat(["--database", db_config.database.to_s])
|
64
65
|
args.unshift(*extra_flags) if extra_flags
|
65
66
|
|
66
67
|
run_cmd("mysql", args, "loading")
|
67
68
|
end
|
68
69
|
|
69
70
|
private
|
71
|
+
attr_reader :db_config, :configuration_hash
|
70
72
|
|
71
|
-
def
|
72
|
-
|
73
|
-
end
|
74
|
-
|
75
|
-
def configuration_without_database
|
76
|
-
configuration.merge("database" => nil)
|
73
|
+
def configuration_hash_without_database
|
74
|
+
configuration_hash.merge(database: nil)
|
77
75
|
end
|
78
76
|
|
79
77
|
def creation_options
|
80
78
|
Hash.new.tap do |options|
|
81
|
-
options[:charset] =
|
82
|
-
options[:collation] =
|
79
|
+
options[:charset] = configuration_hash[:encoding] if configuration_hash.include?(:encoding)
|
80
|
+
options[:collation] = configuration_hash[:collation] if configuration_hash.include?(:collation)
|
83
81
|
end
|
84
82
|
end
|
85
83
|
|
86
84
|
def prepare_command_options
|
87
85
|
args = {
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
}.map { |opt, arg| "#{arg}=#{
|
86
|
+
host: "--host",
|
87
|
+
port: "--port",
|
88
|
+
socket: "--socket",
|
89
|
+
username: "--user",
|
90
|
+
password: "--password",
|
91
|
+
encoding: "--default-character-set",
|
92
|
+
sslca: "--ssl-ca",
|
93
|
+
sslcert: "--ssl-cert",
|
94
|
+
sslcapath: "--ssl-capath",
|
95
|
+
sslcipher: "--ssl-cipher",
|
96
|
+
sslkey: "--ssl-key"
|
97
|
+
}.map { |opt, arg| "#{arg}=#{configuration_hash[opt]}" if configuration_hash[opt] }.compact
|
100
98
|
|
101
99
|
args
|
102
100
|
end
|
@@ -106,7 +104,7 @@ module ActiveRecord
|
|
106
104
|
end
|
107
105
|
|
108
106
|
def run_cmd_error(cmd, args, action)
|
109
|
-
msg = "failed to execute: `#{cmd}`\n"
|
107
|
+
msg = +"failed to execute: `#{cmd}`\n"
|
110
108
|
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
109
|
msg
|
112
110
|
end
|