multiverse 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: efd32f0437186abbe23bde74a36c059124592e49
4
+ data.tar.gz: 848d77b215192dd19f7a5091225120fdfe7cba1b
5
+ SHA512:
6
+ metadata.gz: c2080ba271e93150ddc91d790a109ced1258c1fea53a948f8a2661ef9e89ba34d05d1d081153793c10d1cd246b30ee6da15924d7cdf6fba1f4df5776ade9cd32
7
+ data.tar.gz: ecfc897bedd8b7ccfc2e914db917f783b60256636c0aac57eae7f6e27152d94b96fd743b6b242470e4eaf29696ad86b0f97bbd16c55460361a112e17836bf85b
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ *.lock
@@ -0,0 +1,3 @@
1
+ ## 0.0.1
2
+
3
+ - First release
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in multiverse.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2017 Andrew Kane
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,109 @@
1
+ # Multiverse
2
+
3
+ :fire: Multiple databases for Rails
4
+
5
+ One of the easiest ways to scale your database is to move large, infrequently-joined tables to a separate database. **ActiveRecord supports multiple databases, but Rails doesn’t provide a way to manage them.** Multiverse changes this.
6
+
7
+ Works with Rails 5+
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application’s Gemfile:
12
+
13
+ ```ruby
14
+ gem 'multiverse'
15
+ ```
16
+
17
+ ## Getting Started
18
+
19
+ Generate a new database
20
+
21
+ ```sh
22
+ rails generate multiverse:db catalog
23
+ ```
24
+
25
+ This generates `CatalogRecord` class for models to inherit from and adds configuration to `config/database.yml`. It also creates a `db/catalog` directory for migrations and `schema.rb` to live.
26
+
27
+ `rails` and `rake` commands will run for the original database by default. To run commands for the new database, use the `DB` environment variable. For instance:
28
+
29
+ Create the database
30
+
31
+ ```sh
32
+ DB=catalog rake db:create
33
+ ```
34
+
35
+ Create a migration
36
+
37
+ ```sh
38
+ DB=catalog rails generate migration add_name_to_products
39
+ ```
40
+
41
+ Run migrations
42
+
43
+ ```sh
44
+ DB=catalog rake db:migrate
45
+ ```
46
+
47
+ Rollback
48
+
49
+ ```sh
50
+ DB=catalog rake db:rollback
51
+ ```
52
+
53
+ ## Models
54
+
55
+ Also works for models
56
+
57
+ ```sh
58
+ DB=catalog rails generate model Product
59
+ ```
60
+
61
+ This generates
62
+
63
+ ```rb
64
+ class Product < CatalogRecord
65
+ end
66
+ ```
67
+
68
+ ## Seeds
69
+
70
+ Use `db/seeds.rb` for all databases
71
+
72
+ ## Web Servers
73
+
74
+ ### Puma
75
+
76
+ Add to `config/puma.rb`
77
+
78
+ ```ruby
79
+ on_worker_boot do
80
+ CatalogRecord.establish_connection :"catalog_#{Rails.env}"
81
+ end
82
+ ```
83
+
84
+ ### Unicorn
85
+
86
+ Add to `config/unicorn.rb`
87
+
88
+ ```ruby
89
+ before_fork do |server, worker|
90
+ CatalogRecord.connection.disconnect!
91
+ end
92
+
93
+ after_fork do |server, worker|
94
+ CatalogRecord.establish_connection :"catalog_#{Rails.env}"
95
+ end
96
+ ```
97
+
98
+ ## History
99
+
100
+ View the [changelog](https://github.com/ankane/multiverse/blob/master/CHANGELOG.md)
101
+
102
+ ## Contributing
103
+
104
+ Everyone is encouraged to help improve this project. Here are a few ways you can help:
105
+
106
+ - [Report bugs](https://github.com/ankane/multiverse/issues)
107
+ - Fix bugs and [submit pull requests](https://github.com/ankane/multiverse/pulls)
108
+ - Write, clarify, or fix documentation
109
+ - Suggest or add new features
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,35 @@
1
+ require "rails/generators"
2
+
3
+ module Multiverse
4
+ module Generators
5
+ class DbGenerator < Rails::Generators::Base
6
+ source_root File.expand_path("../templates", __FILE__)
7
+
8
+ argument :name
9
+
10
+ def create_initializer
11
+ lower_name = name.underscore
12
+
13
+ template "record.rb", "app/models/#{lower_name}_record.rb"
14
+
15
+ append_to_file "config/database.yml" do
16
+ "
17
+ #{name}_development:
18
+ <<: *default
19
+ database: #{lower_name}_development
20
+
21
+ #{name}_test:
22
+ <<: *default
23
+ database: #{lower_name}_test
24
+
25
+ #{name}_production:
26
+ <<: *default
27
+ url: <%= ENV['#{lower_name.upcase}_DATABASE_URL'] %>
28
+ "
29
+ end
30
+
31
+ empty_directory "db/#{lower_name}/migrate"
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,4 @@
1
+ class <%= name.camelize %>Record < ActiveRecord::Base
2
+ self.abstract_class = true
3
+ establish_connection :"<%= name.underscore %>_#{Rails.env}"
4
+ end
@@ -0,0 +1,41 @@
1
+ require "multiverse/generators"
2
+ require "multiverse/patches"
3
+ require "multiverse/railtie"
4
+ require "multiverse/version"
5
+
6
+ module Multiverse
7
+ class << self
8
+ attr_writer :db
9
+
10
+ def db
11
+ @db ||= ENV["DB"]
12
+ end
13
+
14
+ def db_dir
15
+ db_dir = db ? "db/#{db}" : "db"
16
+ abort "Unknown DB: #{db}" if db && !Dir.exist?(db_dir)
17
+ db_dir
18
+ end
19
+
20
+ def parent_class_name
21
+ db ? "#{db.camelize}Record" : "ApplicationRecord"
22
+ end
23
+
24
+ def record_class
25
+ record_class = parent_class_name.safe_constantize
26
+ abort "Missing model: #{parent_class_name}" unless record_class
27
+ record_class
28
+ end
29
+
30
+ def migrate_path
31
+ "#{db_dir}/migrate"
32
+ end
33
+ end
34
+ end
35
+
36
+ ActiveSupport.on_load(:active_record) do
37
+ ActiveRecord::Tasks::DatabaseTasks.singleton_class.prepend Multiverse::DatabaseTasks
38
+ ActiveRecord::Migration.prepend Multiverse::Migration
39
+ ActiveRecord::SchemaMigration.singleton_class.prepend Multiverse::SchemaMigration
40
+ ActiveRecord::SchemaDumper.singleton_class.prepend Multiverse::SchemaDumper
41
+ end
@@ -0,0 +1,15 @@
1
+ module Multiverse
2
+ module Generators
3
+ module ModelGenerator
4
+ def parent_class_name
5
+ Multiverse.parent_class_name
6
+ end
7
+ end
8
+
9
+ module Migration
10
+ def db_migrate_path
11
+ Multiverse.migrate_path
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,46 @@
1
+ module Multiverse
2
+ module DatabaseTasks
3
+ def each_current_configuration(environment)
4
+ if Multiverse.db
5
+ environments = ["#{Multiverse.db}_#{environment}"]
6
+ environments << "#{Multiverse.db}_test" if environment == "development"
7
+ self.migrations_paths = Multiverse.migrate_path
8
+ self.db_dir = Multiverse.db_dir
9
+ else
10
+ environments = [environment]
11
+ environments << "test" if environment == "development"
12
+ end
13
+
14
+ configurations = ActiveRecord::Base.configurations.values_at(*environments)
15
+ configurations.compact.each do |configuration|
16
+ yield configuration unless configuration['database'].blank?
17
+ end
18
+ end
19
+ end
20
+
21
+ module Migration
22
+ def connection
23
+ @connection || Multiverse.record_class.connection
24
+ end
25
+
26
+ # TODO don't checkout main connection at all
27
+ def exec_migration(_, direction)
28
+ Multiverse.record_class.connection_pool.with_connection do |conn|
29
+ super(conn, direction)
30
+ end
31
+ end
32
+ end
33
+
34
+ module SchemaMigration
35
+ def connection
36
+ Multiverse.record_class.connection
37
+ end
38
+ end
39
+
40
+ module SchemaDumper
41
+ def dump(connection = ActiveRecord::Base.connection, stream = STDOUT, config = ActiveRecord::Base)
42
+ new(Multiverse.record_class.connection, generate_options(Multiverse.record_class)).dump(stream)
43
+ stream
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,22 @@
1
+ require "rails/railtie"
2
+
3
+ module Multiverse
4
+ class Railtie < Rails::Railtie
5
+ generators do
6
+ require "rails/generators/active_record/migration"
7
+ ActiveRecord::Generators::Migration.prepend(Multiverse::Generators::Migration)
8
+
9
+ require "rails/generators/active_record/model/model_generator"
10
+ ActiveRecord::Generators::ModelGenerator.prepend(Multiverse::Generators::ModelGenerator)
11
+ end
12
+
13
+ rake_tasks do
14
+ namespace :db do
15
+ task :load_config do
16
+ ActiveRecord::Tasks::DatabaseTasks.migrations_paths = [Multiverse.migrate_path]
17
+ ActiveRecord::Tasks::DatabaseTasks.db_dir = [Multiverse.db_dir]
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ module Multiverse
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,27 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "multiverse/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "multiverse"
8
+ spec.version = Multiverse::VERSION
9
+ spec.authors = ["Andrew Kane"]
10
+ spec.email = ["andrew@chartkick.com"]
11
+
12
+ spec.summary = "Multiple databases for Rails"
13
+ spec.homepage = "https://github.com/ankane/multiverse"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
+ f.match(%r{^(test|spec|features)/})
17
+ end
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "rails", ">= 5"
23
+
24
+ spec.add_development_dependency "bundler"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "minitest"
27
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: multiverse
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Kane
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-11-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description:
70
+ email:
71
+ - andrew@chartkick.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - CHANGELOG.md
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - lib/generators/multiverse/db_generator.rb
83
+ - lib/generators/multiverse/templates/record.rb
84
+ - lib/multiverse.rb
85
+ - lib/multiverse/generators.rb
86
+ - lib/multiverse/patches.rb
87
+ - lib/multiverse/railtie.rb
88
+ - lib/multiverse/version.rb
89
+ - multiverse.gemspec
90
+ homepage: https://github.com/ankane/multiverse
91
+ licenses: []
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.6.13
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Multiple databases for Rails
113
+ test_files: []