This commit is contained in:
Rafael Mendonça França 2021-12-21 07:28:39 +00:00
commit 2ea5268cd2
No known key found for this signature in database
GPG Key ID: FC23B6D0F1EEE948
3 changed files with 75 additions and 2 deletions

View File

@ -1,3 +1,10 @@
* Fix migration compatibility to create SQLite references/belongs_to column as integer when migration version is 6.0.
Reference/belongs_to in migrations with version 6.0 were creating columns as
bigint instead of integer for the SQLite Adapter.
*Marcelo Lauxen*
* Add a deprecation warning when `prepared_statements` configuration is not
set for the mysql2 adapter.

View File

@ -136,6 +136,22 @@ module ActiveRecord
end
end
module SQLite3
module TableDefinition
def references(*args, **options)
args.each do |ref_name|
ReferenceDefinition.new(ref_name, type: :integer, **options).add_to(self)
end
end
alias :belongs_to :references
def column(name, type, index: nil, **options)
options[:precision] ||= nil
super
end
end
end
module TableDefinition
def references(*args, **options)
args.each do |ref_name|
@ -179,8 +195,13 @@ module ActiveRecord
end
def add_reference(table_name, ref_name, **options)
ReferenceDefinition.new(ref_name, **options)
.add_to(connection.update_table_definition(table_name, self))
if connection.adapter_name == "SQLite"
reference_definition = ReferenceDefinition.new(ref_name, type: :integer, **options)
else
reference_definition = ReferenceDefinition.new(ref_name, **options)
end
reference_definition.add_to(connection.update_table_definition(table_name, self))
end
alias :add_belongs_to :add_reference
@ -188,6 +209,7 @@ module ActiveRecord
def compatible_table_definition(t)
class << t
prepend TableDefinition
prepend SQLite3::TableDefinition
end
t
end

View File

@ -533,6 +533,50 @@ module ActiveRecord
assert_not connection.column_exists?(:testings, :foo)
end
if current_adapter?(:SQLite3Adapter)
def test_references_stays_as_integer_column_on_create_table_with_reference_6_0
migration = Class.new(ActiveRecord::Migration[6.0]) {
def migrate(x)
create_table :more_testings do |t|
t.references :testings
end
end
}.new
ActiveRecord::Migrator.new(:up, [migration], @schema_migration).migrate
testings_id_column = connection.columns(:more_testings).find { |el| el.name == "testings_id" }
assert_equal "integer", testings_id_column.sql_type
ensure
connection.drop_table :more_testings rescue nil
end
def test_references_stays_as_integer_column_on_add_reference_6_0
create_migration = Class.new(ActiveRecord::Migration[6.0]) {
def version; 100 end
def migrate(x)
create_table :more_testings do |t|
t.string :test
end
end
}.new
migration = Class.new(ActiveRecord::Migration[6.0]) {
def version; 101 end
def migrate(x)
add_reference :more_testings, :testings
end
}.new
ActiveRecord::Migrator.new(:up, [create_migration, migration], @schema_migration).migrate
testings_id_column = connection.columns(:more_testings).find { |el| el.name == "testings_id" }
assert_equal "integer", testings_id_column.sql_type
ensure
connection.drop_table :more_testings rescue nil
end
end
private
def precision_implicit_default
if current_adapter?(:Mysql2Adapter)