Skip to content

Commit

Permalink
Merge pull request rails#35522 from gmcgibbon/rails_db_system_change_…
Browse files Browse the repository at this point in the history
…versioning

Add version awareness to rails db:system:change
  • Loading branch information
kaspth authored Mar 8, 2019
2 parents 4b68e6d + dcbb79d commit 199de6b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ def edit_database_config
end

def edit_gemfile
database_gem_name, _ = gem_for_database
gsub_file("Gemfile", all_database_gems_regex, database_gem_name)
name, version = gem_for_database
gsub_file("Gemfile", all_database_gems_regex, name)
gsub_file("Gemfile", gem_entry_regex_for(name), gem_entry_for(name, *version))
end

private
Expand All @@ -48,6 +49,15 @@ def all_database_gems_regex
all_database_gem_names = all_database_gems.map(&:first)
/(\b#{all_database_gem_names.join('\b|\b')}\b)/
end

def gem_entry_regex_for(gem_name)
/^gem.*\b#{gem_name}\b.*/
end

def gem_entry_for(*gem_name_and_version)
gem_name_and_version.map! { |segment| "'#{segment}'" }
"gem #{gem_name_and_version.join(", ")}"
end
end
end
end
Expand Down
21 changes: 18 additions & 3 deletions railties/test/generators/db_system_change_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ChangeGeneratorTest < Rails::Generators::TestCase

assert_file("Gemfile") do |content|
assert_match "# Use pg as the database for Active Record", content
assert_match "gem 'pg'", content
assert_match "gem 'pg', '>= 0.18', '< 2.0'", content
end
end

Expand All @@ -54,7 +54,7 @@ class ChangeGeneratorTest < Rails::Generators::TestCase

assert_file("Gemfile") do |content|
assert_match "# Use mysql2 as the database for Active Record", content
assert_match "gem 'mysql2'", content
assert_match "gem 'mysql2', '>= 0.4.4'", content
end
end

Expand All @@ -68,7 +68,22 @@ class ChangeGeneratorTest < Rails::Generators::TestCase

assert_file("Gemfile") do |content|
assert_match "# Use sqlite3 as the database for Active Record", content
assert_match "gem 'sqlite3'", content
assert_match "gem 'sqlite3', '~> 1.3', '>= 1.3.6'", content
end
end

test "change from versioned gem to other versioned gem" do
run_generator ["--to", "sqlite3"]
run_generator ["--to", "mysql", "--force"]

assert_file("config/database.yml") do |content|
assert_match "adapter: mysql2", content
assert_match "database: test_app", content
end

assert_file("Gemfile") do |content|
assert_match "# Use mysql2 as the database for Active Record", content
assert_match "gem 'mysql2', '>= 0.4.4'", content
end
end
end
Expand Down

0 comments on commit 199de6b

Please sign in to comment.