Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gracefully fallback on version migrations for sqlite < 3.7.11 #24685

Merged
merged 1 commit into from Apr 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -985,11 +985,23 @@ def foreign_key_options(from_table, to_table, options) # :nodoc:
end

def dump_schema_information #:nodoc:
versions = ActiveRecord::SchemaMigration.order('version').pluck(:version)
insert_versions_sql(versions)
end

def insert_versions_sql(versions)
sm_table = ActiveRecord::Migrator.schema_migrations_table_name

sql = "INSERT INTO #{sm_table} (version) VALUES "
sql << ActiveRecord::SchemaMigration.order('version').pluck(:version).map {|v| "('#{v}')" }.join(', ')
sql << ";\n\n"
if supports_multi_insert?
sql = "INSERT INTO #{sm_table} (version) VALUES "
sql << versions.map {|v| "('#{v}')" }.join(', ')
sql << ";\n\n"
sql
else
versions.map { |version|
"INSERT INTO #{sm_table} (version) VALUES ('#{version}');"
}.join "\n\n"
end
end

# Should not be called normally, but this operation is non-destructive.
Expand Down Expand Up @@ -1026,7 +1038,7 @@ def assume_migrated_upto_version(version, migrations_paths)
if (duplicate = inserting.detect {|v| inserting.count(v) > 1})
raise "Duplicate migration #{duplicate}. Please renumber your migrations to resolve the conflict."
end
execute "INSERT INTO #{sm_table} (version) VALUES #{inserting.map {|v| "('#{v}')"}.join(', ') }"
execute insert_versions_sql(versions)
end
end

Expand Down
Expand Up @@ -298,6 +298,11 @@ def supports_comments_in_create?
false
end

# Does this adapter support multi-value insert
def supports_multi_insert?
true
end

# This is meant to be implemented by the adapters that support extensions
def disable_extension(name)
end
Expand Down
Expand Up @@ -130,6 +130,10 @@ def supports_datetime_with_precision?
true
end

def supports_multi_insert?
sqlite_version >= '3.7.11'
end

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

>> '3.7.2' > '3.7.11'
=> true

We'll need a test case for supports_multi_insert? as well 😊

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

puts "#{sqlite_version.inspect}" # #<ActiveRecord::ConnectionAdapters::AbstractAdapter::Version:0x007fdd4fb12bf8 @version=[3, 8, 10, 2]>
puts "#{sqlite_version >= '3.8.8'}" # true

This is supported, I will add a test we run properly on both versions.

def active?
@active != false
end
Expand Down
18 changes: 18 additions & 0 deletions activerecord/test/cases/schema_dumper_test.rb
Expand Up @@ -29,6 +29,24 @@ def test_dump_schema_information_outputs_lexically_ordered_versions
ActiveRecord::SchemaMigration.delete_all
end

if current_adapter?(:SQLite3Adapter)
%w{3.7.8 3.7.11 3.7.12}.each do |version_string|
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added check for different versions <, <=, >

test "dumps schema version for sqlite version #{version_string}" do
version = ActiveRecord::ConnectionAdapters::SQLite3Adapter::Version.new(version_string)
ActiveRecord::Base.connection.stubs(:sqlite_version).returns(version)

versions = %w{ 20100101010101 20100201010101 20100301010101 }
versions.reverse_each do |v|
ActiveRecord::SchemaMigration.create!(:version => v)
end

schema_info = ActiveRecord::Base.connection.dump_schema_information
assert_match(/20100201010101.*20100301010101/m, schema_info)
ActiveRecord::SchemaMigration.delete_all
end
end
end

def test_magic_comment
assert_match "# encoding: #{Encoding.default_external.name}", standard_dump
end
Expand Down