DB migration fails

Hi all,

this week I’ve changed laptops and hence the OS was installed from scratch.
Previously I was on Ubuntu 20.04 with Hanami 1.3 and Ruby 3.0.0. Then I’ve upgraded to Ubuntu 22.04. My app still worked, both locally and in Docker.

On the new laptop I now have a fresh install of Ubuntu 22.04 with Ruby 2.7.2. I’ve tried 2.5.1, 3.0.0 and 3.1.0 as well but 3 or above won’t work with bundle install due to Hanami model 1.3.3 dependency. I can use 2.7.2 though.

When I run bundle exec hanami db prepare it creates the DB, but the migration fails. I’ve only found out after manually checking the pg databases.

bundle exec hanami db migrate fails with the same error:

Here is only the Control frame information. There is also Ruby level backtrace and process memory map which is eventually interrupted.

bundle exec hanami db migrate

/home/hribarinho/.rvm/gems/ruby-2.7.2/gems/pg-1.4.5/lib/pg/connection.rb:626: [BUG] Segmentation fault at 0x00007f676aeaf140
ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-linux]
-- Control frame information -----------------------------------------------
c:0036 p:---- s:0241 e:000240 CFUNC :connect_poll
c:0035 p:0551 s:0237 e:000236 METHOD /home/hribarinho/.rvm/gems/ruby-2.7.2/gems/pg-1.4.5/lib/pg/connection.rb:626
c:0034 p:0356 s:0224 e:000223 METHOD /home/hribarinho/.rvm/gems/ruby-2.7.2/gems/pg-1.4.5/lib/pg/connection.rb:760
c:0033 p:0007 s:0213 e:000212 METHOD /home/hribarinho/.rvm/gems/ruby-2.7.2/gems/pg-1.4.5/lib/pg/connection.rb:695
c:0032 p:0134 s:0207 e:000206 METHOD /home/hribarinho/.rvm/gems/ruby-2.7.2/gems/sequel-4.49.0/lib/sequel/adapters/postgres.rb:225
c:0031 p:0007 s:0197 e:000196 METHOD /home/hribarinho/.rvm/gems/ruby-2.7.2/gems/sequel-4.49.0/lib/sequel/connection_pool.rb:123
c:0030 p:0048 s:0189 e:000188 METHOD /home/hribarinho/.rvm/gems/ruby-2.7.2/gems/sequel-4.49.0/lib/sequel/connection_pool/threaded.rb:228
c:0029 p:0012 s:0183 e:000182 METHOD /home/hribarinho/.rvm/gems/ruby-2.7.2/gems/sequel-4.49.0/lib/sequel/connection_pool/threaded.rb:201
c:0028 p:0003 s:0179 e:000178 METHOD /home/hribarinho/.rvm/gems/ruby-2.7.2/gems/sequel-4.49.0/lib/sequel/connection_pool/threaded.rb:137
c:0027 p:0006 s:0173 e:000172 BLOCK /home/hribarinho/.rvm/gems/ruby-2.7.2/gems/sequel-4.49.0/lib/sequel/connection_pool/threaded.rb:151
c:0026 p:0003 s:0166 e:000165 BLOCK /home/hribarinho/.rvm/gems/ruby-2.7.2/gems/sequel-4.49.0/lib/sequel/connection_pool/threaded.rb:282 [FINISH]
c:0025 p:---- s:0163 e:000162 CFUNC :synchronize

c:0016 p:0021 s:0101 e:000100 METHOD /home/hribarinho/.rvm/gems/ruby-2.7.2/gems/sequel-4.49.0/lib/sequel/extensions/migration.rb:687 [FINISH]
c:0010 p:0029 s:0061 E:000aa8 METHOD /home/hribarinho/.rvm/gems/ruby-2.7.2/gems/hanami-1.3.0/lib/hanami/cli/commands/db/migrate.rb:33
c:0009 p:0022 s:0056 E:001690 METHOD /home/hribarinho/.rvm/gems/ruby-2.7.2/gems/hanami-1.3.0/lib/hanami/cli/commands/db/migrate.rb:24
c:0008 p:0070 s:0048 E:001e38 METHOD /home/hribarinho/.rvm/gems/ruby-2.7.2/gems/hanami-1.3.0/lib/hanami/cli/commands/command.rb:85
c:0007 p:0071 s:0041 E:000190 METHOD /home/hribarinho/.rvm/gems/ruby-2.7.2/gems/hanami-cli-0.3.1/lib/hanami/cli.rb:57
c:0006 p:0091 s:0031 E:000640 TOP /home/hribarinho/.rvm/gems/ruby-2.7.2/gems/hanami-1.3.0/bin/hanami:6 [FINISH]
c:0005 p:---- s:0028 e:000027 CFUNC :load
c:0004 p:0112 s:0023 E:000930 EVAL /home/hribarinho/.rvm/gems/ruby-2.7.2/bin/hanami:23 [FINISH]
c:0003 p:---- s:0018 e:000017 CFUNC :eval
c:0002 p:0189 s:0011 E:000d78 EVAL /home/hribarinho/.rvm/gems/ruby-2.7.2/bin/ruby_executable_hooks:22 [FINISH]

