Today I configured a new Rails app with Scripts to Rule Them All. It’s a “normalized script pattern that GitHub uses in its projects” to standardise projects and simplify on-boarding. It’s probably premature for an app I’m working on solo… but if you can’t experiment on side-apps, when can you? 😜
I ran into a catch-22 where, the first time you setup the repository, the setup
script tries to initialise the database, which relies on the database server being available. The support
script that starts the database relies on the setup
script (or more specifically, the bootstrap
script it calls) already having been run to install the database server. But the setup
script relies on the support
script… etc, etc
Rather than get fancy I decided to let the developer deal with it. The setup
script is idempotent so there’s no harm in running most of it the first time and then all of it the second time. After DuckDuckGoing how to check if Postgres is running and how to write if/else statements in shell scripts I settled on this clause in the setup
script:
if pg_isready; then
echo "==> Setting up database…"
bin/rails db:create db:reset
else
echo "==> Postgres is not running (this is normal on first setup)."
echo "==> Launch ‘script/support’ in another console and then try again"
exit 1
fi
echo "==> App is now ready to go!"