extract database_cleaner-sequel adapter.

This commit is contained in:
Micah Geisel 2018-05-26 00:33:38 -07:00
parent 9100251d67
commit 6a64860486
32 changed files with 673 additions and 1 deletions

View File

@ -11,4 +11,5 @@ path "./adapters" do
gem "database_cleaner-neo4j"
gem "database_cleaner-ohm"
gem "database_cleaner-redis"
gem "database_cleaner-sequel"
end

View File

@ -38,6 +38,9 @@ PATH
database_cleaner-redis (1.8.0)
database_cleaner (~> 1.8.0)
redis
database_cleaner-sequel (1.8.0)
database_cleaner (~> 1.8.0)
sequel
GEM
remote: https://rubygems.org/
@ -293,6 +296,7 @@ DEPENDENCIES
database_cleaner-neo4j!
database_cleaner-ohm!
database_cleaner-redis!
database_cleaner-sequel!
datamapper
dm-migrations
dm-sqlite-adapter

View File

@ -0,0 +1,13 @@
/.bundle/
/.yardoc
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/spec/support/config.yml
/tmp/
!/tmp/.keep
# rspec failure tracking
.rspec_status

View File

@ -0,0 +1,3 @@
--format documentation
--color
--require spec_helper

View File

@ -0,0 +1,5 @@
sudo: false
language: ruby
rvm:
- 2.2.9
before_install: gem install bundler -v 1.16.1

View File

@ -0,0 +1,8 @@
source "https://rubygems.org"
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
# Specify your gem's dependencies in database_cleaner-sequel.gemspec
gemspec
gem "database_cleaner", path: "../.."

View File

@ -0,0 +1,52 @@
PATH
remote: ../..
specs:
database_cleaner (1.8.0)
PATH
remote: .
specs:
database_cleaner-sequel (1.8.0)
database_cleaner (~> 1.8.0)
sequel
GEM
remote: https://rubygems.org/
specs:
diff-lcs (1.3)
mysql (2.9.1)
mysql2 (0.3.18)
pg (0.18.2)
rake (10.4.2)
rspec (3.7.0)
rspec-core (~> 3.7.0)
rspec-expectations (~> 3.7.0)
rspec-mocks (~> 3.7.0)
rspec-core (3.7.1)
rspec-support (~> 3.7.0)
rspec-expectations (3.7.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.7.0)
rspec-mocks (3.7.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.7.0)
rspec-support (3.7.1)
sequel (3.21.0)
sqlite3 (1.3.10)
PLATFORMS
ruby
DEPENDENCIES
bundler (~> 1.16)
database_cleaner!
database_cleaner-sequel!
mysql (~> 2.9.1)
mysql2
pg
rake (~> 10.0)
rspec (~> 3.0)
sqlite3
BUNDLED WITH
1.16.1

View File

@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2009 Ben Mabey
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View File

@ -0,0 +1,39 @@
# DatabaseCleaner::Sequel
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/database_cleaner/sequel`. To experiment with that code, run `bin/console` for an interactive prompt.
TODO: Delete this and the text above, and describe your gem
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'database_cleaner-sequel'
```
And then execute:
$ bundle
Or install it yourself as:
$ gem install database_cleaner-sequel
## Usage
TODO: Write usage instructions here
## Development
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/database_cleaner-sequel.
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).

View File

@ -0,0 +1,6 @@
require "bundler/gem_tasks"
require "rspec/core/rake_task"
RSpec::Core::RakeTask.new(:spec)
task :default => :spec

View File

@ -0,0 +1,14 @@
#!/usr/bin/env ruby
require "bundler/setup"
require "database_cleaner/sequel"
# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.
# (If you use this, don't forget to add pry to your Gemfile!)
# require "pry"
# Pry.start
require "irb"
IRB.start(__FILE__)

View File

@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
set -vx
bundle install
# Do any other automated setup that you need to do here

View File

