secondbase 0.6.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +9 -0
  3. data/.travis.yml +16 -0
  4. data/.yardopts +1 -0
  5. data/Appraisals +12 -0
  6. data/CHANGELOG.md +18 -6
  7. data/Gemfile +3 -12
  8. data/Guardfile +16 -0
  9. data/LICENSE.txt +2 -1
  10. data/README.md +144 -0
  11. data/Rakefile +18 -41
  12. data/VERSION +1 -1
  13. data/gemfiles/rails40.gemfile +8 -0
  14. data/gemfiles/rails41.gemfile +8 -0
  15. data/gemfiles/rails42.gemfile +8 -0
  16. data/lib/rails/second_base/generators/migration_generator.rb +25 -0
  17. data/lib/second_base.rb +20 -0
  18. data/lib/second_base/base.rb +8 -0
  19. data/lib/second_base/databases.rake +127 -0
  20. data/lib/second_base/forced.rb +21 -0
  21. data/lib/second_base/on_base.rb +36 -0
  22. data/lib/second_base/railtie.rb +40 -0
  23. data/lib/second_base/test_help.rb +11 -0
  24. data/lib/second_base/version.rb +3 -0
  25. data/lib/secondbase.rb +1 -46
  26. data/secondbase.gemspec +25 -75
  27. data/test/cases/dbtask_test.rb +225 -0
  28. data/test/cases/forced_test.rb +49 -0
  29. data/test/cases/generator_test.rb +41 -0
  30. data/test/cases/on_base_test.rb +35 -0
  31. data/test/cases/railtie_test.rb +31 -0
  32. data/test/dummy_app/Rakefile +2 -0
  33. data/test/dummy_app/app/controllers/application_controller.rb +7 -0
  34. data/test/dummy_app/app/helpers/application_helper.rb +3 -0
  35. data/test/dummy_app/app/models/comment.rb +6 -0
  36. data/test/dummy_app/app/models/comment_forced.rb +6 -0
  37. data/test/dummy_app/app/models/post.rb +7 -0
  38. data/test/dummy_app/app/models/user.rb +6 -0
  39. data/test/dummy_app/bin/rails +5 -0
  40. data/test/dummy_app/config/database.yml +14 -0
  41. data/test/dummy_app/config/routes.rb +3 -0
  42. data/test/dummy_app/db/migrate/20141209165002_create_users.rb +11 -0
  43. data/test/dummy_app/db/migrate/20141214142700_create_posts.rb +12 -0
  44. data/test/dummy_app/db/secondbase/migrate/20151202075826_create_comments.rb +11 -0
  45. data/test/dummy_app/init.rb +40 -0
  46. data/test/dummy_app/log/.keep +0 -0
  47. data/test/dummy_app/tmp/.keep +0 -0
  48. data/test/test_helper.rb +36 -0
  49. data/test/test_helpers/dummy_app_helpers.rb +90 -0
  50. data/test/test_helpers/rails_version_helpers.rb +29 -0
  51. data/test/test_helpers/stream_helpers.rb +40 -0
  52. metadata +220 -139
  53. data/.document +0 -5
  54. data/Gemfile.lock +0 -38
  55. data/README.rdoc +0 -143
  56. data/lib/generators/secondbase/USAGE +0 -19
  57. data/lib/generators/secondbase/migration_generator.rb +0 -36
  58. data/lib/generators/secondbase/templates/migration.rb +0 -13
  59. data/lib/secondbase/active_record/associations/has_and_belongs_to_many_association.rb +0 -66
  60. data/lib/secondbase/active_record/base.rb +0 -13
  61. data/lib/secondbase/active_record/fixtures.rb +0 -66
  62. data/lib/secondbase/active_record/patches.rb +0 -13
  63. data/lib/secondbase/active_record/test_fixtures.rb +0 -32
  64. data/lib/secondbase/model.rb +0 -11
  65. data/lib/secondbase/railtie.rb +0 -10
  66. data/lib/secondbase/rake_method_chain.rb +0 -22
  67. data/lib/secondbase/tasks.rb +0 -219
  68. data/rails_generators/secondbase/USAGE +0 -29
  69. data/rails_generators/secondbase/secondbase_migration_generator.rb +0 -20
  70. data/rails_generators/secondbase/templates/migration.rb +0 -15
  71. data/test/helper.rb +0 -18
  72. data/test/test_secondbase.rb +0 -7
