Skip to content

Configuration

Vsevolod Romashov edited this page Oct 20, 2017 · 2 revisions

In order to connect to your database DbSchema needs to be properly configured with connection settings. You can do that with DbSchema.configure:

DbSchema.configure(
  adapter:  'postgres',
  host:     'localhost',
  port:     5432,
  database: 'my_db',
  user:     '7even',
  password: 'secret'
)

If you have a Postgres URL you can pass it under the :url option:

DbSchema.configure(url: 'postgres://7even:secret@localhost:5432/my_db')

You can mix both approaches by overriding some parts of the connection string (:url has the lowest priority):

DbSchema.configure(
  url:      ENV['DATABASE_URL'],
  database: 'my_database'
)

Subsequent DbSchema.configure calls will override only the options you pass into them.

DbSchema also has several options not related to database connection parameters:

  • log_changes - log the operations actually performed on a database (true by default)
  • dry_run - show the operations that will be applied to the database, then roll everything back (false by default)
  • post_check - make sure the schema was applied correctly (true by default)

Dry run may be useful while you are building your schema definition for an existing app; adjust your schema.rb and apply it in dry run mode until it fits your database and next dry run doesn't report any changes.

DbSchema.configure(
  url:     ENV['DATABASE_URL'],
  dry_run: true
)

Don't forget to turn dry_run off afterwards!

If your application uses Sequel you can inject your database connection into DbSchema to keep it from establishing it's own connection:

db_conn = Sequel.connect(adapter: 'postgres', database: 'some_database')
DbSchema.connection = db_conn
# then use db_conn in your application

Rails

Rails users can use the DbSchema.configure_from_yaml method passing it the database.yml file path along with the current environment (and other options if needed):

DbSchema.configure_from_yaml(Rails.root.join('config', 'database.yml'), Rails.env)

Hanami

Hanami users can just pass the database url to DbSchema:

DbSchema.configure(url: ENV['DATABASE_URL'])
Clone this wiki locally