diff --git a/lib/hanami/cli/generators/gem/app.rb b/lib/hanami/cli/generators/gem/app.rb index 29aaa501..757d2fe2 100644 --- a/lib/hanami/cli/generators/gem/app.rb +++ b/lib/hanami/cli/generators/gem/app.rb @@ -102,6 +102,10 @@ def generate_app(app, context) # rubocop:disable Metrics/AbcSize if context.generate_sqlite? fs.create("db/.keep", t("keep.erb", context)) end + + if context.generate_postgres? || context.generate_mysql? + fs.create("compose.yml", t("compose.yml.erb", context)) + end end fs.create("app/operation.rb", t("operation.erb", context)) diff --git a/lib/hanami/cli/generators/gem/app/compose.yml.erb b/lib/hanami/cli/generators/gem/app/compose.yml.erb new file mode 100644 index 00000000..b31728c2 --- /dev/null +++ b/lib/hanami/cli/generators/gem/app/compose.yml.erb @@ -0,0 +1,28 @@ +services: +<%- if generate_postgres? -%> + postgres: + image: postgres:latest + ports: + - 5432:5432 + environment: + POSTGRES_DB: <%= app %> + POSTGRES_HOST_AUTH_METHOD: trust + volumes: + - postgres_data:/var/lib/postgresql/data + +volumes: + postgres_data: +<%- elsif generate_mysql? -%> + mysql: + image: mysql:latest + ports: + - 3306:3306 + environment: + MYSQL_DATABASE: <%= app %> + MYSQL_ALLOW_EMPTY_PASSWORD: "yes" + volumes: + - mysql_data:/var/lib/mysql + +volumes: + mysql_data: +<%- end -%> diff --git a/spec/unit/hanami/cli/commands/gem/new_spec.rb b/spec/unit/hanami/cli/commands/gem/new_spec.rb index 77c41540..85f01ac4 100644 --- a/spec/unit/hanami/cli/commands/gem/new_spec.rb +++ b/spec/unit/hanami/cli/commands/gem/new_spec.rb @@ -1211,6 +1211,7 @@ module Types expect(fs.exist?("config/db/")).to be(false) expect(fs.read(".gitignore")).to_not include("db/*.sqlite") expect(fs.exist?("db/")).to be(false) + expect(fs.exist?("compose.yml")).to be(false) # bin/setup bin_setup = <<~EXPECTED @@ -1491,6 +1492,7 @@ class App < Hanami::App expect(fs.read(".env")).to include("DATABASE_URL=sqlite://db/#{app}.sqlite") expect(fs.read(".gitignore")).to include("db/*.sqlite") expect(fs.exist?("db/.keep")).to be(true) + expect(fs.exist?("compose.yml")).to be(false) end end @@ -1503,6 +1505,7 @@ class App < Hanami::App expect(fs.read(".env")).to include("DATABASE_URL=sqlite://db/#{app}.sqlite") expect(fs.read(".gitignore")).to include("db/*.sqlite") expect(fs.exist?("db/.keep")).to be(true) + expect(fs.exist?("compose.yml")).to be(false) end end end @@ -1517,6 +1520,8 @@ class App < Hanami::App expect(fs.read(".env")).to include("DATABASE_URL=postgres://localhost/#{app}") expect(fs.read(".gitignore")).to_not include("db/*.sqlite") expect(fs.exist?("db/")).to be(false) + expect(fs.read("compose.yml")).to include("postgres:") + expect(fs.read("compose.yml")).to include("POSTGRES_DB: #{app}") end end @@ -1529,6 +1534,8 @@ class App < Hanami::App expect(fs.read(".env")).to include("DATABASE_URL=postgres://localhost/#{app}") expect(fs.read(".gitignore")).to_not include("db/*.sqlite") expect(fs.exist?("db/")).to be(false) + expect(fs.read("compose.yml")).to include("postgres:") + expect(fs.read("compose.yml")).to include("POSTGRES_DB: #{app}") end end @@ -1541,6 +1548,8 @@ class App < Hanami::App expect(fs.read(".env")).to include("DATABASE_URL=postgres://localhost/#{app}") expect(fs.read(".gitignore")).to_not include("db/*.sqlite") expect(fs.exist?("db/")).to be(false) + expect(fs.read("compose.yml")).to include("postgres:") + expect(fs.read("compose.yml")).to include("POSTGRES_DB: #{app}") end end end @@ -1555,6 +1564,8 @@ class App < Hanami::App expect(fs.read(".env")).to include("DATABASE_URL=mysql2://root@localhost/#{app}") expect(fs.read(".gitignore")).to_not include("db/*.sqlite") expect(fs.exist?("db/")).to be(false) + expect(fs.read("compose.yml")).to include("mysql:") + expect(fs.read("compose.yml")).to include("MYSQL_DATABASE: #{app}") end end @@ -1567,6 +1578,8 @@ class App < Hanami::App expect(fs.read(".env")).to include("DATABASE_URL=mysql2://root@localhost/#{app}") expect(fs.read(".gitignore")).to_not include("db/*.sqlite") expect(fs.exist?("db/")).to be(false) + expect(fs.read("compose.yml")).to include("mysql:") + expect(fs.read("compose.yml")).to include("MYSQL_DATABASE: #{app}") end end end