Reputation: 1435
I'm following this guide for getting started with hanami
:
https://guides.hanamirb.org/v2.0/introduction/getting-started/
Rather than using ROM, I prefer Sequel, but I stumble upon the persistancy :
https://guides.hanamirb.org/v2.0/introduction/getting-started/#persisting-books
In my gemfile
, I use sequel
and pg
instead of rom
.
I added this to config/app.rb
:
require "sequel"
and config/providers/persistence.rb
contains this:
Hanami.app.register_provider :persistence, namespace: true do
prepare do
#config = ROM::Configuration.new(:sql, target["settings"].database_url)
config = DB = Sequel.connect(database_url)
register "config", config
register "db", config.gateways[:default].connection
end
start do
config = target["persistence.config"]
# config.auto_registration(
# target.root.join("lib/bookshelf/persistence"),
# namespace: "Bookshelf::Persistence"
# )
register "sequel", DB
end
end
config/settings.rb
:
module Bookshelf
class Settings < Hanami::Settings
# Define your app settings here, for example:
#
# setting :my_flag, default: false, constructor: Types::Params::Bool
setting :database_url, constructor: Types::String
end
end
and finally, the database_url is defined in a .env
and and .env.test
file.
# .env
DATABASE_URL=postgres://thiebo@localhost:5432/bookshelf_development
Running this from the hanami console works fine:
moi@Hugo bookshelf % bundle exec hanami console
bookshelf[development]> Hanami.app["settings"].database_url
=> "postgres://moi@localhost:5432/bookshelf_development"
bookshelf[development]>
Sequel allows for migrations to be run as a rake task: http://sequel.jeremyevans.net/rdoc/files/doc/migration_rdoc.html#label-Running+migrations+from+a+Rake+task
My Rakefile:
require "sequel"
namespace :db do
desc "Run migrations"
task :migrate, [:version] do |t, args|
require "sequel/core"
Sequel.extension :migration
version = args[:version].to_i if args[:version]
Sequel.connect(ENV.fetch("DATABASE_URL")) do |db|
Sequel::Migrator.run(db, "db/migrations", target: version)
end
end
end
however... when I run try to run that migration from the console, as per Sequel documentation :
thiebo@Hugo bookshelf % rake db:migrate
rake aborted!
KeyError: key not found: "DATABASE_URL"
/Users/thiebo/bookshelf/Rakefile:12:in `fetch'
/Users/thiebo/bookshelf/Rakefile:12:in `block (2 levels) in <top (required)>'
/Users/thiebo/.rvm/gems/ruby-3.0.0/gems/rake-13.0.6/exe/rake:27:in `<top (required)>'
/Users/thiebo/.rvm/gems/ruby-3.0.0/bin/ruby_executable_hooks:22:in `eval'
/Users/thiebo/.rvm/gems/ruby-3.0.0/bin/ruby_executable_hooks:22:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
(it also doesn't work with database_url
instead of DATABASE_URL
. Same error...)
Upvotes: 0
Views: 361
Reputation: 1250
Hanami By default loads environment variables via Dotenv. You can just load it manually like @engineersmnky mentioned in the comment. Quoted his response for reference:
It appears you are using Dotenv if so you should just be able to add require 'dotenv/tasks' and then namespace your rake task under the the :dotenv key or add require 'dotenv/load' which will load the .env files. – @engineersmnky
However, I prefer to just load the hanami/prepare
in my rake tasks, which loads the same configuration as we have for console, keeping our tasks fast and clean, but automatically resolving all the config for you automatically.
I've an example of this in configuring ROM from scratch tutorial
Long story short, by calling:
# rakefile
require "hanami/prepare"
* ....
In your rakefile, you'll load all the settings, resolved by preconfigured engine, which is dotenv
in this case.
It's useful for example, when you would like to load environment/settings via different engine (from YAML for example?), and in that case, no change would be required in your rakefile.
Upvotes: 0