c:0001 p:0000 s:0003 E:0022b0 (none) [FINISH]

To be honest this is the first time I’ve encountered this and I don’t know where to start debugging. The Hanami project is exactly the same with all .env and docker files as it was on the old laptop.

Can someone point me in the right direction?

Thank you, seba

Hey Sebastjan, I’ve seen something similar to this in the past on macOS (different issue, not a segmentation fault) where I had to do this (due to running on Apple Silicon):

gem uninstall pg
arch -arm64 gem install pg

The above won’t solve your problem but attempting to uninstall and reinstall the gem might be enough to double check. You might also need to double check you are installing the correct version of the gem when doing this as well.

Thank you. I’ve tried uninstalling and reinstalling on several rubies, but no success. I’ve narrowed it down to pg gem and possibly the combination with openssl: v1 on 20.04 vs v3 on 22.04. See here for possible related issue (Cannot build rubies 2.6.x, 2.7.x and 3.0.x on Ubuntu 22.04 because they relies on openssl1.1 and Ubuntu now provides openssl3 · Issue #5209 · rvm/rvm · GitHub).

As the comments say it should build fine with ruby 3.2, however as far as I know I need to stay under 3.0 due to Hanami 1.3.

Also, everything works in Docker environment so I could use just that for development, the downside is a tad longer build time as opposed to running a local server.

Oh, that reminds me. In that case, you might need to ensure you install and compile Ruby with the specific version of OpenSSL you. Here’s how I do it on macOS (hopefully you can translate to Linux using this Bash function):

# Label: Ruby Install
# Description: Install a specific version with safe defaults.
# Parameters: $1 (required) - Version.
rbi() {
  local version="$1"

  frum install "$version" \
               --with-openssl-dir="$(brew --prefix openssl)" \
               --enable-shared \

For more details, here’s my original article on managing Ruby through version managers. The option, you’ll most care about is:

--with-openssl-dir="<put path to your version of OpenSSL here>"

That might be enough to get you unstuck.

Yes, that’s what I did to be able to install via RVM in the first place. Just for reference, on Ubuntu the command is rvm install 2.7.2 --with-openssl-dir=$HOME/.rvm/usr to install ruby 2.7.2 and bypassing the openssl issue.

I then also used this bundle config build.pg --with-pg-config=which pg_config && bundle install since I thought it might be the config problem. But no, the segfault persists. The DB is created but no migration runs.

It’s currently installing pg -v 1.4.5. How can I check if the version is the issue?

Hmm, have you seen this closed issue on the PG gem’s repository? Sounds related to OpenSSL but not so much the gem version and looks like you might have to install OpenSSL differently. You could also search for more specific gem version issues but seems like OpenSSL is the hangup.

I saw that issue and in fact I’ve tried this with no success:

rvm pkg install openssl
rvm install ruby-2.7.2 --with-openssl-dir=$HOME/.rvm/usr

After your last post I went through the entire issue again and noticed I missed this one before as I was fixated on openssl:

sudo apt install libssl-dev=1.1.1l-1ubuntu1.4

After that I still performed the installation with rvm install ruby-2.7.2 --with-openssl-dir=$HOME/.rvm/usr and then bundle install.

And it worked!!! :smiley: :fireworks:

I was ready to either downgrade Ubuntu or move to Arch altogether. So much frustration.

Thank you so much for helping me solve this!

Best, seba

Ha, awesome…and interesting because this roughly mimics the macOS setup (roughly). Nice work and good to know. :sparkle: