lib/generators/dockerfile_generator.rb in dockerfile-rails-1.6.18 vs lib/generators/dockerfile_generator.rb in dockerfile-rails-1.6.19
- old
+ new
@@ -329,10 +329,12 @@
template "docker-entrypoint.erb", "bin/docker-entrypoint"
chmod "bin/docker-entrypoint", 0755 & ~File.umask, verbose: false
template "docker-compose.yml.erb", "docker-compose.yml" if options.compose
+ template "database.yml.erb", "config/database.yml" if fix_database_config
+
if using_litefs?
template "litefs.yml.erb", "config/litefs.yml"
fly_attach_consul
end
@@ -1447,7 +1449,34 @@
toml += "[[statics]]\n guest_path = \"#{workdir}/public\"\n url_prefix = \"/\"\n\n"
end
end
toml
+ end
+
+ # if there are multiple production databases defined, allow them all to be
+ # configured via DATABASE_URL.
+ def fix_database_config
+ yaml = IO.read("config/database.yml")
+
+ production = YAML.load(yaml, aliases: true)["production"]
+ return unless production.is_a?(Hash) && production.values.all?(Hash)
+ return if production.keys == [ "primary" ]
+
+ section = yaml[/^(production:.*?)(^\S|\z)/m, 1]
+
+ replacement = section.gsub(/( ).*?\n((\1\s+).*?\n)*/) do |subsection|
+ spaces = $3
+ name = subsection[/\w+/]
+
+ if /^ +url:/.match?(subsection)
+ subsection
+ elsif name == "primary"
+ subsection + spaces + %(url: <%= ENV["DATABASE_URL"] %>\n)
+ else
+ subsection + spaces + %(url: <%= URI.parse(ENV["DATABASE_URL"]).tap { |url| url.path += "_#{name}" } if ENV["DATABASE_URL"] %>\n)
+ end
+ end
+
+ yaml.sub(section, replacement)
end
end