Skip to content

Commit 199de6b

Browse files
authored
Merge pull request rails#35522 from gmcgibbon/rails_db_system_change_versioning
Add version awareness to rails db:system:change
2 parents 4b68e6d + dcbb79d commit 199de6b

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

railties/lib/rails/generators/rails/db/system/change/change_generator.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ def edit_database_config
3535
end
3636

3737
def edit_gemfile
38-
database_gem_name, _ = gem_for_database
39-
gsub_file("Gemfile", all_database_gems_regex, database_gem_name)
38+
name, version = gem_for_database
39+
gsub_file("Gemfile", all_database_gems_regex, name)
40+
gsub_file("Gemfile", gem_entry_regex_for(name), gem_entry_for(name, *version))
4041
end
4142

4243
private
@@ -48,6 +49,15 @@ def all_database_gems_regex
4849
all_database_gem_names = all_database_gems.map(&:first)
4950
/(\b#{all_database_gem_names.join('\b|\b')}\b)/
5051
end
52+
53+
def gem_entry_regex_for(gem_name)
54+
/^gem.*\b#{gem_name}\b.*/
55+
end
56+
57+
def gem_entry_for(*gem_name_and_version)
58+
gem_name_and_version.map! { |segment| "'#{segment}'" }
59+
"gem #{gem_name_and_version.join(", ")}"
60+
end
5161
end
5262
end
5363
end

railties/test/generators/db_system_change_generator_test.rb

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class ChangeGeneratorTest < Rails::Generators::TestCase
4040

4141
assert_file("Gemfile") do |content|
4242
assert_match "# Use pg as the database for Active Record", content
43-
assert_match "gem 'pg'", content
43+
assert_match "gem 'pg', '>= 0.18', '< 2.0'", content
4444
end
4545
end
4646

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

5555
assert_file("Gemfile") do |content|
5656
assert_match "# Use mysql2 as the database for Active Record", content
57-
assert_match "gem 'mysql2'", content
57+
assert_match "gem 'mysql2', '>= 0.4.4'", content
5858
end
5959
end
6060

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

6969
assert_file("Gemfile") do |content|
7070
assert_match "# Use sqlite3 as the database for Active Record", content
71-
assert_match "gem 'sqlite3'", content
71+
assert_match "gem 'sqlite3', '~> 1.3', '>= 1.3.6'", content
72+
end
73+
end
74+
75+
test "change from versioned gem to other versioned gem" do
76+
run_generator ["--to", "sqlite3"]
77+
run_generator ["--to", "mysql", "--force"]
78+
79+
assert_file("config/database.yml") do |content|
80+
assert_match "adapter: mysql2", content
81+
assert_match "database: test_app", content
82+
end
83+
84+
assert_file("Gemfile") do |content|
85+
assert_match "# Use mysql2 as the database for Active Record", content
86+
assert_match "gem 'mysql2', '>= 0.4.4'", content
7287
end
7388
end
7489
end

0 commit comments

Comments
 (0)