secondbase 0.3.3 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +25 -16
- data/VERSION +1 -1
- data/lib/secondbase/tasks.rb +23 -26
- data/secondbase.gemspec +3 -4
- metadata +17 -17
data/README.rdoc
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
= secondbase
|
2
2
|
|
3
|
-
|
3
|
+
Secondbase adds a second database to your application. While Rails enables you to establish connections to as many external databases as you like, Rails can only manage a single database with it's migration and testing tasks.
|
4
4
|
|
5
5
|
Secondbase enables Rails to work with, and manage, a second database (almost) transparently. As a developer, you should not even realize a second database is in play. Core rake tasks such as db:create, db:migrate, and test will continue to work seamlessly with both databases without you, the developer, having to run any extra rake tasks.
|
6
6
|
|
7
|
-
== Contributing to
|
7
|
+
== Contributing to Secondbase
|
8
8
|
|
9
9
|
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
10
10
|
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
@@ -17,24 +17,22 @@ Secondbase enables Rails to work with, and manage, a second database (almost) tr
|
|
17
17
|
== System Requirements
|
18
18
|
Secondbase now supports Rails 2.x and Rails 3.x.
|
19
19
|
|
20
|
-
== Known Issues
|
21
|
-
The custom migration generator does not work in Rails 2.x. The work around for the time being is to use the normal migration generator provided in Rails 2.x, and then move the migration to db/migrate/secondbase:
|
22
|
-
|
23
|
-
./script/generate migration CreateWidgetsTable
|
24
|
-
mv db/migrate/20101203211338_create_widgets_table.rb db/migrate/secondbase
|
25
|
-
|
26
20
|
== Installation
|
27
21
|
Modify your Gemfile to include Secondbase:
|
28
22
|
|
29
|
-
gem 'secondbase', '0.
|
23
|
+
gem 'secondbase', '0.4.0'
|
30
24
|
|
31
25
|
Run `bundle install`. You thought it would be harder? If you're using Rails 2.x, then yes, a little bit harder. You must also add this to your Rakefile:
|
32
26
|
|
33
|
-
require 'secondbase/tasks'
|
27
|
+
require 'secondbase/tasks' if defined?(Secondbase)
|
34
28
|
|
29
|
+
PLEASE NOTE that if you are using bundler with Rails 2.x, then you simply need to add this to your Rakefile:
|
30
|
+
|
31
|
+
require 'secondbase/tasks'
|
32
|
+
|
35
33
|
== Usage
|
36
34
|
=== Database
|
37
|
-
Configure your database.yml to define your
|
35
|
+
Configure your database.yml to define your Secondbase:
|
38
36
|
|
39
37
|
# Your normal rails definitions...
|
40
38
|
development:
|
@@ -62,9 +60,13 @@ Configure your database.yml to define your secondbase:
|
|
62
60
|
|
63
61
|
=== Migrations
|
64
62
|
SecondBase comes with a generator to assist in managing your migrations
|
65
|
-
|
63
|
+
|
64
|
+
Rails 3.x:
|
66
65
|
rails generator secondbase:migration CreateWidgetsTable
|
67
66
|
|
67
|
+
Rails 2.x:
|
68
|
+
script/generate secondbase_migration CreateWidgetsTable
|
69
|
+
|
68
70
|
The generator will organize your second (data)base migrations alongside of your primary database. The above command will generate the file:
|
69
71
|
|
70
72
|
db/migrate/secondbase/20101203211338_create_widgets_table.rb
|
@@ -99,6 +101,13 @@ SecondBase offers a base model that you can simply extend:
|
|
99
101
|
# you're Widget model is now pointing to your second (data)base table 'widgets'
|
100
102
|
end
|
101
103
|
|
104
|
+
ActiveRecord associations will still work between your Firstbase and Secondbase models!
|
105
|
+
|
106
|
+
# Notice how normal this all looks...
|
107
|
+
class User < ActiveRecord::Base
|
108
|
+
has_many :widgets
|
109
|
+
end
|
110
|
+
|
102
111
|
=== Rake Tasks & Custom Classes
|
103
112
|
If you need to write rake tasks, or some other code that does not extend ActiveRecord, you can simply establish a connection to your second (data)base:
|
104
113
|
|
@@ -115,15 +124,15 @@ Tests can still be run using `rake test` or `rake test:units`, etc. However, if
|
|
115
124
|
|
116
125
|
This is patch to fixtures that will identify the fixtures which belong to models that extend SecondBase::Base. The patch will then ensure that the table descendants of SecondBase::Base get loaded into your second test (data)base.
|
117
126
|
|
118
|
-
At this time,
|
127
|
+
At this time, I can verify that Secondbase works with Fixtures and Machinist. Conceivably, other test factories should work (like Factory Girl), but there is currently no support for this. If you have the time to update this gem to be test object compatible, by all means...
|
119
128
|
|
120
129
|
== TODO
|
121
|
-
- Migration generator needs support for attribute generation (similar to rails generate migration). For example:
|
122
|
-
`rails generate
|
130
|
+
- Migration generator in Rails 3.x needs support for attribute generation (similar to rails generate migration). For example:
|
131
|
+
`rails generate secondbase:migration AddTitleBodyToPost title:string body:text published:boolean`
|
123
132
|
|
124
133
|
- rake db:fixtures:load is currently broken. Like many other things I have fixed, it assumes you only one a single database and attempts to load all fixtures into it. I don't believe we can get away with alias chaining this one, I think (like the Fixtures class), we'll have to freedom patch it.
|
125
134
|
|
126
|
-
- secondbase
|
135
|
+
- secondbase needs to be tested with other factory frameworks (other then Fixtures and Machinist)
|
127
136
|
|
128
137
|
- TESTS!! Not 100% sure how to test the rake tasks, but I can definitely write tests for the classes and generators. I did this in my spare time, sorry...
|
129
138
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.4.0
|
data/lib/secondbase/tasks.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'secondbase'
|
2
|
+
|
2
3
|
####################################
|
3
4
|
#
|
4
5
|
# SecondBase database managment tasks
|
@@ -60,14 +61,14 @@ namespace :db do
|
|
60
61
|
# in a dual db mode, it could be confusing to have two schemas.
|
61
62
|
|
62
63
|
# reset connection to secondbase...
|
63
|
-
|
64
|
+
SecondBase::has_runner(Rails.env)
|
64
65
|
|
65
66
|
# run secondbase migrations...
|
66
67
|
ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true
|
67
68
|
ActiveRecord::Migrator.migrate("db/migrate/#{SecondBase::CONNECTION_PREFIX}/", ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
|
68
69
|
|
69
70
|
# reset connection back to firstbase...
|
70
|
-
|
71
|
+
FirstBase::has_runner(Rails.env)
|
71
72
|
end
|
72
73
|
|
73
74
|
namespace :up do
|
@@ -77,12 +78,12 @@ namespace :db do
|
|
77
78
|
raise "VERSION is required" unless version
|
78
79
|
|
79
80
|
# reset connection to secondbase...
|
80
|
-
|
81
|
+
SecondBase::has_runner(Rails.env)
|
81
82
|
|
82
83
|
ActiveRecord::Migrator.run(:up, "db/migrate/#{SecondBase::CONNECTION_PREFIX}/", version)
|
83
84
|
|
84
85
|
# reset connection back to firstbase...
|
85
|
-
|
86
|
+
FirstBase::has_runner(Rails.env)
|
86
87
|
end
|
87
88
|
end
|
88
89
|
|
@@ -93,12 +94,12 @@ namespace :db do
|
|
93
94
|
raise "VERSION is required" unless version
|
94
95
|
|
95
96
|
# reset connection to secondbase...
|
96
|
-
|
97
|
+
SecondBase::has_runner(Rails.env)
|
97
98
|
|
98
99
|
ActiveRecord::Migrator.run(:down, "db/migrate/#{SecondBase::CONNECTION_PREFIX}/", version)
|
99
100
|
|
100
101
|
# reset connection back to firstbase...
|
101
|
-
|
102
|
+
FirstBase::has_runner(Rails.env)
|
102
103
|
end
|
103
104
|
end
|
104
105
|
end
|
@@ -119,14 +120,20 @@ namespace :db do
|
|
119
120
|
task :secondbase do
|
120
121
|
Rake::Task['environment'].invoke
|
121
122
|
|
122
|
-
|
123
|
-
|
123
|
+
SecondBase::has_runner(Rails.env)
|
124
|
+
|
125
|
+
# dump the current env's db, be sure to add the schema information!!!
|
126
|
+
dump_file = "#{RAILS_ROOT}/db/#{SecondBase::CONNECTION_PREFIX}_#{RAILS_ENV}_structure.sql"
|
124
127
|
|
125
|
-
File.open(
|
128
|
+
File.open(dump_file, "w+") do |f|
|
126
129
|
f << ActiveRecord::Base.connection.structure_dump
|
127
130
|
end
|
128
131
|
|
129
|
-
|
132
|
+
if ActiveRecord::Base.connection.supports_migrations?
|
133
|
+
File.open(dump_file, "a") { |f| f << ActiveRecord::Base.connection.dump_schema_information }
|
134
|
+
end
|
135
|
+
|
136
|
+
FirstBase::has_runner(Rails.env)
|
130
137
|
end
|
131
138
|
end
|
132
139
|
end
|
@@ -143,11 +150,11 @@ namespace :db do
|
|
143
150
|
task :secondbase do
|
144
151
|
Rake::Task['environment'].invoke
|
145
152
|
|
146
|
-
|
153
|
+
SecondBase::has_runner('test')
|
147
154
|
|
148
155
|
ActiveRecord::Base.connection.recreate_database(secondbase_config('test')["database"], secondbase_config('test'))
|
149
156
|
|
150
|
-
|
157
|
+
FirstBase::has_runner(Rails.env)
|
151
158
|
end
|
152
159
|
end
|
153
160
|
|
@@ -160,13 +167,15 @@ namespace :db do
|
|
160
167
|
`rake db:test:purge:secondbase`
|
161
168
|
|
162
169
|
# now lets clone the structure for secondbase
|
163
|
-
|
170
|
+
SecondBase::has_runner('test')
|
171
|
+
|
164
172
|
ActiveRecord::Base.connection.execute('SET foreign_key_checks = 0') if secondbase_config(RAILS_ENV)['adapter'][/mysql/]
|
173
|
+
|
165
174
|
IO.readlines("#{RAILS_ROOT}/db/#{SecondBase::CONNECTION_PREFIX}_#{RAILS_ENV}_structure.sql").join.split("\n\n").each do |table|
|
166
175
|
ActiveRecord::Base.connection.execute(table)
|
167
176
|
end
|
168
177
|
|
169
|
-
|
178
|
+
FirstBase::has_runner(Rails.env)
|
170
179
|
end
|
171
180
|
end
|
172
181
|
end
|
@@ -177,18 +186,6 @@ end
|
|
177
186
|
####################################
|
178
187
|
#
|
179
188
|
# Some helper methods to run back and forth between first and second base.
|
180
|
-
def use_firstbase(env)
|
181
|
-
ActiveRecord::Base.establish_connection(firstbase_config(env))
|
182
|
-
end
|
183
|
-
|
184
|
-
def use_secondbase(env)
|
185
|
-
ActiveRecord::Base.establish_connection(secondbase_config(env))
|
186
|
-
end
|
187
|
-
|
188
189
|
def secondbase_config(env)
|
189
190
|
ActiveRecord::Base.configurations[SecondBase::CONNECTION_PREFIX][env]
|
190
|
-
end
|
191
|
-
|
192
|
-
def firstbase_config(env)
|
193
|
-
ActiveRecord::Base.configurations[env]
|
194
191
|
end
|
data/secondbase.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{secondbase}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.4.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["karledurante"]
|
12
|
-
s.date = %q{2011-
|
12
|
+
s.date = %q{2011-03-03}
|
13
13
|
s.description = %q{Secondbase provides support to Rails to create a homogeneous for a dual database project. Using the rake tasks already familiar to you, this gem enables Rails to work with two primary databases, instead of just one.}
|
14
14
|
s.email = %q{kdurante@customink.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -43,7 +43,7 @@ Gem::Specification.new do |s|
|
|
43
43
|
s.homepage = %q{http://github.com/karledurante/secondbase}
|
44
44
|
s.licenses = ["MIT"]
|
45
45
|
s.require_paths = ["lib"]
|
46
|
-
s.rubygems_version = %q{1.
|
46
|
+
s.rubygems_version = %q{1.4.2}
|
47
47
|
s.summary = %q{Allow Rails manage second database in your projects}
|
48
48
|
s.test_files = [
|
49
49
|
"test/helper.rb",
|
@@ -51,7 +51,6 @@ Gem::Specification.new do |s|
|
|
51
51
|
]
|
52
52
|
|
53
53
|
if s.respond_to? :specification_version then
|
54
|
-
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
55
54
|
s.specification_version = 3
|
56
55
|
|
57
56
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: secondbase
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
5
|
-
prerelease:
|
4
|
+
hash: 15
|
5
|
+
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 4
|
9
|
+
- 0
|
10
|
+
version: 0.4.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- karledurante
|
@@ -15,13 +15,11 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-03-03 00:00:00 -05:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
22
|
-
prerelease: false
|
23
22
|
type: :development
|
24
|
-
name: shoulda
|
25
23
|
version_requirements: &id001 !ruby/object:Gem::Requirement
|
26
24
|
none: false
|
27
25
|
requirements:
|
@@ -32,10 +30,10 @@ dependencies:
|
|
32
30
|
- 0
|
33
31
|
version: "0"
|
34
32
|
requirement: *id001
|
35
|
-
- !ruby/object:Gem::Dependency
|
36
33
|
prerelease: false
|
34
|
+
name: shoulda
|
35
|
+
- !ruby/object:Gem::Dependency
|
37
36
|
type: :development
|
38
|
-
name: bundler
|
39
37
|
version_requirements: &id002 !ruby/object:Gem::Requirement
|
40
38
|
none: false
|
41
39
|
requirements:
|
@@ -48,10 +46,10 @@ dependencies:
|
|
48
46
|
- 0
|
49
47
|
version: 1.0.0
|
50
48
|
requirement: *id002
|
51
|
-
- !ruby/object:Gem::Dependency
|
52
49
|
prerelease: false
|
50
|
+
name: bundler
|
51
|
+
- !ruby/object:Gem::Dependency
|
53
52
|
type: :development
|
54
|
-
name: jeweler
|
55
53
|
version_requirements: &id003 !ruby/object:Gem::Requirement
|
56
54
|
none: false
|
57
55
|
requirements:
|
@@ -64,10 +62,10 @@ dependencies:
|
|
64
62
|
- 1
|
65
63
|
version: 1.5.1
|
66
64
|
requirement: *id003
|
67
|
-
- !ruby/object:Gem::Dependency
|
68
65
|
prerelease: false
|
66
|
+
name: jeweler
|
67
|
+
- !ruby/object:Gem::Dependency
|
69
68
|
type: :development
|
70
|
-
name: activerecord
|
71
69
|
version_requirements: &id004 !ruby/object:Gem::Requirement
|
72
70
|
none: false
|
73
71
|
requirements:
|
@@ -80,10 +78,10 @@ dependencies:
|
|
80
78
|
- 0
|
81
79
|
version: 3.0.0
|
82
80
|
requirement: *id004
|
83
|
-
- !ruby/object:Gem::Dependency
|
84
81
|
prerelease: false
|
85
|
-
type: :development
|
86
82
|
name: activerecord
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
type: :development
|
87
85
|
version_requirements: &id005 !ruby/object:Gem::Requirement
|
88
86
|
none: false
|
89
87
|
requirements:
|
@@ -96,6 +94,8 @@ dependencies:
|
|
96
94
|
- 0
|
97
95
|
version: 3.0.0
|
98
96
|
requirement: *id005
|
97
|
+
prerelease: false
|
98
|
+
name: activerecord
|
99
99
|
description: Secondbase provides support to Rails to create a homogeneous for a dual database project. Using the rake tasks already familiar to you, this gem enables Rails to work with two primary databases, instead of just one.
|
100
100
|
email: kdurante@customink.com
|
101
101
|
executables: []
|
@@ -158,7 +158,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
158
158
|
requirements: []
|
159
159
|
|
160
160
|
rubyforge_project:
|
161
|
-
rubygems_version: 1.
|
161
|
+
rubygems_version: 1.4.2
|
162
162
|
signing_key:
|
163
163
|
specification_version: 3
|
164
164
|
summary: Allow Rails manage second database in your projects
|