Skip to content

Commit 22c3308

Browse files
justin808claude
andcommitted
Address code review feedback for DatabaseChecker
- Refactor instance variables to result object pattern for thread safety - database_ready? now returns a Hash with :ready, :error_type, :error_message - print_database_failed now takes error_type and error_message as parameters - This makes the code thread-safe and improves maintainability - Add RBS type signatures (required by project standards) - Created sig/react_on_rails/dev/database_checker.rbs - Added to Steepfile (alphabetically ordered) - Validated with bundle exec rake rbs:validate - Add test case for DEBUG=false scenario - Verifies no warning is output when DEBUG is not enabled 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent a637826 commit 22c3308

File tree

4 files changed

+34
-15
lines changed

4 files changed

+34
-15
lines changed

react_on_rails/Steepfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ target :lib do
2828
check "lib/react_on_rails.rb"
2929
check "lib/react_on_rails/configuration.rb"
3030
check "lib/react_on_rails/controller.rb"
31+
check "lib/react_on_rails/dev/database_checker.rb"
3132
check "lib/react_on_rails/dev/file_manager.rb"
3233
check "lib/react_on_rails/dev/pack_generator.rb"
3334
check "lib/react_on_rails/dev/process_manager.rb"

react_on_rails/lib/react_on_rails/dev/database_checker.rb

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,38 +30,35 @@ def rails_available?
3030
def check_and_report_database
3131
print_check_header
3232

33-
if database_ready?
33+
result = database_ready?
34+
if result[:ready]
3435
print_database_ok
3536
true
3637
else
37-
print_database_failed
38+
print_database_failed(result[:error_type], result[:error_message])
3839
false
3940
end
4041
end
4142

4243
def database_ready?
4344
# Try to establish connection and run a simple query
4445
ActiveRecord::Base.connection.execute("SELECT 1")
45-
true
46+
{ ready: true }
4647
rescue ActiveRecord::NoDatabaseError
4748
# Database doesn't exist
48-
@error_type = :no_database
49-
false
49+
{ ready: false, error_type: :no_database }
5050
rescue ActiveRecord::PendingMigrationError
5151
# Database exists but migrations are pending
52-
@error_type = :pending_migrations
53-
false
52+
{ ready: false, error_type: :pending_migrations }
5453
rescue ActiveRecord::ConnectionNotEstablished,
5554
ActiveRecord::StatementInvalid => e
5655
# Connection failed or other database error
57-
@error_type = :connection_error
58-
@error_message = e.message
59-
false
56+
{ ready: false, error_type: :connection_error, error_message: e.message }
6057
rescue StandardError => e
6158
# Unexpected error - log but don't block startup
6259
# This allows apps without databases to still use bin/dev
6360
warn "Database check warning: #{e.message}" if ENV["DEBUG"]
64-
true
61+
{ ready: true }
6562
end
6663

6764
def print_check_header
@@ -76,13 +73,13 @@ def print_database_ok
7673
end
7774

7875
# rubocop:disable Metrics/AbcSize
79-
def print_database_failed
76+
def print_database_failed(error_type, error_message)
8077
puts " #{Rainbow('✗').red} Database is not ready"
8178
puts ""
8279
puts Rainbow("❌ Database not set up!").red.bold
8380
puts ""
8481

85-
case @error_type
82+
case error_type
8683
when :no_database
8784
puts Rainbow("The database does not exist.").yellow
8885
puts ""
@@ -96,9 +93,9 @@ def print_database_failed
9693
puts " #{Rainbow('bin/rails db:migrate').green} # Run pending migrations"
9794
when :connection_error
9895
puts Rainbow("Could not connect to the database.").yellow
99-
if @error_message
96+
if error_message
10097
puts ""
101-
puts Rainbow("Error: #{@error_message}").red
98+
puts Rainbow("Error: #{error_message}").red
10299
end
103100
puts ""
104101
puts Rainbow("Possible solutions:").cyan.bold
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module ReactOnRails
2+
module Dev
3+
class DatabaseChecker
4+
def self.check_database: () -> bool
5+
6+
private
7+
8+
def self.rails_available?: () -> bool
9+
def self.check_and_report_database: () -> bool
10+
def self.database_ready?: () -> Hash[Symbol, untyped]
11+
def self.print_check_header: () -> void
12+
def self.print_database_ok: () -> void
13+
def self.print_database_failed: (Symbol?, String?) -> void
14+
end
15+
end
16+
end

react_on_rails/spec/react_on_rails/dev/database_checker_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ def self.connection; end
140140
allow(ENV).to receive(:[]).with("DEBUG").and_return("true")
141141
expect { described_class.check_database }.to output(/Database check warning/).to_stderr
142142
end
143+
144+
it "does not output a warning when DEBUG is not enabled" do
145+
allow(ENV).to receive(:[]).with("DEBUG").and_return(nil)
146+
expect { described_class.check_database }.not_to output(/Database check warning/).to_stderr
147+
end
143148
end
144149
end
145150
end

0 commit comments

Comments
 (0)