Skip to content
This repository was archived by the owner on Dec 1, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
engines:
rubocop:
enabled: true
bundler-audit:
enabled: true
brakeman:
enabled: true
eslint:
enabled: false
csslint:
enabled: false
duplication:
enabled: true
config:
languages:
- ruby
- javascript
ratings:
paths:
- "lib/**"
- "examples/**"
exclude_paths:
- "spec/**/*"
- "**/vendor/**/*"
- ".bundle/"
- "tmp/"
18 changes: 13 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
addons:
postgresql: "9.3"
postgresql: '9.3'
code_climate:
repo_token:
secure: YEh2fV5IEqmR5jmC7mB9ILhQ90qOYKQ9i4pX9NzmWB4DfEo2VJjiiC3D6TJPNb3mqBLgbOyfrvAJ4RrJmWpK0PEDcZv+U9OZ0dEVvWPQ6yy2qBtF/PssIuw3pkgyKxzQpteybAXd3rbwK6Bel66ndCGLmF4gwCKCLuNNRuu38Eo=
language: ruby
rvm:
- 2.0.0-p247
- 2.1.2
before_script: "bundle exec rake sequel:db:create"
script: "bundle exec rspec"
- 2.0.0-p648
- 2.1.10
- 2.2.7
- 2.3.4
- 2.4.1
before_script: bundle exec rake sequel:db:create
script: bundle exec rspec
after_success:
- bundle exec codeclimate-test-reporter
notifications:
email: false
6 changes: 0 additions & 6 deletions CONTRIBUTORS.md

This file was deleted.

2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ source 'https://rubygems.org'
gemspec

group :test do
gem 'simplecov'
gem 'rspec'
gem 'rspec-benchmark'
gem 'mocha', require: false
gem 'pry-nav'
end
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
sequel-schema-sharding
======================

