Writing an Activerecord Adapter

Writing an ActiveRecord adapter

I think the easiest thing to do is go to the source.

The rails trunk has adapters for mysql, postgres and sqlite, which you could base your adapter on. abstract_adapter.rb is pretty short and fairly straight forward to implement.

forcing an unmaintained ActiveRecord adapter to Rails version 6

@rmlockerd and @ruby_object were both correct in helping to debug, but at the end of the day I'd just written the section of code in a way that didn't work.

debugging was key

Getting ruby activerecord to work with mysql adapter

Have you tried to change

:adapter => 'mysql'

to

:adapter => 'mysql2'

How to load custom db adapter ? so that its ready when `database.yml` is evaluated?

Rails only needs that file to be on load path, you can place it in one of directories that are in $LOAD_PATH after bundler is initialized, but before app is loaded, so <rails_root>/lib/active_record/connection_adapters/proxy_mysql2_adapter.rb is suitable.

Make sure it does not raise LoadError or Gem::LoadError upon load - if your adapter fails to require some other dependency, rails will think that it cannot find the adapter or its gem.

Rails ActiveRecord mysql2 adapter, use PreparedStatement by default

ActiveRecord with Mysql2 < 5, doesn't support configurable parameter to enable prepared_statement

Code snippet from ActiveRecord 4.2.6

connection_adapters/mysql2_adapter.rb

module ConnectionAdapters
class Mysql2Adapter < AbstractMysqlAdapter
ADAPTER_NAME = 'Mysql2'.freeze

def initialize(connection, logger, connection_options, config)
super
@prepared_statements = false # No configurable param, default set to false
configure_connection
end
...
end

ActiveRecord with Mysql2 = 5.2.1 adapter support configurable parameter to enable prepared_statement
Code snippet from ActiveRecord 5.2.1

connection_adapters/mysql2_adapter.rb

module ConnectionAdapters
class Mysql2Adapter < AbstractMysqlAdapter
ADAPTER_NAME = "Mysql2".freeze

include MySQL::DatabaseStatements

def initialize(connection, logger, connection_options, config)
super
@prepared_statements = false unless config.key?(:prepared_statements)
configure_connection
end
...
end

So, in ActiveRecord 5.2.1, one can just add following line in database.yml to enable prepared_statements

prepared_statements: true

How does Activerecord 'knows' which adapter it should use?

You can get an idea for how Rails adapts to different databases by checking out Ryan Bates's RailsCast on migrating from Sqlite3 to Postgres.

Read the Migrating an Existing Application section: http://railscasts.com/episodes/342-migrating-to-postgresql?view=asciicast

As you'll see, the magic happens in the database.yml file. Whichever adapter is present in those settings is the one Rails will use.

For a more in-depth look at the code that handles this, check out the activerecord/lib/active_record/connection_adapters/connection_specification.rb file.



Related Topics



Leave a reply



Submit