Migrations - ROM config vs DRY container

So, in another thread I mentioned I had found some issues so I figure I should put them in individual threads so they don’t get mixed up.

Just today I found this problem.

I’ll just present it and see what people think.

I am using the dry-container library to set up all of my applications config, services etc. This includes ROM.

In the ROM config I specify a relation, with an inferred schema e.g;

rom = ROM.container(:sql, url) do |conf|
  conf.relation(:accounts) do
    schema(infer: true) do
    end 
  end  
end

When I attempt to migrate a clean database this causes the following error to be thrown up (but the migration works)

[ROM::Relation[Accounts]] failed to infer schema. Make sure tables exist before ROM container is set up. This may also happen when your migration tasks load ROM container, which is not needed for migrations as only the connection is required (PG::UndefinedTable: ERROR:  relation "accounts" does not exist
LINE 1: ..."."attnum" > 0) AND ("pg_class"."oid" = CAST(CAST('"accounts...
                                                             ^
)

So, is there any way of making these things go away? Right now it means I would need to create a different ROM config for migrations than for general usage which feels awkward.

I found another problem earlier with schema inferring but I need to try and replicate it.

Thanks

Oh it’s because you registered a relation within a config that doesn’t create tables, this means you would have to have tables already created to make it work.

ROM.container method is a shortcut that both configures rom and finalizes the configuration. So if you don’t create tables inside that block and try to establish relations, then it’ll blow up. If you create tables inside that block everything will work.

In typical application setup, ROM.container is not used because it’s a shortcut for quick setup in places like standalone scripts. I’m not sure where you want to use rom, but if it’s an application then it’s better to use explicit setup which allows you to create rom config. In places like rake tasks that migrate dbs, it’s sufficient to only create that config, as it provides db connections that are needed for migrations. If you’re using Rails then rom-rails takes care of everything for you.

Right, I have separated them out into different boot files and now I am using the config for migrations instead of the entire container. Thanks.

I will pester you with more questions as and when I have them :slight_smile:

1 Like