[![Gem Version](https://badge.fury.io/rb/sequel-schema-sharding.png)](http://badge.fury.io/rb/sequel-schema-sharding)
[![Build Status](https://travis-ci.org/wanelo/sequel-schema-sharding.png?branch=master)](https://travis-ci.org/wanelo/sequel-schema-sharding)
[![Code Climate](https://codeclimate.com/github/wanelo/sequel-schema-sharding.png)](https://codeclimate.com/github/wanelo/sequel-schema-sharding)
[![Gem Version](https://badge.fury.io/rb/sequel-schema-sharding.svg)](https://badge.fury.io/rb/sequel-schema-sharding)
[![Build Status](https://travis-ci.org/wanelo/sequel-schema-sharding.svg?branch=ruby-2.4.sequel-4)](https://travis-ci.org/wanelo/sequel-schema-sharding)
[![Code Climate](https://codeclimate.com/github/wanelo/sequel-schema-sharding/badges/gpa.svg)](https://codeclimate.com/github/wanelo/sequel-schema-sharding)
[![Test Coverage](https://codeclimate.com/github/wanelo/sequel-schema-sharding/badges/coverage.svg)](https://codeclimate.com/github/wanelo/sequel-schema-sharding/coverage)

Horizontally shard PostgreSQL tables with the Sequel gem, where each shard
lives in its own PostgreSQL schema.
Expand Down Expand Up @@ -359,3 +360,11 @@ may have been transposed between queries.
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request

Contributors
============

* [James Hart](https://github.com/hjhart)
* [Paul Henry](https://github.com/f3nry)
* [Eric Saxby](https://github.com/sax)
* [Konstatin Gredeskoul](https://github.com/kigster)
7 changes: 7 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
require "bundler/gem_tasks"
import 'lib/sequel/tasks/test.rake'
import 'lib/sequel/tasks/reset.rake'

task :default do
system %Q{bundle exec rspec --exclude-pattern="spec/**/*database_manager_spec.rb"}
end



4 changes: 3 additions & 1 deletion lib/sequel/schema-sharding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
require 'sequel/schema-sharding/connection_manager'
require 'sequel/schema-sharding/database_manager'
require 'sequel/schema-sharding/ring'
require 'sequel/schema-sharding/finder'
require 'sequel/schema-sharding/shard_finder'
require 'sequel/schema-sharding/monkey_patching'
require 'sequel/schema-sharding/model'
require 'sequel/schema-sharding/logger_proxy'
require 'sequel/schema-sharding/connection_strategies/random'

Sequel.split_symbols = true if defined?(Sequel) && Sequel.respond_to?(:split_symbols=)

module Sequel
module SchemaSharding
def self.config
Expand Down
18 changes: 13 additions & 5 deletions lib/sequel/schema-sharding/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,9 @@ def sharded_column
# run by the query into the model.
def shard_for(id)
result = self.result_for(id)
ds = result.connection[schema_and_table(result)]
ds.row_proc = self
ds = result.connection[schema_and_table(result)].clone(row_proc: self, model: self)
dataset_method_modules.each { |m| ds.instance_eval { extend(m) } }
ds.shard_number = result.shard_number
ds.model = self
ds.tap do |d|
Sequel::SchemaSharding::DTraceProvider.provider.shard_for.fire(id.to_s, d.shard_number, self.table_name_s) if Sequel::SchemaSharding::DTraceProvider.provider.shard_for.enabled?
end
Expand All @@ -66,9 +64,19 @@ def read_only_shard_for(id)
end
end

# Dangerous method useful in tests
def truncate_shards(*ids)
if %w(RACK_ENV RAILS_ENV).any? { |v| ENV[v].eql?('production') }
raise ArgumentError, '#truncate_shards is not meant for production'
end

ids.each{ |id| read_only_shard_for(id).truncate }
ids.size
end

# The result of a lookup for the given id. See Sequel::SchemaSharding::Finder::Result
def result_for(id)
Sequel::SchemaSharding::Finder.instance.lookup(self.implicit_table_name, id)
Sequel::SchemaSharding::ShardFinder.instance.lookup(self.implicit_table_name, id)
end

# Construct the schema and table for use in a dataset.
Expand All @@ -78,7 +86,7 @@ def schema_and_table(result)

def create(values = {}, &block)
sharded_column_value = values[sharded_column]
shard_number = result_for(sharded_column_value).shard_number
shard_number = result_for(sharded_column_value).shard_number
super.tap do |m|
m.values[:shard_number] = shard_number
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Sequel
module SchemaSharding
class Finder
class ShardFinder
class Result
attr_reader :connection, :schema, :shard_number

Expand Down
2 changes: 1 addition & 1 deletion lib/sequel/schema-sharding/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Sequel
module SchemaSharding
VERSION = "0.13.4"
VERSION = '1.0.2'
end
end
11 changes: 6 additions & 5 deletions sequel-schema-sharding.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ require 'sequel/schema-sharding/version'
Gem::Specification.new do |spec|
spec.name = 'sequel-schema-sharding'
spec.version = Sequel::SchemaSharding::VERSION
spec.authors = ['Paul Henry', 'James Hart', 'Eric Saxby']
spec.email = %w(dev@wanelo.com)
spec.authors = ['Paul Henry', 'James Hart', 'Eric Saxby', 'Konstantin Gredeskoul']
spec.email = %w(dev@wanelo.com kigster@gmail.com)
spec.description = %q{}
spec.summary = %q{Create horizontally sharded Sequel models with Postgres}
spec.homepage = 'https://github.com/wanelo/sequel-schema-sharding'
Expand All @@ -18,11 +18,12 @@ Gem::Specification.new do |spec|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = %w(lib)

spec.add_dependency 'sequel'
spec.add_dependency 'sequel', '~> 4'
spec.add_dependency 'pg'
spec.add_dependency 'sequel-replica-failover', '>= 2.0.0'
spec.add_dependency 'sequel-replica-failover', '~> 2'
spec.add_dependency 'ruby-usdt', '>= 0.2.2'

spec.add_development_dependency 'bundler', '~> 1.3'
spec.add_development_dependency 'codeclimate-test-reporter'
spec.add_development_dependency 'bundler'
spec.add_development_dependency 'rake'
end
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
Sequel.migration do
up do
create_table(migration_schema_for_table(:artists)) do
primary_key :id
Integer :artist_id
Integer :artist_id, unique: true
String :name, :null=>false
end
end
Expand Down
8 changes: 4 additions & 4 deletions spec/schema-sharding/connection_manager_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
describe '#[]' do
it 'returns a valid connection instance for the specified physical shard' do
expect(subject['shard1']).to be_a(Sequel::Postgres::Database)
subject['shard1'].execute("SELECT 1")
subject['shard1'].execute('SELECT 1')
expect(subject['shard2']).to be_a(Sequel::Postgres::Database)
end

Expand Down Expand Up @@ -45,7 +45,7 @@

it 'executes a select against a replica' do
shard = subject['shard2']
ds = shard[:"sequel_explosions_boof_pickles_3__artists"]
ds = shard[:'sequel_explosions_boof_pickles_3__artists']
shard.expects(:execute).once.with(anything, server: :read_only)
ds.first
end
Expand All @@ -58,8 +58,8 @@
end
end

describe "#default_dataset_for" do
it "returns a dataset scoped to a configured schema" do
describe '#default_dataset_for' do
it 'returns a dataset scoped to a configured schema' do
# TODO ConnectionManager is dependent on global state from Sequel::SchemaSharding.config.
# This should be deconstructed to allow for injection of a mock config for testing.
dataset = subject.default_dataset_for("artists")
Expand Down
Loading