mysociety / alaveteli

Provide a Freedom of Information request system for your jurisdiction
https://alaveteli.org
Other
389 stars 195 forks source link

`docker/reset` fails after bundling #7511

Closed garethrees closed 1 year ago

garethrees commented 1 year ago

$ docker/reset

# …snip…

Bundle complete! 86 Gemfile dependencies, 202 gems now installed.
Bundled gems are installed into `/bundle/vendor`
1 installed gem you directly depend on is looking for funding.
  Run `bundle fund` for details
done
Switching to none... rails aborted!
ActiveRecord::ConnectionNotEstablished: could not translate host name "db" to address: Name or service not known
/alaveteli/app/models/concerns/admin_column.rb:37:in `content_columns_names'
/alaveteli/app/models/concerns/admin_column.rb:27:in `all_columns'
/alaveteli/app/models/concerns/admin_column.rb:22:in `prominenceable_admin_columns?'
/alaveteli/app/models/concerns/admin_column.rb:14:in `ordered_columns'
/alaveteli/app/models/concerns/admin_column.rb:9:in `admin_columns'
/alaveteli/app/models/info_request.rb:55:in `<class:InfoRequest>'
/alaveteli/app/models/info_request.rb:42:in `<top (required)>'
/alaveteli/app/models/alaveteli_pro/draft_info_request_batch.rb:17:in `<class:DraftInfoRequestBatch>'
/alaveteli/app/models/alaveteli_pro/draft_info_request_batch.rb:15:in `<top (required)>'
/alaveteli/spec/factories/alaveteli_pro/draft_info_request_batches.rb:16:in `block in <top (required)>'
/alaveteli/spec/factories/alaveteli_pro/draft_info_request_batches.rb:15:in `<top (required)>'
/alaveteli/config/environment.rb:5:in `<top (required)>'

Caused by:
PG::ConnectionBad: could not translate host name "db" to address: Name or service not known
/alaveteli/app/models/concerns/admin_column.rb:37:in `content_columns_names'
/alaveteli/app/models/concerns/admin_column.rb:27:in `all_columns'
/alaveteli/app/models/concerns/admin_column.rb:22:in `prominenceable_admin_columns?'
/alaveteli/app/models/concerns/admin_column.rb:14:in `ordered_columns'
/alaveteli/app/models/concerns/admin_column.rb:9:in `admin_columns'
/alaveteli/app/models/info_request.rb:55:in `<class:InfoRequest>'
/alaveteli/app/models/info_request.rb:42:in `<top (required)>'
/alaveteli/app/models/alaveteli_pro/draft_info_request_batch.rb:17:in `<class:DraftInfoRequestBatch>'
/alaveteli/app/models/alaveteli_pro/draft_info_request_batch.rb:15:in `<top (required)>'
/alaveteli/spec/factories/alaveteli_pro/draft_info_request_batches.rb:16:in `block in <top (required)>'
/alaveteli/spec/factories/alaveteli_pro/draft_info_request_batches.rb:15:in `<top (required)>'
/alaveteli/config/environment.rb:5:in `<top (required)>'
Tasks: TOP => environment
(See full trace by running task with --trace)
garethrees commented 1 year ago

On trying to run a console immediately after:

gareth: ~/src/mysociety/alaveteli (develop $=)
$ dc run --rm app bin/rails c
[+] Running 1/0
 ⠿ Container alaveteli-db-1  Created                                                                                       0.0s
[+] Running 1/1
 ⠿ Container alaveteli-db-1  Started                                                                                       0.2s
/bundle/vendor/ruby/3.0.0/gems/activerecord-6.1.7/lib/active_record/connection_adapters/postgresql_adapter.rb:83:in `rescue in new_client': could not translate host name "db" to address: Name or service not known (ActiveRecord::ConnectionNotEstablished)
        from /bundle/vendor/ruby/3.0.0/gems/activerecord-6.1.7/lib/active_record/connection_adapters/postgresql_adapter.rb:77:in `new_client'
        from /bundle/vendor/ruby/3.0.0/gems/activerecord-6.1.7/lib/active_record/connection_adapters/postgresql_adapter.rb:37:in `postgresql_connection'

# …snip…

My config/database.yml isn't trying to connect to the database by hostname though:

default: &default
  adapter: postgresql
  template: template_utf8
  host: /var/run/postgresql/
  port: 5432
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

I think it probably should be! Wonder if the reset script trashed a change I'd made there? Still, I don't have a host configured so why is it trying to access the "db" hostname?

garethrees commented 1 year ago

The issue was that the db container couldn't boot because the postgres version was out of date.

We found it by doing:

diff --git a/docker/setup b/docker/setup
index ba25badd1..ea84fe9eb 100755
--- a/docker/setup
+++ b/docker/setup
@@ -24,6 +24,9 @@ if [ -L config/general.yml ]; then
   theme=$(basename -s .yml $(readlink config/general.yml) | sed 's/^general-//')
   notice_msg "Switching to $theme..."
   bundle exec script/switch-theme.rb $theme 2>/dev/null
+
+  /bin/wait-for-it db:5432 --strict -- echo 'Database is up'
+
   bin/rails assets:clean >/dev/null
   success_msg 'done'
 fi

Running docker ps in a different terminal window while docker/reset was running, we could see that only the app container was booting, even though app depends on db.

Booting the db container manually (docker-compose run db) revealed the error message that the database was created using postgres 9.6 and that it needed to be migrated to postgres 13.x (annoyingly I closed the terminal window before capturing the message).

Choosing "Yes" when asked whether I wanted to reset the development database on running script/reset resolved the issue.