Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions .github/workflows/database-e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,22 @@ HANAMI_DB_GEM=$(grep '^gem "hanami-db"' "$GITHUB_WORKSPACE/Gemfile")
sed -i "s|^\([[:space:]]*\)gem \"hanami-db\".*|\1$HANAMI_DB_GEM|" testapp/Gemfile
grep -qF "$HANAMI_DB_GEM" testapp/Gemfile

# `hanami new` should choose the JDBC driver gem and URL when run on JRuby
if [ "$JRUBY" = "true" ]; then
case "$E2E_DATABASE_FLAG" in
sqlite)
original_gem="sqlite3"
jdbc_gem="jdbc-sqlite3"
;;
postgres)
original_gem="pg"
jdbc_gem="jdbc-postgres"
;;
mysql)
original_gem="mysql2"
jdbc_gem="jdbc-mysql"
;;
esac

sed -i "s#^gem \"${original_gem}\"#gem \"${jdbc_gem}\"#" testapp/Gemfile
grep -q "gem \"${jdbc_gem}\"" testapp/Gemfile
grep -q "^gem \"${jdbc_gem}\"" testapp/Gemfile
grep -q "^DATABASE_URL=jdbc:" testapp/.env
fi

cd testapp
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ and this project adheres to [Break Versioning](https://www.taoensso.com/break-ve
- Decode percent-encoded DB user and password for CLI tools (@aaronmallen #457)
- `hanami generate slice` no longer generates view files (`view.rb`, `views/helpers.rb`, `views/context.rb` and the templates directory) when hanami-view is not bundled, such as in apps generated with `hanami new --skip-view`. (@aaronmallen in #443)
- Fix a typo in a comment in the `config/assets.js` generated by `hanami new`. (@oleksify in #439)
- Generate apps that work on JRuby when `hanami new` runs on JRuby. The Gemfile uses the JDBC driver gem (`jdbc-sqlite3`, `jdbc-postgres` or `jdbc-mysql`) in place of the C extension gem, `.env` sets a `jdbc:` `DATABASE_URL`, and `config/puma.rb` leaves out the cluster mode settings, since JRuby can't fork workers. Previously the generated app failed at `bundle install`. (@timriley in #483)
- `bin/setup` generated by `hanami new --skip-assets` no longer runs `npm install`. (@timriley in #483)

### Security

Expand Down
25 changes: 22 additions & 3 deletions lib/hanami/cli/generators/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,37 @@ def generate_mysql?
generate_db? && database_option == Commands::Gem::New::DATABASE_MYSQL
end

# JRuby connects through JDBC, so it needs a "jdbc:" URL. JDBC drivers take the user as a
# query param rather than userinfo.
def database_url
if generate_sqlite?
"sqlite://db/#{app}.sqlite"
jruby? ? "jdbc:sqlite:db/#{app}.sqlite" : "sqlite://db/#{app}.sqlite"
elsif generate_postgres?
"postgres://localhost/#{app}"
jruby? ? "jdbc:postgresql://localhost/#{app}" : "postgres://localhost/#{app}"
elsif generate_mysql?
"mysql2://root@localhost/#{app}"
jruby? ? "jdbc:mysql://localhost/#{app}?user=root" : "mysql2://root@localhost/#{app}"
else
raise "Unknown database option: #{database_option}"
end
end

# JRuby can't install the C extension database gems, so it uses the JDBC driver gems.
def database_gem
if generate_sqlite?
jruby? ? "jdbc-sqlite3" : "sqlite3"
elsif generate_postgres?
jruby? ? "jdbc-postgres" : "pg"
elsif generate_mysql?
jruby? ? "jdbc-mysql" : "mysql2"
else
raise "Unknown database option: #{database_option}"
end
end

def jruby?
RUBY_ENGINE == "jruby"
end

def bundled_views?
Hanami.bundled?("hanami-view")
end
Expand Down
8 changes: 2 additions & 6 deletions lib/hanami/cli/generators/gem/app/gemfile.erb
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,8 @@ gem "dry-validation", "~> 1.11"
gem "i18n", "~> 1.14"
gem "puma", ">= 7.1"
gem "rake"
<%- if generate_sqlite? -%>
gem "sqlite3"
<%- elsif generate_postgres? -%>
gem "pg"
<%- elsif generate_mysql? -%>
gem "mysql2"
<%- if generate_db? -%>
gem "<%= database_gem %>"
<%- end -%>
<%- if generate_view? && template_engine == "haml" -%>
gem "haml"
Expand Down
6 changes: 6 additions & 0 deletions lib/hanami/cli/generators/gem/app/puma.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ max_threads_count = ENV.fetch("HANAMI_MAX_THREADS", 5)
min_threads_count = ENV.fetch("HANAMI_MIN_THREADS") { max_threads_count }

threads min_threads_count, max_threads_count
<%- if jruby? -%>

# Puma runs a single process on JRuby, since JRuby can't fork workers. JRuby threads run in
# parallel, so to handle more requests, raise HANAMI_MAX_THREADS instead.
<%- else -%>

#
# Workers (aka Puma/Ruby processes)
Expand Down Expand Up @@ -42,3 +47,4 @@ if puma_cluster_mode
Hanami.shutdown
end
end
<%- end -%>
2 changes: 2 additions & 0 deletions lib/hanami/cli/generators/gem/app/setup.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ announce() {
announce "Running bundle install..."
bundle check || bundle install

<%- if generate_assets? -%>
announce "\nRunning npm install..."
npm install

<%- end -%>
<%- if generate_db? -%>
announce "\nPreparing the database..."
hanami db prepare
Expand Down
77 changes: 77 additions & 0 deletions spec/unit/hanami/cli/commands/gem/new_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@
before do
allow(bundler).to receive(:bundle).with(a_string_starting_with("binstubs"))
.and_return(successful_system_call_result)

# The generated files differ on JRuby. Expect the CRuby files unless an example says otherwise,
# so these specs pass on either Ruby.
#
# Stub the constant within Context only, which its own lookup finds first. Stubbing the global
# constant would break other code, such as JRuby loading its native extensions.
stub_const("Hanami::CLI::Generators::Context::RUBY_ENGINE", "ruby")
end

it "normalizes app name" do
Expand Down Expand Up @@ -1433,6 +1440,9 @@ module Types
# Procfile.dev
expect(fs.read("Procfile.dev")).to_not match(/hanami assets watch/)

# bin/setup
expect(fs.read("bin/setup")).to_not match(/npm/)

# config/assets.js
expect(fs.exist?("config/assets.js")).to be(false)

Expand Down Expand Up @@ -1751,6 +1761,73 @@ class App < Hanami::App
end
end

describe "on JRuby" do
before do
stub_const("Hanami::CLI::Generators::Context::RUBY_ENGINE", "jruby")
end

it "generates app with JDBC gem and URL for --database=sqlite" do
subject.call(app: app, database: "sqlite")

fs.chdir(app) do
expect(fs.read("Gemfile")).to include(%(gem "jdbc-sqlite3"))
expect(fs.read("Gemfile")).to_not include(%(gem "sqlite3"))
expect(fs.read(".env")).to include("DATABASE_URL=jdbc:sqlite:db/#{app}.sqlite\n")
expect(fs.read(".gitignore")).to include("db/*.sqlite*")
expect(fs.exist?("db/.keep")).to be(true)
end
end

it "generates app with JDBC gem and URL for --database=postgres" do
subject.call(app: app, database: "postgres")

fs.chdir(app) do
expect(fs.read("Gemfile")).to include(%(gem "jdbc-postgres"))
expect(fs.read("Gemfile")).to_not include(%(gem "pg"))
expect(fs.read(".env")).to include("DATABASE_URL=jdbc:postgresql://localhost/#{app}\n")
end
end

it "generates app with JDBC gem and URL for --database=mysql" do
subject.call(app: app, database: "mysql")

fs.chdir(app) do
expect(fs.read("Gemfile")).to include(%(gem "jdbc-mysql"))
expect(fs.read("Gemfile")).to_not include(%(gem "mysql2"))
expect(fs.read(".env")).to include("DATABASE_URL=jdbc:mysql://localhost/#{app}?user=root\n")
end
end

it "generates puma config without workers" do
subject.call(app: app)

puma = <<~EXPECTED
# frozen_string_literal: true

#
# Environment and port
#
port ENV.fetch("HANAMI_PORT", 2300)
environment ENV.fetch("HANAMI_ENV", "development")

#
# Threads within each Puma/Ruby process (aka worker)
#

# Configure the minimum and maximum number of threads to use to answer requests.
max_threads_count = ENV.fetch("HANAMI_MAX_THREADS", 5)
min_threads_count = ENV.fetch("HANAMI_MIN_THREADS") { max_threads_count }

threads min_threads_count, max_threads_count

# Puma runs a single process on JRuby, since JRuby can't fork workers. JRuby threads run in
# parallel, so to handle more requests, raise HANAMI_MAX_THREADS instead.
EXPECTED

expect(fs.read("#{app}/config/puma.rb")).to eq(puma)
end
end

it "doesn't generate for unsupported database" do
expect {
subject.call(app: app, database: "foodb")
Expand Down