activerecord 5.1.7 → 5.2.0.beta1
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 +221 -900
- data/README.rdoc +3 -3
- data/examples/performance.rb +2 -0
- data/examples/simple.rb +2 -0
- data/lib/active_record.rb +10 -3
- data/lib/active_record/aggregations.rb +2 -0
- data/lib/active_record/association_relation.rb +2 -0
- data/lib/active_record/associations.rb +13 -42
- data/lib/active_record/associations/alias_tracker.rb +17 -17
- data/lib/active_record/associations/association.rb +11 -22
- data/lib/active_record/associations/association_scope.rb +32 -44
- data/lib/active_record/associations/belongs_to_association.rb +6 -4
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +3 -1
- data/lib/active_record/associations/builder/association.rb +2 -5
- data/lib/active_record/associations/builder/belongs_to.rb +7 -12
- data/lib/active_record/associations/builder/collection_association.rb +1 -1
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +3 -1
- data/lib/active_record/associations/builder/has_many.rb +2 -0
- data/lib/active_record/associations/builder/has_one.rb +2 -0
- data/lib/active_record/associations/builder/singular_association.rb +2 -0
- data/lib/active_record/associations/collection_association.rb +41 -33
- data/lib/active_record/associations/collection_proxy.rb +11 -14
- data/lib/active_record/associations/foreign_association.rb +2 -0
- data/lib/active_record/associations/has_many_association.rb +4 -2
- data/lib/active_record/associations/has_many_through_association.rb +4 -2
- data/lib/active_record/associations/has_one_association.rb +3 -1
- data/lib/active_record/associations/has_one_through_association.rb +3 -1
- data/lib/active_record/associations/join_dependency.rb +22 -40
- data/lib/active_record/associations/join_dependency/join_association.rb +17 -56
- data/lib/active_record/associations/join_dependency/join_base.rb +9 -8
- data/lib/active_record/associations/join_dependency/join_part.rb +2 -9
- data/lib/active_record/associations/preloader.rb +17 -37
- data/lib/active_record/associations/preloader/association.rb +42 -58
- data/lib/active_record/associations/preloader/through_association.rb +71 -79
- data/lib/active_record/associations/singular_association.rb +14 -10
- data/lib/active_record/associations/through_association.rb +3 -1
- data/lib/active_record/attribute_assignment.rb +2 -0
- data/lib/active_record/attribute_decorators.rb +3 -2
- data/lib/active_record/attribute_methods.rb +47 -7
- data/lib/active_record/attribute_methods/before_type_cast.rb +2 -0
- data/lib/active_record/attribute_methods/dirty.rb +25 -214
- data/lib/active_record/attribute_methods/primary_key.rb +7 -6
- data/lib/active_record/attribute_methods/query.rb +2 -0
- data/lib/active_record/attribute_methods/read.rb +8 -2
- data/lib/active_record/attribute_methods/serialization.rb +23 -0
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +6 -8
- data/lib/active_record/attribute_methods/write.rb +21 -9
- data/lib/active_record/attributes.rb +7 -6
- data/lib/active_record/autosave_association.rb +5 -11
- data/lib/active_record/base.rb +2 -0
- data/lib/active_record/callbacks.rb +6 -8
- data/lib/active_record/coders/json.rb +2 -0
- data/lib/active_record/coders/yaml_column.rb +2 -0
- data/lib/active_record/collection_cache_key.rb +10 -5
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +110 -35
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +2 -0
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +120 -28
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +7 -2
- data/lib/active_record/connection_adapters/abstract/quoting.rb +14 -33
- data/lib/active_record/connection_adapters/abstract/savepoints.rb +2 -0
- data/lib/active_record/connection_adapters/abstract/schema_creation.rb +13 -5
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +40 -2
- data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +31 -53
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +103 -63
- data/lib/active_record/connection_adapters/abstract/transaction.rb +45 -9
- data/lib/active_record/connection_adapters/abstract_adapter.rb +62 -90
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +75 -138
- data/lib/active_record/connection_adapters/column.rb +3 -1
- data/lib/active_record/connection_adapters/connection_specification.rb +17 -3
- data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +2 -0
- data/lib/active_record/connection_adapters/mysql/column.rb +2 -0
- data/lib/active_record/connection_adapters/mysql/database_statements.rb +3 -1
- data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +2 -0
- data/lib/active_record/connection_adapters/mysql/quoting.rb +9 -10
- data/lib/active_record/connection_adapters/mysql/schema_creation.rb +5 -3
- data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +7 -6
- data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +30 -30
- data/lib/active_record/connection_adapters/mysql/schema_statements.rb +91 -1
- data/lib/active_record/connection_adapters/mysql/type_metadata.rb +2 -0
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +8 -2
- data/lib/active_record/connection_adapters/postgresql/column.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +6 -0
- data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid.rb +2 -1
- data/lib/active_record/connection_adapters/postgresql/oid/array.rb +3 -11
- data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +2 -0
- 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 +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +3 -1
- data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/inet.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +3 -1
- data/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/money.rb +3 -1
- data/lib/active_record/connection_adapters/postgresql/oid/oid.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/point.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/range.rb +3 -5
- data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +4 -2
- data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +3 -1
- 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/quoting.rb +10 -0
- data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +19 -25
- data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +11 -7
- data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +20 -13
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +79 -65
- data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/utils.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +47 -82
- data/lib/active_record/connection_adapters/schema_cache.rb +2 -0
- data/lib/active_record/connection_adapters/sql_type_metadata.rb +2 -0
- data/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb +2 -0
- data/lib/active_record/connection_adapters/sqlite3/quoting.rb +19 -2
- data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +2 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +6 -15
- data/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb +3 -2
- data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +71 -1
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +34 -89
- data/lib/active_record/connection_adapters/statement_pool.rb +2 -0
- data/lib/active_record/connection_handling.rb +4 -2
- data/lib/active_record/core.rb +27 -57
- data/lib/active_record/counter_cache.rb +15 -12
- data/lib/active_record/define_callbacks.rb +5 -3
- data/lib/active_record/dynamic_matchers.rb +9 -9
- data/lib/active_record/enum.rb +15 -13
- data/lib/active_record/errors.rb +54 -21
- data/lib/active_record/explain.rb +3 -1
- data/lib/active_record/explain_registry.rb +2 -0
- data/lib/active_record/explain_subscriber.rb +2 -0
- data/lib/active_record/fixture_set/file.rb +2 -0
- data/lib/active_record/fixtures.rb +40 -24
- data/lib/active_record/gem_version.rb +5 -3
- data/lib/active_record/inheritance.rb +6 -5
- data/lib/active_record/integration.rb +58 -19
- data/lib/active_record/internal_metadata.rb +2 -0
- data/lib/active_record/legacy_yaml_adapter.rb +3 -1
- data/lib/active_record/locking/optimistic.rb +31 -20
- data/lib/active_record/locking/pessimistic.rb +10 -7
- data/lib/active_record/log_subscriber.rb +2 -0
- data/lib/active_record/migration.rb +47 -21
- data/lib/active_record/migration/command_recorder.rb +11 -9
- data/lib/active_record/migration/compatibility.rb +20 -2
- data/lib/active_record/migration/join_table.rb +2 -0
- data/lib/active_record/model_schema.rb +29 -38
- data/lib/active_record/nested_attributes.rb +18 -6
- data/lib/active_record/no_touching.rb +3 -1
- data/lib/active_record/null_relation.rb +2 -0
- data/lib/active_record/persistence.rb +184 -40
- data/lib/active_record/query_cache.rb +17 -12
- data/lib/active_record/querying.rb +3 -1
- data/lib/active_record/railtie.rb +54 -1
- data/lib/active_record/railties/console_sandbox.rb +2 -0
- data/lib/active_record/railties/controller_runtime.rb +2 -0
- data/lib/active_record/railties/databases.rake +41 -28
- data/lib/active_record/readonly_attributes.rb +3 -2
- data/lib/active_record/reflection.rb +100 -182
- data/lib/active_record/relation.rb +61 -193
- data/lib/active_record/relation/batches.rb +20 -5
- data/lib/active_record/relation/batches/batch_enumerator.rb +2 -0
- data/lib/active_record/relation/calculations.rb +40 -23
- data/lib/active_record/relation/delegation.rb +10 -27
- data/lib/active_record/relation/finder_methods.rb +53 -49
- data/lib/active_record/relation/from_clause.rb +2 -8
- data/lib/active_record/relation/merger.rb +22 -19
- data/lib/active_record/relation/predicate_builder.rb +42 -79
- data/lib/active_record/relation/predicate_builder/array_handler.rb +10 -7
- data/lib/active_record/relation/predicate_builder/association_query_value.rb +46 -0
- data/lib/active_record/relation/predicate_builder/base_handler.rb +2 -2
- data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +12 -1
- data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +54 -0
- data/lib/active_record/relation/predicate_builder/range_handler.rb +22 -6
- data/lib/active_record/relation/predicate_builder/relation_handler.rb +6 -0
- data/lib/active_record/relation/query_attribute.rb +9 -2
- data/lib/active_record/relation/query_methods.rb +80 -69
- data/lib/active_record/relation/record_fetch_warning.rb +2 -0
- data/lib/active_record/relation/spawn_methods.rb +2 -0
- data/lib/active_record/relation/where_clause.rb +50 -67
- data/lib/active_record/relation/where_clause_factory.rb +4 -46
- data/lib/active_record/result.rb +2 -0
- data/lib/active_record/runtime_registry.rb +2 -0
- data/lib/active_record/sanitization.rb +15 -9
- data/lib/active_record/schema.rb +3 -1
- data/lib/active_record/schema_dumper.rb +24 -23
- data/lib/active_record/schema_migration.rb +2 -0
- data/lib/active_record/scoping.rb +9 -8
- data/lib/active_record/scoping/default.rb +6 -7
- data/lib/active_record/scoping/named.rb +15 -7
- data/lib/active_record/secure_token.rb +2 -0
- data/lib/active_record/serialization.rb +2 -0
- data/lib/active_record/statement_cache.rb +22 -12
- data/lib/active_record/store.rb +2 -0
- data/lib/active_record/suppressor.rb +2 -0
- data/lib/active_record/table_metadata.rb +3 -1
- data/lib/active_record/tasks/database_tasks.rb +23 -12
- data/lib/active_record/tasks/mysql_database_tasks.rb +9 -48
- data/lib/active_record/tasks/postgresql_database_tasks.rb +10 -2
- data/lib/active_record/tasks/sqlite_database_tasks.rb +25 -3
- data/lib/active_record/timestamp.rb +5 -12
- data/lib/active_record/touch_later.rb +2 -0
- data/lib/active_record/transactions.rb +9 -7
- data/lib/active_record/translation.rb +2 -0
- data/lib/active_record/type.rb +4 -1
- data/lib/active_record/type/adapter_specific_registry.rb +2 -0
- 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 +2 -0
- data/lib/active_record/type/hash_lookup_type_map.rb +2 -0
- 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 +2 -4
- data/lib/active_record/type/text.rb +2 -0
- data/lib/active_record/type/time.rb +2 -0
- data/lib/active_record/type/type_map.rb +2 -0
- data/lib/active_record/type/unsigned_integer.rb +2 -0
- data/lib/active_record/type_caster.rb +2 -0
- data/lib/active_record/type_caster/connection.rb +2 -0
- data/lib/active_record/type_caster/map.rb +2 -0
- data/lib/active_record/validations.rb +2 -0
- data/lib/active_record/validations/absence.rb +2 -0
- data/lib/active_record/validations/associated.rb +2 -0
- data/lib/active_record/validations/length.rb +2 -0
- data/lib/active_record/validations/presence.rb +2 -0
- data/lib/active_record/validations/uniqueness.rb +36 -6
- data/lib/active_record/version.rb +2 -0
- data/lib/rails/generators/active_record.rb +3 -1
- 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.rb +2 -0
- data/lib/rails/generators/active_record/migration/migration_generator.rb +3 -1
- data/lib/rails/generators/active_record/migration/templates/{create_table_migration.rb → create_table_migration.rb.tt} +0 -0
- data/lib/rails/generators/active_record/migration/templates/{migration.rb → migration.rb.tt} +0 -0
- data/lib/rails/generators/active_record/model/model_generator.rb +2 -23
- data/lib/rails/generators/active_record/model/templates/{model.rb → model.rb.tt} +0 -0
- data/lib/rails/generators/active_record/model/templates/{module.rb → module.rb.tt} +0 -0
- metadata +25 -38
- data/lib/active_record/associations/preloader/belongs_to.rb +0 -15
- data/lib/active_record/associations/preloader/collection_association.rb +0 -17
- data/lib/active_record/associations/preloader/has_many.rb +0 -15
- 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 -18
- data/lib/active_record/attribute.rb +0 -240
- data/lib/active_record/attribute/user_provided_default.rb +0 -30
- data/lib/active_record/attribute_mutation_tracker.rb +0 -122
- data/lib/active_record/attribute_set.rb +0 -113
- data/lib/active_record/attribute_set/builder.rb +0 -126
- data/lib/active_record/attribute_set/yaml_encoder.rb +0 -41
- data/lib/active_record/connection_adapters/postgresql/oid/json.rb +0 -10
- 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/polymorphic_array_handler.rb +0 -59
- data/lib/active_record/type/internal/abstract_json.rb +0 -37
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "active_record/connection_adapters/abstract_adapter"
|
2
4
|
require "active_record/connection_adapters/statement_pool"
|
3
5
|
require "active_record/connection_adapters/sqlite3/explain_pretty_printer"
|
@@ -7,7 +9,7 @@ require "active_record/connection_adapters/sqlite3/schema_definitions"
|
|
7
9
|
require "active_record/connection_adapters/sqlite3/schema_dumper"
|
8
10
|
require "active_record/connection_adapters/sqlite3/schema_statements"
|
9
11
|
|
10
|
-
gem "sqlite3", "~> 1.3
|
12
|
+
gem "sqlite3", "~> 1.3.6"
|
11
13
|
require "sqlite3"
|
12
14
|
|
13
15
|
module ActiveRecord
|
@@ -55,7 +57,6 @@ module ActiveRecord
|
|
55
57
|
ADAPTER_NAME = "SQLite".freeze
|
56
58
|
|
57
59
|
include SQLite3::Quoting
|
58
|
-
include SQLite3::ColumnDumper
|
59
60
|
include SQLite3::SchemaStatements
|
60
61
|
|
61
62
|
NATIVE_DATABASE_TYPES = {
|
@@ -72,6 +73,23 @@ module ActiveRecord
|
|
72
73
|
boolean: { name: "boolean" }
|
73
74
|
}
|
74
75
|
|
76
|
+
##
|
77
|
+
# :singleton-method:
|
78
|
+
# Indicates whether boolean values are stored in sqlite3 databases as 1
|
79
|
+
# and 0 or 't' and 'f'. Leaving <tt>ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer</tt>
|
80
|
+
# set to false is deprecated. SQLite databases have used 't' and 'f' to
|
81
|
+
# serialize boolean values and must have old data converted to 1 and 0
|
82
|
+
# (its native boolean serialization) before setting this flag to true.
|
83
|
+
# Conversion can be accomplished by setting up a rake task which runs
|
84
|
+
#
|
85
|
+
# ExampleModel.where("boolean_column = 't'").update_all(boolean_column: 1)
|
86
|
+
# ExampleModel.where("boolean_column = 'f'").update_all(boolean_column: 0)
|
87
|
+
# for all models and all boolean columns, after which the flag must be set
|
88
|
+
# to true by adding the following to your <tt>application.rb</tt> file:
|
89
|
+
#
|
90
|
+
# Rails.application.config.active_record.sqlite3.represent_boolean_as_integer = true
|
91
|
+
class_attribute :represent_boolean_as_integer, default: false
|
92
|
+
|
75
93
|
class StatementPool < ConnectionAdapters::StatementPool
|
76
94
|
private
|
77
95
|
|
@@ -80,18 +98,6 @@ module ActiveRecord
|
|
80
98
|
end
|
81
99
|
end
|
82
100
|
|
83
|
-
def update_table_definition(table_name, base) # :nodoc:
|
84
|
-
SQLite3::Table.new(table_name, base)
|
85
|
-
end
|
86
|
-
|
87
|
-
def schema_creation # :nodoc:
|
88
|
-
SQLite3::SchemaCreation.new self
|
89
|
-
end
|
90
|
-
|
91
|
-
def arel_visitor # :nodoc:
|
92
|
-
Arel::Visitors::SQLite.new(self)
|
93
|
-
end
|
94
|
-
|
95
101
|
def initialize(connection, logger, connection_options, config)
|
96
102
|
super(connection, logger, config)
|
97
103
|
|
@@ -113,12 +119,6 @@ module ActiveRecord
|
|
113
119
|
sqlite_version >= "3.8.0"
|
114
120
|
end
|
115
121
|
|
116
|
-
# Returns true, since this connection adapter supports prepared statement
|
117
|
-
# caching.
|
118
|
-
def supports_statement_cache?
|
119
|
-
true
|
120
|
-
end
|
121
|
-
|
122
122
|
def requires_reloading?
|
123
123
|
true
|
124
124
|
end
|
@@ -267,53 +267,6 @@ module ActiveRecord
|
|
267
267
|
|
268
268
|
# SCHEMA STATEMENTS ========================================
|
269
269
|
|
270
|
-
def new_column_from_field(table_name, field) # :nondoc:
|
271
|
-
case field["dflt_value"]
|
272
|
-
when /^null$/i
|
273
|
-
field["dflt_value"] = nil
|
274
|
-
when /^'(.*)'$/m
|
275
|
-
field["dflt_value"] = $1.gsub("''", "'")
|
276
|
-
when /^"(.*)"$/m
|
277
|
-
field["dflt_value"] = $1.gsub('""', '"')
|
278
|
-
end
|
279
|
-
|
280
|
-
collation = field["collation"]
|
281
|
-
sql_type = field["type"]
|
282
|
-
type_metadata = fetch_type_metadata(sql_type)
|
283
|
-
new_column(field["name"], field["dflt_value"], type_metadata, field["notnull"].to_i == 0, table_name, nil, collation)
|
284
|
-
end
|
285
|
-
|
286
|
-
# Returns an array of indexes for the given table.
|
287
|
-
def indexes(table_name, name = nil) #:nodoc:
|
288
|
-
if name
|
289
|
-
ActiveSupport::Deprecation.warn(<<-MSG.squish)
|
290
|
-
Passing name to #indexes is deprecated without replacement.
|
291
|
-
MSG
|
292
|
-
end
|
293
|
-
|
294
|
-
exec_query("PRAGMA index_list(#{quote_table_name(table_name)})", "SCHEMA").map do |row|
|
295
|
-
sql = <<-SQL
|
296
|
-
SELECT sql
|
297
|
-
FROM sqlite_master
|
298
|
-
WHERE name=#{quote(row['name'])} AND type='index'
|
299
|
-
UNION ALL
|
300
|
-
SELECT sql
|
301
|
-
FROM sqlite_temp_master
|
302
|
-
WHERE name=#{quote(row['name'])} AND type='index'
|
303
|
-
SQL
|
304
|
-
index_sql = exec_query(sql).first["sql"]
|
305
|
-
match = /\sWHERE\s+(.+)$/i.match(index_sql)
|
306
|
-
where = match[1] if match
|
307
|
-
IndexDefinition.new(
|
308
|
-
table_name,
|
309
|
-
row["name"],
|
310
|
-
row["unique"] != 0,
|
311
|
-
exec_query("PRAGMA index_info('#{row['name']}')", "SCHEMA").map { |col|
|
312
|
-
col["name"]
|
313
|
-
}, nil, nil, where)
|
314
|
-
end
|
315
|
-
end
|
316
|
-
|
317
270
|
def primary_keys(table_name) # :nodoc:
|
318
271
|
pks = table_structure(table_name).select { |f| f["pk"] > 0 }
|
319
272
|
pks.sort_by { |f| f["pk"] }.map { |f| f["name"] }
|
@@ -333,14 +286,14 @@ module ActiveRecord
|
|
333
286
|
rename_table_indexes(table_name, new_name)
|
334
287
|
end
|
335
288
|
|
336
|
-
# See:
|
289
|
+
# See: https://www.sqlite.org/lang_altertable.html
|
337
290
|
# SQLite has an additional restriction on the ALTER TABLE statement
|
338
291
|
def valid_alter_table_type?(type)
|
339
292
|
type.to_sym != :primary_key
|
340
293
|
end
|
341
294
|
|
342
295
|
def add_column(table_name, column_name, type, options = {}) #:nodoc:
|
343
|
-
if valid_alter_table_type?(type)
|
296
|
+
if valid_alter_table_type?(type)
|
344
297
|
super(table_name, column_name, type, options)
|
345
298
|
else
|
346
299
|
alter_table(table_name) do |definition|
|
@@ -410,6 +363,12 @@ module ActiveRecord
|
|
410
363
|
end
|
411
364
|
end
|
412
365
|
|
366
|
+
def insert_fixtures(rows, table_name)
|
367
|
+
rows.each do |row|
|
368
|
+
insert_fixture(row, table_name)
|
369
|
+
end
|
370
|
+
end
|
371
|
+
|
413
372
|
private
|
414
373
|
|
415
374
|
def table_structure(table_name)
|
@@ -440,21 +399,18 @@ module ActiveRecord
|
|
440
399
|
options[:id] = false
|
441
400
|
create_table(to, options) do |definition|
|
442
401
|
@definition = definition
|
443
|
-
if from_primary_key.
|
444
|
-
@definition.primary_keys from_primary_key
|
445
|
-
end
|
402
|
+
@definition.primary_key(from_primary_key) if from_primary_key.present?
|
446
403
|
columns(from).each do |column|
|
447
404
|
column_name = options[:rename] ?
|
448
405
|
(options[:rename][column.name] ||
|
449
406
|
options[:rename][column.name.to_sym] ||
|
450
407
|
column.name) : column.name
|
408
|
+
next if column_name == from_primary_key
|
451
409
|
|
452
410
|
@definition.column(column_name, column.type,
|
453
411
|
limit: column.limit, default: column.default,
|
454
412
|
precision: column.precision, scale: column.scale,
|
455
|
-
null: column.null, collation: column.collation
|
456
|
-
primary_key: column_name == from_primary_key
|
457
|
-
)
|
413
|
+
null: column.null, collation: column.collation)
|
458
414
|
end
|
459
415
|
yield @definition if block_given?
|
460
416
|
end
|
@@ -467,9 +423,6 @@ module ActiveRecord
|
|
467
423
|
def copy_table_indexes(from, to, rename = {})
|
468
424
|
indexes(from).each do |index|
|
469
425
|
name = index.name
|
470
|
-
# indexes sqlite creates for internal use start with `sqlite_` and
|
471
|
-
# don't need to be copied
|
472
|
-
next if name.starts_with?("sqlite_")
|
473
426
|
if to == "a#{from}"
|
474
427
|
name = "t#{name}"
|
475
428
|
elsif from == "a#{to}"
|
@@ -485,7 +438,6 @@ module ActiveRecord
|
|
485
438
|
# index name can't be the same
|
486
439
|
opts = { name: name.gsub(/(^|_)(#{from})_/, "\\1#{to}_"), internal: true }
|
487
440
|
opts[:unique] = true if index.unique
|
488
|
-
opts[:where] = index.where if index.where
|
489
441
|
add_index(to, columns, opts)
|
490
442
|
end
|
491
443
|
end
|
@@ -566,21 +518,14 @@ module ActiveRecord
|
|
566
518
|
end
|
567
519
|
end
|
568
520
|
|
569
|
-
def
|
570
|
-
|
571
|
-
end
|
572
|
-
|
573
|
-
def extract_foreign_key_action(specifier)
|
574
|
-
case specifier
|
575
|
-
when "CASCADE"; :cascade
|
576
|
-
when "SET NULL"; :nullify
|
577
|
-
when "RESTRICT"; :restrict
|
578
|
-
end
|
521
|
+
def arel_visitor
|
522
|
+
Arel::Visitors::SQLite.new(self)
|
579
523
|
end
|
580
524
|
|
581
525
|
def configure_connection
|
582
526
|
execute("PRAGMA foreign_keys = ON", "SCHEMA")
|
583
527
|
end
|
584
528
|
end
|
529
|
+
ActiveSupport.run_load_hooks(:active_record_sqlite3adapter, SQLite3Adapter)
|
585
530
|
end
|
586
531
|
end
|
@@ -1,6 +1,8 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActiveRecord
|
2
4
|
module ConnectionHandling
|
3
|
-
RAILS_ENV = -> { (Rails.env if defined?(Rails.env)) || ENV["RAILS_ENV"] || ENV["RACK_ENV"] }
|
5
|
+
RAILS_ENV = -> { (Rails.env if defined?(Rails.env)) || ENV["RAILS_ENV"].presence || ENV["RACK_ENV"].presence }
|
4
6
|
DEFAULT_ENV = -> { RAILS_ENV.call || "default_env" }
|
5
7
|
|
6
8
|
# Establishes the connection to the database. Accepts a hash as input where
|
@@ -138,6 +140,6 @@ module ActiveRecord
|
|
138
140
|
end
|
139
141
|
|
140
142
|
delegate :clear_active_connections!, :clear_reloadable_connections!,
|
141
|
-
:clear_all_connections!, to: :connection_handler
|
143
|
+
:clear_all_connections!, :flush_idle_connections!, to: :connection_handler
|
142
144
|
end
|
143
145
|
end
|
data/lib/active_record/core.rb
CHANGED
@@ -1,7 +1,8 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
2
3
|
require "active_support/core_ext/hash/indifferent_access"
|
3
|
-
require "active_support/core_ext/object/duplicable"
|
4
4
|
require "active_support/core_ext/string/filters"
|
5
|
+
require "concurrent/map"
|
5
6
|
|
6
7
|
module ActiveRecord
|
7
8
|
module Core
|
@@ -56,8 +57,7 @@ module ActiveRecord
|
|
56
57
|
# :singleton-method:
|
57
58
|
# Determines whether to use Time.utc (using :utc) or Time.local (using :local) when pulling
|
58
59
|
# dates and times from the database. This is set to :utc by default.
|
59
|
-
mattr_accessor :default_timezone, instance_writer: false
|
60
|
-
self.default_timezone = :utc
|
60
|
+
mattr_accessor :default_timezone, instance_writer: false, default: :utc
|
61
61
|
|
62
62
|
##
|
63
63
|
# :singleton-method:
|
@@ -67,42 +67,27 @@ module ActiveRecord
|
|
67
67
|
# ActiveRecord::Schema file which can be loaded into any database that
|
68
68
|
# supports migrations. Use :ruby if you want to have different database
|
69
69
|
# adapters for, e.g., your development and test environments.
|
70
|
-
mattr_accessor :schema_format, instance_writer: false
|
71
|
-
self.schema_format = :ruby
|
70
|
+
mattr_accessor :schema_format, instance_writer: false, default: :ruby
|
72
71
|
|
73
72
|
##
|
74
73
|
# :singleton-method:
|
75
74
|
# Specifies if an error should be raised if the query has an order being
|
76
75
|
# ignored when doing batch queries. Useful in applications where the
|
77
76
|
# scope being ignored is error-worthy, rather than a warning.
|
78
|
-
mattr_accessor :error_on_ignored_order, instance_writer: false
|
79
|
-
self.error_on_ignored_order = false
|
80
|
-
|
81
|
-
def self.error_on_ignored_order_or_limit
|
82
|
-
ActiveSupport::Deprecation.warn(<<-MSG.squish)
|
83
|
-
The flag error_on_ignored_order_or_limit is deprecated. Limits are
|
84
|
-
now supported. Please use error_on_ignored_order instead.
|
85
|
-
MSG
|
86
|
-
error_on_ignored_order
|
87
|
-
end
|
88
|
-
|
89
|
-
def error_on_ignored_order_or_limit
|
90
|
-
self.class.error_on_ignored_order_or_limit
|
91
|
-
end
|
77
|
+
mattr_accessor :error_on_ignored_order, instance_writer: false, default: false
|
92
78
|
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
79
|
+
# :singleton-method:
|
80
|
+
# Specify the behavior for unsafe raw query methods. Values are as follows
|
81
|
+
# deprecated - Warnings are logged when unsafe raw SQL is passed to
|
82
|
+
# query methods.
|
83
|
+
# disabled - Unsafe raw SQL passed to query methods results in
|
84
|
+
# UnknownAttributeReference exception.
|
85
|
+
mattr_accessor :allow_unsafe_raw_sql, instance_writer: false, default: :deprecated
|
100
86
|
|
101
87
|
##
|
102
88
|
# :singleton-method:
|
103
89
|
# Specify whether or not to use timestamps for migration versions
|
104
|
-
mattr_accessor :timestamped_migrations, instance_writer: false
|
105
|
-
self.timestamped_migrations = true
|
90
|
+
mattr_accessor :timestamped_migrations, instance_writer: false, default: true
|
106
91
|
|
107
92
|
##
|
108
93
|
# :singleton-method:
|
@@ -110,8 +95,7 @@ module ActiveRecord
|
|
110
95
|
# db:migrate rake task. This is true by default, which is useful for the
|
111
96
|
# development environment. This should ideally be false in the production
|
112
97
|
# environment where dumping schema is rarely needed.
|
113
|
-
mattr_accessor :dump_schema_after_migration, instance_writer: false
|
114
|
-
self.dump_schema_after_migration = true
|
98
|
+
mattr_accessor :dump_schema_after_migration, instance_writer: false, default: true
|
115
99
|
|
116
100
|
##
|
117
101
|
# :singleton-method:
|
@@ -120,8 +104,7 @@ module ActiveRecord
|
|
120
104
|
# schema_search_path are dumped. Use :all to dump all schemas regardless
|
121
105
|
# of schema_search_path, or a string of comma separated schemas for a
|
122
106
|
# custom list.
|
123
|
-
mattr_accessor :dump_schemas, instance_writer: false
|
124
|
-
self.dump_schemas = :schema_search_path
|
107
|
+
mattr_accessor :dump_schemas, instance_writer: false, default: :schema_search_path
|
125
108
|
|
126
109
|
##
|
127
110
|
# :singleton-method:
|
@@ -130,7 +113,6 @@ module ActiveRecord
|
|
130
113
|
# be used to identify queries which load thousands of records and
|
131
114
|
# potentially cause memory bloat.
|
132
115
|
mattr_accessor :warn_on_records_fetched_greater_than, instance_writer: false
|
133
|
-
self.warn_on_records_fetched_greater_than = nil
|
134
116
|
|
135
117
|
mattr_accessor :maintain_test_schema, instance_accessor: false
|
136
118
|
|
@@ -149,14 +131,14 @@ module ActiveRecord
|
|
149
131
|
self.default_connection_handler = ConnectionAdapters::ConnectionHandler.new
|
150
132
|
end
|
151
133
|
|
152
|
-
module ClassMethods
|
134
|
+
module ClassMethods # :nodoc:
|
153
135
|
def allocate
|
154
136
|
define_attribute_methods
|
155
137
|
super
|
156
138
|
end
|
157
139
|
|
158
140
|
def initialize_find_by_cache # :nodoc:
|
159
|
-
@find_by_statement_cache = { true =>
|
141
|
+
@find_by_statement_cache = { true => Concurrent::Map.new, false => Concurrent::Map.new }
|
160
142
|
end
|
161
143
|
|
162
144
|
def inherited(child_class) # :nodoc:
|
@@ -175,8 +157,7 @@ module ActiveRecord
|
|
175
157
|
|
176
158
|
id = ids.first
|
177
159
|
|
178
|
-
return super if
|
179
|
-
id.is_a?(ActiveRecord::Base)
|
160
|
+
return super if StatementCache.unsupported_value?(id)
|
180
161
|
|
181
162
|
key = primary_key
|
182
163
|
|
@@ -184,7 +165,7 @@ module ActiveRecord
|
|
184
165
|
where(key => params.bind).limit(1)
|
185
166
|
}
|
186
167
|
|
187
|
-
record = statement.execute([id],
|
168
|
+
record = statement.execute([id], connection).first
|
188
169
|
unless record
|
189
170
|
raise RecordNotFound.new("Couldn't find #{name} with '#{primary_key}'=#{id}",
|
190
171
|
name, primary_key, id)
|
@@ -201,7 +182,7 @@ module ActiveRecord
|
|
201
182
|
hash = args.first
|
202
183
|
|
203
184
|
return super if !(Hash === hash) || hash.values.any? { |v|
|
204
|
-
|
185
|
+
StatementCache.unsupported_value?(v)
|
205
186
|
}
|
206
187
|
|
207
188
|
# We can't cache Post.find_by(author: david) ...yet
|
@@ -216,7 +197,7 @@ module ActiveRecord
|
|
216
197
|
where(wheres).limit(1)
|
217
198
|
}
|
218
199
|
begin
|
219
|
-
statement.execute(hash.values,
|
200
|
+
statement.execute(hash.values, connection).first
|
220
201
|
rescue TypeError
|
221
202
|
raise ActiveRecord::StatementInvalid
|
222
203
|
rescue ::RangeError
|
@@ -258,7 +239,7 @@ module ActiveRecord
|
|
258
239
|
end
|
259
240
|
end
|
260
241
|
|
261
|
-
# Overwrite the default class equality method to provide support for
|
242
|
+
# Overwrite the default class equality method to provide support for decorated models.
|
262
243
|
def ===(object)
|
263
244
|
object.is_a?(self)
|
264
245
|
end
|
@@ -272,16 +253,6 @@ module ActiveRecord
|
|
272
253
|
@arel_table ||= Arel::Table.new(table_name, type_caster: type_caster)
|
273
254
|
end
|
274
255
|
|
275
|
-
# Returns the Arel engine.
|
276
|
-
def arel_engine # :nodoc:
|
277
|
-
@arel_engine ||=
|
278
|
-
if Base == self || connection_handler.retrieve_connection_pool(connection_specification_name)
|
279
|
-
self
|
280
|
-
else
|
281
|
-
superclass.arel_engine
|
282
|
-
end
|
283
|
-
end
|
284
|
-
|
285
256
|
def arel_attribute(name, table = arel_table) # :nodoc:
|
286
257
|
name = attribute_alias(name) if attribute_alias?(name)
|
287
258
|
table[name]
|
@@ -299,16 +270,15 @@ module ActiveRecord
|
|
299
270
|
|
300
271
|
def cached_find_by_statement(key, &block)
|
301
272
|
cache = @find_by_statement_cache[connection.prepared_statements]
|
302
|
-
cache
|
303
|
-
cache[key] ||= StatementCache.create(connection, &block)
|
304
|
-
}
|
273
|
+
cache.compute_if_absent(key) { StatementCache.create(connection, &block) }
|
305
274
|
end
|
306
275
|
|
307
276
|
def relation
|
308
277
|
relation = Relation.create(self, arel_table, predicate_builder)
|
309
278
|
|
310
279
|
if finder_needs_type_condition? && !ignore_default_scope?
|
311
|
-
relation.where(type_condition)
|
280
|
+
relation.where!(type_condition)
|
281
|
+
relation.create_with!(inheritance_column.to_s => sti_name)
|
312
282
|
else
|
313
283
|
relation
|
314
284
|
end
|
@@ -548,7 +518,7 @@ module ActiveRecord
|
|
548
518
|
#
|
549
519
|
# So we can avoid the +method_missing+ hit by explicitly defining +#to_ary+ as +nil+ here.
|
550
520
|
#
|
551
|
-
# See also
|
521
|
+
# See also https://tenderlovemaking.com/2011/06/28/til-its-ok-to-return-nil-from-to_ary.html
|
552
522
|
def to_ary
|
553
523
|
nil
|
554
524
|
end
|