Skip to content

Commit

Permalink
Merge pull request #51749 from Shopify/devcontainer-use-sqlite-feature
Browse files Browse the repository at this point in the history
SQLite3 feature for dev container
  • Loading branch information
rafaelfranca committed May 7, 2024
2 parents 59064f8 + 6e0b867 commit 6c73ee0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
20 changes: 14 additions & 6 deletions railties/lib/rails/generators/devcontainer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
module Rails
module Generators
module Devcontainer
DB_FEATURES = {
"mysql" => "ghcr.io/rails/devcontainer/features/mysql-client",
"postgresql" => "ghcr.io/rails/devcontainer/features/postgres-client",
"sqlite3" => "ghcr.io/rails/devcontainer/features/sqlite3"
}

private
def devcontainer_dependencies
return @devcontainer_dependencies if @devcontainer_dependencies
Expand Down Expand Up @@ -124,6 +130,7 @@ def db_service_for_devcontainer(database = options[:database])

def db_feature_for_devcontainer(database = options[:database])
case database
when "sqlite3" then sqlite3_feature
when "mysql" then mysql_feature
when "postgresql" then postgres_feature
end
Expand Down Expand Up @@ -178,18 +185,19 @@ def db_service_names
end

def mysql_feature
{ "ghcr.io/rails/devcontainer/features/mysql-client" => {} }
{ DB_FEATURES["mysql"] => {} }
end

def postgres_feature
{ "ghcr.io/rails/devcontainer/features/postgres-client" => {} }
{ DB_FEATURES["postgresql"] => {} }
end

def sqlite3_feature
{ DB_FEATURES["sqlite3"] => {} }
end

def db_features
[
"ghcr.io/rails/devcontainer/features/mysql-client",
"ghcr.io/rails/devcontainer/features/postgres-client"
]
@db_features ||= DB_FEATURES.values
end

def local_rails_mount
Expand Down
1 change: 1 addition & 0 deletions railties/test/generators/app_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1287,6 +1287,7 @@ def test_devcontainer
assert_equal "selenium", content["containerEnv"]["SELENIUM_HOST"]
assert_equal({}, content["features"]["ghcr.io/rails/devcontainer/features/activestorage"])
assert_equal({}, content["features"]["ghcr.io/devcontainers/features/github-cli:1"])
assert_equal({}, content["features"]["ghcr.io/rails/devcontainer/features/sqlite3"])
assert_includes(content["forwardPorts"], 3000)
assert_includes(content["forwardPorts"], 6379)
end
Expand Down
27 changes: 14 additions & 13 deletions railties/test/generators/db_system_change_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ class ChangeGeneratorTest < Rails::Generators::TestCase
assert_match "curl libvips postgresql-client", content
end

assert_file(".devcontainer/devcontainer.json") do |content|
assert_match(/"DB_HOST": "postgres"/, content)
assert_match(/"ghcr.io\/rails\/devcontainer\/features\/postgres-client":/, content)
assert_devcontainer_json_file do |content|
assert_equal "postgres", content["containerEnv"]["DB_HOST"]
assert_includes content["features"].keys, "ghcr.io/rails/devcontainer/features/postgres-client"
assert_not_includes content["features"].keys, "ghcr.io/rails/devcontainer/features/sqlite"
end

assert_compose_file do |compose_config|
Expand Down Expand Up @@ -94,9 +95,9 @@ class ChangeGeneratorTest < Rails::Generators::TestCase
assert_match "curl default-mysql-client libvips", content
end

assert_file(".devcontainer/devcontainer.json") do |content|
assert_match(/"DB_HOST": "mysql"/, content)
assert_match(/"ghcr.io\/rails\/devcontainer\/features\/mysql-client":/, content)
assert_devcontainer_json_file do |content|
assert_equal "mysql", content["containerEnv"]["DB_HOST"]
assert_equal({}, content["features"]["ghcr.io/rails/devcontainer/features/mysql-client"])
end

assert_compose_file do |compose_config|
Expand Down Expand Up @@ -136,8 +137,8 @@ class ChangeGeneratorTest < Rails::Generators::TestCase
assert_match "curl libsqlite3-0 libvips", content
end

assert_file(".devcontainer/devcontainer.json") do |content|
assert_no_match(/"DB_HOST"/, content)
assert_devcontainer_json_file do |content|
assert_not_includes content["containerEnv"].keys, "DB_HOST"
end
end

Expand All @@ -160,8 +161,8 @@ class ChangeGeneratorTest < Rails::Generators::TestCase
assert_no_match "default-libmysqlclient-dev", content
end

assert_file(".devcontainer/devcontainer.json") do |content|
assert_match(/"DB_HOST": "mariadb"/, content)
assert_devcontainer_json_file do |content|
assert_match "mariadb", content["containerEnv"]["DB_HOST"]
end

assert_compose_file do |compose_config|
Expand Down Expand Up @@ -203,9 +204,9 @@ class ChangeGeneratorTest < Rails::Generators::TestCase
run_generator ["--to", "mysql"]
run_generator ["--to", "sqlite3", "--force"]

assert_file(".devcontainer/devcontainer.json") do |content|
assert_no_match(/"DB_HOST"/, content)
assert_no_match(/"ghcr.io\/rails\/devcontainer\/features\/mysql-client":/, content)
assert_devcontainer_json_file do |content|
assert_not_includes content["containerEnv"].keys, "DB_HOST"
assert_not_includes content["features"].keys, "ghcr.io\/rails\/devcontainer\/features\/mysql-client"
end

assert_compose_file do |compose_config|
Expand Down

0 comments on commit 6c73ee0

Please sign in to comment.