2020-04-09 19:24:29 -04:00
# WORK IN PROGRESS - PLEASE DO NOT FOLLOW THESE INSTRUCTIONS UNTIL v2.0.0 FINAL IS RELEASED!
## How to add a new adapter
_Note the following tutorial is for database_cleaner version 2 and above_
2020-04-09 19:39:11 -04:00
Every adapter is a separate gem, the first step in the creation of an adapter is to create a new gem.
2020-04-09 19:24:29 -04:00
### Naming
2020-04-09 19:39:11 -04:00
Please follow the [Rubygems convention for gem naming ](https://guides.rubygems.org/name-your-gem/ ). The namespace you will be working within is `DatabaseCleaner::Namespace` where Namespace is the name of the ORM you are creation an adapter for.
For example, `database_cleaner-active_record` provides `DatabaseCleaner::ActiveRecord` , and `database_cleaner-redis` provides `DatabaseCleaner::Redis` , etc.
2020-04-09 19:24:29 -04:00
### Bootstrapping a gem
2020-04-09 19:39:11 -04:00
We will use `bundle` to bootstrap the new gem. This will produce all the initial files needed.
2020-04-09 19:24:29 -04:00
```
2020-04-09 19:39:11 -04:00
bundle gem database_cleaner-orm_name
2020-04-09 19:24:29 -04:00
```
2020-04-09 19:39:11 -04:00
#### Modifying .gemspec
You need to add a couple of dependecies to `.gemspec` :
2020-04-09 19:24:29 -04:00
* `database_cleaner-core`
* Adapter you're creating this gem for
```
2020-04-09 19:39:11 -04:00
spec.add_dependency "database_cleaner-core"
spec.add_dependency "orm_name", "some version if required"
2020-04-09 19:24:29 -04:00
```
####
### File structure
2020-04-09 19:39:11 -04:00
Inside the `lib/database_cleaner/orm_name` directory, you will need to create a few files:
2020-04-09 19:24:29 -04:00
* Separate files for each strategy you have
2020-04-09 19:39:11 -04:00
The file structure you end up with will look something like this
2020-04-09 19:24:29 -04:00
```
\-lib
\-database_cleaner
2020-04-09 19:39:11 -04:00
\- orm_name
2020-04-09 19:24:29 -04:00
\- truncation.rb
\- deletion.rb
\- transaction.rb
\- version.rb
2020-04-09 19:39:11 -04:00
\- orm_name.rb
2020-04-09 19:24:29 -04:00
```
2020-04-10 12:25:22 -04:00
#### orm_name.rb
2020-04-09 19:24:29 -04:00
2020-04-10 12:25:22 -04:00
File `orm_name.rb` **must** do the following:
2020-04-10 12:37:13 -04:00
* define module method `.available_strategies` on DatabaseCleaner::OrmName.
2020-04-10 12:25:22 -04:00
* require all the gems strategies
2020-04-10 12:25:13 -04:00
* configure DatabaseCleaner with the default strategy for the ORM.
2020-04-09 19:24:29 -04:00
2020-04-10 12:25:22 -04:00
So, in the end you will end up with a file that might look something like this:
2020-04-09 19:24:29 -04:00
```ruby
2020-04-10 12:25:22 -04:00
# lib/database_cleaner/orm_name.rb
require 'database_cleaner/orm_name/version'
require 'database_cleaner/core'
require 'database_cleaner/orm_name/transaction'
require 'database_cleaner/orm_name/truncation'
require 'database_cleaner/orm_name/deletion'
2020-04-10 12:25:13 -04:00
2020-04-09 19:24:29 -04:00
module DatabaseCleaner
2020-04-09 19:39:11 -04:00
module OrmName
2020-04-09 19:24:29 -04:00
def self.available_strategies
2020-04-10 12:25:13 -04:00
%i[transaction truncation deletion]
2020-04-09 19:24:29 -04:00
end
end
end
2020-04-10 12:25:13 -04:00
DatabaseCleaner[:orm_name].strategy = :transaction
2020-04-09 19:24:29 -04:00
```
### Strategy classes
2020-04-10 12:25:22 -04:00
Each strategy class can inherit from DatabaseCleaner::Strategy to get it most of the way there. If you do, you only need to define one method!
2020-04-09 19:24:29 -04:00
Each strategy **must** have the following instance methods
* `#clean` -- where the cleaning happens
2020-04-10 12:25:13 -04:00
Optionally, depending on how your strategy works you may define
2020-04-10 12:25:22 -04:00
* `#start` -- if your strategy is transactional, this is where you would start the database transaction that `#clean` later rolls back.
2020-04-10 12:25:13 -04:00
Given that we're creating a strategy for truncation, you may end up with something like the following class:
2020-04-09 19:24:29 -04:00
```ruby
2020-04-10 12:25:22 -04:00
# lib/database_cleaner/orm_name/truncation.rb
require 'database_cleaner/strategy'
require 'orm'
2020-04-09 19:24:29 -04:00
module DatabaseCleaner
2020-04-09 19:39:11 -04:00
module OrmName
2020-04-10 12:25:22 -04:00
class Truncation < Strategy
2020-04-09 19:24:29 -04:00
def clean
2020-04-09 19:39:11 -04:00
# actual database cleaning code goes here
2020-04-10 12:25:13 -04:00
ORM.truncate_all_tables!
2020-04-09 19:24:29 -04:00
end
end
end
end
```
2020-04-09 19:39:11 -04:00
That's about it for the code needed to create your own adapter!
2020-04-09 19:24:29 -04:00
### Testing
2020-04-10 12:25:13 -04:00
To make sure that your new adapter adheres to the Database Cleaner API, database_cleaner-core provides an RSpec shared example. This shared example only checks to make sure all the right methods exist. You will still want to write tests to verify that the cleaning actually works as you expect!
2020-04-09 19:24:29 -04:00
```ruby
2020-04-09 19:39:11 -04:00
# spec/database_cleaner/orm_name_spec.rb
require 'database_cleaner/orm_name'
2020-04-09 19:24:29 -04:00
require 'database_cleaner/spec'
2020-04-09 19:39:11 -04:00
RSpec.describe DatabaseCleaner::OrmName do
it_should_behave_like "a database_cleaner strategy"
2020-04-09 19:24:29 -04:00
end
```
### What's next
2020-04-10 12:25:13 -04:00
Now you should be set up to with your own database_cleaner ORM adapter.
2020-04-09 19:39:11 -04:00
Also, don't forget to take a look at the already created adapters, if you encounter any problems.
When you are done with your adapter gem, only a few things left to do
2020-04-09 19:24:29 -04:00
* Create a repository with your code
* Push code to rubygems
2020-04-10 12:25:13 -04:00
* Open a PR to add your adapter to the [list ](https://github.com/DatabaseCleaner/database_cleaner#list-of-adapters )