Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Running bin/setup fails after generating a new app #1224

Closed
stevepolitodesign opened this issue Aug 16, 2024 · 5 comments · Fixed by #1242
Closed

Running bin/setup fails after generating a new app #1224

stevepolitodesign opened this issue Aug 16, 2024 · 5 comments · Fixed by #1242
Labels

Comments

@stevepolitodesign
Copy link
Contributor

When I run bin/setup in a newly generated app, I get the following error:

       rails  db:prepare
Created database 'fail_development'
Created database 'fail_test'

Congratulations! You just pulled our suspenders.

~/Sites via 💎 v3.3.0 on ☁️    took 3m59s
❯ v

~/Sites via 💎 v3.3.0 on ☁️    took 2s
❯ cd fail

fail on  main [?] via  v22.1.0 via 💎 v3.3.0 on ☁️
❯ bin/setup
== Installing dependencies ==
The Gemfile's dependencies are satisfied
yarn install v1.22.17
[1/4] 🔍  Resolving packages...
[2/4] 🚚  Fetching packages...
[3/4] 🔗  Linking dependencies...
warning " > @thoughtbot/[email protected]" has unmet peer dependency "jest@>= 25.0.0".
warning " > @thoughtbot/[email protected]" has unmet peer dependency "typescript@>= 4.0.0".
warning "@thoughtbot/eslint-config > @typescript-eslint/eslint-plugin > [email protected]" has unmet peer dependency "typescript@>=2.8.0 || >= 3.2.0-dev || >= 3.3.0-dev || >= 3.4.0-dev || >= 3.5.0-dev || >= 3.6.0-dev || >= 3.6.0-beta || >= 3.7.0-dev || >= 3.7.0-beta".
[4/4] 🔨  Building fresh packages...
✨  Done in 0.88s.

== Preparing database and adding development seed data ==
Database 'fail_development' already exists
Database 'fail_test' already exists
/Users/polito/Sites/fail/db/schema.rb doesn't exist yet. Run `bin/rails db:migrate` to create it, then try again. If you do not intend to use a database, you should instead alter /Users/polito/Sites/fail/config/application.rb to limit the frameworks that will be loaded.
bin/setup:9:in `system': Command failed with exit 1: bin/rails (RuntimeError)
        from bin/setup:9:in `system!'
        from bin/setup:24:in `block in <main>'
        from /Users/polito/.asdf/installs/ruby/3.3.0/lib/ruby/3.3.0/fileutils.rb:240:in `chdir'
        from /Users/polito/.asdf/installs/ruby/3.3.0/lib/ruby/3.3.0/fileutils.rb:240:in `cd'
        from bin/setup:16:in `<main>'

About your application's environment
Rails version             7.2.0
Ruby version              ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [arm64-darwin23]
RubyGems version          3.5.6
Rack version              3.1.7
Middleware                ActionDispatch::HostAuthorization, Rack::Sendfile, ActionDispatch::Static, ActionDispatch::Executor, ActionDispatch::ServerTiming, ActiveSupport::Cache::Strategy::LocalCache::Middleware, Rack::Runtime, Rack::MethodOverride, ActionDispatch::RequestId, ActionDispatch::RemoteIp, Sprockets::Rails::QuietAssets, Rails::Rack::Logger, ActionDispatch::ShowExceptions, WebConsole::Middleware, ActionDispatch::DebugExceptions, ActionDispatch::ActionableExceptions, ActionDispatch::Reloader, ActionDispatch::Callbacks, ActiveRecord::Migration::CheckPending, ActionDispatch::Cookies, ActionDispatch::Session::CookieStore, ActionDispatch::Flash, ActionDispatch::ContentSecurityPolicy::Middleware, ActionDispatch::PermissionsPolicy::Middleware, Rack::Head, Rack::ConditionalGet, Rack::ETag, Rack::TempfileReaper
Application root          /Users/polito/Sites/fail
Environment               development
Database adapter          postgresql
Database schema version   0
@stevepolitodesign
Copy link
Contributor Author

stevepolitodesign commented Aug 16, 2024

Our application template invokes bin/rails db:prepare, which is what the Rails does in its bin/setup script.

Even invoking bin/rails db:prepare again within the newly generated application has no affect.

Running bin/rails db:migrate did the trick.

@stevepolitodesign
Copy link
Contributor Author

I wonder if we need to remove rails_command "db:prepare" from the application template and instead add it to our custom setup script?

@stevepolitodesign stevepolitodesign changed the title Running bin/setup files after generating a new app Running bin/setup fails after generating a new app Aug 16, 2024
@stevepolitodesign
Copy link
Contributor Author

stevepolitodesign commented Oct 8, 2024

For context, this is what bin/setup looks like for a new Rails 7 application.

#!/usr/bin/env ruby
require "fileutils"

APP_ROOT = File.expand_path("..", __dir__)
APP_NAME = "setup-script"

def system!(*args)
  system(*args, exception: true)
end