@@ -0,0 +1,49 @@
1
+ require 'test_helper'
2
+
3
+ class ForcedTest < SecondBase::TestCase
4
+
5
+ setup do
6
+ run_db :create
7
+ run_db :migrate
8
+ establish_connection
9
+ end
10
+
11
+ def test_shared_pool
12
+ assert_equal SecondBase::Base.connection_pool.object_id,
13
+ CommentForced.connection_pool.object_id
14
+ end
15
+
16
+ def test_shared_connection
17
+ assert_equal SecondBase::Base.connection.raw_connection.object_id,
18
+ CommentForced.connection.raw_connection.object_id
19
+ end
20
+
21
+ def test_shared_new_connection_in_a_different_thread
22
+ current_base_connection_id = SecondBase::Base.connection.raw_connection.object_id
23
+ new_base_connection_id, new_forced_connection_id = Thread.new {
24
+ [ SecondBase::Base.connection.raw_connection.object_id,
25
+ CommentForced.connection.raw_connection.object_id ]
26
+ }.value
27
+ refute_equal new_base_connection_id, current_base_connection_id
28
+ assert_equal new_base_connection_id, new_forced_connection_id
29
+ end
30
+
31
+ def test_shared_connected_query
32
+ assert SecondBase::Base.connected?
33
+ assert CommentForced.connected?
34
+ CommentForced.clear_all_connections!
35
+ refute SecondBase::Base.connected?
36
+ refute CommentForced.connected?
37
+ end
38
+
39
+ def test_can_remove_connection_properly
40
+ base_connection = SecondBase::Base.connection
41
+ forced_connection = CommentForced.connection
42
+ assert base_connection.active?
43
+ assert forced_connection.active?
44
+ CommentForced.remove_connection
45
+ refute base_connection.active?
46
+ refute forced_connection.active?
47
+ end
48
+
49
+ end
@@ -0,0 +1,41 @@
1
+ require 'test_helper'
2
+
3
+ class GeneratorTest < SecondBase::TestCase
4
+
5
+ teardown { generated_migration_delete }
6
+
7
+ def test_initialization_via_help
8
+ output = Dir.chdir(dummy_root) { `rails g -h` }
9
+ assert_match /second_base\:migration/, output
10
+ end
11
+
12
+ def test_description_uses_rails_base
13
+ output = Dir.chdir(dummy_root) { `rails g second_base:migration -h` }
14
+ assert_match %r{db/migrate/20080514090912_add_ssl_flag\.rb}, output
15
+ end
16
+
17
+ def test_migration
18
+ output = Dir.chdir(dummy_root) { `rails g second_base:migration CreateFavorites post_id:integer count:integer` }
19
+ assert_match %r{create.*db/secondbase/migrate/.*create_favorites\.rb}, output
20
+ migration = generated_migration_data
21
+ assert_match %r{create_table :favorites}, migration
22
+ assert_match %r{t.integer :post_id}, migration
23
+ assert_match %r{t.integer :count}, migration
24
+ end
25
+
26
+
27
+ private
28
+
29
+ def generated_migration
30
+ Dir["#{dummy_db}/secondbase/migrate/*favorites.{rb}"].first
31
+ end
32
+
33
+ def generated_migration_data
34
+ generated_migration ? File.read(generated_migration) : ''
35
+ end
36
+
37
+ def generated_migration_delete
38
+ FileUtils.rm_rf(generated_migration) if generated_migration
39
+ end
40
+
41
+ end
@@ -0,0 +1,35 @@
1
+ require 'test_helper'
2
+
3
+ class OnBaseTest < SecondBase::TestCase
4
+
5
+ setup do
6
+ run_db :create
7
+ run_db :migrate
8
+ establish_connection
9
+ end
10
+
11
+ def test_on_base
12
+ refute SecondBase.is_on_base
13
+ SecondBase.on_base do
14
+ assert SecondBase.is_on_base
15
+ assert_equal SecondBase::Base.connection.class, ActiveRecord::Base.connection.class
16
+ assert_equal SecondBase::Railtie.fullpath('migrate'), ActiveRecord::Tasks::DatabaseTasks.migrations_paths
17
+ assert_equal SecondBase::Railtie.fullpath, ActiveRecord::Tasks::DatabaseTasks.db_dir
18
+ end
19
+ refute SecondBase.is_on_base
20
+ end
21
+
22
+ def test_on_base_nested
23
+ refute SecondBase.is_on_base
24
+ SecondBase.on_base do
25
+ assert SecondBase.is_on_base
26
+ SecondBase.on_base do
27
+ assert SecondBase.is_on_base
28
+ end
29
+ assert SecondBase.is_on_base
30
+ end
31
+ refute SecondBase.is_on_base
32
+ end
33
+
34
+
35
+ end
@@ -0,0 +1,31 @@
1
+ require 'test_helper'
2
+
3
+ class RailtieTest < SecondBase::TestCase
4
+
5
+ def test_config
6
+ expected_path = 'db/secondbase'
7
+ assert_equal expected_path, railtie_inst.config.second_base.path
8
+ assert_equal expected_path, railtie_klass.config.second_base.path
9
+ expected_config_key = 'secondbase'
10
+ assert_equal expected_config_key, railtie_inst.config.second_base.config_key
11
+ assert_equal expected_config_key, railtie_klass.config.second_base.config_key
12
+ end
13
+
14
+ def test_fullpath
15
+ expected = dummy_db.join('secondbase').to_s
16
+ assert_equal expected, railtie_inst.fullpath
17
+ assert_equal expected, railtie_klass.fullpath
18
+ end
19
+
20
+
21
+ private
22
+
23
+ def railtie_inst
24
+ dummy_app.railties.grep(railtie_klass).first
25
+ end
26
+
27
+ def railtie_klass
28
+ SecondBase::Railtie
29
+ end
30
+
31
+ end
@@ -0,0 +1,2 @@
1
+ require File.expand_path '../init', __FILE__
2
+ Dummy::Application.load_tasks
@@ -0,0 +1,7 @@
1
+ class ApplicationController < ActionController::Base
2
+
3
+ def index
4
+ render :html => '<h1>Dummy::Application</h1>'.html_safe, :layout => false
5
+ end
6
+
7
+ end
@@ -0,0 +1,3 @@
1
+ module ApplicationHelper
2
+
3
+ end
@@ -0,0 +1,6 @@
1
+ class Comment < SecondBase::Base
2
+
3
+ belongs_to :user
4
+
5
+
6
+ end
@@ -0,0 +1,6 @@
1
+ class CommentForced < ActiveRecord::Base
2
+ self.table_name = 'comments'
3
+ belongs_to :user
4
+ end
5
+
6
+ CommentForced.extend SecondBase::Forced
@@ -0,0 +1,7 @@
1
+ class Post < ActiveRecord::Base
2
+
3
+ belongs_to :user
4
+ has_many :comments
5
+
6
+
7
+ end
@@ -0,0 +1,6 @@
1
+ class User < ActiveRecord::Base
2
+
3
+ has_many :posts
4
+
5
+
6
+ end
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ APP_PATH = File.expand_path('../../init', __FILE__)
4
+ require APP_PATH
5
+ require 'rails/commands'
@@ -0,0 +1,14 @@
1
+
2
+ test:
3
+ adapter: sqlite3
4
+ database: ./db/base.sqlite3
5
+
6
+ secondbase:
7
+
8
+ test:
9
+ adapter: mysql
10
+ database: 'secondbase_test'
11
+ username: root
12
+ password:
13
+ host: localhost
14
+
@@ -0,0 +1,3 @@
1
+ Dummy::Application.routes.draw do
2
+ root :to => 'application#index'
3
+ end
@@ -0,0 +1,11 @@
1
+ class CreateUsers < ActiveRecord::Migration
2
+
3
+ def change
4
+ create_table :users, force: true do |t|
5
+ t.string :name
6
+ t.string :email
7
+ t.timestamps null: false
8
+ end
9
+ end
10
+
11
+ end
@@ -0,0 +1,12 @@
1
+ class CreatePosts < ActiveRecord::Migration
2
+
3
+ def change
4
+ create_table :posts, force: true do |t|
5
+ t.text :title
6
+ t.text :body
7
+ t.references :user, index: true
8
+ t.timestamps null: false
9
+ end
10
+ end
11
+
12
+ end
@@ -0,0 +1,11 @@
1
+ class CreateComments < ActiveRecord::Migration
2
+
3
+ def change
4
+ create_table :comments, force: true do |t|
5
+ t.text :body
6
+ t.references :user, index: true
7
+ t.timestamps null: false
8
+ end
9
+ end
10
+
11
+ end
@@ -0,0 +1,40 @@
1
+ ENV['RAILS_ENV'] ||= 'test'
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../../Gemfile', __FILE__)
3
+ require 'bundler/setup'
4
+ require 'rails/all'
5
+ Bundler.require(:default, Rails.env)
6
+
7
+ module Dummy
8
+ class Application < ::Rails::Application
9
+
10
+ # Basic Engine
11
+ config.root = File.join __FILE__, '..'
12
+ config.cache_store = :memory_store
13
+ config.assets.enabled = false
14
+ config.secret_token = '012345678901234567890123456789'
15
+ config.active_support.test_order = :random
16
+
17
+ # Mimic Test Environment Config.
18
+ config.consider_all_requests_local = true
19
+ config.action_controller.perform_caching = false
20
+ config.action_dispatch.show_exceptions = false
21
+ config.action_controller.allow_forgery_protection = false
22
+ config.action_mailer.delivery_method = :test
23
+ config.active_support.deprecation = :stderr
24
+ config.allow_concurrency = true
25
+ config.cache_classes = true
26
+ config.dependency_loading = true
27
+ config.preload_frameworks = true
28
+ config.eager_load = true
29
+ config.secret_key_base = '012345678901234567890123456789'
30
+
31
+ # Keep pending test:prepare via pending migrations from running.
32
+ config.active_record.maintain_test_schema = false if ActiveRecord::Base.respond_to?(:maintain_test_schema)
33
+
34
+ config.active_record.schema_format = ENV['SCHEMA_FORMAT'] ? :sql : :ruby
35
+
36
+
37
+ end
38
+ end
39
+
40
+ Dummy::Application.initialize!
File without changes
File without changes
@@ -0,0 +1,36 @@
1
+ ENV['RAILS_ENV'] ||= 'test'
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
3
+ require 'bundler/setup'
4
+ Bundler.require :default, :development
5
+ require 'second_base'
6
+ require 'active_support/test_case'
7
+ require 'active_support/testing/autorun'
8
+ require 'dummy_app/init'
9
+ require 'rails/test_help'
10
+ Dir['test/test_helpers/*.{rb}'].each { |f| require_relative "../#{f}" }
11
+
12
+ ActiveSupport.test_order = :random if ActiveSupport.respond_to?(:test_order)
13
+
14
+ module SecondBase
15
+ class TestCase < ActiveSupport::TestCase
16
+
17
+ self.use_transactional_fixtures = false
18
+
19
+ include RailsVersionHelpers,
20
+ DummyAppHelpers,
21
+ StreamHelpers
22
+
23
+ setup :delete_dummy_files
24
+ teardown :delete_dummy_files
25
+
26
+ private
27
+
28
+ def establish_connection
29
+ ActiveRecord::Base.establish_connection
30
+ ActiveRecord::Base.connection
31
+ SecondBase::Base.establish_connection(SecondBase.config)
32
+ SecondBase::Base.connection
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,90 @@
1
+ module SecondBase
2
+ module DummyAppHelpers
3
+
4
+ extend ActiveSupport::Concern
5
+
6
+ private
7
+
8
+ def dummy_app
9
+ ::Dummy::Application
10
+ end
11
+
12
+ def dummy_root
13
+ dummy_app.root
14
+ end
15
+
16
+ def dummy_config
17
+ dummy_app.config
18
+ end
19
+
20
+ def dummy_tmp
21
+ dummy_app.root.join 'tmp'
22
+ end
23
+
24
+ def dummy_db
25
+ dummy_app.root.join 'db'
26
+ end
27
+
28
+ def dummy_schema
29
+ dummy_db.join 'schema.rb'
30
+ end
31
+
32
+ def dummy_secondbase_schema
33
+ dummy_db.join('secondbase', 'schema.rb')
34
+ end
35
+
36
+ def dummy_database_sqlite
37
+ Dir.chdir(dummy_db){ Dir['*.sqlite3'] }.first
38
+ end
39
+
40
+ def dummy_migration
41
+ @dummy_migration ||= begin
42
+ vers = Time.now.utc.strftime '%Y%m%d%H%M%S'
43
+ file = dummy_root.join 'db', 'secondbase', 'migrate', "#{vers}_create_foos.rb"
44
+ migr = %|class CreateFoos < ActiveRecord::Migration ; def change ; create_table(:foos) ; end ; end|
45
+ File.open(file,'w') { |f| f.write(migr) }
46
+ {version: vers, file: file}
47
+ end
48
+ end
49
+
50
+ def delete_dummy_files
51
+ FileUtils.rm_rf dummy_schema
52
+ FileUtils.rm_rf dummy_secondbase_schema
53
+ Dir.chdir(dummy_db) { Dir['**/structure.sql'].each { |structure| FileUtils.rm_rf(structure) } }
54
+ Dir.chdir(dummy_db) { FileUtils.rm_rf(dummy_database_sqlite) } if dummy_database_sqlite
55
+ FileUtils.rm_rf(dummy_migration[:file]) if @dummy_migration
56
+ `mysql -uroot -e "DROP DATABASE IF EXISTS secondbase_test"`
57
+ end
58
+
59
+ # Runners
60
+
61
+ def run_cmd
62
+ 'rake'
63
+ end
64
+
65
+ def run_db(args, stream=:stdout)
66
+ capture(stream) do
67
+ Dir.chdir(dummy_root) { Kernel.system "#{run_cmd} db:#{args}" }
68
+ end
69
+ end
70
+
71
+ def run_secondbase(args, stream=:stdout)
72
+ capture(stream) do
73
+ Dir.chdir(dummy_root) { Kernel.system "#{run_cmd} db:second_base:#{args}" }
74
+ end
75
+ end
76
+
77
+ # Assertions
78
+
79
+ def assert_dummy_databases
80
+ assert_equal 'base.sqlite3', dummy_database_sqlite
81
+ assert_match /secondbase_test/, `mysql -uroot -e "SHOW DATABASES"`
82
+ end
83
+
84
+ def refute_dummy_databases
85
+ assert_nil dummy_database_sqlite
86
+ refute_match /secondbase_test/, `mysql -uroot -e "SHOW DATABASES"`
87
+ end
88
+
89
+ end
90
+ end