tako 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a1b4936efb2bb9c024c85a7e41d835a1c0c23041
4
- data.tar.gz: d8392ebb554a5186db289f7ecfcdc7f81ebf9188
3
+ metadata.gz: dff98c4a83f4ce6d0ed8154a4307b94c38589e1d
4
+ data.tar.gz: d671907d64c06501c442293125f728fa2d13c517
5
5
  SHA512:
6
- metadata.gz: 7e6af9b9c02d28b361469c626ec181bddfc448804aa5444a4f16b19c813ab0f5e6a8a327712f1c3d6098853e573dc50cb333f9d87aae425afa05e0c44ca977d7
7
- data.tar.gz: afc1f92c71a81c670b3ad139a0a654f629d51c96e53853511b60742c258342cca5b6e0d690f2efe8bfa93ad239c080c990b4399057bd0226f00d9872a5aa9dbc
6
+ metadata.gz: 188ddfbd46646a1103b29f2fd4391db979d89d5a23a1fbe36bb6181e36fc8db3afda0aea71dafdc520e4b79f392733895ddd05dd7e27ca21b55995e1417ddfb8
7
+ data.tar.gz: 9f4f1f4f50d3e929a70ade86bc7cd22bd036c72d2c83ad4af4e0f6af05c3fba76aeed92c5f5cf38169bad925530c3168761844f61036edc721b45f83139b35bd
data/.gitignore CHANGED
@@ -8,3 +8,4 @@
8
8
  /spec/reports/
9
9
  /tmp/
10
10
  *.swp
11
+ *.gem
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # Tako
2
2
 
3
3
  Provides Horizontal-Sharding for ActiveRecord.
4
+ Rails 5 Ready.
4
5
 
5
6
  ## Installation
6
7
 
@@ -50,6 +51,11 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
50
51
 
51
52
  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).
52
53
 
54
+ ### Run test
55
+
56
+ Run `bundle exec rake` to run rspec
57
+ Run `bundle exec rake` in `spec/dummy5` will run rspec with rails 5
58
+
53
59
  ## Contributing
54
60
 
55
61
  Bug reports and pull requests are welcome on GitHub at https://github.com/the40san/tako. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
data/Rakefile CHANGED
@@ -1,6 +1,8 @@
1
1
  require "bundler/gem_tasks"
2
2
  require "rspec/core/rake_task"
3
3
 
4
- RSpec::Core::RakeTask.new(:spec)
4
+ RSpec::Core::RakeTask.new(:spec) do |t|
5
+ t.exclude_pattern = "spec/dummy*/**/*"
6
+ end
5
7
 
6
8
  task :default => :spec
@@ -0,0 +1,15 @@
1
+ module Tako
2
+ module MultiShardExecution
3
+ def with_all_shards
4
+ shard_names.each do |shard_name|
5
+ shard(shard_name) do
6
+ yield
7
+ end
8
+ end
9
+ end
10
+
11
+ def shard_names
12
+ Tako::Repository.shard_names
13
+ end
14
+ end
15
+ end
@@ -10,7 +10,13 @@ module Tako
10
10
 
11
11
  def method_missing(method, *args)
12
12
  @proxy.in_proxy do
13
- base_object.send(method, *args)
13
+ if block_given?
14
+ base_object.send(method, *args) do
15
+ yield
16
+ end
17
+ else
18
+ base_object.send(method, *args)
19
+ end
14
20
  end
15
21
  end
16
22
 
@@ -0,0 +1,15 @@
1
+ if defined?(::Rails)
2
+ module Tako
3
+ class Railtie < Rails::Railtie
4
+ rake_tasks do
5
+ load "tako/railties/databases.rake"
6
+ end
7
+
8
+ initializer "tako.initialize_database" do
9
+ ActiveSupport.on_load(:active_record) do
10
+ Tako.load_connections_from_yaml
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,57 @@
1
+ require 'tako'
2
+
3
+ namespace :db do
4
+ namespace :tako do
5
+ task :load_config do
6
+ Tako.load_connections_from_yaml
7
+ end
8
+
9
+ task :check_protected_environments => [:environment, :load_config] do
10
+ ActiveRecord::Tasks::DatabaseTasks.check_protected_environments!
11
+ end
12
+
13
+ task :create do
14
+ (Tako.config[Tako.env] || []).values.each do |conf|
15
+ ActiveRecord::Tasks::DatabaseTasks.create(conf)
16
+ end
17
+ end
18
+
19
+ task :migrate => [:environment] do
20
+ paths = ActiveRecord::Tasks::DatabaseTasks.migrations_paths
21
+ # load all migration files
22
+ paths.each do |path|
23
+ Dir[File.join(path, "*")].each {|f| require f }
24
+ end
25
+
26
+ Tako.with_all_shards do
27
+ version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
28
+ revert = !!ENV["DOWN_MIGRATION"]
29
+
30
+ migrations = if version
31
+ [
32
+ ActiveRecord::Migrator.migrations(paths).find {|proxy| proxy.version == version }.name.constantize
33
+ ]
34
+ else
35
+ ActiveRecord::Migrator.migrations(paths).map(&:name).map(&:constantize)
36
+ end
37
+
38
+ ActiveRecord::Migration.run(*migrations, revert: revert)
39
+ end
40
+ end
41
+
42
+ namespace :migrate do
43
+ task :up => ['db:tako:migrate']
44
+
45
+ task :down => [:environment] do
46
+ ENV["DOWN_MIGRATION"] = "y"
47
+ Rake::Task['db:tako:migrate'].invoke
48
+ end
49
+ end
50
+
51
+ task :drop => [:load_config, :check_protected_environments] do
52
+ (Tako.config[Tako.env] || []).values.each do |conf|
53
+ ActiveRecord::Tasks::DatabaseTasks.drop(conf)
54
+ end
55
+ end
56
+ end
57
+ end
@@ -2,30 +2,31 @@ module Tako
2
2
  class Repository