FileUtils.chdir APP_ROOT do
  # This script is a way to set up or update your development environment automatically.
  # This script is idempotent, so that you can run it at any time and get an expectable outcome.
  # Add necessary setup steps to this file.

  puts "== Installing dependencies =="
  system! "gem install bundler --conservative"
  system("bundle check") || system!("bundle install")

  # puts "\n== Copying sample files =="
  # unless File.exist?("config/database.yml")
  #   FileUtils.cp "config/database.yml.sample", "config/database.yml"
  # end

  puts "\n== Preparing database =="
  system! "bin/rails db:prepare"

  puts "\n== Removing old logs and tempfiles =="
  system! "bin/rails log:clear tmp:clear"

  puts "\n== Restarting application server =="
  system! "bin/rails restart"

  # puts "\n== Configuring puma-dev =="
  # system "ln -nfs #{APP_ROOT} ~/.puma-dev/#{APP_NAME}"
  # system "curl -Is https://#{APP_NAME}.test/up | head -n 1"
end

For context, this is what bin/setup looks like for a new Rails 8 application.

#!/usr/bin/env ruby
require "fileutils"

APP_ROOT = File.expand_path("..", __dir__)

def system!(*args)
  system(*args, exception: true)
end

FileUtils.chdir APP_ROOT do
  # This script is a way to set up or update your development environment automatically.
  # This script is idempotent, so that you can run it at any time and get an expectable outcome.
  # Add necessary setup steps to this file.

  puts "== Installing dependencies =="
  system("bundle check") || system!("bundle install")

  # puts "\n== Copying sample files =="
  # unless File.exist?("config/database.yml")
  #   FileUtils.cp "config/database.yml.sample", "config/database.yml"
  # end

  puts "\n== Preparing database =="
  system! "bin/rails db:prepare"

  puts "\n== Removing old logs and tempfiles =="
  system! "bin/rails log:clear tmp:clear"

  unless ARGV.include?("--skip-server")
    puts "\n== Starting development server =="
    STDOUT.flush # flush the output before exec(2) so that it displays
    exec "bin/dev"
  end
end

@stevepolitodesign
Copy link
Contributor Author

Removing rails_command "db:prepare" from the application template did not work. Still got:

== Preparing database and adding development seed data ==
Created database 'setup_script_development'
Created database 'setup_script_test'
/Users/polito/Sites/playground/setup_script/db/schema.rb doesn't exist yet. Run `bin/rails db:migrate` to create it, then try again. If you do not intend to use a database, you should instead alter /Users/polito/Sites/playground/setup_script/config/application.rb to limit the frameworks that will be loaded.
bin/setup:9:in `system': Command failed with exit 1: bin/rails (RuntimeError)
	from bin/setup:9:in `system!'
	from bin/setup:24:in `block in <main>'
	from /Users/polito/.asdf/installs/ruby/3.3.5/lib/ruby/3.3.0/fileutils.rb:240:in `chdir'
	from /Users/polito/.asdf/installs/ruby/3.3.5/lib/ruby/3.3.0/fileutils.rb:240:in `cd'
	from bin/setup:16:in `<main>'

@stevepolitodesign
Copy link
Contributor Author

This worked locally:

diff --git a/lib/install/web.rb b/lib/install/web.rb
index 6f7d4fd..80a7e19 100644
--- a/lib/install/web.rb
+++ b/lib/install/web.rb
@@ -37,7 +37,7 @@ def apply_template!
       run "bundle install"
 
       generate "suspenders:install:web"
-      rails_command "db:prepare"
+      rails_command "db:migrate"
 
       say "\nCongratulations! You just pulled our suspenders."
     end

stevepolitodesign added a commit that referenced this issue Dec 5, 2024
Closes #1224

It's unclear why we need to run migrations, since we were originally
calling [db:prepare][dbp] which does the following:

> If the database exists but the tables have not been created, the
command will load the schema, run any pending migrations, dump the
updated schema, and finally load the seed data. See the Seeding Data
documentation for more details.

However, I found that calling `db:migrate` in the application template
worked.

[dbp]: https://guides.rubyonrails.org/active_record_migrations.html#preparing-the-database
stevepolitodesign added a commit that referenced this issue Dec 5, 2024
Closes #1224

It's unclear why we need to run migrations, since we were originally
calling [db:prepare][dbp] which does the following:

> If the database exists but the tables have not been created, the
command will load the schema, run any pending migrations, dump the
updated schema, and finally load the seed data. See the Seeding Data
documentation for more details.

However, I found that calling `db:migrate` in the application template
worked.

[dbp]: https://guides.rubyonrails.org/active_record_migrations.html#preparing-the-database
stevepolitodesign added a commit that referenced this issue Dec 5, 2024
Closes #1224

It's unclear why we need to run migrations, since we're already calling
[db:prepare][dbp] which does the following:

> If the database exists but the tables have not been created, the
command will load the schema, run any pending migrations, dump the
updated schema, and finally load the seed data. See the Seeding Data
documentation for more details.

However, I found that calling `db:migrate` in the application template
worked.

[dbp]: https://guides.rubyonrails.org/active_record_migrations.html#preparing-the-database
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant