activerecord 7.0.8.7 → 7.2.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +631 -1944
- data/MIT-LICENSE +1 -1
- data/README.rdoc +29 -29
- data/examples/performance.rb +2 -2
- data/lib/active_record/aggregations.rb +16 -13
- data/lib/active_record/association_relation.rb +2 -2
- data/lib/active_record/associations/alias_tracker.rb +25 -19
- data/lib/active_record/associations/association.rb +35 -12
- data/lib/active_record/associations/association_scope.rb +16 -9
- data/lib/active_record/associations/belongs_to_association.rb +23 -8
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +3 -2
- data/lib/active_record/associations/builder/association.rb +3 -3
- data/lib/active_record/associations/builder/belongs_to.rb +22 -8
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +3 -7
- data/lib/active_record/associations/builder/has_many.rb +3 -4
- data/lib/active_record/associations/builder/has_one.rb +3 -4
- data/lib/active_record/associations/builder/singular_association.rb +4 -0
- data/lib/active_record/associations/collection_association.rb +26 -14
- data/lib/active_record/associations/collection_proxy.rb +29 -11
- data/lib/active_record/associations/errors.rb +265 -0
- data/lib/active_record/associations/foreign_association.rb +10 -3
- data/lib/active_record/associations/has_many_association.rb +21 -14
- data/lib/active_record/associations/has_many_through_association.rb +17 -7
- data/lib/active_record/associations/has_one_association.rb +10 -3
- data/lib/active_record/associations/join_dependency/join_association.rb +30 -27
- data/lib/active_record/associations/join_dependency.rb +10 -10
- data/lib/active_record/associations/nested_error.rb +47 -0
- data/lib/active_record/associations/preloader/association.rb +33 -8
- data/lib/active_record/associations/preloader/branch.rb +7 -1
- data/lib/active_record/associations/preloader/through_association.rb +1 -3
- data/lib/active_record/associations/preloader.rb +13 -10
- data/lib/active_record/associations/singular_association.rb +7 -1
- data/lib/active_record/associations/through_association.rb +22 -11
- data/lib/active_record/associations.rb +354 -485
- data/lib/active_record/attribute_assignment.rb +0 -4
- data/lib/active_record/attribute_methods/before_type_cast.rb +17 -0
- data/lib/active_record/attribute_methods/composite_primary_key.rb +84 -0
- data/lib/active_record/attribute_methods/dirty.rb +53 -35
- data/lib/active_record/attribute_methods/primary_key.rb +45 -25
- data/lib/active_record/attribute_methods/query.rb +28 -16
- data/lib/active_record/attribute_methods/read.rb +8 -7
- data/lib/active_record/attribute_methods/serialization.rb +131 -32
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +11 -6
- data/lib/active_record/attribute_methods/write.rb +6 -6
- data/lib/active_record/attribute_methods.rb +148 -33
- data/lib/active_record/attributes.rb +64 -50
- data/lib/active_record/autosave_association.rb +69 -37
- data/lib/active_record/base.rb +9 -5
- data/lib/active_record/callbacks.rb +11 -25
- data/lib/active_record/coders/column_serializer.rb +61 -0
- data/lib/active_record/coders/json.rb +1 -1
- data/lib/active_record/coders/yaml_column.rb +70 -42
- data/lib/active_record/connection_adapters/abstract/connection_handler.rb +123 -131
- data/lib/active_record/connection_adapters/abstract/connection_pool/queue.rb +2 -0
- data/lib/active_record/connection_adapters/abstract/connection_pool/reaper.rb +4 -1
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +323 -88
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +5 -0
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +160 -45
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +217 -63
- data/lib/active_record/connection_adapters/abstract/quoting.rb +72 -63
- data/lib/active_record/connection_adapters/abstract/savepoints.rb +4 -3
- data/lib/active_record/connection_adapters/abstract/schema_creation.rb +18 -4
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +137 -11
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +307 -129
- data/lib/active_record/connection_adapters/abstract/transaction.rb +367 -75
- data/lib/active_record/connection_adapters/abstract_adapter.rb +510 -111
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +278 -125
- data/lib/active_record/connection_adapters/column.rb +9 -0
- data/lib/active_record/connection_adapters/mysql/column.rb +1 -0
- data/lib/active_record/connection_adapters/mysql/database_statements.rb +26 -139
- data/lib/active_record/connection_adapters/mysql/quoting.rb +53 -54
- data/lib/active_record/connection_adapters/mysql/schema_creation.rb +9 -0
- data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +6 -0
- data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +1 -1
- data/lib/active_record/connection_adapters/mysql/schema_statements.rb +25 -13
- data/lib/active_record/connection_adapters/mysql2/database_statements.rb +152 -0
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +101 -68
- data/lib/active_record/connection_adapters/pool_config.rb +20 -10
- data/lib/active_record/connection_adapters/pool_manager.rb +19 -9
- data/lib/active_record/connection_adapters/postgresql/column.rb +14 -3
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +100 -43
- data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +6 -0
- data/lib/active_record/connection_adapters/postgresql/oid/interval.rb +1 -1
- data/lib/active_record/connection_adapters/postgresql/oid/money.rb +3 -2
- data/lib/active_record/connection_adapters/postgresql/oid/range.rb +11 -2
- data/lib/active_record/connection_adapters/postgresql/oid/timestamp_with_time_zone.rb +1 -1
- data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +14 -4
- data/lib/active_record/connection_adapters/postgresql/quoting.rb +65 -61
- data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +3 -9
- data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +76 -6
- data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +151 -2
- data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +53 -0
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +370 -63
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +367 -201
- data/lib/active_record/connection_adapters/schema_cache.rb +302 -79
- data/lib/active_record/connection_adapters/sqlite3/column.rb +62 -0
- data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +60 -43
- data/lib/active_record/connection_adapters/sqlite3/quoting.rb +45 -46
- data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +22 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +14 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb +16 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +50 -8
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +290 -110
- data/lib/active_record/connection_adapters/statement_pool.rb +7 -0
- data/lib/active_record/connection_adapters/trilogy/database_statements.rb +99 -0
- data/lib/active_record/connection_adapters/trilogy_adapter.rb +229 -0
- data/lib/active_record/connection_adapters.rb +124 -1
- data/lib/active_record/connection_handling.rb +96 -104
- data/lib/active_record/core.rb +251 -176
- data/lib/active_record/counter_cache.rb +68 -34
- data/lib/active_record/database_configurations/connection_url_resolver.rb +8 -3
- data/lib/active_record/database_configurations/database_config.rb +26 -5
- data/lib/active_record/database_configurations/hash_config.rb +52 -34
- data/lib/active_record/database_configurations/url_config.rb +37 -12
- data/lib/active_record/database_configurations.rb +87 -34
- data/lib/active_record/delegated_type.rb +39 -10
- data/lib/active_record/deprecator.rb +7 -0
- data/lib/active_record/destroy_association_async_job.rb +3 -1
- data/lib/active_record/dynamic_matchers.rb +2 -2
- data/lib/active_record/encryption/auto_filtered_parameters.rb +66 -0
- data/lib/active_record/encryption/cipher/aes256_gcm.rb +4 -1
- data/lib/active_record/encryption/config.rb +25 -1
- data/lib/active_record/encryption/configurable.rb +12 -19
- data/lib/active_record/encryption/context.rb +10 -3
- data/lib/active_record/encryption/contexts.rb +5 -1
- data/lib/active_record/encryption/derived_secret_key_provider.rb +8 -2
- data/lib/active_record/encryption/encryptable_record.rb +45 -21
- data/lib/active_record/encryption/encrypted_attribute_type.rb +47 -12
- data/lib/active_record/encryption/encryptor.rb +18 -3
- data/lib/active_record/encryption/extended_deterministic_queries.rb +66 -69
- data/lib/active_record/encryption/extended_deterministic_uniqueness_validator.rb +3 -3
- data/lib/active_record/encryption/key_generator.rb +12 -1
- data/lib/active_record/encryption/key_provider.rb +1 -1
- data/lib/active_record/encryption/message_pack_message_serializer.rb +76 -0
- data/lib/active_record/encryption/message_serializer.rb +6 -0
- data/lib/active_record/encryption/null_encryptor.rb +4 -0
- data/lib/active_record/encryption/properties.rb +3 -3
- data/lib/active_record/encryption/read_only_null_encryptor.rb +4 -0
- data/lib/active_record/encryption/scheme.rb +22 -21
- data/lib/active_record/encryption.rb +3 -0
- data/lib/active_record/enum.rb +129 -28
- data/lib/active_record/errors.rb +151 -31
- data/lib/active_record/explain.rb +21 -12
- data/lib/active_record/fixture_set/model_metadata.rb +14 -4
- data/lib/active_record/fixture_set/render_context.rb +2 -0
- data/lib/active_record/fixture_set/table_row.rb +29 -8
- data/lib/active_record/fixtures.rb +167 -97
- data/lib/active_record/future_result.rb +47 -8
- data/lib/active_record/gem_version.rb +4 -4
- data/lib/active_record/inheritance.rb +34 -18
- data/lib/active_record/insert_all.rb +72 -22
- data/lib/active_record/integration.rb +11 -8
- data/lib/active_record/internal_metadata.rb +124 -20
- data/lib/active_record/locking/optimistic.rb +8 -7
- data/lib/active_record/locking/pessimistic.rb +5 -2
- data/lib/active_record/log_subscriber.rb +18 -22
- data/lib/active_record/marshalling.rb +59 -0
- data/lib/active_record/message_pack.rb +124 -0
- data/lib/active_record/middleware/database_selector/resolver.rb +4 -0
- data/lib/active_record/middleware/database_selector.rb +6 -8
- data/lib/active_record/middleware/shard_selector.rb +3 -1
- data/lib/active_record/migration/command_recorder.rb +106 -8
- data/lib/active_record/migration/compatibility.rb +147 -5
- data/lib/active_record/migration/default_strategy.rb +22 -0
- data/lib/active_record/migration/execution_strategy.rb +19 -0
- data/lib/active_record/migration/pending_migration_connection.rb +21 -0
- data/lib/active_record/migration.rb +234 -117
- data/lib/active_record/model_schema.rb +90 -102
- data/lib/active_record/nested_attributes.rb +48 -11
- data/lib/active_record/normalization.rb +163 -0
- data/lib/active_record/persistence.rb +168 -339
- data/lib/active_record/promise.rb +84 -0
- data/lib/active_record/query_cache.rb +18 -25
- data/lib/active_record/query_logs.rb +92 -52
- data/lib/active_record/query_logs_formatter.rb +41 -0
- data/lib/active_record/querying.rb +33 -8
- data/lib/active_record/railtie.rb +129 -85
- data/lib/active_record/railties/controller_runtime.rb +22 -7
- data/lib/active_record/railties/databases.rake +145 -154
- data/lib/active_record/railties/job_runtime.rb +23 -0
- data/lib/active_record/readonly_attributes.rb +32 -5
- data/lib/active_record/reflection.rb +267 -69
- data/lib/active_record/relation/batches/batch_enumerator.rb +20 -5
- data/lib/active_record/relation/batches.rb +198 -63
- data/lib/active_record/relation/calculations.rb +250 -93
- data/lib/active_record/relation/delegation.rb +30 -19
- data/lib/active_record/relation/finder_methods.rb +93 -18
- data/lib/active_record/relation/merger.rb +6 -6
- data/lib/active_record/relation/predicate_builder/array_handler.rb +2 -2
- data/lib/active_record/relation/predicate_builder/association_query_value.rb +18 -3
- data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +10 -7
- data/lib/active_record/relation/predicate_builder/relation_handler.rb +5 -1
- data/lib/active_record/relation/predicate_builder.rb +28 -16
- data/lib/active_record/relation/query_attribute.rb +2 -1
- data/lib/active_record/relation/query_methods.rb +576 -107
- data/lib/active_record/relation/record_fetch_warning.rb +3 -0
- data/lib/active_record/relation/spawn_methods.rb +5 -4
- data/lib/active_record/relation/where_clause.rb +7 -19
- data/lib/active_record/relation.rb +580 -90
- data/lib/active_record/result.rb +49 -48
- data/lib/active_record/runtime_registry.rb +63 -1
- data/lib/active_record/sanitization.rb +70 -25
- data/lib/active_record/schema.rb +8 -7
- data/lib/active_record/schema_dumper.rb +63 -14
- data/lib/active_record/schema_migration.rb +75 -24
- data/lib/active_record/scoping/default.rb +15 -5
- data/lib/active_record/scoping/named.rb +3 -2
- data/lib/active_record/scoping.rb +2 -1
- data/lib/active_record/secure_password.rb +60 -0
- data/lib/active_record/secure_token.rb +21 -3
- data/lib/active_record/signed_id.rb +27 -6
- data/lib/active_record/statement_cache.rb +7 -7
- data/lib/active_record/store.rb +8 -8
- data/lib/active_record/suppressor.rb +3 -1
- data/lib/active_record/table_metadata.rb +1 -1
- data/lib/active_record/tasks/database_tasks.rb +190 -118
- data/lib/active_record/tasks/mysql_database_tasks.rb +15 -6
- data/lib/active_record/tasks/postgresql_database_tasks.rb +16 -13
- data/lib/active_record/tasks/sqlite_database_tasks.rb +16 -7
- data/lib/active_record/test_fixtures.rb +170 -155
- data/lib/active_record/testing/query_assertions.rb +121 -0
- data/lib/active_record/timestamp.rb +31 -17
- data/lib/active_record/token_for.rb +123 -0
- data/lib/active_record/touch_later.rb +12 -7
- data/lib/active_record/transaction.rb +132 -0
- data/lib/active_record/transactions.rb +106 -24
- data/lib/active_record/translation.rb +0 -2
- data/lib/active_record/type/adapter_specific_registry.rb +1 -8
- data/lib/active_record/type/internal/timezone.rb +7 -2
- data/lib/active_record/type/serialized.rb +1 -3
- data/lib/active_record/type/time.rb +4 -0
- data/lib/active_record/type_caster/connection.rb +4 -4
- data/lib/active_record/validations/absence.rb +1 -1
- data/lib/active_record/validations/associated.rb +9 -3
- data/lib/active_record/validations/numericality.rb +5 -4
- data/lib/active_record/validations/presence.rb +5 -28
- data/lib/active_record/validations/uniqueness.rb +61 -11
- data/lib/active_record/validations.rb +12 -5
- data/lib/active_record/version.rb +1 -1
- data/lib/active_record.rb +247 -33
- data/lib/arel/alias_predication.rb +1 -1
- data/lib/arel/collectors/bind.rb +2 -0
- data/lib/arel/collectors/composite.rb +7 -0
- data/lib/arel/collectors/sql_string.rb +1 -1
- data/lib/arel/collectors/substitute_binds.rb +1 -1
- data/lib/arel/errors.rb +10 -0
- data/lib/arel/factory_methods.rb +4 -0
- data/lib/arel/nodes/binary.rb +6 -7
- data/lib/arel/nodes/bound_sql_literal.rb +65 -0
- data/lib/arel/nodes/cte.rb +36 -0
- data/lib/arel/nodes/fragments.rb +35 -0
- data/lib/arel/nodes/homogeneous_in.rb +1 -9
- data/lib/arel/nodes/leading_join.rb +8 -0
- data/lib/arel/nodes/{and.rb → nary.rb} +5 -2
- data/lib/arel/nodes/node.rb +115 -5
- data/lib/arel/nodes/sql_literal.rb +13 -0
- data/lib/arel/nodes/table_alias.rb +4 -0
- data/lib/arel/nodes.rb +6 -2
- data/lib/arel/predications.rb +3 -1
- data/lib/arel/select_manager.rb +1 -1
- data/lib/arel/table.rb +9 -5
- data/lib/arel/tree_manager.rb +8 -3
- data/lib/arel/update_manager.rb +2 -1
- data/lib/arel/visitors/dot.rb +1 -0
- data/lib/arel/visitors/mysql.rb +17 -5
- data/lib/arel/visitors/postgresql.rb +1 -12
- data/lib/arel/visitors/sqlite.rb +25 -0
- data/lib/arel/visitors/to_sql.rb +112 -34
- data/lib/arel/visitors/visitor.rb +2 -2
- data/lib/arel.rb +21 -3
- data/lib/rails/generators/active_record/application_record/USAGE +8 -0
- data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb.tt +4 -1
- data/lib/rails/generators/active_record/migration.rb +3 -1
- data/lib/rails/generators/active_record/model/USAGE +113 -0
- data/lib/rails/generators/active_record/model/model_generator.rb +15 -6
- metadata +54 -12
- data/lib/active_record/connection_adapters/legacy_pool_manager.rb +0 -35
- data/lib/active_record/null_relation.rb +0 -63
data/lib/arel/visitors/to_sql.rb
CHANGED
@@ -20,6 +20,7 @@ module Arel # :nodoc: all
|
|
20
20
|
|
21
21
|
private
|
22
22
|
def visit_Arel_Nodes_DeleteStatement(o, collector)
|
23
|
+
collector.retryable = false
|
23
24
|
o = prepare_delete_statement(o)
|
24
25
|
|
25
26
|
if has_join_sources?(o)
|
@@ -37,6 +38,7 @@ module Arel # :nodoc: all
|
|
37
38
|
end
|
38
39
|
|
39
40
|
def visit_Arel_Nodes_UpdateStatement(o, collector)
|
41
|
+
collector.retryable = false
|
40
42
|
o = prepare_update_statement(o)
|
41
43
|
|
42
44
|
collector << "UPDATE "
|
@@ -49,6 +51,7 @@ module Arel # :nodoc: all
|
|
49
51
|
end
|
50
52
|
|
51
53
|
def visit_Arel_Nodes_InsertStatement(o, collector)
|
54
|
+
collector.retryable = false
|
52
55
|
collector << "INSERT INTO "
|
53
56
|
collector = visit o.relation, collector
|
54
57
|
|
@@ -333,7 +336,7 @@ module Arel # :nodoc: all
|
|
333
336
|
def visit_Arel_Nodes_HomogeneousIn(o, collector)
|
334
337
|
collector.preparable = false
|
335
338
|
|
336
|
-
|
339
|
+
visit o.left, collector
|
337
340
|
|
338
341
|
if o.type == :in
|
339
342
|
collector << " IN ("
|
@@ -350,7 +353,6 @@ module Arel # :nodoc: all
|
|
350
353
|
end
|
351
354
|
|
352
355
|
collector << ")"
|
353
|
-
collector
|
354
356
|
end
|
355
357
|
|
356
358
|
def visit_Arel_SelectManager(o, collector)
|
@@ -382,6 +384,7 @@ module Arel # :nodoc: all
|
|
382
384
|
end
|
383
385
|
|
384
386
|
def visit_Arel_Nodes_NamedFunction(o, collector)
|
387
|
+
collector.retryable = false
|
385
388
|
collector << o.name
|
386
389
|
collector << "("
|
387
390
|
collector << "DISTINCT " if o.distinct
|
@@ -569,18 +572,25 @@ module Arel # :nodoc: all
|
|
569
572
|
end
|
570
573
|
|
571
574
|
def visit_Arel_Table(o, collector)
|
572
|
-
if o.
|
573
|
-
|
575
|
+
if Arel::Nodes::Node === o.name
|
576
|
+
visit o.name, collector
|
574
577
|
else
|
575
578
|
collector << quote_table_name(o.name)
|
576
579
|
end
|
580
|
+
|
581
|
+
if o.table_alias
|
582
|
+
collector << " " << quote_table_name(o.table_alias)
|
583
|
+
end
|
584
|
+
|
585
|
+
collector
|
577
586
|
end
|
578
587
|
|
579
588
|
def visit_Arel_Nodes_In(o, collector)
|
580
|
-
collector.preparable = false
|
581
589
|
attr, values = o.left, o.right
|
582
590
|
|
583
591
|
if Array === values
|
592
|
+
collector.preparable = false
|
593
|
+
|
584
594
|
unless values.empty?
|
585
595
|
values.delete_if { |value| unboundable?(value) }
|
586
596
|
end
|
@@ -593,10 +603,11 @@ module Arel # :nodoc: all
|
|
593
603
|
end
|
594
604
|
|
595
605
|
def visit_Arel_Nodes_NotIn(o, collector)
|
596
|
-
collector.preparable = false
|
597
606
|
attr, values = o.left, o.right
|
598
607
|
|
599
608
|
if Array === values
|
609
|
+
collector.preparable = false
|
610
|
+
|
600
611
|
unless values.empty?
|
601
612
|
values.delete_if { |value| unboundable?(value) }
|
602
613
|
end
|
@@ -613,18 +624,7 @@ module Arel # :nodoc: all
|
|
613
624
|
end
|
614
625
|
|
615
626
|
def visit_Arel_Nodes_Or(o, collector)
|
616
|
-
|
617
|
-
|
618
|
-
while o = stack.pop
|
619
|
-
if o.is_a?(Arel::Nodes::Or)
|
620
|
-
stack.push o.right, o.left
|
621
|
-
else
|
622
|
-
visit o, collector
|
623
|
-
collector << " OR " unless stack.empty?
|
624
|
-
end
|
625
|
-
end
|
626
|
-
|
627
|
-
collector
|
627
|
+
inject_join o.children, collector, " OR "
|
628
628
|
end
|
629
629
|
|
630
630
|
def visit_Arel_Nodes_Assignment(o, collector)
|
@@ -729,6 +729,20 @@ module Arel # :nodoc: all
|
|
729
729
|
collector << quote_column_name(o.name)
|
730
730
|
end
|
731
731
|
|
732
|
+
def visit_Arel_Nodes_Cte(o, collector)
|
733
|
+
collector << quote_table_name(o.name)
|
734
|
+
collector << " AS "
|
735
|
+
|
736
|
+
case o.materialized
|
737
|
+
when true
|
738
|
+
collector << "MATERIALIZED "
|
739
|
+
when false
|
740
|
+
collector << "NOT MATERIALIZED "
|
741
|
+
end
|
742
|
+
|
743
|
+
visit o.relation, collector
|
744
|
+
end
|
745
|
+
|
732
746
|
def visit_Arel_Attributes_Attribute(o, collector)
|
733
747
|
join_name = o.relation.table_alias || o.relation.name
|
734
748
|
collector << quote_table_name(join_name) << "." << quote_column_name(o.name)
|
@@ -749,9 +763,64 @@ module Arel # :nodoc: all
|
|
749
763
|
|
750
764
|
def visit_Arel_Nodes_SqlLiteral(o, collector)
|
751
765
|
collector.preparable = false
|
766
|
+
collector.retryable = o.retryable
|
752
767
|
collector << o.to_s
|
753
768
|
end
|
754
769
|
|
770
|
+
def visit_Arel_Nodes_BoundSqlLiteral(o, collector)
|
771
|
+
collector.retryable = false
|
772
|
+
bind_index = 0
|
773
|
+
|
774
|
+
new_bind = lambda do |value|
|
775
|
+
if Arel.arel_node?(value)
|
776
|
+
visit value, collector
|
777
|
+
elsif value.is_a?(Array)
|
778
|
+
if value.empty?
|
779
|
+
collector << @connection.quote(nil)
|
780
|
+
else
|
781
|
+
if value.none? { |v| Arel.arel_node?(v) }
|
782
|
+
collector.add_binds(value.map { |v| @connection.cast_bound_value(v) }, &bind_block)
|
783
|
+
else
|
784
|
+
value.each_with_index do |v, i|
|
785
|
+
collector << ", " unless i == 0
|
786
|
+
if Arel.arel_node?(v)
|
787
|
+
visit v, collector
|
788
|
+
else
|
789
|
+
collector.add_bind(@connection.cast_bound_value(v), &bind_block)
|
790
|
+
end
|
791
|
+
end
|
792
|
+
end
|
793
|
+
end
|
794
|
+
else
|
795
|
+
collector.add_bind(@connection.cast_bound_value(value), &bind_block)
|
796
|
+
end
|
797
|
+
end
|
798
|
+
|
799
|
+
if o.positional_binds
|
800
|
+
o.sql_with_placeholders.scan(/\?|([^?]+)/) do
|
801
|
+
if $1
|
802
|
+
collector << $1
|
803
|
+
else
|
804
|
+
value = o.positional_binds[bind_index]
|
805
|
+
bind_index += 1
|
806
|
+
|
807
|
+
new_bind.call(value)
|
808
|
+
end
|
809
|
+
end
|
810
|
+
else
|
811
|
+
o.sql_with_placeholders.scan(/:(?<!::)([a-zA-Z]\w*)|([^:]+|.)/) do
|
812
|
+
if $2
|
813
|
+
collector << $2
|
814
|
+
else
|
815
|
+
value = o.named_binds[$1.to_sym]
|
816
|
+
new_bind.call(value)
|
817
|
+
end
|
818
|
+
end
|
819
|
+
end
|
820
|
+
|
821
|
+
collector
|
822
|
+
end
|
823
|
+
|
755
824
|
def visit_Integer(o, collector)
|
756
825
|
collector << o.to_s
|
757
826
|
end
|
@@ -791,6 +860,10 @@ module Arel # :nodoc: all
|
|
791
860
|
end
|
792
861
|
alias :visit_Set :visit_Array
|
793
862
|
|
863
|
+
def visit_Arel_Nodes_Fragments(o, collector)
|
864
|
+
inject_join o.values, collector, " "
|
865
|
+
end
|
866
|
+
|
794
867
|
def quote(value)
|
795
868
|
return value if Arel::Nodes::SqlLiteral === value
|
796
869
|
@connection.quote value
|
@@ -854,7 +927,8 @@ module Arel # :nodoc: all
|
|
854
927
|
stmt.limit = nil
|
855
928
|
stmt.offset = nil
|
856
929
|
stmt.orders = []
|
857
|
-
|
930
|
+
columns = Arel::Nodes::Grouping.new(o.key)
|
931
|
+
stmt.wheres = [Nodes::In.new(columns, [build_subselect(o.key, o)])]
|
858
932
|
stmt.relation = o.relation.left if has_join_sources?(o)
|
859
933
|
stmt.groups = o.groups unless o.groups.empty?
|
860
934
|
stmt.havings = o.havings unless o.havings.empty?
|
@@ -891,18 +965,34 @@ module Arel # :nodoc: all
|
|
891
965
|
collector = if o.left.class == o.class
|
892
966
|
infix_value_with_paren(o.left, collector, value, true)
|
893
967
|
else
|
894
|
-
|
968
|
+
grouping_parentheses o.left, collector, false
|
895
969
|
end
|
896
970
|
collector << value
|
897
971
|
collector = if o.right.class == o.class
|
898
972
|
infix_value_with_paren(o.right, collector, value, true)
|
899
973
|
else
|
900
|
-
|
974
|
+
grouping_parentheses o.right, collector, false
|
901
975
|
end
|
902
976
|
collector << " )" unless suppress_parens
|
903
977
|
collector
|
904
978
|
end
|
905
979
|
|
980
|
+
# Used by some visitors to enclose select queries in parentheses
|
981
|
+
def grouping_parentheses(o, collector, always_wrap_selects = true)
|
982
|
+
if o.is_a?(Nodes::SelectStatement) && (always_wrap_selects || require_parentheses?(o))
|
983
|
+
collector << "("
|
984
|
+
visit o, collector
|
985
|
+
collector << ")"
|
986
|
+
collector
|
987
|
+
else
|
988
|
+
visit o, collector
|
989
|
+
end
|
990
|
+
end
|
991
|
+
|
992
|
+
def require_parentheses?(o)
|
993
|
+
!o.orders.empty? || o.limit || o.offset
|
994
|
+
end
|
995
|
+
|
906
996
|
def aggregate(name, o, collector)
|
907
997
|
collector << "#{name}("
|
908
998
|
if o.distinct
|
@@ -933,19 +1023,7 @@ module Arel # :nodoc: all
|
|
933
1023
|
def collect_ctes(children, collector)
|
934
1024
|
children.each_with_index do |child, i|
|
935
1025
|
collector << ", " unless i == 0
|
936
|
-
|
937
|
-
case child
|
938
|
-
when Arel::Nodes::As
|
939
|
-
name = child.left.name
|
940
|
-
relation = child.right
|
941
|
-
when Arel::Nodes::TableAlias
|
942
|
-
name = child.name
|
943
|
-
relation = child.relation
|
944
|
-
end
|
945
|
-
|
946
|
-
collector << quote_table_name(name)
|
947
|
-
collector << " AS "
|
948
|
-
visit relation, collector
|
1026
|
+
visit child.to_cte, collector
|
949
1027
|
end
|
950
1028
|
|
951
1029
|
collector
|
@@ -16,8 +16,8 @@ module Arel # :nodoc: all
|
|
16
16
|
|
17
17
|
def self.dispatch_cache
|
18
18
|
@dispatch_cache ||= Hash.new do |hash, klass|
|
19
|
-
hash[klass] = "visit_#{(klass.name ||
|
20
|
-
end
|
19
|
+
hash[klass] = :"visit_#{(klass.name || "").gsub("::", "_")}"
|
20
|
+
end.compare_by_identity
|
21
21
|
end
|
22
22
|
|
23
23
|
def get_dispatch_cache
|
data/lib/arel.rb
CHANGED
@@ -35,12 +35,30 @@ module Arel
|
|
35
35
|
# Great caution should be taken to avoid SQL injection vulnerabilities.
|
36
36
|
# This method should not be used with unsafe values such as request
|
37
37
|
# parameters or model attributes.
|
38
|
-
|
39
|
-
|
38
|
+
#
|
39
|
+
# Take a look at the {security guide}[https://guides.rubyonrails.org/security.html#sql-injection]
|
40
|
+
# for more information.
|
41
|
+
#
|
42
|
+
# To construct a more complex query fragment, including the possible
|
43
|
+
# use of user-provided values, the +sql_string+ may contain <tt>?</tt> and
|
44
|
+
# +:key+ placeholders, corresponding to the additional arguments. Note
|
45
|
+
# that this behavior only applies when bind value parameters are
|
46
|
+
# supplied in the call; without them, the placeholder tokens have no
|
47
|
+
# special meaning, and will be passed through to the query as-is.
|
48
|
+
#
|
49
|
+
# The +:retryable+ option can be used to mark the SQL as safe to retry.
|
50
|
+
# Use this option only if the SQL is idempotent, as it could be executed
|
51
|
+
# more than once.
|
52
|
+
def self.sql(sql_string, *positional_binds, retryable: false, **named_binds)
|
53
|
+
if positional_binds.empty? && named_binds.empty?
|
54
|
+
Arel::Nodes::SqlLiteral.new(sql_string, retryable: retryable)
|
55
|
+
else
|
56
|
+
Arel::Nodes::BoundSqlLiteral.new sql_string, positional_binds, named_binds
|
57
|
+
end
|
40
58
|
end
|
41
59
|
|
42
60
|
def self.star # :nodoc:
|
43
|
-
sql
|
61
|
+
sql("*", retryable: true)
|
44
62
|
end
|
45
63
|
|
46
64
|
def self.arel_node?(value) # :nodoc:
|
@@ -0,0 +1,8 @@
|
|
1
|
+
Description:
|
2
|
+
Generates an `ApplicationRecord` base class for other models to inherit from.
|
3
|
+
|
4
|
+
Example:
|
5
|
+
`bin/rails generate application_record`
|
6
|
+
|
7
|
+
This generates the base class. A test is not generated because no
|
8
|
+
behaviour is included in `ApplicationRecord` by default.
|
@@ -12,7 +12,10 @@ class <%= migration_class_name %> < ActiveRecord::Migration[<%= ActiveRecord::Mi
|
|
12
12
|
t.<%= attribute.type %> :<%= attribute.name %><%= attribute.inject_options %>
|
13
13
|
<% end -%>
|
14
14
|
<% end -%>
|
15
|
-
<%
|
15
|
+
<% unless attributes.empty? -%>
|
16
|
+
|
17
|
+
<% end -%>
|
18
|
+
<% if options[:timestamps] -%>
|
16
19
|
t.timestamps
|
17
20
|
<% end -%>
|
18
21
|
end
|
@@ -41,11 +41,13 @@ module ActiveRecord
|
|
41
41
|
|
42
42
|
def configured_migrate_path
|
43
43
|
return unless database = options[:database]
|
44
|
+
|
44
45
|
config = ActiveRecord::Base.configurations.configs_for(
|
45
46
|
env_name: Rails.env,
|
46
47
|
name: database
|
47
48
|
)
|
48
|
-
|
49
|
+
|
50
|
+
Array(config&.migrations_paths).first
|
49
51
|
end
|
50
52
|
end
|
51
53
|
end
|
@@ -0,0 +1,113 @@
|
|
1
|
+
Description:
|
2
|
+
Generates a new model. Pass the model name, either CamelCased or
|
3
|
+
under_scored, and an optional list of attribute pairs as arguments.
|
4
|
+
|
5
|
+
Attribute pairs are field:type arguments specifying the
|
6
|
+
model's attributes. Timestamps are added by default, so you don't have to
|
7
|
+
specify them by hand as 'created_at:datetime updated_at:datetime'.
|
8
|
+
|
9
|
+
As a special case, specifying 'password:digest' will generate a
|
10
|
+
password_digest field of string type, and configure your generated model and
|
11
|
+
tests for use with Active Model has_secure_password (assuming the default ORM
|
12
|
+
and test framework are being used).
|
13
|
+
|
14
|
+
You don't have to think up every attribute up front, but it helps to
|
15
|
+
sketch out a few so you can start working with the model immediately.
|
16
|
+
|
17
|
+
This generator invokes your configured ORM and test framework, which
|
18
|
+
defaults to Active Record and TestUnit.
|
19
|
+
|
20
|
+
Finally, if --parent option is given, it's used as superclass of the
|
21
|
+
created model. This allows you create Single Table Inheritance models.
|
22
|
+
|
23
|
+
If you pass a namespaced model name (e.g. admin/account or Admin::Account)
|
24
|
+
then the generator will create a module with a table_name_prefix method
|
25
|
+
to prefix the model's table name with the module name (e.g. admin_accounts)
|
26
|
+
|
27
|
+
Available field types:
|
28
|
+
|
29
|
+
Just after the field name you can specify a type like text or boolean.
|
30
|
+
It will generate the column with the associated SQL type. For instance:
|
31
|
+
|
32
|
+
`bin/rails generate model post title:string body:text`
|
33
|
+
|
34
|
+
will generate a title column with a varchar type and a body column with a text
|
35
|
+
type. If no type is specified the string type will be used by default.
|
36
|
+
You can use the following types:
|
37
|
+
|
38
|
+
integer
|
39
|
+
primary_key
|
40
|
+
decimal
|
41
|
+
float
|
42
|
+
boolean
|
43
|
+
binary
|
44
|
+
string
|
45
|
+
text
|
46
|
+
date
|
47
|
+
time
|
48
|
+
datetime
|
49
|
+
|
50
|
+
You can also consider `references` as a kind of type. For instance, if you run:
|
51
|
+
|
52
|
+
`bin/rails generate model photo title:string album:references`
|
53
|
+
|
54
|
+
It will generate an `album_id` column. You should generate these kinds of fields when
|
55
|
+
you will use a `belongs_to` association, for instance. `references` also supports
|
56
|
+
polymorphism, you can enable polymorphism like this:
|
57
|
+
|
58
|
+
`bin/rails generate model product supplier:references{polymorphic}`
|
59
|
+
|
60
|
+
For integer, string, text and binary fields, an integer in curly braces will
|
61
|
+
be set as the limit:
|
62
|
+
|
63
|
+
`bin/rails generate model user pseudo:string{30}`
|
64
|
+
|
65
|
+
For decimal, two integers separated by a comma in curly braces will be used
|
66
|
+
for precision and scale:
|
67
|
+
|
68
|
+
`bin/rails generate model product 'price:decimal{10,2}'`
|
69
|
+
|
70
|
+
You can add a `:uniq` or `:index` suffix for unique or standard indexes
|
71
|
+
respectively:
|
72
|
+
|
73
|
+
`bin/rails generate model user pseudo:string:uniq`
|
74
|
+
`bin/rails generate model user pseudo:string:index`
|
75
|
+
|
76
|
+
You can combine any single curly brace option with the index options:
|
77
|
+
|
78
|
+
`bin/rails generate model user username:string{30}:uniq`
|
79
|
+
`bin/rails generate model product supplier:references{polymorphic}:index`
|
80
|
+
|
81
|
+
If you require a `password_digest` string column for use with
|
82
|
+
has_secure_password, you can specify `password:digest`:
|
83
|
+
|
84
|
+
`bin/rails generate model user password:digest`
|
85
|
+
|
86
|
+
If you require a `token` string column for use with
|
87
|
+
has_secure_token, you can specify `auth_token:token`:
|
88
|
+
|
89
|
+
`bin/rails generate model user auth_token:token`
|
90
|
+
|
91
|
+
Examples:
|
92
|
+
`bin/rails generate model account`
|
93
|
+
|
94
|
+
For Active Record and TestUnit it creates:
|
95
|
+
|
96
|
+
Model: app/models/account.rb
|
97
|
+
Test: test/models/account_test.rb
|
98
|
+
Fixtures: test/fixtures/accounts.yml
|
99
|
+
Migration: db/migrate/XXX_create_accounts.rb
|
100
|
+
|
101
|
+
`bin/rails generate model post title:string body:text published:boolean`
|
102
|
+
|
103
|
+
Creates a Post model with a string title, text body, and published flag.
|
104
|
+
|
105
|
+
`bin/rails generate model admin/account`
|
106
|
+
|
107
|
+
For Active Record and TestUnit it creates:
|
108
|
+
|
109
|
+
Module: app/models/admin.rb
|
110
|
+
Model: app/models/admin/account.rb
|
111
|
+
Test: test/models/admin/account_test.rb
|
112
|
+
Fixtures: test/fixtures/admin/accounts.yml
|
113
|
+
Migration: db/migrate/XXX_create_admin_accounts.rb
|
@@ -11,20 +11,25 @@ module ActiveRecord
|
|
11
11
|
|
12
12
|
class_option :migration, type: :boolean
|
13
13
|
class_option :timestamps, type: :boolean
|
14
|
-
class_option :parent, type: :string, desc: "The parent class for the generated model"
|
14
|
+
class_option :parent, type: :string, default: "ApplicationRecord", desc: "The parent class for the generated model"
|
15
15
|
class_option :indexes, type: :boolean, default: true, desc: "Add indexes for references and belongs_to columns"
|
16
16
|
class_option :primary_key_type, type: :string, desc: "The type for primary key"
|
17
17
|
class_option :database, type: :string, aliases: %i(--db), desc: "The database for your model's migration. By default, the current environment's primary database is used."
|
18
18
|
|
19
|
+
Rails::Generators.templates_path.each do |path|
|
20
|
+
source_paths << File.join(path, base_name, "migration")
|
21
|
+
end
|
22
|
+
source_paths << File.expand_path(File.join(base_name, "migration", "templates"), base_root)
|
23
|
+
|
19
24
|
# creates the migration file for the model.
|
20
25
|
def create_migration_file
|
21
26
|
return if skip_migration_creation?
|
22
27
|
attributes.each { |a| a.attr_options.delete(:index) if a.reference? && !a.has_index? } if options[:indexes] == false
|
23
|
-
migration_template "
|
28
|
+
migration_template "create_table_migration.rb", File.join(db_migrate_path, "create_#{table_name}.rb")
|
24
29
|
end
|
25
30
|
|
26
31
|
def create_model_file
|
27
|
-
generate_abstract_class if database && !
|
32
|
+
generate_abstract_class if database && !custom_parent?
|
28
33
|
template "model.rb", File.join("app/models", class_path, "#{file_name}.rb")
|
29
34
|
end
|
30
35
|
|
@@ -40,7 +45,7 @@ module ActiveRecord
|
|
40
45
|
# - options parent is present and database option is not present
|
41
46
|
# - migrations option is nil or false
|
42
47
|
def skip_migration_creation?
|
43
|
-
|
48
|
+
custom_parent? && !database || !migration
|
44
49
|
end
|
45
50
|
|
46
51
|
def attributes_with_index
|
@@ -49,12 +54,12 @@ module ActiveRecord
|
|
49
54
|
|
50
55
|
# Used by the migration template to determine the parent name of the model
|
51
56
|
def parent_class_name
|
52
|
-
if
|
57
|
+
if custom_parent?
|
53
58
|
parent
|
54
59
|
elsif database
|
55
60
|
abstract_class_name
|
56
61
|
else
|
57
|
-
|
62
|
+
parent
|
58
63
|
end
|
59
64
|
end
|
60
65
|
|
@@ -77,6 +82,10 @@ module ActiveRecord
|
|
77
82
|
options[:parent]
|
78
83
|
end
|
79
84
|
|
85
|
+
def custom_parent?
|
86
|
+
parent != self.class.class_options[:parent].default
|
87
|
+
end
|
88
|
+
|
80
89
|
def migration
|
81
90
|
options[:migration]
|
82
91
|
end
|