connection_ninja 0.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +4 -0
- data/MIT-License +22 -0
- data/Manifest +13 -0
- data/README.markdown +51 -0
- data/Rakefile +15 -0
- data/config/database.example.yml +6 -0
- data/connection_ninja.gemspec +30 -0
- data/lib/connection_ninja.rb +35 -0
- data/spec/connection_ninja_spec.rb +46 -0
- data/spec/fixtures/models.rb +7 -0
- data/spec/fixtures/structure.sql +39 -0
- data/spec/spec_helper.rb +28 -0
- data/spec/test_helper.rb +42 -0
- metadata +74 -0
data/CHANGELOG
ADDED
data/MIT-License
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2009 Chris Herring
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person
|
4
|
+
obtaining a copy of this software and associated documentation
|
5
|
+
files (the "Software"), to deal in the Software without
|
6
|
+
restriction, including without limitation the rights to use,
|
7
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
8
|
+
copies of the Software, and to permit persons to whom the
|
9
|
+
Software is furnished to do so, subject to the following
|
10
|
+
conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be
|
13
|
+
included in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
17
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
19
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
20
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
21
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
data/Manifest
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
CHANGELOG
|
2
|
+
config/database.example.yml
|
3
|
+
connection_ninja.gemspec
|
4
|
+
lib/connection_ninja.rb
|
5
|
+
Manifest
|
6
|
+
MIT-License
|
7
|
+
Rakefile
|
8
|
+
README.markdown
|
9
|
+
spec/connection_ninja_spec.rb
|
10
|
+
spec/fixtures/models.rb
|
11
|
+
spec/fixtures/structure.sql
|
12
|
+
spec/spec_helper.rb
|
13
|
+
spec/test_helper.rb
|
data/README.markdown
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
Introduction
|
2
|
+
============
|
3
|
+
|
4
|
+
Connection Ninja is a really simple gem to help connection to multiple databases. I wrote it to help with the way we were connected a new application at work to our older application that holds the data we needed to do reporting on. We have code (models) that is shared between those two applications, and I wanted them to be able to connect to the correct database regardless which app they were in, and therefore which database.yml they were reading.
|
5
|
+
|
6
|
+
Configuration
|
7
|
+
=============
|
8
|
+
|
9
|
+
class MyModel < ActiveRecord::Base
|
10
|
+
use_connection_ninja(:database)
|
11
|
+
end
|
12
|
+
|
13
|
+
Then in your after your normal configuration in database.yml:
|
14
|
+
|
15
|
+
database_development:
|
16
|
+
adapter: postgresql
|
17
|
+
database: database_name
|
18
|
+
user: username
|
19
|
+
|
20
|
+
database_test:
|
21
|
+
adapter: postgresql
|
22
|
+
database: database_name
|
23
|
+
user: username
|
24
|
+
|
25
|
+
database_production:
|
26
|
+
adapter: postgresql
|
27
|
+
database: database_name
|
28
|
+
user: username
|
29
|
+
|
30
|
+
Connection ninja takes the database name you pass in and appends the environment on the end so it is important that you name the entries in database.yml as "#{database}_#{RAILS_ENV}" or it won't work.
|
31
|
+
|
32
|
+
WHY?
|
33
|
+
===
|
34
|
+
|
35
|
+
If you look at the code you will probably wonder why I didn't just call establish_connection "database_#{RAILS_ENV}", well at first I went about it differently, and thought I needed it. Now I realised maybe I don't, but it was a great exercise in learning how to write a gem and I learned a few things about ActiveRecord along the way which was great. It also gave me IMO a nice clean way to connect a bunch of models to the right database regardless of which of my apps the code was sitting in.
|
36
|
+
|
37
|
+
How I used it
|
38
|
+
=============
|
39
|
+
|
40
|
+
To get my 20 or 30 models to all use one connection I used a simple abstract class to do it. I also got to learn about these along the way
|
41
|
+
|
42
|
+
class OtherConnection < ActiveRecord::Base
|
43
|
+
self.abstract_class = true
|
44
|
+
use_connection_ninja(:database)
|
45
|
+
end
|
46
|
+
|
47
|
+
So I could:
|
48
|
+
|
49
|
+
class ModelFromOtherDB < OtherConnection
|
50
|
+
|
51
|
+
end
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'echoe'
|
4
|
+
|
5
|
+
Echoe.new('connection_ninja', '0.3.3') do |p|
|
6
|
+
p.description = "Inject a new connection to an alternate database into an ActiveRecord Model at runtime"
|
7
|
+
p.summary = ""
|
8
|
+
p.url = "http://github.com/cherring/connection_ninja"
|
9
|
+
p.author = "Chris Herring"
|
10
|
+
p.email = "chris.herring.iphone@gmail.com"
|
11
|
+
p.ignore_pattern = ["tmp/*", "script/*"]
|
12
|
+
p.development_dependencies = []
|
13
|
+
end
|
14
|
+
|
15
|
+
Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{connection_ninja}
|
5
|
+
s.version = "0.3.3"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Chris Herring"]
|
9
|
+
s.date = %q{2009-06-08}
|
10
|
+
s.description = %q{Inject a new connection to an alternate database into an ActiveRecord Model at runtime}
|
11
|
+
s.email = %q{chris.herring.iphone@gmail.com}
|
12
|
+
s.extra_rdoc_files = ["CHANGELOG", "lib/connection_ninja.rb", "README.markdown"]
|
13
|
+
s.files = ["CHANGELOG", "config/database.example.yml", "connection_ninja.gemspec", "lib/connection_ninja.rb", "Manifest", "MIT-License", "Rakefile", "README.markdown", "spec/connection_ninja_spec.rb", "spec/fixtures/models.rb", "spec/fixtures/structure.sql", "spec/spec_helper.rb", "spec/test_helper.rb"]
|
14
|
+
s.homepage = %q{http://github.com/cherring/connection_ninja}
|
15
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Connection_ninja", "--main", "README.markdown"]
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
s.rubyforge_project = %q{connection_ninja}
|
18
|
+
s.rubygems_version = %q{1.3.3}
|
19
|
+
s.summary = %q{Inject a new connection to an alternate database into an ActiveRecord Model at runtime}
|
20
|
+
|
21
|
+
if s.respond_to? :specification_version then
|
22
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
23
|
+
s.specification_version = 3
|
24
|
+
|
25
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
26
|
+
else
|
27
|
+
end
|
28
|
+
else
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module ConnectionNinja
|
2
|
+
|
3
|
+
def self.included(base)
|
4
|
+
base.extend ClassMethods
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
#These are just here to use to run my specs, will work out how to fake these later
|
9
|
+
# RAILS_ENV = "development"
|
10
|
+
# RAILS_ROOT = File.dirname(__FILE__) + "/.."
|
11
|
+
# ActiveRecord::Base.configurations = { 'alternate_development' => {
|
12
|
+
# 'adapter' => 'postgresql',
|
13
|
+
# 'database' => 'connection_ninja_alternate',
|
14
|
+
# 'host' => 'localhost'
|
15
|
+
# }
|
16
|
+
# }
|
17
|
+
|
18
|
+
def use_connection_ninja(config_name)
|
19
|
+
connect_to_db(config(config_name))
|
20
|
+
end
|
21
|
+
|
22
|
+
def config(config)
|
23
|
+
ActiveRecord::Base.configurations["#{config.to_s}_#{RAILS_ENV}"]
|
24
|
+
end
|
25
|
+
|
26
|
+
def connect_to_db(config)
|
27
|
+
establish_connection(config)
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
ActiveRecord::Base.class_eval do
|
34
|
+
include ConnectionNinja
|
35
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'spec/spec_helper'
|
2
|
+
require 'connection_ninja'
|
3
|
+
include ConnectionNinja
|
4
|
+
|
5
|
+
describe Order do
|
6
|
+
it "should be connected to the default database" do
|
7
|
+
Order.connection.current_database.should == "connection_ninja"
|
8
|
+
end
|
9
|
+
|
10
|
+
end
|
11
|
+
|
12
|
+
describe Customer do
|
13
|
+
it "should be connected to the alternate database" do
|
14
|
+
Customer.connection.current_database.should == "connection_ninja_alternate"
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should respond to use_connection_ninja" do
|
18
|
+
Customer.respond_to?(:use_connection_ninja).should == true
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe Customer,"config" do
|
23
|
+
|
24
|
+
it "should return the database config" do
|
25
|
+
Customer.config(:alternate).should == ActiveRecord::Base.configurations['alternate_development']
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
describe Customer,"use_connection_ninja" do
|
31
|
+
|
32
|
+
it "should call connect_to_db" do
|
33
|
+
Customer.stub!(:config).and_return(ActiveRecord::Base.configurations['alternate_development'])
|
34
|
+
Customer.should_receive(:connect_to_db).with(Customer.config)
|
35
|
+
Customer.use_connection_ninja(:alternate)
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
describe Customer,"connect_to_db" do
|
41
|
+
|
42
|
+
it "should call establish_connection" do
|
43
|
+
Customer.should_receive(:establish_connection)
|
44
|
+
Customer.connect_to_db('alternate_development')
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
DROP TABLE IF EXISTS orders;
|
2
|
+
|
3
|
+
DROP SEQUENCE IF EXISTS orders_id_seq;
|
4
|
+
|
5
|
+
CREATE SEQUENCE orders_id_seq
|
6
|
+
START WITH 1
|
7
|
+
INCREMENT BY 1
|
8
|
+
NO MAXVALUE
|
9
|
+
NO MINVALUE
|
10
|
+
CACHE 1;
|
11
|
+
|
12
|
+
CREATE TABLE orders (
|
13
|
+
id integer NOT NULL default nextval('orders_id_seq'),
|
14
|
+
ordered_on timestamp,
|
15
|
+
fulfilled_on timestamp,
|
16
|
+
shipped_on timestamp,
|
17
|
+
created_at timestamp without time zone,
|
18
|
+
updated_at timestamp without time zone
|
19
|
+
);
|
20
|
+
|
21
|
+
DROP TABLE IF EXISTS customers;
|
22
|
+
|
23
|
+
DROP SEQUENCE IF EXISTS customers_id_seq;
|
24
|
+
|
25
|
+
CREATE SEQUENCE customers_id_seq
|
26
|
+
START WITH 1
|
27
|
+
INCREMENT BY 1
|
28
|
+
NO MAXVALUE
|
29
|
+
NO MINVALUE
|
30
|
+
CACHE 1;
|
31
|
+
|
32
|
+
CREATE TABLE customers (
|
33
|
+
id integer NOT NULL default nextval('customers_id_seq'),
|
34
|
+
given_name char(255),
|
35
|
+
family_name char(255),
|
36
|
+
created_at timestamp without time zone,
|
37
|
+
updated_at timestamp without time zone
|
38
|
+
);
|
39
|
+
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# Inspiration gained from Thinking Sphinx's test suite via Ryan Bigg's by_star test suite.
|
2
|
+
|
3
|
+
$:.unshift File.dirname(__FILE__) + '/../lib'
|
4
|
+
|
5
|
+
require 'rubygems'
|
6
|
+
require 'activerecord'
|
7
|
+
require 'connection_ninja'
|
8
|
+
require 'spec/fixtures/models'
|
9
|
+
require 'spec/test_helper'
|
10
|
+
|
11
|
+
FileUtils.mkdir_p "#{Dir.pwd}/tmp"
|
12
|
+
|
13
|
+
ActiveRecord::Base.logger = Logger.new(StringIO.new)
|
14
|
+
|
15
|
+
Spec::Runner.configure do |config|
|
16
|
+
test = TestHelper.new
|
17
|
+
test.setup_postgresql
|
18
|
+
RAILS_ENV = "development"
|
19
|
+
RAILS_ROOT = File.dirname(__FILE__) + "/.."
|
20
|
+
|
21
|
+
# This is to simulate what would be loaded out of datbase.yml for ActiveRecord
|
22
|
+
ActiveRecord::Base.configurations = { 'alternate_development' => {
|
23
|
+
'adapter' => 'postgresql',
|
24
|
+
'database' => 'connection_ninja_alternate',
|
25
|
+
'host' => 'localhost'
|
26
|
+
}
|
27
|
+
}
|
28
|
+
end
|
data/spec/test_helper.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
class TestHelper
|
2
|
+
attr_accessor :host, :username, :password
|
3
|
+
attr_reader :path
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@host = "localhost"
|
7
|
+
@username = "chris" #Insert username for db here
|
8
|
+
@password = ""
|
9
|
+
|
10
|
+
@path = File.expand_path(File.dirname(__FILE__))
|
11
|
+
end
|
12
|
+
|
13
|
+
def setup_postgresql
|
14
|
+
ActiveRecord::Base.establish_connection(
|
15
|
+
:adapter => 'postgresql',
|
16
|
+
:database => 'connection_ninja',
|
17
|
+
:username => @username,
|
18
|
+
:password => @password,
|
19
|
+
:host => @host
|
20
|
+
)
|
21
|
+
ActiveRecord::Base.logger = Logger.new(File.open("tmp/activerecord.log", "a"))
|
22
|
+
|
23
|
+
structure = File.open("spec/fixtures/structure.sql") { |f| f.read.chomp }
|
24
|
+
structure.split(';').each { |table|
|
25
|
+
ActiveRecord::Base.connection.execute table
|
26
|
+
}
|
27
|
+
|
28
|
+
10.times do
|
29
|
+
Order.create(:ordered_on => 2.weeks.ago)
|
30
|
+
end
|
31
|
+
|
32
|
+
10.times do
|
33
|
+
Order.create(:ordered_on => 3.weeks.ago, :fulfilled_on => 1.day.ago)
|
34
|
+
end
|
35
|
+
|
36
|
+
5.times do
|
37
|
+
Order.create(:ordered_on => 1.weeks.ago)
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: connection_ninja
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.3.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Chris Herring
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-06-08 00:00:00 +10:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Inject a new connection to an alternate database into an ActiveRecord Model at runtime
|
17
|
+
email: chris.herring.iphone@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- CHANGELOG
|
24
|
+
- lib/connection_ninja.rb
|
25
|
+
- README.markdown
|
26
|
+
files:
|
27
|
+
- CHANGELOG
|
28
|
+
- config/database.example.yml
|
29
|
+
- connection_ninja.gemspec
|
30
|
+
- lib/connection_ninja.rb
|
31
|
+
- Manifest
|
32
|
+
- MIT-License
|
33
|
+
- Rakefile
|
34
|
+
- README.markdown
|
35
|
+
- spec/connection_ninja_spec.rb
|
36
|
+
- spec/fixtures/models.rb
|
37
|
+
- spec/fixtures/structure.sql
|
38
|
+
- spec/spec_helper.rb
|
39
|
+
- spec/test_helper.rb
|
40
|
+
has_rdoc: true
|
41
|
+
homepage: http://github.com/cherring/connection_ninja
|
42
|
+
licenses: []
|
43
|
+
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options:
|
46
|
+
- --line-numbers
|
47
|
+
- --inline-source
|
48
|
+
- --title
|
49
|
+
- Connection_ninja
|
50
|
+
- --main
|
51
|
+
- README.markdown
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: "0"
|
59
|
+
version:
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: "1.2"
|
65
|
+
version:
|
66
|
+
requirements: []
|
67
|
+
|
68
|
+
rubyforge_project: connection_ninja
|
69
|
+
rubygems_version: 1.3.4
|
70
|
+
signing_key:
|
71
|
+
specification_version: 3
|
72
|
+
summary: Inject a new connection to an alternate database into an ActiveRecord Model at runtime
|
73
|
+
test_files: []
|
74
|
+
|