Skip to content

Commit 4d62ea3

Browse files
andrewn617rafaelfranca
authored andcommitted
Update database.yml from DevcontainerGenerator
Postgresql's database.yml has devcontainer specific logic that should only appear when the app has a devcontainer. We need the DevcontainerGenerator to update database.yml, so when it is called by the devcontainer command the database.yml will have the right configuration. This commit also fixes db:system:change to make sure it updates the database.yml with the correct devcontainer configuration.
1 parent 1275e93 commit 4d62ea3

File tree

5 files changed

+30
-3
lines changed

5 files changed

+30
-3
lines changed

railties/lib/rails/generators/app_base.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,10 @@ def skip_devcontainer?
413413
!options[:devcontainer]
414414
end
415415

416+
def devcontainer?
417+
options[:devcontainer]
418+
end
419+
416420
def skip_kamal?
417421
options[:skip_kamal]
418422
end

railties/lib/rails/generators/rails/app/templates/config/databases/postgresql.yml.tt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ default: &default
1818
# For details on connection pooling, see Rails configuration guide
1919
# https://guides.rubyonrails.org/configuring.html#database-pooling
2020
pool: <%%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
21-
<% if options[:devcontainer] -%>
21+
<% if devcontainer? -%>
2222
<%% if ENV["DB_HOST"] %>
2323
host: <%%= ENV["DB_HOST"] %>
2424
username: postgres

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ def edit_dockerfile
5353
end
5454

5555
def edit_devcontainer_files
56-
devcontainer_path = File.expand_path(".devcontainer", destination_root)
57-
return unless File.exist?(devcontainer_path)
56+
return unless devcontainer?
5857

5958
edit_devcontainer_json
6059
edit_compose_yaml
@@ -194,6 +193,12 @@ def devcontainer_json_path
194193
def database
195194
@database ||= Database.build(options[:database])
196195
end
196+
197+
def devcontainer?
198+
return @devcontainer if defined?(@devcontainer)
199+
200+
@devcontainer = File.exist?(File.expand_path(".devcontainer", destination_root))
201+
end
197202
end
198203
end
199204
end

railties/lib/rails/generators/rails/devcontainer/devcontainer_generator.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ class DevcontainerGenerator < Base # :nodoc:
2626
class_option :dev, type: :boolean, default: false,
2727
desc: "For applications pointing to a local Rails checkout"
2828

29+
source_paths << File.expand_path(File.join(base_name, "app", "templates"), base_root)
30+
2931
def create_devcontainer
3032
empty_directory ".devcontainer"
3133

@@ -41,7 +43,22 @@ def update_application_system_test_case
4143
gsub_file("test/application_system_test_case.rb", /^(\s*driven_by\b.*)/, system_test_configuration)
4244
end
4345

46+
def update_database_yml
47+
# Only postgresql has devcontainer specific configuration, so only update database.yml if we are using postgres
48+
return unless options[:database] == "postgresql"
49+
50+
template("config/databases/#{options[:database]}.yml", "config/database.yml")
51+
end
52+
4453
private
54+
def devcontainer?
55+
true
56+
end
57+
58+
def app_name
59+
options[:app_name]
60+
end
61+
4562
def dependencies
4663
return @dependencies if @dependencies
4764

railties/test/generators/db_system_change_generator_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class ChangeGeneratorTest < Rails::Generators::TestCase
3838
assert_file("config/database.yml") do |content|
3939
assert_match "adapter: postgresql", content
4040
assert_match "database: tmp_production", content
41+
assert_match "host: <%= ENV[\"DB_HOST\"] %>", content
4142
end
4243

4344
assert_file("Gemfile") do |content|

0 commit comments

Comments
 (0)