@ -0,0 +1,35 @@
lib = File.expand_path("../lib", __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require "database_cleaner/sequel/version"
Gem::Specification.new do |spec|
spec.name = "database_cleaner-sequel"
spec.version = DatabaseCleaner::Sequel::VERSION
spec.authors = ["Ernesto Tagwerker"]
spec.email = ["ernesto@ombulabs.com"]
spec.summary = "Strategies for cleaning databases using Sequel. Can be used to ensure a clean state for testing."
spec.description = "Strategies for cleaning databases using Sequel. Can be used to ensure a clean state for testing."
spec.homepage = "https://github.com/DatabaseCleaner/database_cleaner-sequel"
spec.license = "MIT"
spec.add_dependency "database_cleaner", "~> 1.8.0"
spec.add_dependency "sequel"
spec.files = `git ls-files -z`.split("\x0").reject do |f|
f.match(%r{^(test|spec|features)/})
end
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
spec.add_development_dependency "bundler", "~> 1.16"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "rspec", "~> 3.0"
spec.add_development_dependency 'mysql', '~> 2.9.1'
spec.add_development_dependency 'mysql2'
spec.add_development_dependency 'pg'
spec.add_development_dependency "sqlite3"
end

View File

@ -0,0 +1 @@
require "database_cleaner/sequel"

View File

@ -0,0 +1,6 @@
require "database_cleaner/sequel/version"
require "database_cleaner"
require "database_cleaner/sequel/truncation"
require "database_cleaner/sequel/transaction"
require "database_cleaner/sequel/deletion"

View File

@ -0,0 +1,22 @@
require 'database_cleaner/generic/base'
module DatabaseCleaner
module Sequel
def self.available_strategies
%w(truncation transaction deletion)
end
module Base
include ::DatabaseCleaner::Generic::Base
def db=(desired_db)
@db = desired_db
end
def db
return @db if @db && @db != :default
raise "As you have more than one active sequel database you have to specify the one to use manually!" if ::Sequel::DATABASES.count > 1
::Sequel::DATABASES.first || :default
end
end
end
end

View File

@ -0,0 +1,47 @@
require 'database_cleaner/sequel/base'
require 'database_cleaner/generic/truncation'
require 'database_cleaner/sequel/truncation'
module DatabaseCleaner::Sequel
class Deletion < Truncation
def disable_referential_integrity(tables)
case db.database_type
when :postgres
db.run('SET CONSTRAINTS ALL DEFERRED')
tables_to_truncate(db).each do |table|
db.run("ALTER TABLE \"#{table}\" DISABLE TRIGGER ALL")
end
when :mysql
old = db.fetch('SELECT @@FOREIGN_KEY_CHECKS').first[:@@FOREIGN_KEY_CHECKS]
db.run('SET FOREIGN_KEY_CHECKS = 0')
end
yield
ensure
case db.database_type
when :postgres
tables.each do |table|
db.run("ALTER TABLE \"#{table}\" ENABLE TRIGGER ALL")
end
when :mysql
db.run("SET FOREIGN_KEY_CHECKS = #{old}")
end
end
def delete_tables(db, tables)
tables.each do |table|
db[table.to_sym].delete
end
end
def clean
return unless dirty?
tables = tables_to_truncate(db)
db.transaction do
disable_referential_integrity(tables) do
delete_tables(db, tables)
end
end
end
end
end

View File

@ -0,0 +1,40 @@
require 'database_cleaner/sequel/base'
module DatabaseCleaner
module Sequel
class Transaction
include ::DatabaseCleaner::Sequel::Base
def self.check_fiber_brokenness
if !@checked_fiber_brokenness && Fiber.new { Thread.current }.resume != Thread.current
raise RuntimeError, "This ruby engine's Fibers are not compatible with Sequel's connection pool. " +
"To work around this, please use DatabaseCleaner.cleaning with a block instead of " +
"DatabaseCleaner.start and DatabaseCleaner.clean"
end
@checked_fiber_brokenness = true
end
def start
self.class.check_fiber_brokenness
@fibers ||= []
db = self.db
f = Fiber.new do
db.transaction(:rollback => :always, :savepoint => true) do
Fiber.yield
end
end
f.resume
@fibers << f
end
def clean
f = @fibers.pop
f.resume
end
def cleaning
self.db.transaction(:rollback => :always, :savepoint => true) { yield }
end
end
end
end

View File

@ -0,0 +1,79 @@
require 'database_cleaner/generic/truncation'
require 'database_cleaner/sequel/base'
module DatabaseCleaner
module Sequel
class Truncation
include ::DatabaseCleaner::Sequel::Base
include ::DatabaseCleaner::Generic::Truncation
def start
@last_txid = txid
end
def clean
return unless dirty?
tables = tables_to_truncate(db)
# Count rows before truncating
if pre_count?
tables = pre_count_tables(tables)
end
case db.database_type
when :postgres
# PostgreSQL requires all tables with FKs to be truncates in the same command, or have the CASCADE keyword
# appended. Bulk truncation without CASCADE is:
# * Safer. Tables outside of tables_to_truncate won't be affected.
# * Faster. Less roundtrips to the db.
unless tables.empty?
tables_sql = tables.map { |t| %("#{t}") }.join(',')
db.run "TRUNCATE TABLE #{tables_sql} RESTART IDENTITY;"
end
else
truncate_tables(db, tables)
end
end
private
def pre_count_tables tables
tables.reject { |table| db[table.to_sym].count == 0 }
end
def truncate_tables(db, tables)
tables.each do |table|
db[table.to_sym].truncate
if db.database_type == :sqlite && db.table_exists?(:sqlite_sequence)
db[:sqlite_sequence].where(name: table).delete
end
end
end
def dirty?
@last_txid != txid || @last_txid.nil?
end
def txid
case db.database_type
when :postgres
db.fetch('SELECT txid_snapshot_xmax(txid_current_snapshot()) AS txid').first[:txid]
end
end
def tables_to_truncate(db)
(@only || db.tables.map(&:to_s)) - @tables_to_exclude
end
# overwritten
def migration_storage_names
%w(schema_info schema_migrations)
end
def pre_count?
@pre_count == true
end
end
end
end

View File

@ -0,0 +1,5 @@
module DatabaseCleaner
module Sequel
VERSION = "1.8.0"
end
end

View File

@ -0,0 +1,28 @@
require 'database_cleaner/sequel/base'
require 'database_cleaner/spec'
require 'sequel'
module DatabaseCleaner
RSpec.describe Sequel do
it { is_expected.to respond_to(:available_strategies) }
end
module Sequel
class ExampleStrategy
include ::DatabaseCleaner::Sequel::Base
end
RSpec.describe ExampleStrategy do
it_should_behave_like "a generic strategy"
it { is_expected.to respond_to(:db) }
it { is_expected.to respond_to(:db=) }
it "should store my desired db" do
subject.db = :my_db
expect(subject.db).to eq :my_db
end
pending "I figure out how to use Sequel and write some real tests for it..."
end
end
end

View File

@ -0,0 +1,41 @@
require 'database_cleaner/sequel/deletion'
require 'database_cleaner/spec'
require 'support/sequel_helper'
module DatabaseCleaner
module Sequel
RSpec.describe Deletion do
it_should_behave_like "a generic strategy"
SequelHelper.with_all_dbs do |helper|
context "using a #{helper.db} connection" do
around do |example|
helper.setup
example.run
helper.teardown
end
let(:connection) { helper.connection }
before { subject.db = connection }
context 'when several tables have data' do
before do
connection[:users].insert
connection[:agents].insert
end
context 'by default' do
it 'deletes all the tables' do
subject.clean
expect(connection[:users]).to be_empty
expect(connection[:agents]).to be_empty
end
end
end
end
end
end
end
end

View File

@ -0,0 +1,20 @@
require 'database_cleaner/sequel/transaction'
require 'database_cleaner/spec'
require 'support/sequel_helper'
module DatabaseCleaner
module Sequel
RSpec.describe Transaction do
it_should_behave_like "a generic strategy"
it_should_behave_like "a generic transaction strategy"
describe "start" do
it "should start a transaction"
end
describe "clean" do
it "should finish a transaction"
end
end
end
end

View File

@ -0,0 +1,92 @@
require 'database_cleaner/sequel/truncation'
require 'database_cleaner/spec'
require 'support/sequel_helper'
module DatabaseCleaner
module Sequel
RSpec.describe Truncation do
it_should_behave_like "a generic strategy"
it_should_behave_like "a generic truncation strategy"
SequelHelper.with_all_dbs do |helper|
context "using a #{helper.db} connection" do
around do |example|
helper.setup
example.run
helper.teardown
end
let(:connection) { helper.connection }
before { subject.db = connection }
context 'when several tables have data' do
before do
connection[:users].insert
connection[:agents].insert
end
context 'by default' do
it 'truncates all the tables' do
subject.clean
expect(connection[:users]).to be_empty
expect(connection[:agents]).to be_empty
end
end
context 'restricted to "only: [...]" some tables' do
subject { described_class.new(only: ['users']) }
it 'truncates only the mentioned tables (and leaves the rest alone)' do
subject.clean
expect(connection[:users]).to be_empty
expect(connection[:agents].count).to eq(1)
end
end
context 'restricted to "except: [...]" some tables' do
subject { described_class.new(except: ['users']) } # XXX: Strings only, symbols are ignored
it 'leaves the mentioned tables alone (and truncates the rest)' do
subject.clean
expect(connection[:users].count).to eq(1)
expect(connection[:agents]).to be_empty
end
end
end
describe 'auto increment sequences' do
it "resets AUTO_INCREMENT primary key seqeunce" do
table = connection[:users]
2.times { table.insert }
subject.clean
id_after_clean = table.insert
expect(id_after_clean).to eq 1
end
end
describe "with pre_count optimization option" do
subject { described_class.new(pre_count: true) }
before { connection[:users].insert }
it "only truncates non-empty tables" do
sql = case helper.db
when :sqlite3 then ["DELETE FROM `users`", anything]
when :postgres then ['TRUNCATE TABLE "users" RESTART IDENTITY;', anything]
else ["TRUNCATE TABLE `users`", anything]
end
expect(subject.db).to receive(:execute_ddl).once.with(*sql)
subject.clean
end
end
end
end
end
end
end

View File

@ -0,0 +1,14 @@
require "bundler/setup"
require "database_cleaner/sequel"
RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
config.example_status_persistence_file_path = ".rspec_status"
# Disable RSpec exposing methods globally on `Module` and `main`
config.disable_monkey_patching!
config.expect_with :rspec do |c|
c.syntax = :expect
end
end

View File

@ -0,0 +1,34 @@
mysql:
adapter: mysql
database: database_cleaner_test
username: root
password:
host: 127.0.0.1
port: 3306
encoding: utf8
mysql2:
adapter: mysql2
database: database_cleaner_test
username: root
password:
host: 127.0.0.1
port: 3306
encoding: utf8
postgres:
adapter: postgresql
database: database_cleaner_test
username: postgres
password:
host: 127.0.0.1
encoding: unicode
template: template0
sqlite3:
adapter: sqlite3
database: tmp/database_cleaner_test.sqlite3
pool: 5
timeout: 5000
encoding: utf8

View File

@ -0,0 +1,12 @@
require 'sequel'
require 'database_cleaner/spec/database_helper'
class SequelHelper < DatabaseCleaner::Spec::DatabaseHelper
private
def establish_connection(config = default_config)
url = "#{db}:///"
url = "sqlite:///" if db == :sqlite3
@connection = ::Sequel.connect(url, config)
end
end

View File

@ -25,6 +25,9 @@ Feature: database cleaning
| Neo4j | transaction |
| Ohm | truncation |
| Redis | truncation |
| Sequel | transaction |
| Sequel | truncation |
| Sequel | deletion |
Scenario Outline: ruby app
Given I am using <ORM>

View File

@ -20,6 +20,7 @@ Feature: database cleaning
| Neo4j |
| Ohm |
| Redis |
| Sequel |
Scenario Outline: ruby app
Given I am using <ORM>

View File

@ -18,6 +18,8 @@ Feature: multiple database cleaning
| DataMapper | truncation |
| DataMapper | transaction |
| MongoMapper | truncation |
| Sequel | truncation |
| Sequel | transaction |
Scenario Outline: ruby app
Given I am using <ORM>

View File

@ -18,6 +18,7 @@ Feature: database cleaning using multiple ORMs
| ActiveRecord | Neo4j |
| ActiveRecord | Ohm |
| ActiveRecord | Redis |
| ActiveRecord | Sequel |
| CouchPotato | ActiveRecord |
| CouchPotato | DataMapper |
| CouchPotato | Mongoid |
@ -25,6 +26,7 @@ Feature: database cleaning using multiple ORMs
| CouchPotato | Neo4j |
| CouchPotato | Ohm |
| CouchPotato | Redis |
| CouchPotato | Sequel |
| DataMapper | ActiveRecord |
| DataMapper | CouchPotato |
| DataMapper | Mongoid |
@ -32,6 +34,7 @@ Feature: database cleaning using multiple ORMs
| DataMapper | Neo4j |
| DataMapper | Ohm |
| DataMapper | Redis |
| DataMapper | Sequel |
| Mongoid | ActiveRecord |
| Mongoid | CouchPotato |
| Mongoid | DataMapper |
@ -39,6 +42,7 @@ Feature: database cleaning using multiple ORMs
| Mongoid | Neo4j |
| Mongoid | Ohm |
| Mongoid | Redis |
| Mongoid | Sequel |
| MongoMapper | ActiveRecord |
| MongoMapper | CouchPotato |
| MongoMapper | DataMapper |
@ -46,6 +50,7 @@ Feature: database cleaning using multiple ORMs
| MongoMapper | Neo4j |
| MongoMapper | Ohm |
| MongoMapper | Redis |
| MongoMapper | Sequel |
| Neo4j | ActiveRecord |
| Neo4j | CouchPotato |
| Neo4j | DataMapper |
@ -53,6 +58,7 @@ Feature: database cleaning using multiple ORMs
| Neo4j | MongoMapper |
| Neo4j | Ohm |
| Neo4j | Redis |
| Neo4j | Sequel |
| Ohm | ActiveRecord |
| Ohm | CouchPotato |
| Ohm | DataMapper |
@ -60,13 +66,23 @@ Feature: database cleaning using multiple ORMs
| Ohm | MongoMapper |
| Ohm | Neo4j |
| Ohm | Redis |
| Ohm | Sequel |
| Redis | ActiveRecord |
| Redis | DataMapper |
| Redis | CouchPotato |
| Redis | DataMapper |
| Redis | Mongoid |
| Redis | MongoMapper |
| Redis | Neo4j |
| Redis | Ohm |
| Redis | Sequel |
| Sequel | ActiveRecord |
| Sequel | CouchPotato |
| Sequel | DataMapper |
| Sequel | Mongoid |
| Sequel | MongoMapper |
| Sequel | Neo4j |
| Sequel | Ohm |
| Sequel | Redis |
Scenario Outline: ruby app
Given I am using <ORM1> and <ORM2>