secondbase 0.6.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +16 -0
- data/.yardopts +1 -0
- data/Appraisals +12 -0
- data/CHANGELOG.md +18 -6
- data/Gemfile +3 -12
- data/Guardfile +16 -0
- data/LICENSE.txt +2 -1
- data/README.md +144 -0
- data/Rakefile +18 -41
- data/VERSION +1 -1
- data/gemfiles/rails40.gemfile +8 -0
- data/gemfiles/rails41.gemfile +8 -0
- data/gemfiles/rails42.gemfile +8 -0
- data/lib/rails/second_base/generators/migration_generator.rb +25 -0
- data/lib/second_base.rb +20 -0
- data/lib/second_base/base.rb +8 -0
- data/lib/second_base/databases.rake +127 -0
- data/lib/second_base/forced.rb +21 -0
- data/lib/second_base/on_base.rb +36 -0
- data/lib/second_base/railtie.rb +40 -0
- data/lib/second_base/test_help.rb +11 -0
- data/lib/second_base/version.rb +3 -0
- data/lib/secondbase.rb +1 -46
- data/secondbase.gemspec +25 -75
- data/test/cases/dbtask_test.rb +225 -0
- data/test/cases/forced_test.rb +49 -0
- data/test/cases/generator_test.rb +41 -0
- data/test/cases/on_base_test.rb +35 -0
- data/test/cases/railtie_test.rb +31 -0
- data/test/dummy_app/Rakefile +2 -0
- data/test/dummy_app/app/controllers/application_controller.rb +7 -0
- data/test/dummy_app/app/helpers/application_helper.rb +3 -0
- data/test/dummy_app/app/models/comment.rb +6 -0
- data/test/dummy_app/app/models/comment_forced.rb +6 -0
- data/test/dummy_app/app/models/post.rb +7 -0
- data/test/dummy_app/app/models/user.rb +6 -0
- data/test/dummy_app/bin/rails +5 -0
- data/test/dummy_app/config/database.yml +14 -0
- data/test/dummy_app/config/routes.rb +3 -0
- data/test/dummy_app/db/migrate/20141209165002_create_users.rb +11 -0
- data/test/dummy_app/db/migrate/20141214142700_create_posts.rb +12 -0
- data/test/dummy_app/db/secondbase/migrate/20151202075826_create_comments.rb +11 -0
- data/test/dummy_app/init.rb +40 -0
- data/test/dummy_app/log/.keep +0 -0
- data/test/dummy_app/tmp/.keep +0 -0
- data/test/test_helper.rb +36 -0
- data/test/test_helpers/dummy_app_helpers.rb +90 -0
- data/test/test_helpers/rails_version_helpers.rb +29 -0
- data/test/test_helpers/stream_helpers.rb +40 -0
- metadata +220 -139
- data/.document +0 -5
- data/Gemfile.lock +0 -38
- data/README.rdoc +0 -143
- data/lib/generators/secondbase/USAGE +0 -19
- data/lib/generators/secondbase/migration_generator.rb +0 -36
- data/lib/generators/secondbase/templates/migration.rb +0 -13
- data/lib/secondbase/active_record/associations/has_and_belongs_to_many_association.rb +0 -66
- data/lib/secondbase/active_record/base.rb +0 -13
- data/lib/secondbase/active_record/fixtures.rb +0 -66
- data/lib/secondbase/active_record/patches.rb +0 -13
- data/lib/secondbase/active_record/test_fixtures.rb +0 -32
- data/lib/secondbase/model.rb +0 -11
- data/lib/secondbase/railtie.rb +0 -10
- data/lib/secondbase/rake_method_chain.rb +0 -22
- data/lib/secondbase/tasks.rb +0 -219
- data/rails_generators/secondbase/USAGE +0 -29
- data/rails_generators/secondbase/secondbase_migration_generator.rb +0 -20
- data/rails_generators/secondbase/templates/migration.rb +0 -15
- data/test/helper.rb +0 -18
- data/test/test_secondbase.rb +0 -7
data/lib/secondbase/model.rb
DELETED
@@ -1,11 +0,0 @@
|
|
1
|
-
# Secondbase model definition
|
2
|
-
#
|
3
|
-
# NOTE: By extending this model, you assume that the underlying table will be located in your Second (Data)base
|
4
|
-
module SecondBase
|
5
|
-
|
6
|
-
class Base < ActiveRecord::Base
|
7
|
-
establish_connection ActiveRecord::Base.configurations[SecondBase::CONNECTION_PREFIX][Rails.env]
|
8
|
-
|
9
|
-
self.abstract_class = true
|
10
|
-
end
|
11
|
-
end
|
data/lib/secondbase/railtie.rb
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
require 'rake'
|
2
|
-
|
3
|
-
# We want to provide a way to alias tasks so we can hook our custom logic
|
4
|
-
# into the existing rails framework. For more information and usage, see:
|
5
|
-
# http://www.metaskills.net/2010/5/26/the-alias_method_chain-of-rake-override-rake-task
|
6
|
-
Rake::TaskManager.class_eval do
|
7
|
-
def alias_task(fq_name)
|
8
|
-
new_name = "#{fq_name}:original"
|
9
|
-
@tasks[new_name] = @tasks.delete(fq_name)
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
def alias_task(fq_name)
|
14
|
-
Rake.application.alias_task(fq_name)
|
15
|
-
end
|
16
|
-
|
17
|
-
def override_task(*args, &block)
|
18
|
-
name, params, deps = Rake.application.resolve_args(args.dup)
|
19
|
-
fq_name = Rake.application.instance_variable_get(:@scope).dup.push(name).join(':')
|
20
|
-
alias_task(fq_name)
|
21
|
-
Rake::Task.define_task(*args, &block)
|
22
|
-
end
|
data/lib/secondbase/tasks.rb
DELETED
@@ -1,219 +0,0 @@
|
|
1
|
-
require 'secondbase'
|
2
|
-
|
3
|
-
####################################
|
4
|
-
#
|
5
|
-
# SecondBase database managment tasks
|
6
|
-
#
|
7
|
-
# We are overriding a handful of rake tasks here:
|
8
|
-
# db:create
|
9
|
-
# db:migrate
|
10
|
-
# db:test:prepare
|
11
|
-
#
|
12
|
-
# We ARE NOT redefining the implementation of these tasks, we are simply
|
13
|
-
# appending custom functionality to them. We just want to be sure that in
|
14
|
-
# addition to creating, migrating, and preparing your default (Rails.env)
|
15
|
-
# database, that we can also work with with the second (data)base.
|
16
|
-
|
17
|
-
namespace :db do
|
18
|
-
override_task :create do
|
19
|
-
# First, we execute the original/default create task
|
20
|
-
Rake::Task["db:create:original"].invoke
|
21
|
-
|
22
|
-
# now, we create our secondary databases
|
23
|
-
Rake::Task['environment'].invoke
|
24
|
-
ActiveRecord::Base.configurations[SecondBase::CONNECTION_PREFIX].each_value do |config|
|
25
|
-
next unless config['database']
|
26
|
-
|
27
|
-
# Only connect to local databases
|
28
|
-
local_database?(config) { create_database(config) }
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
override_task :migrate do
|
33
|
-
Rake::Task['environment'].invoke
|
34
|
-
|
35
|
-
# Migrate secondbase...
|
36
|
-
Rake::Task["db:migrate:secondbase"].invoke
|
37
|
-
|
38
|
-
# Execute the original/default prepare task
|
39
|
-
Rake::Task["db:migrate:original"].invoke
|
40
|
-
end
|
41
|
-
|
42
|
-
override_task :abort_if_pending_migrations do
|
43
|
-
# Execute the original/default prepare task
|
44
|
-
Rake::Task["db:abort_if_pending_migrations"].invoke
|
45
|
-
|
46
|
-
Rake::Task["db:abort_if_pending_migrations:secondbase"].invoke
|
47
|
-
end
|
48
|
-
|
49
|
-
namespace :test do
|
50
|
-
override_task :prepare do
|
51
|
-
Rake::Task['environment'].invoke
|
52
|
-
|
53
|
-
# Clone the secondary database structure
|
54
|
-
Rake::Task["db:test:prepare:secondbase"].invoke
|
55
|
-
|
56
|
-
# Execute the original/default prepare task
|
57
|
-
Rake::Task["db:test:prepare:original"].invoke
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
##################################
|
62
|
-
# SecondBase specific database tasks
|
63
|
-
namespace :abort_if_pending_migrations do
|
64
|
-
desc "determines if your secondbase has pending migrations"
|
65
|
-
task :secondbase => :environment do
|
66
|
-
# reset connection to secondbase...
|
67
|
-
SecondBase::has_runner(Rails.env)
|
68
|
-
|
69
|
-
pending_migrations = ActiveRecord::Migrator.new(:up, "db/migrate/#{SecondBase::CONNECTION_PREFIX}").pending_migrations
|
70
|
-
|
71
|
-
if pending_migrations.any?
|
72
|
-
puts "You have #{pending_migrations.size} pending migrations:"
|
73
|
-
pending_migrations.each do |pending_migration|
|
74
|
-
puts ' %4d %s' % [pending_migration.version, pending_migration.name]
|
75
|
-
end
|
76
|
-
abort %{Run "rake db:migrate" to update your database then try again.}
|
77
|
-
end
|
78
|
-
|
79
|
-
# reset connection back to firstbase...
|
80
|
-
FirstBase::has_runner(Rails.env)
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
namespace :migrate do
|
85
|
-
desc "migrates the second database"
|
86
|
-
task :secondbase => :load_config do
|
87
|
-
Rake::Task['environment'].invoke
|
88
|
-
# NOTE: We are not generating a db schema on purpose. Since we're running
|
89
|
-
# in a dual db mode, it could be confusing to have two schemas.
|
90
|
-
|
91
|
-
# reset connection to secondbase...
|
92
|
-
SecondBase::has_runner(Rails.env)
|
93
|
-
|
94
|
-
# run secondbase migrations...
|
95
|
-
ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true
|
96
|
-
ActiveRecord::Migrator.migrate("db/migrate/#{SecondBase::CONNECTION_PREFIX}/", ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
|
97
|
-
|
98
|
-
# reset connection back to firstbase...
|
99
|
-
FirstBase::has_runner(Rails.env)
|
100
|
-
end
|
101
|
-
|
102
|
-
namespace :up do
|
103
|
-
desc 'Runs the "up" for a given SecondBase migration VERSION.'
|
104
|
-
task :secondbase => :environment do
|
105
|
-
version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
|
106
|
-
raise "VERSION is required" unless version
|
107
|
-
|
108
|
-
# reset connection to secondbase...
|
109
|
-
SecondBase::has_runner(Rails.env)
|
110
|
-
|
111
|
-
ActiveRecord::Migrator.run(:up, "db/migrate/#{SecondBase::CONNECTION_PREFIX}/", version)
|
112
|
-
|
113
|
-
# reset connection back to firstbase...
|
114
|
-
FirstBase::has_runner(Rails.env)
|
115
|
-
end
|
116
|
-
end
|
117
|
-
|
118
|
-
namespace :down do
|
119
|
-
desc 'Runs the "down" for a given SecondBase migration VERSION.'
|
120
|
-
task :secondbase => :environment do
|
121
|
-
version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
|
122
|
-
raise "VERSION is required" unless version
|
123
|
-
|
124
|
-
# reset connection to secondbase...
|
125
|
-
SecondBase::has_runner(Rails.env)
|
126
|
-
|
127
|
-
ActiveRecord::Migrator.run(:down, "db/migrate/#{SecondBase::CONNECTION_PREFIX}/", version)
|
128
|
-
|
129
|
-
# reset connection back to firstbase...
|
130
|
-
FirstBase::has_runner(Rails.env)
|
131
|
-
end
|
132
|
-
end
|
133
|
-
end
|
134
|
-
|
135
|
-
namespace :create do
|
136
|
-
desc 'Create the database defined in config/database.yml for the current RAILS_ENV'
|
137
|
-
task :secondbase => :load_config do
|
138
|
-
|
139
|
-
# We can still use the #create_database method defined in activerecord's databases.rake
|
140
|
-
# we call it passing the secondbase config instead of the default (Rails.env) config...
|
141
|
-
create_database(secondbase_config(Rails.env))
|
142
|
-
end
|
143
|
-
end
|
144
|
-
|
145
|
-
namespace :structure do
|
146
|
-
namespace :dump do
|
147
|
-
desc "dumps structure for both (first and second) databases."
|
148
|
-
task :secondbase do
|
149
|
-
Rake::Task['environment'].invoke
|
150
|
-
|
151
|
-
SecondBase::has_runner(Rails.env)
|
152
|
-
|
153
|
-
# dump the current env's db, be sure to add the schema information!!!
|
154
|
-
dump_file = "#{RAILS_ROOT}/db/#{SecondBase::CONNECTION_PREFIX}_#{RAILS_ENV}_structure.sql"
|
155
|
-
|
156
|
-
File.open(dump_file, "w+") do |f|
|
157
|
-
f << ActiveRecord::Base.connection.structure_dump
|
158
|
-
end
|
159
|
-
|
160
|
-
if ActiveRecord::Base.connection.supports_migrations?
|
161
|
-
File.open(dump_file, "a") { |f| f << ActiveRecord::Base.connection.dump_schema_information }
|
162
|
-
end
|
163
|
-
|
164
|
-
FirstBase::has_runner(Rails.env)
|
165
|
-
end
|
166
|
-
end
|
167
|
-
end
|
168
|
-
|
169
|
-
namespace :test do
|
170
|
-
namespace :prepare do
|
171
|
-
desc 'Prepares the test instance of secondbase'
|
172
|
-
task :secondbase => 'db:abort_if_pending_migrations:secondbase' do
|
173
|
-
Rake::Task["db:test:clone_structure:secondbase"].invoke
|
174
|
-
end
|
175
|
-
end
|
176
|
-
|
177
|
-
namespace :purge do
|
178
|
-
task :secondbase do
|
179
|
-
Rake::Task['environment'].invoke
|
180
|
-
|
181
|
-
SecondBase::has_runner('test')
|
182
|
-
|
183
|
-
ActiveRecord::Base.connection.recreate_database(secondbase_config('test')["database"], secondbase_config('test'))
|
184
|
-
|
185
|
-
FirstBase::has_runner(Rails.env)
|
186
|
-
end
|
187
|
-
end
|
188
|
-
|
189
|
-
namespace :clone_structure do
|
190
|
-
task :secondbase do
|
191
|
-
Rake::Task['environment'].invoke
|
192
|
-
|
193
|
-
# dump secondbase structure and purge the test secondbase
|
194
|
-
`rake db:structure:dump:secondbase`
|
195
|
-
`rake db:test:purge:secondbase`
|
196
|
-
|
197
|
-
# now lets clone the structure for secondbase
|
198
|
-
SecondBase::has_runner('test')
|
199
|
-
|
200
|
-
ActiveRecord::Base.connection.execute('SET foreign_key_checks = 0') if secondbase_config(RAILS_ENV)['adapter'][/mysql/]
|
201
|
-
|
202
|
-
IO.readlines("#{RAILS_ROOT}/db/#{SecondBase::CONNECTION_PREFIX}_#{RAILS_ENV}_structure.sql").join.split("\n\n").each do |table|
|
203
|
-
ActiveRecord::Base.connection.execute(table)
|
204
|
-
end
|
205
|
-
|
206
|
-
FirstBase::has_runner(Rails.env)
|
207
|
-
end
|
208
|
-
end
|
209
|
-
end
|
210
|
-
|
211
|
-
end
|
212
|
-
|
213
|
-
|
214
|
-
####################################
|
215
|
-
#
|
216
|
-
# Some helper methods to run back and forth between first and second base.
|
217
|
-
def secondbase_config(env)
|
218
|
-
ActiveRecord::Base.configurations[SecondBase::CONNECTION_PREFIX][env]
|
219
|
-
end
|
@@ -1,29 +0,0 @@
|
|
1
|
-
Description:
|
2
|
-
Stubs out a new database migration. Pass the migration name, either
|
3
|
-
CamelCased or under_scored, and an optional list of attribute pairs as arguments.
|
4
|
-
|
5
|
-
A migration class is generated in db/migrate_mysql prefixed by a timestamp of the current date and time.
|
6
|
-
|
7
|
-
You can name your migration in either of these formats to generate add/remove
|
8
|
-
column lines from supplied attributes: AddColumnsToTable or RemoveColumnsFromTable
|
9
|
-
|
10
|
-
Example:
|
11
|
-
`./script/generate secondbase_migration AddSslFlag`
|
12
|
-
|
13
|
-
If the current date is May 14, 2008 and the current time 09:09:12, this creates the AddSslFlag migration
|
14
|
-
db/migrate_mysql/20080514090912_add_ssl_flag.rb
|
15
|
-
|
16
|
-
`./script/generate secondbase_migration AddTitleBodyToPost title:string body:text published:boolean`
|
17
|
-
|
18
|
-
This will create the AddTitleBodyToPost in db/migrate_mysql/20080514090912_add_title_body_to_post.rb with
|
19
|
-
this in the Up migration:
|
20
|
-
|
21
|
-
add_column :posts, :title, :string
|
22
|
-
add_column :posts, :body, :text
|
23
|
-
add_column :posts, :published, :boolean
|
24
|
-
|
25
|
-
And this in the Down migration:
|
26
|
-
|
27
|
-
remove_column :posts, :published
|
28
|
-
remove_column :posts, :body
|
29
|
-
remove_column :posts, :title
|
@@ -1,20 +0,0 @@
|
|
1
|
-
class SecondbaseMigrationGenerator < Rails::Generator::NamedBase
|
2
|
-
def manifest
|
3
|
-
record do |m|
|
4
|
-
m.migration_template 'migration.rb', "db/migrate/#{SecondBase::CONNECTION_PREFIX}", :assigns => get_local_assigns
|
5
|
-
end
|
6
|
-
end
|
7
|
-
|
8
|
-
|
9
|
-
private
|
10
|
-
def get_local_assigns
|
11
|
-
returning(assigns = {}) do
|
12
|
-
if class_name.underscore =~ /^(add|remove)_.*_(?:to|from)_(.*)/
|
13
|
-
assigns[:migration_action] = $1
|
14
|
-
assigns[:table_name] = $2.pluralize
|
15
|
-
else
|
16
|
-
assigns[:attributes] = []
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
@@ -1,15 +0,0 @@
|
|
1
|
-
class <%= class_name.underscore.camelize %> < ActiveRecord::Migration
|
2
|
-
############################################################
|
3
|
-
# Database migration targeting the Secondbase!
|
4
|
-
# Generated using: ./script/generator secondbase_migration [ModelName]
|
5
|
-
|
6
|
-
def self.up<% attributes.each do |attribute| %>
|
7
|
-
<%= migration_action %>_column :<%= table_name %>, :<%= attribute.name %><% if migration_action == 'add' %>, :<%= attribute.type %><% end -%>
|
8
|
-
<%- end %>
|
9
|
-
end
|
10
|
-
|
11
|
-
def self.down<% attributes.reverse.each do |attribute| %>
|
12
|
-
<%= migration_action == 'add' ? 'remove' : 'add' %>_column :<%= table_name %>, :<%= attribute.name %><% if migration_action == 'remove' %>, :<%= attribute.type %><% end -%>
|
13
|
-
<%- end %>
|
14
|
-
end
|
15
|
-
end
|
data/test/helper.rb
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'bundler'
|
3
|
-
begin
|
4
|
-
Bundler.setup(:default, :development)
|
5
|
-
rescue Bundler::BundlerError => e
|
6
|
-
$stderr.puts e.message
|
7
|
-
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
-
exit e.status_code
|
9
|
-
end
|
10
|
-
require 'test/unit'
|
11
|
-
require 'shoulda'
|
12
|
-
|
13
|
-
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
14
|
-
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
15
|
-
require 'secondbase'
|
16
|
-
|
17
|
-
class Test::Unit::TestCase
|
18
|
-
end
|