3
3
  class << self
4
4
  def proxy_configs
5
- @proxy_configs
5
+ @proxy_configs ||= {}
6
6
  end
7
7
 
8
8
  def proxy_connections
9
- @proxy_connections
9
+ @proxy_connections ||= {}
10
10
  end
11
11
 
12
12
  def add(shard_name, conf)
13
- @proxy_configs ||= {}
14
- @proxy_connections ||= {}
15
-
16
13
  shard_name = shard_name.to_sym
17
- return if @proxy_configs[shard_name]
14
+ return if proxy_configs[shard_name]
18
15
 
19
16
  temporary_class = Class.new(ActiveRecord::Base)
20
17
  const_set("TAKO_AR_CLASS_#{shard_name.upcase}", temporary_class)
21
18
  temporary_class.establish_connection(conf)
22
19
 
23
- @proxy_connections[shard_name] = temporary_class.connection
24
- @proxy_configs[shard_name] = conf
20
+ proxy_connections[shard_name] = temporary_class.connection
21
+ proxy_configs[shard_name] = conf
25
22
  end
26
23
 
27
24
  def shard(shard_name, base = nil)
28
- Proxy.new(shard_name, @proxy_connections[shard_name.to_sym], base)
25
+ Proxy.new(shard_name, proxy_connections[shard_name.to_sym], base)
26
+ end
27
+
28
+ def shard_names
29
+ proxy_configs.keys
29
30
  end
30
31
  end
31
32
  end
data/lib/tako/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Tako
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/tako.rb CHANGED
@@ -6,8 +6,11 @@ require "tako/repository"
6
6
  require "tako/proxy_stack"
7
7
  require "tako/proxy"
8
8
  require "tako/query_chain"
9
+ require "tako/multi_shard_execution"
9
10
 
10
11
  module Tako
12
+ extend MultiShardExecution
13
+
11
14
  class << self
12
15
  def shard(shard_name)
13
16
  if block_given?
@@ -19,11 +22,19 @@ module Tako
19
22
  end
20
23
  end
21
24
 
22
- def load_connections_from_yaml(config = Tako::Config.shards_yml)
23
- (config[:tako][Tako::Config.env] || []).each do |shard_name, conf|
25
+ def load_connections_from_yaml
26
+ (config[env] || []).each do |shard_name, conf|
24
27
  Tako::Repository.add(shard_name, conf)
25
28
  end
26
29
  end
30
+
31
+ def config
32
+ Tako::Config.shards_yml[:tako]
33
+ end
34
+
35
+ def env
36
+ Tako::Config.env
37
+ end
27
38
  end
28
39
  end
29
40
 
@@ -36,3 +47,5 @@ end
36
47
  ActiveRecord::Base.class_eval do
37
48
  extend Tako::ActiveRecordExt::Base::Extend
38
49
  end
50
+
51
+ require 'tako/railtie' if defined?(::Rails)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tako
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Masashi AKISUE
@@ -100,9 +100,12 @@ files:
100
100
  - lib/tako.rb
101
101
  - lib/tako/active_record_ext.rb
102
102
  - lib/tako/config.rb
103
+ - lib/tako/multi_shard_execution.rb
103
104
  - lib/tako/proxy.rb
104
105
  - lib/tako/proxy_stack.rb
105
106
  - lib/tako/query_chain.rb
107
+ - lib/tako/railtie.rb
108
+ - lib/tako/railties/databases.rake
106
109
  - lib/tako/repository.rb
107
110
  - lib/tako/version.rb
108
111
  - tako.gemspec