activerecord 5.0.6 → 6.0.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 +638 -2023
- data/MIT-LICENSE +3 -1
- data/README.rdoc +8 -6
- data/examples/performance.rb +31 -29
- data/examples/simple.rb +5 -3
- data/lib/active_record/aggregations.rb +249 -246
- data/lib/active_record/association_relation.rb +24 -13
- data/lib/active_record/associations/alias_tracker.rb +24 -33
- data/lib/active_record/associations/association.rb +119 -56
- data/lib/active_record/associations/association_scope.rb +94 -94
- data/lib/active_record/associations/belongs_to_association.rb +58 -42
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +8 -12
- data/lib/active_record/associations/builder/association.rb +18 -25
- data/lib/active_record/associations/builder/belongs_to.rb +43 -54
- data/lib/active_record/associations/builder/collection_association.rb +7 -18
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +42 -61
- data/lib/active_record/associations/builder/has_many.rb +4 -0
- data/lib/active_record/associations/builder/has_one.rb +37 -1
- data/lib/active_record/associations/builder/singular_association.rb +4 -0
- data/lib/active_record/associations/collection_association.rb +80 -252
- data/lib/active_record/associations/collection_proxy.rb +158 -121
- data/lib/active_record/associations/foreign_association.rb +9 -0
- data/lib/active_record/associations/has_many_association.rb +23 -29
- data/lib/active_record/associations/has_many_through_association.rb +58 -44
- data/lib/active_record/associations/has_one_association.rb +59 -54
- data/lib/active_record/associations/has_one_through_association.rb +20 -11
- data/lib/active_record/associations/join_dependency/join_association.rb +38 -90
- data/lib/active_record/associations/join_dependency/join_base.rb +10 -9
- data/lib/active_record/associations/join_dependency/join_part.rb +12 -12
- data/lib/active_record/associations/join_dependency.rb +134 -176
- data/lib/active_record/associations/preloader/association.rb +84 -125
- data/lib/active_record/associations/preloader/through_association.rb +82 -75
- data/lib/active_record/associations/preloader.rb +90 -102
- data/lib/active_record/associations/singular_association.rb +12 -45
- data/lib/active_record/associations/through_association.rb +26 -14
- data/lib/active_record/associations.rb +1603 -1592
- data/lib/active_record/attribute_assignment.rb +54 -60
- data/lib/active_record/attribute_decorators.rb +38 -15
- data/lib/active_record/attribute_methods/before_type_cast.rb +12 -7
- data/lib/active_record/attribute_methods/dirty.rb +179 -109
- data/lib/active_record/attribute_methods/primary_key.rb +86 -91
- data/lib/active_record/attribute_methods/query.rb +4 -3
- data/lib/active_record/attribute_methods/read.rb +21 -49
- data/lib/active_record/attribute_methods/serialization.rb +30 -7
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +39 -64
- data/lib/active_record/attribute_methods/write.rb +35 -33
- data/lib/active_record/attribute_methods.rb +66 -106
- data/lib/active_record/attributes.rb +38 -24
- data/lib/active_record/autosave_association.rb +53 -32
- data/lib/active_record/base.rb +27 -24
- data/lib/active_record/callbacks.rb +63 -33
- data/lib/active_record/coders/json.rb +2 -0
- data/lib/active_record/coders/yaml_column.rb +11 -11
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +553 -321
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +23 -5
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +213 -94
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +59 -28
- data/lib/active_record/connection_adapters/abstract/quoting.rb +119 -75
- data/lib/active_record/connection_adapters/abstract/savepoints.rb +2 -0
- data/lib/active_record/connection_adapters/abstract/schema_creation.rb +33 -27
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +207 -126
- data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +68 -80
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +369 -199
- data/lib/active_record/connection_adapters/abstract/transaction.rb +169 -78
- data/lib/active_record/connection_adapters/abstract_adapter.rb +363 -202
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +405 -551
- data/lib/active_record/connection_adapters/column.rb +41 -13
- data/lib/active_record/connection_adapters/connection_specification.rb +172 -138
- data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +11 -4
- data/lib/active_record/connection_adapters/mysql/column.rb +8 -31
- data/lib/active_record/connection_adapters/mysql/database_statements.rb +143 -49
- data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +24 -22
- data/lib/active_record/connection_adapters/mysql/quoting.rb +50 -20
- data/lib/active_record/connection_adapters/mysql/schema_creation.rb +50 -45
- data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +58 -56
- data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +70 -36
- data/lib/active_record/connection_adapters/mysql/schema_statements.rb +264 -0
- data/lib/active_record/connection_adapters/mysql/type_metadata.rb +12 -13
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +49 -30
- data/lib/active_record/connection_adapters/postgresql/column.rb +22 -7
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +60 -54
- data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +5 -3
- data/lib/active_record/connection_adapters/postgresql/oid/array.rb +22 -10
- data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +6 -5
- data/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +3 -1
- data/lib/active_record/connection_adapters/postgresql/oid/date.rb +23 -0
- data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +4 -2
- data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +3 -1
- data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +5 -3
- data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +19 -17
- data/lib/active_record/connection_adapters/postgresql/oid/inet.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +3 -11
- data/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb +45 -0
- data/lib/active_record/connection_adapters/postgresql/oid/money.rb +7 -5
- data/lib/active_record/connection_adapters/postgresql/oid/{json.rb → oid.rb} +6 -1
- data/lib/active_record/connection_adapters/postgresql/oid/point.rb +31 -9
- data/lib/active_record/connection_adapters/postgresql/oid/range.rb +34 -30
- 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 +58 -54
- data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +9 -4
- data/lib/active_record/connection_adapters/postgresql/oid/vector.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/xml.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid.rb +24 -21
- data/lib/active_record/connection_adapters/postgresql/quoting.rb +95 -35
- data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +19 -25
- data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +76 -0
- data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +147 -105
- data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +35 -32
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +380 -300
- data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +26 -25
- data/lib/active_record/connection_adapters/postgresql/utils.rb +10 -6
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +382 -275
- data/lib/active_record/connection_adapters/schema_cache.rb +46 -12
- data/lib/active_record/connection_adapters/sql_type_metadata.rb +13 -8
- data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +120 -0
- data/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb +3 -1
- data/lib/active_record/connection_adapters/sqlite3/quoting.rb +74 -19
- data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +3 -8
- 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 +254 -262
- data/lib/active_record/connection_adapters/statement_pool.rb +9 -7
- data/lib/active_record/connection_handling.rb +159 -40
- data/lib/active_record/core.rb +202 -162
- data/lib/active_record/counter_cache.rb +57 -28
- 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 +87 -86
- data/lib/active_record/enum.rb +60 -23
- data/lib/active_record/errors.rb +114 -18
- data/lib/active_record/explain.rb +4 -3
- data/lib/active_record/explain_registry.rb +3 -1
- data/lib/active_record/explain_subscriber.rb +9 -4
- data/lib/active_record/fixture_set/file.rb +13 -8
- 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 +195 -502
- data/lib/active_record/gem_version.rb +4 -2
- data/lib/active_record/inheritance.rb +151 -97
- data/lib/active_record/insert_all.rb +179 -0
- data/lib/active_record/integration.rb +116 -25
- data/lib/active_record/internal_metadata.rb +15 -18
- data/lib/active_record/legacy_yaml_adapter.rb +4 -2
- data/lib/active_record/locking/optimistic.rb +78 -87
- data/lib/active_record/locking/pessimistic.rb +18 -6
- data/lib/active_record/log_subscriber.rb +48 -29
- data/lib/active_record/middleware/database_selector/resolver/session.rb +45 -0
- data/lib/active_record/middleware/database_selector/resolver.rb +88 -0
- data/lib/active_record/middleware/database_selector.rb +75 -0
- data/lib/active_record/migration/command_recorder.rb +143 -97
- data/lib/active_record/migration/compatibility.rb +174 -56
- data/lib/active_record/migration/join_table.rb +8 -6
- data/lib/active_record/migration.rb +367 -300
- data/lib/active_record/model_schema.rb +145 -139
- data/lib/active_record/nested_attributes.rb +214 -201
- data/lib/active_record/no_touching.rb +10 -1
- data/lib/active_record/null_relation.rb +13 -34
- data/lib/active_record/persistence.rb +442 -72
- data/lib/active_record/query_cache.rb +15 -14
- data/lib/active_record/querying.rb +36 -23
- data/lib/active_record/railtie.rb +128 -36
- 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 +309 -177
- data/lib/active_record/readonly_attributes.rb +5 -4
- data/lib/active_record/reflection.rb +211 -249
- data/lib/active_record/relation/batches/batch_enumerator.rb +3 -1
- data/lib/active_record/relation/batches.rb +99 -52
- data/lib/active_record/relation/calculations.rb +211 -172
- data/lib/active_record/relation/delegation.rb +67 -65
- data/lib/active_record/relation/finder_methods.rb +208 -247
- data/lib/active_record/relation/from_clause.rb +2 -8
- data/lib/active_record/relation/merger.rb +78 -61
- data/lib/active_record/relation/predicate_builder/array_handler.rb +20 -14
- data/lib/active_record/relation/predicate_builder/association_query_value.rb +43 -0
- data/lib/active_record/relation/predicate_builder/base_handler.rb +4 -3
- data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +6 -4
- data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +53 -0
- data/lib/active_record/relation/predicate_builder/range_handler.rb +7 -18
- data/lib/active_record/relation/predicate_builder/relation_handler.rb +6 -0
- data/lib/active_record/relation/predicate_builder.rb +86 -104
- data/lib/active_record/relation/query_attribute.rb +33 -2
- data/lib/active_record/relation/query_methods.rb +458 -329
- data/lib/active_record/relation/record_fetch_warning.rb +5 -3
- data/lib/active_record/relation/spawn_methods.rb +8 -7
- data/lib/active_record/relation/where_clause.rb +111 -95
- data/lib/active_record/relation/where_clause_factory.rb +6 -11
- data/lib/active_record/relation.rb +429 -318
- data/lib/active_record/result.rb +69 -39
- data/lib/active_record/runtime_registry.rb +5 -3
- data/lib/active_record/sanitization.rb +83 -99
- data/lib/active_record/schema.rb +7 -14
- data/lib/active_record/schema_dumper.rb +71 -69
- data/lib/active_record/schema_migration.rb +15 -5
- data/lib/active_record/scoping/default.rb +93 -95
- data/lib/active_record/scoping/named.rb +45 -25
- data/lib/active_record/scoping.rb +20 -19
- data/lib/active_record/secure_token.rb +4 -2
- data/lib/active_record/serialization.rb +2 -0
- data/lib/active_record/statement_cache.rb +63 -28
- data/lib/active_record/store.rb +121 -41
- data/lib/active_record/suppressor.rb +4 -1
- data/lib/active_record/table_metadata.rb +26 -20
- data/lib/active_record/tasks/database_tasks.rb +276 -85
- data/lib/active_record/tasks/mysql_database_tasks.rb +54 -90
- data/lib/active_record/tasks/postgresql_database_tasks.rb +78 -47
- data/lib/active_record/tasks/sqlite_database_tasks.rb +34 -16
- data/lib/active_record/test_databases.rb +23 -0
- data/lib/active_record/test_fixtures.rb +224 -0
- data/lib/active_record/timestamp.rb +70 -35
- data/lib/active_record/touch_later.rb +7 -4
- data/lib/active_record/transactions.rb +133 -149
- data/lib/active_record/translation.rb +3 -1
- data/lib/active_record/type/adapter_specific_registry.rb +44 -45
- data/lib/active_record/type/date.rb +2 -0
- data/lib/active_record/type/date_time.rb +2 -0
- data/lib/active_record/type/decimal_without_scale.rb +15 -0
- data/lib/active_record/type/hash_lookup_type_map.rb +5 -3
- data/lib/active_record/type/internal/timezone.rb +2 -0
- data/lib/active_record/type/json.rb +30 -0
- data/lib/active_record/type/serialized.rb +16 -8
- data/lib/active_record/type/text.rb +11 -0
- data/lib/active_record/type/time.rb +2 -1
- data/lib/active_record/type/type_map.rb +13 -15
- data/lib/active_record/type/unsigned_integer.rb +17 -0
- data/lib/active_record/type.rb +23 -17
- data/lib/active_record/type_caster/connection.rb +17 -12
- data/lib/active_record/type_caster/map.rb +5 -4
- data/lib/active_record/type_caster.rb +4 -2
- data/lib/active_record/validations/absence.rb +2 -0
- data/lib/active_record/validations/associated.rb +3 -1
- data/lib/active_record/validations/length.rb +2 -0
- data/lib/active_record/validations/presence.rb +4 -2
- data/lib/active_record/validations/uniqueness.rb +29 -42
- data/lib/active_record/validations.rb +7 -4
- data/lib/active_record/version.rb +3 -1
- data/lib/active_record.rb +36 -22
- 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 +58 -0
- data/lib/rails/generators/active_record/application_record/application_record_generator.rb +27 -0
- data/lib/rails/generators/active_record/{model/templates/application_record.rb → application_record/templates/application_record.rb.tt} +0 -0
- data/lib/rails/generators/active_record/migration/migration_generator.rb +37 -35
- data/lib/rails/generators/active_record/migration/templates/{create_table_migration.rb → create_table_migration.rb.tt} +1 -1
- data/lib/rails/generators/active_record/migration/templates/{migration.rb → migration.rb.tt} +4 -2
- data/lib/rails/generators/active_record/migration.rb +17 -2
- data/lib/rails/generators/active_record/model/model_generator.rb +9 -29
- data/lib/rails/generators/active_record/model/templates/{model.rb → model.rb.tt} +10 -1
- data/lib/rails/generators/active_record/model/templates/{module.rb → module.rb.tt} +0 -0
- data/lib/rails/generators/active_record.rb +7 -5
- metadata +133 -50
- data/lib/active_record/associations/preloader/belongs_to.rb +0 -17
- data/lib/active_record/associations/preloader/collection_association.rb +0 -17
- 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 -15
- data/lib/active_record/associations/preloader/has_one_through.rb +0 -9
- data/lib/active_record/associations/preloader/singular_association.rb +0 -20
- data/lib/active_record/attribute/user_provided_default.rb +0 -28
- data/lib/active_record/attribute.rb +0 -213
- data/lib/active_record/attribute_mutation_tracker.rb +0 -70
- data/lib/active_record/attribute_set/builder.rb +0 -130
- data/lib/active_record/attribute_set.rb +0 -110
- data/lib/active_record/collection_cache_key.rb +0 -50
- data/lib/active_record/connection_adapters/postgresql/oid/rails_5_1_point.rb +0 -50
- data/lib/active_record/railties/jdbcmysql_error.rb +0 -16
- data/lib/active_record/relation/predicate_builder/association_query_handler.rb +0 -88
- data/lib/active_record/relation/predicate_builder/class_handler.rb +0 -27
- data/lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb +0 -57
- data/lib/active_record/type/internal/abstract_json.rb +0 -33
@@ -1,32 +1,77 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActiveRecord
|
2
4
|
module ConnectionAdapters #:nodoc:
|
3
5
|
# Abstract representation of an index definition on a table. Instances of
|
4
6
|
# this type are typically created and returned by methods in database
|
5
|
-
# adapters. e.g. ActiveRecord::ConnectionAdapters::
|
6
|
-
class IndexDefinition
|
7
|
+
# adapters. e.g. ActiveRecord::ConnectionAdapters::MySQL::SchemaStatements#indexes
|
8
|
+
class IndexDefinition # :nodoc:
|
9
|
+
attr_reader :table, :name, :unique, :columns, :lengths, :orders, :opclasses, :where, :type, :using, :comment
|
10
|
+
|
11
|
+
def initialize(
|
12
|
+
table, name,
|
13
|
+
unique = false,
|
14
|
+
columns = [],
|
15
|
+
lengths: {},
|
16
|
+
orders: {},
|
17
|
+
opclasses: {},
|
18
|
+
where: nil,
|
19
|
+
type: nil,
|
20
|
+
using: nil,
|
21
|
+
comment: nil
|
22
|
+
)
|
23
|
+
@table = table
|
24
|
+
@name = name
|
25
|
+
@unique = unique
|
26
|
+
@columns = columns
|
27
|
+
@lengths = concise_options(lengths)
|
28
|
+
@orders = concise_options(orders)
|
29
|
+
@opclasses = concise_options(opclasses)
|
30
|
+
@where = where
|
31
|
+
@type = type
|
32
|
+
@using = using
|
33
|
+
@comment = comment
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
def concise_options(options)
|
38
|
+
if columns.size == options.size && options.values.uniq.size == 1
|
39
|
+
options.values.first
|
40
|
+
else
|
41
|
+
options
|
42
|
+
end
|
43
|
+
end
|
7
44
|
end
|
8
45
|
|
9
46
|
# Abstract representation of a column definition. Instances of this type
|
10
47
|
# are typically created by methods in TableDefinition, and added to the
|
11
48
|
# +columns+ attribute of said TableDefinition object, in order to be used
|
12
49
|
# for generating a number of table creation or table changing SQL statements.
|
13
|
-
|
14
|
-
|
50
|
+
ColumnDefinition = Struct.new(:name, :type, :options, :sql_type) do # :nodoc:
|
15
51
|
def primary_key?
|
16
|
-
|
52
|
+
options[:primary_key]
|
17
53
|
end
|
18
|
-
end
|
19
54
|
|
20
|
-
|
21
|
-
|
55
|
+
[:limit, :precision, :scale, :default, :null, :collation, :comment].each do |option_name|
|
56
|
+
module_eval <<-CODE, __FILE__, __LINE__ + 1
|
57
|
+
def #{option_name}
|
58
|
+
options[:#{option_name}]
|
59
|
+
end
|
22
60
|
|
23
|
-
|
61
|
+
def #{option_name}=(value)
|
62
|
+
options[:#{option_name}] = value
|
63
|
+
end
|
64
|
+
CODE
|
65
|
+
end
|
24
66
|
end
|
25
67
|
|
26
|
-
|
27
|
-
|
68
|
+
AddColumnDefinition = Struct.new(:column) # :nodoc:
|
69
|
+
|
70
|
+
ChangeColumnDefinition = Struct.new(:column, :name) #:nodoc:
|
71
|
+
|
72
|
+
PrimaryKeyDefinition = Struct.new(:name) # :nodoc:
|
28
73
|
|
29
|
-
|
74
|
+
ForeignKeyDefinition = Struct.new(:from_table, :to_table, :options) do #:nodoc:
|
30
75
|
def name
|
31
76
|
options[:name]
|
32
77
|
end
|
@@ -51,19 +96,24 @@ module ActiveRecord
|
|
51
96
|
options[:primary_key] != default_primary_key
|
52
97
|
end
|
53
98
|
|
54
|
-
def
|
55
|
-
|
56
|
-
self.to_table == to_table_ord.to_s
|
57
|
-
else
|
58
|
-
(to_table.nil? || to_table.to_s == self.to_table) &&
|
59
|
-
options.all? { |k, v| self.options[k].to_s == v.to_s }
|
60
|
-
end
|
99
|
+
def validate?
|
100
|
+
options.fetch(:validate, true)
|
61
101
|
end
|
102
|
+
alias validated? validate?
|
62
103
|
|
63
|
-
|
64
|
-
|
65
|
-
"id"
|
104
|
+
def export_name_on_schema_dump?
|
105
|
+
!ActiveRecord::SchemaDumper.fk_ignore_pattern.match?(name) if name
|
66
106
|
end
|
107
|
+
|
108
|
+
def defined_for?(to_table: nil, **options)
|
109
|
+
(to_table.nil? || to_table.to_s == self.to_table) &&
|
110
|
+
options.all? { |k, v| self.options[k].to_s == v.to_s }
|
111
|
+
end
|
112
|
+
|
113
|
+
private
|
114
|
+
def default_primary_key
|
115
|
+
"id"
|
116
|
+
end
|
67
117
|
end
|
68
118
|
|
69
119
|
class ReferenceDefinition # :nodoc:
|
@@ -72,7 +122,7 @@ module ActiveRecord
|
|
72
122
|
polymorphic: false,
|
73
123
|
index: true,
|
74
124
|
foreign_key: false,
|
75
|
-
type: :
|
125
|
+
type: :bigint,
|
76
126
|
**options
|
77
127
|
)
|
78
128
|
@name = name
|
@@ -101,85 +151,87 @@ module ActiveRecord
|
|
101
151
|
end
|
102
152
|
end
|
103
153
|
|
104
|
-
protected
|
105
|
-
|
106
|
-
attr_reader :name, :polymorphic, :index, :foreign_key, :type, :options
|
107
|
-
|
108
154
|
private
|
155
|
+
attr_reader :name, :polymorphic, :index, :foreign_key, :type, :options
|
109
156
|
|
110
|
-
|
111
|
-
|
112
|
-
|
157
|
+
def as_options(value)
|
158
|
+
value.is_a?(Hash) ? value : {}
|
159
|
+
end
|
113
160
|
|
114
|
-
|
115
|
-
|
116
|
-
|
161
|
+
def polymorphic_options
|
162
|
+
as_options(polymorphic).merge(options.slice(:null, :first, :after))
|
163
|
+
end
|
117
164
|
|
118
|
-
|
119
|
-
|
120
|
-
|
165
|
+
def index_options
|
166
|
+
as_options(index)
|
167
|
+
end
|
121
168
|
|
122
|
-
|
123
|
-
|
124
|
-
|
169
|
+
def foreign_key_options
|
170
|
+
as_options(foreign_key).merge(column: column_name)
|
171
|
+
end
|
125
172
|
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
173
|
+
def columns
|
174
|
+
result = [[column_name, type, options]]
|
175
|
+
if polymorphic
|
176
|
+
result.unshift(["#{name}_type", :string, polymorphic_options])
|
177
|
+
end
|
178
|
+
result
|
130
179
|
end
|
131
|
-
result
|
132
|
-
end
|
133
180
|
|
134
|
-
|
135
|
-
|
136
|
-
|
181
|
+
def column_name
|
182
|
+
"#{name}_id"
|
183
|
+
end
|
137
184
|
|
138
|
-
|
139
|
-
|
140
|
-
|
185
|
+
def column_names
|
186
|
+
columns.map(&:first)
|
187
|
+
end
|
141
188
|
|
142
|
-
|
143
|
-
|
144
|
-
|
189
|
+
def foreign_table_name
|
190
|
+
foreign_key_options.fetch(:to_table) do
|
191
|
+
Base.pluralize_table_names ? name.to_s.pluralize : name
|
192
|
+
end
|
145
193
|
end
|
146
|
-
end
|
147
194
|
end
|
148
195
|
|
149
196
|
module ColumnMethods
|
197
|
+
extend ActiveSupport::Concern
|
198
|
+
|
150
199
|
# Appends a primary key definition to the table definition.
|
151
200
|
# Can be called multiple times, but this is probably not a good idea.
|
152
201
|
def primary_key(name, type = :primary_key, **options)
|
153
202
|
column(name, type, options.merge(primary_key: true))
|
154
203
|
end
|
155
204
|
|
205
|
+
##
|
206
|
+
# :method: column
|
207
|
+
# :call-seq: column(name, type, **options)
|
208
|
+
#
|
156
209
|
# Appends a column or columns of a specified type.
|
157
210
|
#
|
158
211
|
# t.string(:goat)
|
159
212
|
# t.string(:goat, :sheep)
|
160
213
|
#
|
161
214
|
# See TableDefinition#column
|
162
|
-
|
163
|
-
|
164
|
-
:binary,
|
165
|
-
|
166
|
-
|
167
|
-
:
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
:
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
215
|
+
|
216
|
+
included do
|
217
|
+
define_column_methods :bigint, :binary, :boolean, :date, :datetime, :decimal,
|
218
|
+
:float, :integer, :json, :string, :text, :time, :timestamp, :virtual
|
219
|
+
|
220
|
+
alias :numeric :decimal
|
221
|
+
end
|
222
|
+
|
223
|
+
class_methods do
|
224
|
+
private def define_column_methods(*column_types) # :nodoc:
|
225
|
+
column_types.each do |column_type|
|
226
|
+
module_eval <<-RUBY, __FILE__, __LINE__ + 1
|
227
|
+
def #{column_type}(*names, **options)
|
228
|
+
raise ArgumentError, "Missing column name(s) for #{column_type}" if names.empty?
|
229
|
+
names.each { |name| column(name, :#{column_type}, options) }
|
230
|
+
end
|
231
|
+
RUBY
|
179
232
|
end
|
180
|
-
|
233
|
+
end
|
181
234
|
end
|
182
|
-
alias_method :numeric, :decimal
|
183
235
|
end
|
184
236
|
|
185
237
|
# Represents the schema of an SQL table in an abstract way. This class
|
@@ -203,15 +255,25 @@ module ActiveRecord
|
|
203
255
|
class TableDefinition
|
204
256
|
include ColumnMethods
|
205
257
|
|
206
|
-
|
207
|
-
attr_reader :name, :temporary, :options, :as, :foreign_keys, :comment
|
258
|
+
attr_reader :name, :temporary, :if_not_exists, :options, :as, :comment, :indexes, :foreign_keys
|
208
259
|
|
209
|
-
def initialize(
|
260
|
+
def initialize(
|
261
|
+
conn,
|
262
|
+
name,
|
263
|
+
temporary: false,
|
264
|
+
if_not_exists: false,
|
265
|
+
options: nil,
|
266
|
+
as: nil,
|
267
|
+
comment: nil,
|
268
|
+
**
|
269
|
+
)
|
270
|
+
@conn = conn
|
210
271
|
@columns_hash = {}
|
211
272
|
@indexes = []
|
212
273
|
@foreign_keys = []
|
213
274
|
@primary_keys = nil
|
214
275
|
@temporary = temporary
|
276
|
+
@if_not_exists = if_not_exists
|
215
277
|
@options = options
|
216
278
|
@as = as
|
217
279
|
@name = name
|
@@ -295,16 +357,20 @@ module ActiveRecord
|
|
295
357
|
#
|
296
358
|
# create_table :taggings do |t|
|
297
359
|
# t.references :tag, index: { name: 'index_taggings_on_tag_id' }
|
298
|
-
# t.references :tagger, polymorphic: true
|
299
|
-
# t.references :taggable, polymorphic: { default: 'Photo' }
|
360
|
+
# t.references :tagger, polymorphic: true
|
361
|
+
# t.references :taggable, polymorphic: { default: 'Photo' }, index: false
|
300
362
|
# end
|
301
|
-
def column(name, type, options
|
363
|
+
def column(name, type, **options)
|
302
364
|
name = name.to_s
|
303
365
|
type = type.to_sym if type
|
304
366
|
options = options.dup
|
305
367
|
|
306
|
-
if @columns_hash[name]
|
307
|
-
|
368
|
+
if @columns_hash[name]
|
369
|
+
if @columns_hash[name].primary_key?
|
370
|
+
raise ArgumentError, "you can't redefine the primary key column '#{name}'. To define a custom primary key, pass { id: false } to create_table."
|
371
|
+
else
|
372
|
+
raise ArgumentError, "you can't define an already defined column '#{name}'."
|
373
|
+
end
|
308
374
|
end
|
309
375
|
|
310
376
|
index_options = options.delete(:index)
|
@@ -328,21 +394,20 @@ module ActiveRecord
|
|
328
394
|
end
|
329
395
|
|
330
396
|
def foreign_key(table_name, options = {}) # :nodoc:
|
331
|
-
|
332
|
-
table_name_suffix = ActiveRecord::Base.table_name_suffix
|
333
|
-
table_name = "#{table_name_prefix}#{table_name}#{table_name_suffix}"
|
334
|
-
foreign_keys.push([table_name, options])
|
397
|
+
foreign_keys << [table_name, options]
|
335
398
|
end
|
336
399
|
|
337
400
|
# Appends <tt>:datetime</tt> columns <tt>:created_at</tt> and
|
338
401
|
# <tt>:updated_at</tt> to the table. See {connection.add_timestamps}[rdoc-ref:SchemaStatements#add_timestamps]
|
339
402
|
#
|
340
403
|
# t.timestamps null: false
|
341
|
-
def timestamps(
|
342
|
-
options = args.extract_options!
|
343
|
-
|
404
|
+
def timestamps(**options)
|
344
405
|
options[:null] = false if options[:null].nil?
|
345
406
|
|
407
|
+
if !options.key?(:precision) && @conn.supports_datetime_with_precision?
|
408
|
+
options[:precision] = 6
|
409
|
+
end
|
410
|
+
|
346
411
|
column(:created_at, :datetime, options)
|
347
412
|
column(:updated_at, :datetime, options)
|
348
413
|
end
|
@@ -351,41 +416,42 @@ module ActiveRecord
|
|
351
416
|
#
|
352
417
|
# t.references(:user)
|
353
418
|
# t.belongs_to(:supplier, foreign_key: true)
|
419
|
+
# t.belongs_to(:supplier, foreign_key: true, type: :integer)
|
354
420
|
#
|
355
421
|
# See {connection.add_reference}[rdoc-ref:SchemaStatements#add_reference] for details of the options you can use.
|
356
422
|
def references(*args, **options)
|
357
|
-
args.each do |
|
358
|
-
ReferenceDefinition.new(
|
423
|
+
args.each do |ref_name|
|
424
|
+
ReferenceDefinition.new(ref_name, options).add_to(self)
|
359
425
|
end
|
360
426
|
end
|
361
427
|
alias :belongs_to :references
|
362
428
|
|
363
|
-
def new_column_definition(name, type, options) # :nodoc:
|
429
|
+
def new_column_definition(name, type, **options) # :nodoc:
|
430
|
+
if integer_like_primary_key?(type, options)
|
431
|
+
type = integer_like_primary_key_type(type, options)
|
432
|
+
end
|
364
433
|
type = aliased_types(type.to_s, type)
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
column.precision = options[:precision]
|
369
|
-
column.scale = options[:scale]
|
370
|
-
column.default = options[:default]
|
371
|
-
column.null = options[:null]
|
372
|
-
column.first = options[:first]
|
373
|
-
column.after = options[:after]
|
374
|
-
column.auto_increment = options[:auto_increment]
|
375
|
-
column.primary_key = type == :primary_key || options[:primary_key]
|
376
|
-
column.collation = options[:collation]
|
377
|
-
column.comment = options[:comment]
|
378
|
-
column
|
434
|
+
options[:primary_key] ||= type == :primary_key
|
435
|
+
options[:null] = false if options[:primary_key]
|
436
|
+
create_column_definition(name, type, options)
|
379
437
|
end
|
380
438
|
|
381
439
|
private
|
382
|
-
|
383
|
-
|
384
|
-
|
440
|
+
def create_column_definition(name, type, options)
|
441
|
+
ColumnDefinition.new(name, type, options)
|
442
|
+
end
|
385
443
|
|
386
|
-
|
387
|
-
|
388
|
-
|
444
|
+
def aliased_types(name, fallback)
|
445
|
+
"timestamp" == name ? :datetime : fallback
|
446
|
+
end
|
447
|
+
|
448
|
+
def integer_like_primary_key?(type, options)
|
449
|
+
options[:primary_key] && [:integer, :bigint].include?(type) && !options.key?(:default)
|
450
|
+
end
|
451
|
+
|
452
|
+
def integer_like_primary_key_type(type, options)
|
453
|
+
type
|
454
|
+
end
|
389
455
|
end
|
390
456
|
|
391
457
|
class AlterTable # :nodoc:
|
@@ -446,7 +512,11 @@ module ActiveRecord
|
|
446
512
|
# t.date
|
447
513
|
# t.binary
|
448
514
|
# t.boolean
|
515
|
+
# t.foreign_key
|
516
|
+
# t.json
|
517
|
+
# t.virtual
|
449
518
|
# t.remove
|
519
|
+
# t.remove_foreign_key
|
450
520
|
# t.remove_references
|
451
521
|
# t.remove_belongs_to
|
452
522
|
# t.remove_index
|
@@ -468,13 +538,15 @@ module ActiveRecord
|
|
468
538
|
# t.column(:name, :string)
|
469
539
|
#
|
470
540
|
# See TableDefinition#column for details of the options you can use.
|
471
|
-
def column(column_name, type, options
|
541
|
+
def column(column_name, type, **options)
|
542
|
+
index_options = options.delete(:index)
|
472
543
|
@base.add_column(name, column_name, type, options)
|
544
|
+
index(column_name, index_options.is_a?(Hash) ? index_options : {}) if index_options
|
473
545
|
end
|
474
546
|
|
475
547
|
# Checks to see if a column exists.
|
476
548
|
#
|
477
|
-
#
|
549
|
+
# t.string(:name) unless t.column_exists?(:name, :string)
|
478
550
|
#
|
479
551
|
# See {connection.column_exists?}[rdoc-ref:SchemaStatements#column_exists?]
|
480
552
|
def column_exists?(column_name, type = nil, options = {})
|
@@ -495,9 +567,9 @@ module ActiveRecord
|
|
495
567
|
|
496
568
|
# Checks to see if an index exists.
|
497
569
|
#
|
498
|
-
#
|
499
|
-
#
|
500
|
-
#
|
570
|
+
# unless t.index_exists?(:branch_id)
|
571
|
+
# t.index(:branch_id)
|
572
|
+
# end
|
501
573
|
#
|
502
574
|
# See {connection.index_exists?}[rdoc-ref:SchemaStatements#index_exists?]
|
503
575
|
def index_exists?(column_name, options = {})
|
@@ -588,8 +660,7 @@ module ActiveRecord
|
|
588
660
|
# t.belongs_to(:supplier, foreign_key: true)
|
589
661
|
#
|
590
662
|
# See {connection.add_reference}[rdoc-ref:SchemaStatements#add_reference] for details of the options you can use.
|
591
|
-
def references(*args)
|
592
|
-
options = args.extract_options!
|
663
|
+
def references(*args, **options)
|
593
664
|
args.each do |ref_name|
|
594
665
|
@base.add_reference(name, ref_name, options)
|
595
666
|
end
|
@@ -602,29 +673,39 @@ module ActiveRecord
|
|
602
673
|
# t.remove_belongs_to(:supplier, polymorphic: true)
|
603
674
|
#
|
604
675
|
# See {connection.remove_reference}[rdoc-ref:SchemaStatements#remove_reference]
|
605
|
-
def remove_references(*args)
|
606
|
-
options = args.extract_options!
|
676
|
+
def remove_references(*args, **options)
|
607
677
|
args.each do |ref_name|
|
608
678
|
@base.remove_reference(name, ref_name, options)
|
609
679
|
end
|
610
680
|
end
|
611
681
|
alias :remove_belongs_to :remove_references
|
612
682
|
|
613
|
-
# Adds a foreign key.
|
683
|
+
# Adds a foreign key to the table using a supplied table name.
|
614
684
|
#
|
615
|
-
#
|
685
|
+
# t.foreign_key(:authors)
|
686
|
+
# t.foreign_key(:authors, column: :author_id, primary_key: "id")
|
616
687
|
#
|
617
688
|
# See {connection.add_foreign_key}[rdoc-ref:SchemaStatements#add_foreign_key]
|
618
|
-
def foreign_key(*args)
|
689
|
+
def foreign_key(*args)
|
619
690
|
@base.add_foreign_key(name, *args)
|
620
691
|
end
|
621
692
|
|
693
|
+
# Removes the given foreign key from the table.
|
694
|
+
#
|
695
|
+
# t.remove_foreign_key(:authors)
|
696
|
+
# t.remove_foreign_key(column: :author_id)
|
697
|
+
#
|
698
|
+
# See {connection.remove_foreign_key}[rdoc-ref:SchemaStatements#remove_foreign_key]
|
699
|
+
def remove_foreign_key(*args)
|
700
|
+
@base.remove_foreign_key(name, *args)
|
701
|
+
end
|
702
|
+
|
622
703
|
# Checks to see if a foreign key exists.
|
623
704
|
#
|
624
|
-
#
|
705
|
+
# t.foreign_key(:authors) unless t.foreign_key_exists?(:authors)
|
625
706
|
#
|
626
707
|
# See {connection.foreign_key_exists?}[rdoc-ref:SchemaStatements#foreign_key_exists?]
|
627
|
-
def foreign_key_exists?(*args)
|
708
|
+
def foreign_key_exists?(*args)
|
628
709
|
@base.foreign_key_exists?(name, *args)
|
629
710
|
end
|
630
711
|
end
|