activerecord 6.0.0.beta1 → 6.0.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of activerecord might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/CHANGELOG.md +455 -9
- data/README.rdoc +3 -1
- data/lib/active_record/associations/association.rb +18 -1
- data/lib/active_record/associations/builder/association.rb +14 -18
- data/lib/active_record/associations/builder/belongs_to.rb +5 -2
- data/lib/active_record/associations/builder/collection_association.rb +5 -15
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +1 -1
- data/lib/active_record/associations/builder/has_many.rb +2 -0
- data/lib/active_record/associations/builder/has_one.rb +35 -1
- data/lib/active_record/associations/builder/singular_association.rb +2 -0
- data/lib/active_record/associations/collection_association.rb +5 -6
- data/lib/active_record/associations/collection_proxy.rb +13 -42
- data/lib/active_record/associations/has_many_association.rb +1 -9
- data/lib/active_record/associations/has_many_through_association.rb +4 -11
- data/lib/active_record/associations/join_dependency/join_association.rb +21 -7
- data/lib/active_record/associations/join_dependency.rb +10 -9
- data/lib/active_record/associations/preloader/association.rb +37 -34
- data/lib/active_record/associations/preloader/through_association.rb +48 -39
- data/lib/active_record/associations/preloader.rb +11 -6
- data/lib/active_record/associations.rb +3 -2
- data/lib/active_record/attribute_methods/before_type_cast.rb +4 -1
- data/lib/active_record/attribute_methods/dirty.rb +47 -14
- data/lib/active_record/attribute_methods/primary_key.rb +7 -15
- data/lib/active_record/attribute_methods/query.rb +2 -3
- data/lib/active_record/attribute_methods/read.rb +3 -9
- data/lib/active_record/attribute_methods/write.rb +6 -12
- data/lib/active_record/attribute_methods.rb +3 -53
- data/lib/active_record/attributes.rb +13 -0
- data/lib/active_record/autosave_association.rb +15 -5
- data/lib/active_record/base.rb +0 -1
- data/lib/active_record/callbacks.rb +3 -3
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +124 -23
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +8 -4
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +101 -70
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +11 -5
- data/lib/active_record/connection_adapters/abstract/quoting.rb +63 -6
- data/lib/active_record/connection_adapters/abstract/schema_creation.rb +5 -2
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +51 -40
- data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +1 -1
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +95 -30
- data/lib/active_record/connection_adapters/abstract/transaction.rb +17 -6
- data/lib/active_record/connection_adapters/abstract_adapter.rb +108 -39
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +93 -134
- data/lib/active_record/connection_adapters/column.rb +17 -13
- data/lib/active_record/connection_adapters/connection_specification.rb +1 -1
- data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +3 -3
- data/lib/active_record/connection_adapters/mysql/database_statements.rb +45 -7
- data/lib/active_record/connection_adapters/mysql/quoting.rb +44 -7
- data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +40 -32
- data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +14 -6
- data/lib/active_record/connection_adapters/mysql/schema_statements.rb +66 -5
- data/lib/active_record/connection_adapters/mysql/type_metadata.rb +6 -10
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +18 -5
- data/lib/active_record/connection_adapters/postgresql/column.rb +17 -30
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +5 -1
- data/lib/active_record/connection_adapters/postgresql/oid/money.rb +2 -2
- data/lib/active_record/connection_adapters/postgresql/oid/range.rb +1 -1
- data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +6 -3
- data/lib/active_record/connection_adapters/postgresql/quoting.rb +40 -3
- data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +36 -0
- data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +98 -89
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +47 -63
- data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +23 -27
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +91 -24
- data/lib/active_record/connection_adapters/schema_cache.rb +32 -14
- data/lib/active_record/connection_adapters/sql_type_metadata.rb +11 -8
- data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +118 -0
- data/lib/active_record/connection_adapters/sqlite3/quoting.rb +38 -2
- data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +28 -2
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +69 -118
- data/lib/active_record/connection_handling.rb +32 -16
- data/lib/active_record/core.rb +27 -20
- data/lib/active_record/database_configurations/hash_config.rb +11 -11
- data/lib/active_record/database_configurations/url_config.rb +21 -16
- data/lib/active_record/database_configurations.rb +99 -50
- data/lib/active_record/dynamic_matchers.rb +1 -1
- data/lib/active_record/enum.rb +15 -0
- data/lib/active_record/errors.rb +18 -13
- data/lib/active_record/fixtures.rb +11 -6
- data/lib/active_record/gem_version.rb +1 -1
- data/lib/active_record/inheritance.rb +1 -1
- data/lib/active_record/insert_all.rb +179 -0
- data/lib/active_record/integration.rb +13 -1
- data/lib/active_record/internal_metadata.rb +5 -1
- data/lib/active_record/locking/optimistic.rb +3 -4
- data/lib/active_record/log_subscriber.rb +1 -1
- data/lib/active_record/middleware/database_selector/resolver/session.rb +45 -0
- data/lib/active_record/middleware/database_selector/resolver.rb +92 -0
- data/lib/active_record/middleware/database_selector.rb +75 -0
- data/lib/active_record/migration/command_recorder.rb +28 -14
- data/lib/active_record/migration/compatibility.rb +72 -63
- data/lib/active_record/migration.rb +62 -44
- data/lib/active_record/persistence.rb +212 -19
- data/lib/active_record/querying.rb +18 -14
- data/lib/active_record/railtie.rb +9 -1
- data/lib/active_record/railties/collection_cache_association_loading.rb +3 -3
- data/lib/active_record/railties/databases.rake +124 -25
- data/lib/active_record/reflection.rb +18 -32
- data/lib/active_record/relation/calculations.rb +40 -44
- data/lib/active_record/relation/delegation.rb +23 -31
- data/lib/active_record/relation/finder_methods.rb +13 -13
- data/lib/active_record/relation/merger.rb +11 -16
- data/lib/active_record/relation/query_attribute.rb +5 -3
- data/lib/active_record/relation/query_methods.rb +217 -68
- data/lib/active_record/relation/spawn_methods.rb +1 -1
- data/lib/active_record/relation/where_clause.rb +10 -10
- data/lib/active_record/relation.rb +184 -35
- data/lib/active_record/sanitization.rb +33 -4
- data/lib/active_record/schema.rb +1 -1
- data/lib/active_record/schema_dumper.rb +10 -1
- data/lib/active_record/schema_migration.rb +1 -1
- data/lib/active_record/scoping/default.rb +7 -15
- data/lib/active_record/scoping/named.rb +10 -2
- data/lib/active_record/scoping.rb +6 -7
- data/lib/active_record/statement_cache.rb +2 -2
- data/lib/active_record/store.rb +48 -0
- data/lib/active_record/table_metadata.rb +9 -13
- data/lib/active_record/tasks/database_tasks.rb +109 -6
- data/lib/active_record/tasks/mysql_database_tasks.rb +3 -1
- data/lib/active_record/test_databases.rb +1 -16
- data/lib/active_record/test_fixtures.rb +2 -2
- data/lib/active_record/timestamp.rb +35 -19
- data/lib/active_record/touch_later.rb +4 -2
- data/lib/active_record/transactions.rb +55 -45
- data/lib/active_record/type_caster/connection.rb +16 -10
- data/lib/active_record/validations/uniqueness.rb +4 -4
- data/lib/active_record/validations.rb +1 -0
- data/lib/active_record.rb +7 -1
- data/lib/arel/insert_manager.rb +3 -3
- data/lib/arel/nodes/and.rb +1 -1
- data/lib/arel/nodes/case.rb +1 -1
- data/lib/arel/nodes/comment.rb +29 -0
- data/lib/arel/nodes/select_core.rb +16 -12
- data/lib/arel/nodes/unary.rb +1 -0
- data/lib/arel/nodes/values_list.rb +2 -17
- data/lib/arel/nodes.rb +2 -1
- data/lib/arel/select_manager.rb +10 -10
- data/lib/arel/visitors/depth_first.rb +7 -2
- data/lib/arel/visitors/dot.rb +7 -2
- data/lib/arel/visitors/ibm_db.rb +13 -0
- data/lib/arel/visitors/informix.rb +6 -0
- data/lib/arel/visitors/mssql.rb +15 -1
- data/lib/arel/visitors/oracle12.rb +4 -5
- data/lib/arel/visitors/postgresql.rb +4 -10
- data/lib/arel/visitors/to_sql.rb +107 -131
- data/lib/arel/visitors/visitor.rb +9 -5
- data/lib/arel.rb +7 -0
- data/lib/rails/generators/active_record/migration/migration_generator.rb +1 -1
- data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb.tt +1 -1
- data/lib/rails/generators/active_record/migration/templates/migration.rb.tt +4 -2
- data/lib/rails/generators/active_record/model/model_generator.rb +1 -1
- data/lib/rails/generators/active_record/model/templates/model.rb.tt +10 -1
- metadata +17 -13
- data/lib/active_record/collection_cache_key.rb +0 -53
- data/lib/arel/nodes/values.rb +0 -16
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module Middleware
|
5
|
+
class DatabaseSelector
|
6
|
+
class Resolver
|
7
|
+
# The session class is used by the DatabaseSelector::Resolver to save
|
8
|
+
# timestamps of the last write in the session.
|
9
|
+
#
|
10
|
+
# The last_write is used to determine whether it's safe to read
|
11
|
+
# from the replica or the request needs to be sent to the primary.
|
12
|
+
class Session # :nodoc:
|
13
|
+
def self.call(request)
|
14
|
+
new(request.session)
|
15
|
+
end
|
16
|
+
|
17
|
+
# Converts time to a timestamp that represents milliseconds since
|
18
|
+
# epoch.
|
19
|
+
def self.convert_time_to_timestamp(time)
|
20
|
+
time.to_i * 1000 + time.usec / 1000
|
21
|
+
end
|
22
|
+
|
23
|
+
# Converts milliseconds since epoch timestamp into a time object.
|
24
|
+
def self.convert_timestamp_to_time(timestamp)
|
25
|
+
timestamp ? Time.at(timestamp / 1000, (timestamp % 1000) * 1000) : Time.at(0)
|
26
|
+
end
|
27
|
+
|
28
|
+
def initialize(session)
|
29
|
+
@session = session
|
30
|
+
end
|
31
|
+
|
32
|
+
attr_reader :session
|
33
|
+
|
34
|
+
def last_write_timestamp
|
35
|
+
self.class.convert_timestamp_to_time(session[:last_write])
|
36
|
+
end
|
37
|
+
|
38
|
+
def update_last_write_timestamp
|
39
|
+
session[:last_write] = self.class.convert_time_to_timestamp(Time.now)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_record/middleware/database_selector/resolver/session"
|
4
|
+
|
5
|
+
module ActiveRecord
|
6
|
+
module Middleware
|
7
|
+
class DatabaseSelector
|
8
|
+
# The Resolver class is used by the DatabaseSelector middleware to
|
9
|
+
# determine which database the request should use.
|
10
|
+
#
|
11
|
+
# To change the behavior of the Resolver class in your application,
|
12
|
+
# create a custom resolver class that inherits from
|
13
|
+
# DatabaseSelector::Resolver and implements the methods that need to
|
14
|
+
# be changed.
|
15
|
+
#
|
16
|
+
# By default the Resolver class will send read traffic to the replica
|
17
|
+
# if it's been 2 seconds since the last write.
|
18
|
+
class Resolver # :nodoc:
|
19
|
+
SEND_TO_REPLICA_DELAY = 2.seconds
|
20
|
+
|
21
|
+
def self.call(context, options = {})
|
22
|
+
new(context, options)
|
23
|
+
end
|
24
|
+
|
25
|
+
def initialize(context, options = {})
|
26
|
+
@context = context
|
27
|
+
@options = options
|
28
|
+
@delay = @options && @options[:delay] ? @options[:delay] : SEND_TO_REPLICA_DELAY
|
29
|
+
@instrumenter = ActiveSupport::Notifications.instrumenter
|
30
|
+
end
|
31
|
+
|
32
|
+
attr_reader :context, :delay, :instrumenter
|
33
|
+
|
34
|
+
def read(&blk)
|
35
|
+
if read_from_primary?
|
36
|
+
read_from_primary(&blk)
|
37
|
+
else
|
38
|
+
read_from_replica(&blk)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def write(&blk)
|
43
|
+
write_to_primary(&blk)
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def read_from_primary(&blk)
|
49
|
+
ActiveRecord::Base.connected_to(role: ActiveRecord::Base.writing_role) do
|
50
|
+
ActiveRecord::Base.connection_handler.while_preventing_writes(true) do
|
51
|
+
instrumenter.instrument("database_selector.active_record.read_from_primary") do
|
52
|
+
yield
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def read_from_replica(&blk)
|
59
|
+
ActiveRecord::Base.connected_to(role: ActiveRecord::Base.reading_role) do
|
60
|
+
instrumenter.instrument("database_selector.active_record.read_from_replica") do
|
61
|
+
yield
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def write_to_primary(&blk)
|
67
|
+
ActiveRecord::Base.connected_to(role: ActiveRecord::Base.writing_role) do
|
68
|
+
ActiveRecord::Base.connection_handler.while_preventing_writes(false) do
|
69
|
+
instrumenter.instrument("database_selector.active_record.wrote_to_primary") do
|
70
|
+
yield
|
71
|
+
ensure
|
72
|
+
context.update_last_write_timestamp
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def read_from_primary?
|
79
|
+
!time_since_last_write_ok?
|
80
|
+
end
|
81
|
+
|
82
|
+
def send_to_replica_delay
|
83
|
+
delay
|
84
|
+
end
|
85
|
+
|
86
|
+
def time_since_last_write_ok?
|
87
|
+
Time.now - context.last_write_timestamp >= send_to_replica_delay
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_record/middleware/database_selector/resolver"
|
4
|
+
|
5
|
+
module ActiveRecord
|
6
|
+
module Middleware
|
7
|
+
# The DatabaseSelector Middleware provides a framework for automatically
|
8
|
+
# swapping from the primary to the replica database connection. Rails
|
9
|
+
# provides a basic framework to determine when to swap and allows for
|
10
|
+
# applications to write custom strategy classes to override the default
|
11
|
+
# behavior.
|
12
|
+
#
|
13
|
+
# The resolver class defines when the application should switch (i.e. read
|
14
|
+
# from the primary if a write occurred less than 2 seconds ago) and a
|
15
|
+
# resolver context class that sets a value that helps the resolver class
|
16
|
+
# decide when to switch.
|
17
|
+
#
|
18
|
+
# Rails default middleware uses the request's session to set a timestamp
|
19
|
+
# that informs the application when to read from a primary or read from a
|
20
|
+
# replica.
|
21
|
+
#
|
22
|
+
# To use the DatabaseSelector in your application with default settings add
|
23
|
+
# the following options to your environment config:
|
24
|
+
#
|
25
|
+
# config.active_record.database_selector = { delay: 2.seconds }
|
26
|
+
# config.active_record.database_resolver = ActiveRecord::Middleware::DatabaseSelector::Resolver
|
27
|
+
# config.active_record.database_resolver_context = ActiveRecord::Middleware::DatabaseSelector::Resolver::Session
|
28
|
+
#
|
29
|
+
# New applications will include these lines commented out in the production.rb.
|
30
|
+
#
|
31
|
+
# The default behavior can be changed by setting the config options to a
|
32
|
+
# custom class:
|
33
|
+
#
|
34
|
+
# config.active_record.database_selector = { delay: 2.seconds }
|
35
|
+
# config.active_record.database_resolver = MyResolver
|
36
|
+
# config.active_record.database_resolver_context = MyResolver::MySession
|
37
|
+
class DatabaseSelector
|
38
|
+
def initialize(app, resolver_klass = nil, context_klass = nil, options = {})
|
39
|
+
@app = app
|
40
|
+
@resolver_klass = resolver_klass || Resolver
|
41
|
+
@context_klass = context_klass || Resolver::Session
|
42
|
+
@options = options
|
43
|
+
end
|
44
|
+
|
45
|
+
attr_reader :resolver_klass, :context_klass, :options
|
46
|
+
|
47
|
+
# Middleware that determines which database connection to use in a multiple
|
48
|
+
# database application.
|
49
|
+
def call(env)
|
50
|
+
request = ActionDispatch::Request.new(env)
|
51
|
+
|
52
|
+
select_database(request) do
|
53
|
+
@app.call(env)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
def select_database(request, &blk)
|
60
|
+
context = context_klass.call(request)
|
61
|
+
resolver = resolver_klass.call(context, options)
|
62
|
+
|
63
|
+
if reading_request?(request)
|
64
|
+
resolver.read(&blk)
|
65
|
+
else
|
66
|
+
resolver.write(&blk)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def reading_request?(request)
|
71
|
+
request.get? || request.head?
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -14,6 +14,8 @@ module ActiveRecord
|
|
14
14
|
# * change_column
|
15
15
|
# * change_column_default (must supply a :from and :to option)
|
16
16
|
# * change_column_null
|
17
|
+
# * change_column_comment (must supply a :from and :to option)
|
18
|
+
# * change_table_comment (must supply a :from and :to option)
|
17
19
|
# * create_join_table
|
18
20
|
# * create_table
|
19
21
|
# * disable_extension
|
@@ -35,7 +37,8 @@ module ActiveRecord
|
|
35
37
|
:change_column_default, :add_reference, :remove_reference, :transaction,
|
36
38
|
:drop_join_table, :drop_table, :execute_block, :enable_extension, :disable_extension,
|
37
39
|
:change_column, :execute, :remove_columns, :change_column_null,
|
38
|
-
:add_foreign_key, :remove_foreign_key
|
40
|
+
:add_foreign_key, :remove_foreign_key,
|
41
|
+
:change_column_comment, :change_table_comment
|
39
42
|
]
|
40
43
|
include JoinTable
|
41
44
|
|
@@ -231,28 +234,39 @@ module ActiveRecord
|
|
231
234
|
end
|
232
235
|
|
233
236
|
def invert_remove_foreign_key(args)
|
234
|
-
|
237
|
+
options = args.extract_options!
|
238
|
+
from_table, to_table = args
|
235
239
|
|
236
|
-
to_table
|
237
|
-
options_or_to_table[:to_table]
|
238
|
-
else
|
239
|
-
options_or_to_table
|
240
|
-
end
|
241
|
-
|
242
|
-
remove_options = if options_or_to_table.is_a?(Hash)
|
243
|
-
options_or_to_table.except(:to_table)
|
244
|
-
else
|
245
|
-
options_or_nil
|
246
|
-
end
|
240
|
+
to_table ||= options.delete(:to_table)
|
247
241
|
|
248
242
|
raise ActiveRecord::IrreversibleMigration, "remove_foreign_key is only reversible if given a second table" if to_table.nil?
|
249
243
|
|
250
244
|
reversed_args = [from_table, to_table]
|
251
|
-
reversed_args <<
|
245
|
+
reversed_args << options unless options.empty?
|
252
246
|
|
253
247
|
[:add_foreign_key, reversed_args]
|
254
248
|
end
|
255
249
|
|
250
|
+
def invert_change_column_comment(args)
|
251
|
+
table, column, options = *args
|
252
|
+
|
253
|
+
unless options && options.is_a?(Hash) && options.has_key?(:from) && options.has_key?(:to)
|
254
|
+
raise ActiveRecord::IrreversibleMigration, "change_column_comment is only reversible if given a :from and :to option."
|
255
|
+
end
|
256
|
+
|
257
|
+
[:change_column_comment, [table, column, from: options[:to], to: options[:from]]]
|
258
|
+
end
|
259
|
+
|
260
|
+
def invert_change_table_comment(args)
|
261
|
+
table, options = *args
|
262
|
+
|
263
|
+
unless options && options.is_a?(Hash) && options.has_key?(:from) && options.has_key?(:to)
|
264
|
+
raise ActiveRecord::IrreversibleMigration, "change_table_comment is only reversible if given a :from and :to option."
|
265
|
+
end
|
266
|
+
|
267
|
+
[:change_table_comment, [table, from: options[:to], to: options[:from]]]
|
268
|
+
end
|
269
|
+
|
256
270
|
def respond_to_missing?(method, _)
|
257
271
|
super || delegate.respond_to?(method)
|
258
272
|
end
|
@@ -16,13 +16,65 @@ module ActiveRecord
|
|
16
16
|
V6_0 = Current
|
17
17
|
|
18
18
|
class V5_2 < V6_0
|
19
|
+
module TableDefinition
|
20
|
+
def timestamps(**options)
|
21
|
+
options[:precision] ||= nil
|
22
|
+
super
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
19
26
|
module CommandRecorder
|
20
27
|
def invert_transaction(args, &block)
|
21
28
|
[:transaction, args, block]
|
22
29
|
end
|
30
|
+
|
31
|
+
def invert_change_column_comment(args)
|
32
|
+
table_name, column_name, comment = args
|
33
|
+
[:change_column_comment, [table_name, column_name, from: comment, to: comment]]
|
34
|
+
end
|
35
|
+
|
36
|
+
def invert_change_table_comment(args)
|
37
|
+
table_name, comment = args
|
38
|
+
[:change_table_comment, [table_name, from: comment, to: comment]]
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def create_table(table_name, **options)
|
43
|
+
if block_given?
|
44
|
+
super { |t| yield compatible_table_definition(t) }
|
45
|
+
else
|
46
|
+
super
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def change_table(table_name, **options)
|
51
|
+
if block_given?
|
52
|
+
super { |t| yield compatible_table_definition(t) }
|
53
|
+
else
|
54
|
+
super
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def create_join_table(table_1, table_2, **options)
|
59
|
+
if block_given?
|
60
|
+
super { |t| yield compatible_table_definition(t) }
|
61
|
+
else
|
62
|
+
super
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def add_timestamps(table_name, **options)
|
67
|
+
options[:precision] ||= nil
|
68
|
+
super
|
23
69
|
end
|
24
70
|
|
25
71
|
private
|
72
|
+
def compatible_table_definition(t)
|
73
|
+
class << t
|
74
|
+
prepend TableDefinition
|
75
|
+
end
|
76
|
+
t
|
77
|
+
end
|
26
78
|
|
27
79
|
def command_recorder
|
28
80
|
recorder = super
|
@@ -35,20 +87,18 @@ module ActiveRecord
|
|
35
87
|
|
36
88
|
class V5_1 < V5_2
|
37
89
|
def change_column(table_name, column_name, type, options = {})
|
38
|
-
if adapter_name == "PostgreSQL"
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
change_column_null(table_name, column_name, options[:null], options[:default]) if options.key?(:null)
|
44
|
-
change_column_comment(table_name, column_name, options[:comment]) if options.key?(:comment)
|
90
|
+
if connection.adapter_name == "PostgreSQL"
|
91
|
+
super(table_name, column_name, type, options.except(:default, :null, :comment))
|
92
|
+
connection.change_column_default(table_name, column_name, options[:default]) if options.key?(:default)
|
93
|
+
connection.change_column_null(table_name, column_name, options[:null], options[:default]) if options.key?(:null)
|
94
|
+
connection.change_column_comment(table_name, column_name, options[:comment]) if options.key?(:comment)
|
45
95
|
else
|
46
96
|
super
|
47
97
|
end
|
48
98
|
end
|
49
99
|
|
50
100
|
def create_table(table_name, options = {})
|
51
|
-
if adapter_name == "Mysql2"
|
101
|
+
if connection.adapter_name == "Mysql2"
|
52
102
|
super(table_name, options: "ENGINE=InnoDB", **options)
|
53
103
|
else
|
54
104
|
super
|
@@ -70,13 +120,13 @@ module ActiveRecord
|
|
70
120
|
end
|
71
121
|
|
72
122
|
def create_table(table_name, options = {})
|
73
|
-
if adapter_name == "PostgreSQL"
|
123
|
+
if connection.adapter_name == "PostgreSQL"
|
74
124
|
if options[:id] == :uuid && !options.key?(:default)
|
75
125
|
options[:default] = "uuid_generate_v4()"
|
76
126
|
end
|
77
127
|
end
|
78
128
|
|
79
|
-
unless adapter_name == "Mysql2" && options[:id] == :bigint
|
129
|
+
unless connection.adapter_name == "Mysql2" && options[:id] == :bigint
|
80
130
|
if [:integer, :bigint].include?(options[:id]) && !options.key?(:default)
|
81
131
|
options[:default] = nil
|
82
132
|
end
|
@@ -89,35 +139,12 @@ module ActiveRecord
|
|
89
139
|
options[:id] = :integer
|
90
140
|
end
|
91
141
|
|
92
|
-
|
93
|
-
super do |t|
|
94
|
-
yield compatible_table_definition(t)
|
95
|
-
end
|
96
|
-
else
|
97
|
-
super
|
98
|
-
end
|
99
|
-
end
|
100
|
-
|
101
|
-
def change_table(table_name, options = {})
|
102
|
-
if block_given?
|
103
|
-
super do |t|
|
104
|
-
yield compatible_table_definition(t)
|
105
|
-
end
|
106
|
-
else
|
107
|
-
super
|
108
|
-
end
|
142
|
+
super
|
109
143
|
end
|
110
144
|
|
111
145
|
def create_join_table(table_1, table_2, column_options: {}, **options)
|
112
146
|
column_options.reverse_merge!(type: :integer)
|
113
|
-
|
114
|
-
if block_given?
|
115
|
-
super do |t|
|
116
|
-
yield compatible_table_definition(t)
|
117
|
-
end
|
118
|
-
else
|
119
|
-
super
|
120
|
-
end
|
147
|
+
super
|
121
148
|
end
|
122
149
|
|
123
150
|
def add_column(table_name, column_name, type, options = {})
|
@@ -138,7 +165,7 @@ module ActiveRecord
|
|
138
165
|
class << t
|
139
166
|
prepend TableDefinition
|
140
167
|
end
|
141
|
-
|
168
|
+
super
|
142
169
|
end
|
143
170
|
end
|
144
171
|
|
@@ -156,33 +183,13 @@ module ActiveRecord
|
|
156
183
|
end
|
157
184
|
end
|
158
185
|
|
159
|
-
def
|
160
|
-
if block_given?
|
161
|
-
super do |t|
|
162
|
-
yield compatible_table_definition(t)
|
163
|
-
end
|
164
|
-
else
|
165
|
-
super
|
166
|
-
end
|
167
|
-
end
|
168
|
-
|
169
|
-
def change_table(table_name, options = {})
|
170
|
-
if block_given?
|
171
|
-
super do |t|
|
172
|
-
yield compatible_table_definition(t)
|
173
|
-
end
|
174
|
-
else
|
175
|
-
super
|
176
|
-
end
|
177
|
-
end
|
178
|
-
|
179
|
-
def add_reference(*, **options)
|
186
|
+
def add_reference(table_name, ref_name, **options)
|
180
187
|
options[:index] ||= false
|
181
188
|
super
|
182
189
|
end
|
183
190
|
alias :add_belongs_to :add_reference
|
184
191
|
|
185
|
-
def add_timestamps(
|
192
|
+
def add_timestamps(table_name, **options)
|
186
193
|
options[:null] = true if options[:null].nil?
|
187
194
|
super
|
188
195
|
end
|
@@ -193,7 +200,7 @@ module ActiveRecord
|
|
193
200
|
if options[:name].present?
|
194
201
|
options[:name].to_s
|
195
202
|
else
|
196
|
-
index_name(table_name, column: column_names)
|
203
|
+
connection.index_name(table_name, column: column_names)
|
197
204
|
end
|
198
205
|
super
|
199
206
|
end
|
@@ -213,15 +220,17 @@ module ActiveRecord
|
|
213
220
|
end
|
214
221
|
|
215
222
|
def index_name_for_remove(table_name, options = {})
|
216
|
-
index_name = index_name(table_name, options)
|
223
|
+
index_name = connection.index_name(table_name, options)
|
217
224
|
|
218
|
-
unless index_name_exists?(table_name, index_name)
|
225
|
+
unless connection.index_name_exists?(table_name, index_name)
|
219
226
|
if options.is_a?(Hash) && options.has_key?(:name)
|
220
227
|
options_without_column = options.dup
|
221
228
|
options_without_column.delete :column
|
222
|
-
index_name_without_column = index_name(table_name, options_without_column)
|
229
|
+
index_name_without_column = connection.index_name(table_name, options_without_column)
|
223
230
|
|
224
|
-
|
231
|
+
if connection.index_name_exists?(table_name, index_name_without_column)
|
232
|
+
return index_name_without_column
|
233
|
+
end
|
225
234
|
end
|
226
235
|
|
227
236
|
raise ArgumentError, "Index name '#{index_name}' on table '#{table_name}' does not exist"
|