multitenant-mysql 1.2.0 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,3 +4,6 @@
4
4
  1.2.0 (March 20, 2013)
5
5
  * refactored some code
6
6
  * changed config file structure little bit, for older versions just update the config file
7
+
8
+ 1.2.1 (April 1, 2013)
9
+ * fixed bug (https://github.com/eugenekorpan/multitenant-mysql/issues/25)
@@ -1,10 +1,11 @@
1
1
  class ActiveRecord::Base
2
2
  def self.acts_as_tenants_bucket
3
3
  after_create do
4
- config = Rails.configuration.database_configuration
5
- password = config[Rails.env]["password"]
6
- ActiveRecord::Base.connection.execute "GRANT ALL PRIVILEGES ON *.* TO '#{self.name}'@'localhost' IDENTIFIED BY '#{password}' WITH GRANT OPTION;"
7
- ActiveRecord::Base.connection.execute "flush privileges;"
4
+ password = Multitenant::Mysql::DB.configs['password']
5
+ tenant_name = self.send(Multitenant::Mysql.configs.bucket_field)
6
+ connection = Multitenant::Mysql::DB.connection
7
+ connection.execute "GRANT ALL PRIVILEGES ON *.* TO '#{tenant_name}'@'localhost' IDENTIFIED BY '#{password}' WITH GRANT OPTION;"
8
+ connection.execute "flush privileges;"
8
9
  end
9
10
  end
10
11
 
@@ -29,13 +30,16 @@ class ActiveRecord::Base
29
30
  end
30
31
 
31
32
  def self.inherited(child)
33
+ super
34
+
32
35
  model_name = child.to_s
33
36
  if Multitenant::Mysql.configs.models.include? model_name
34
37
  child.send :acts_as_tenant
35
38
  end
36
39
 
37
- if Multitenant::Mysql.configs.tenant == child
40
+ if Multitenant::Mysql.configs.tenant? child
38
41
  child.send :acts_as_tenants_bucket
39
42
  end
43
+
40
44
  end
41
45
  end
@@ -14,6 +14,14 @@ module Multitenant
14
14
  def tenant
15
15
  @bucket.model
16
16
  end
17
+
18
+ def bucket_field
19
+ @bucket.field
20
+ end
21
+
22
+ def tenant?(model)
23
+ tenant == model
24
+ end
17
25
  end
18
26
  end
19
27
  end
@@ -15,6 +15,10 @@ module Multitenant
15
15
  config['username'] = tenant_name.blank? ? 'root' : tenant_name
16
16
  ActiveRecord::Base.establish_connection(config)
17
17
  end
18
+
19
+ def connection
20
+ ActiveRecord::Base.connection
21
+ end
18
22
  end
19
23
  end
20
24
  end
@@ -1,5 +1,5 @@
1
1
  module Multitenant
2
2
  module Mysql
3
- VERSION = "1.2.0"
3
+ VERSION = "1.2.1"
4
4
  end
5
5
  end
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ describe Multitenant::Mysql::Configs::Bucket do
4
+ context '#field' do
5
+ subject { Multitenant::Mysql::Configs::Bucket.new('Post') }
6
+
7
+ before do
8
+ create_table('posts')
9
+ Multitenant::Mysql.stub_chain(:configs, :tenant?).and_return(true)
10
+ Multitenant::Mysql.stub_chain(:configs, :models).and_return([])
11
+ class Post < ActiveRecord::Base; end;
12
+ end
13
+
14
+ it 'should use default' do
15
+ expect(subject.field).to eql(:name)
16
+ end
17
+
18
+ it 'should use specific one' do
19
+ subject.field = 'tenant'
20
+ expect(subject.field).to eql('tenant')
21
+ end
22
+
23
+ it 'should raise error for invalid field' do
24
+ subject.field = 'unexisting column'
25
+ expect { subject.field }.to raise_error(Multitenant::Mysql::InvalidBucketFieldError)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,3 @@
1
+ adapter: mysql2
2
+ username: root
3
+ password:
@@ -11,10 +11,10 @@ describe ActiveRecord::Base do
11
11
  end
12
12
 
13
13
  it 'should redefine table name and primary key and keep original table name' do
14
- ActiveRecord::Base.stub_chain(:connection, :table_exists?).and_return(true)
15
-
16
14
  Multitenant::Mysql.stub_chain(:configs, :models).and_return(['Book'])
17
- Multitenant::Mysql.stub_chain(:configs, :tenant).and_return(false)
15
+ Multitenant::Mysql.stub_chain(:configs, :tenant?).and_return(false)
16
+ create_table('books')
17
+ create_view_for_table('books')
18
18
 
19
19
  class Book < ActiveRecord::Base
20
20
  end
@@ -23,4 +23,24 @@ describe ActiveRecord::Base do
23
23
  expect(Book.primary_key).to eql('id')
24
24
  expect(Book.original_table_name).to eql('books')
25
25
  end
26
+
27
+ context 'bucket' do
28
+ it 'should create new mysql account' do
29
+ Multitenant::Mysql.stub_chain(:configs, :models).and_return([])
30
+ Multitenant::Mysql.stub_chain(:configs, :tenant?).and_return(true)
31
+ Multitenant::Mysql.stub_chain(:configs, :bucket_field).and_return('name')
32
+ Multitenant::Mysql::DB.stub_chain(:configs, :[]).and_return($cnf['password'])
33
+
34
+ create_table('users')
35
+ create_view_for_table('users')
36
+ class User < ActiveRecord::Base; end;
37
+
38
+ mock_connection = double
39
+ mock_connection.should_receive(:execute).with("GRANT ALL PRIVILEGES ON *.* TO 'default_name'@'localhost' IDENTIFIED BY '#{$cnf['password']}' WITH GRANT OPTION;").once
40
+ mock_connection.should_receive(:execute).with("flush privileges;").once
41
+ Multitenant::Mysql::DB.stub(:connection).and_return(mock_connection)
42
+
43
+ expect(User.create(name: 'default_name', tenant: 'bla bla bla')).to be
44
+ end
45
+ end
26
46
  end
@@ -10,13 +10,12 @@ Coveralls.wear!
10
10
 
11
11
  GEM_ROOT_PATH = File.expand_path('../../', __FILE__)
12
12
 
13
+ require 'yaml'
14
+ $cnf = YAML::load_file(File.join(File.dirname(File.expand_path(__FILE__)), './conf/database.yml'))
15
+ ActiveRecord::Base.establish_connection($cnf)
16
+
13
17
  RSpec.configure do |config|
14
18
  config.before do
15
- ActiveRecord::Base.establish_connection({
16
- adapter: 'mysql2',
17
- username: 'root',
18
- password: ''
19
- })
20
19
  ActiveRecord::Base.connection.execute('drop database if exists `tenant_test`;')
21
20
  ActiveRecord::Base.connection.execute('create database `tenant_test`;')
22
21
  ActiveRecord::Base.connection.execute('use `tenant_test`;')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: multitenant-mysql
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-20 00:00:00.000000000 Z
12
+ date: 2013-04-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -73,6 +73,8 @@ files:
73
73
  - lib/multitenant-mysql/version.rb
74
74
  - multitenant-mysql.gemspec
75
75
  - rails/init.rb
76
+ - spec/bucket_spec.rb
77
+ - spec/conf/database.yml
76
78
  - spec/configs_spec.rb
77
79
  - spec/connection_switcher_spec.rb
78
80
  - spec/db_spec.rb
@@ -109,6 +111,8 @@ signing_key:
109
111
  specification_version: 3
110
112
  summary: Add multi-tenancy to Rails application using MySql views
111
113
  test_files:
114
+ - spec/bucket_spec.rb
115
+ - spec/conf/database.yml
112
116
  - spec/configs_spec.rb
113
117
  - spec/connection_switcher_spec.rb
114
118
  - spec/db_spec.rb