Skip to content

Commit cc5b688

Browse files
committed
Enable MYSQL_CODESPACES environment variable in the devcontainer
This commit addresses the following error because it creates database as a root user without a password. In the devcontainer, the root password is set to `root` in the `docker-compose.yaml` file. https://github.com/rails/rails/blob/78de0fae2289e47738441549aa80451875a8b4bc/.devcontainer/compose.yaml#L37-L38 ```yaml environment: MARIADB_ROOT_PASSWORD: root ``` - Error addressed by this commit: ``` $ cd railties $ bin/test test/application/test_runner_test.rb -n test_parallel_testing_when_schema_is_not_up_to_date Run options: -n test_parallel_testing_when_schema_is_not_up_to_date --seed 56426 E Error: ApplicationTests::TestRunnerTest#test_parallel_testing_when_schema_is_not_up_to_date: RuntimeError: rails command failed (1): bin/rails db:create 2>&1 /home/vscode/.rbenv/versions/3.4.2/lib/ruby/gems/3.4.0/gems/rake-13.2.1/lib/rake/application.rb:717: warning: conflicting chdir during another chdir block /workspaces/rails/railties/test/application/test_runner_test.rb:828: note: previous chdir was here /home/vscode/.rbenv/versions/3.4.2/lib/ruby/gems/3.4.0/gems/rake-13.2.1/lib/rake/application.rb:738: warning: conflicting chdir during another chdir block /workspaces/rails/railties/test/application/test_runner_test.rb:828: note: previous chdir was here There is an issue connecting to your database with your username/password, username: root. Please check your database configuration to ensure the username/password are valid. Couldn't create 'railties_5059_development' database. Please check your configuration. bin/rails aborted! ActiveRecord::DatabaseConnectionError: There is an issue connecting to your database with your username/password, username: root. (ActiveRecord::DatabaseConnectionError) Please check your database configuration to ensure the username/password are valid. /workspaces/rails/tmp/d20250305-5059-hve3ds/app/bin/rails:4:in '<top (required)>' Caused by: Mysql2::Error::ConnectionError: Access denied for user 'root'@'172.18.0.6' (using password: NO) (Mysql2::Error::ConnectionError) /workspaces/rails/tmp/d20250305-5059-hve3ds/app/bin/rails:4:in '<top (required)>' Tasks: TOP => db:create (See full trace by running task with --trace) test/isolation/abstract_unit.rb:420:in 'TestHelpers::Generation#rails' test/application/test_runner_test.rb:834:in 'block in ApplicationTests::TestRunnerTest#test_parallel_testing_when_schema_is_not_up_to_date' test/application/test_runner_test.rb:828:in 'Dir.chdir' test/application/test_runner_test.rb:828:in 'ApplicationTests::TestRunnerTest#test_parallel_testing_when_schema_is_not_up_to_date' bin/test test/application/test_runner_test.rb:826 Finished in 4.847434s, 0.2063 runs/s, 0.0000 assertions/s. 1 runs, 0 assertions, 0 failures, 1 errors, 0 skips $ ``` No need to set MYSQL_CODESPACES explicitly in the boot.sh script that allows users to run `exec rake db:mysql:rebuild` in the devcontainer without setting MYSQL_CODESPACES in the command line.
1 parent 7b8ea0b commit cc5b688

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

.devcontainer/boot.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ cd activerecord || { echo "activerecord directory doesn't exist"; exit; }
1414
bundle exec rake db:postgresql:rebuild
1515

1616
# Create MySQL databases
17-
MYSQL_CODESPACES=1 bundle exec rake db:mysql:rebuild
17+
bundle exec rake db:mysql:rebuild

.devcontainer/compose.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ services:
1616
- redis
1717
- memcached
1818

19+
environment:
20+
MYSQL_CODESPACES: "1"
21+
1922
# Use "forwardPorts" in **devcontainer.json** to forward an app port locally.
2023
# (Adding the "ports" property to this file will not forward from a Codespace.)
2124

railties/test/isolation/abstract_unit.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,9 @@ def use_mysql2(multi_db: false)
539539
adapter: mysql2
540540
pool: 5
541541
username: root
542+
<% if ENV['MYSQL_CODESPACES'] %>
543+
password: 'root'
544+
<% end %>
542545
<% if ENV['MYSQL_HOST'] %>
543546
host: <%= ENV['MYSQL_HOST'] %>
544547
<% end %>
@@ -562,6 +565,9 @@ def use_mysql2(multi_db: false)
562565
adapter: mysql2
563566
pool: 5
564567
username: root
568+
<% if ENV['MYSQL_CODESPACES'] %>
569+
password: 'root'
570+
<% end %>
565571
<% if ENV['MYSQL_HOST'] %>
566572
host: <%= ENV['MYSQL_HOST'] %>
567573
<% end %>

0 commit comments

Comments
 (0)