Content-Length: 372452 | pFad | https://github.com/hanami/hanami/pull/1430

BD Add DB provider adapter config & sensible defaults by timriley · Pull Request #1430 · hanami/hanami · GitHub
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DB provider adapter config & sensible defaults #1430

Merged
merged 16 commits into from
Jul 1, 2024

Conversation

timriley
Copy link
Member

@timriley timriley commented Jun 18, 2024

Note to reviewers: I'm 99% confident this is a good approach and should work well for us. I'm interested in your perspectives on the config API here. Have I missed anything?

Add adapter config and sensible adapter defaults to the DB provider.

Now you can configure your adapters like so:

# config/providers/db.rb

Hanami.app.configure_provider :db do
  config.adapter :sql do |a|
    # Add a Sequel extension (available in :sql adapter only)
    a.extension :some_extension

    # Add a ROM plugin
    a.plugin relations: :some_plugin
  end
end

The following defaults are provided for the :sql adapter:

plugin relations: :instrumentation do |plugin|
  plugin.notifications = target["notifications"]
end

plugin relations: :auto_restrictions

extension(
  :caller_logging,
  :error_sql,
  :sql_comments
)

And in addition for Postgres databases:

extension(
  :pg_array,
  :pg_enum,
  :pg_json,
  :pg_range
)

These defaults are set as part of the db provider's prepare lifecycle step. This is necessary (instead of doing it earlier, i.e. at the stage of establishing the setting defaults) to allow the user to configure and set their database_url, which is required to determine the set of defaults to provide.

When configuring the db provider, the defaults may be skipped by the user:

Hanami.app.configure_provider :db do
  config.adapter :sql do |a|
    # Skip all defaults
    a.skip_defaults

    # Or plugins only
	a.skip_defaults :plugins

    # Or extensions only
	a.skip_defaults :extensions
  end
end

If you want to configure the db provider to use a non-sql adapter, then you can configure it independently:

Hanami.app.configure_provider :db do
  config.adapter = :yaml

  config.adapter :yaml do |a|
    # configure plugins here
    # (extensions not available since these are sql-only)
  end
end

You can also configure ROM plugins for any adapter via any_adapter:

Hanami.app.configure_provider :db do
  config.any_adapter do |a|
    # configure plugins here
  end
end

This may be useful in an advanced setup where your app uses different ROM adapters, but you want certain plugins to be used consistently across all of them.

For db providers in slices, when using the default app.config.db.configure_from_parent arrangement (true), adapter config is applied from the parent slice just like any other config.

However, if the configures their own :sql adapter in a slice provider (either plugins or extensions), then the respective plugins or extensions from the parent will not be applied. We might be able to provide more sophisticated handling here, but this felt like a reasonable first approach. In a Hanami app with multiple slices but a single shared database, I think it would be unlikely that different ROM plugins or Sequel extensions would be needed on a slice-by-slice basis anyway.

This change depends on dry-rb/dry-configurable#164 to allow for the custom config class we use for the db provider. I'm going to make sure that PR merges first before this one, but I wanted to open this one up for review first.

Resolves #1389, resolves #1409

@timriley timriley changed the title Add custom config class for DB provider source Expand DB provider config Jun 18, 2024
@timriley timriley force-pushed the expand-db-provider-config branch from 86db090 to d92d331 Compare June 22, 2024 01:05
@timriley timriley force-pushed the expand-db-provider-config branch from d92d331 to 0bf0697 Compare June 22, 2024 01:07
@timriley timriley force-pushed the expand-db-provider-config branch from 3aaded8 to 6399e4c Compare June 23, 2024 07:41
@timriley timriley changed the title Expand DB provider config Add DB provider adapter config & sensible defaults Jun 25, 2024
@configured_for_database = false
end

def finalize_config
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is pulled out into a public method on the provider source so we can call it on the parent provider in #apply_parent_config. Doing it this way is important to avoid unnecessary connections to the database that would otherwise occurred if we called a straight-up #prepare.

@timriley timriley marked this pull request as ready for review June 25, 2024 13:12
@timriley timriley self-assigned this Jun 25, 2024
Copy link
Contributor

@alassek alassek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great ❤️

I think this covers any customizations I would need to do.

@timriley timriley force-pushed the expand-db-provider-config branch from 4d06c60 to 5ca573b Compare July 1, 2024 13:05
@timriley timriley merged commit 2357158 into main Jul 1, 2024
8 checks passed
@timriley timriley deleted the expand-db-provider-config branch July 1, 2024 13:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Expand default set of Sequel extensions Expand configurability of db provider
2 participants








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://github.com/hanami/hanami/pull/1430

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy