innkeeper 0.1.0

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.
Files changed (87) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +15 -0
  3. data/.pryrc +3 -0
  4. data/.travis.yml +15 -0
  5. data/Appraisals +20 -0
  6. data/Gemfile +10 -0
  7. data/Guardfile +24 -0
  8. data/HISTORY.md +337 -0
  9. data/README.md +485 -0
  10. data/Rakefile +92 -0
  11. data/TODO.md +51 -0
  12. data/gemfiles/rails_5_1.gemfile +12 -0
  13. data/innkeeper.gemspec +42 -0
  14. data/lib/generators/innkeeper/install/USAGE +5 -0
  15. data/lib/generators/innkeeper/install/install_generator.rb +10 -0
  16. data/lib/generators/innkeeper/install/templates/innkeeper.rb +76 -0
  17. data/lib/innkeeper.rb +110 -0
  18. data/lib/innkeeper/adapters/abstract_adapter.rb +172 -0
  19. data/lib/innkeeper/adapters/mysql2_adapter.rb +47 -0
  20. data/lib/innkeeper/adapters/postgresql_adapter.rb +112 -0
  21. data/lib/innkeeper/console.rb +12 -0
  22. data/lib/innkeeper/deprecation.rb +10 -0
  23. data/lib/innkeeper/elevators/domain.rb +20 -0
  24. data/lib/innkeeper/elevators/generic.rb +32 -0
  25. data/lib/innkeeper/elevators/host_hash.rb +22 -0
  26. data/lib/innkeeper/elevators/subdomain.rb +62 -0
  27. data/lib/innkeeper/migrator.rb +33 -0
  28. data/lib/innkeeper/railtie.rb +56 -0
  29. data/lib/innkeeper/resolvers/abstract.rb +15 -0
  30. data/lib/innkeeper/resolvers/database.rb +11 -0
  31. data/lib/innkeeper/resolvers/schema.rb +14 -0
  32. data/lib/innkeeper/tasks/enhancements.rb +36 -0
  33. data/lib/innkeeper/tenant.rb +47 -0
  34. data/lib/innkeeper/version.rb +3 -0
  35. data/lib/tasks/innkeeper.rake +128 -0
  36. data/notes.md +31 -0
  37. data/test/config_test.rb +52 -0
  38. data/test/databases.yml.sample +37 -0
  39. data/test/decorator_test.rb +21 -0
  40. data/test/domain_elevator_test.rb +38 -0
  41. data/test/dummy/Rakefile +7 -0
  42. data/test/dummy/app/controllers/application_controller.rb +6 -0
  43. data/test/dummy/app/helpers/application_helper.rb +2 -0
  44. data/test/dummy/app/models/company.rb +3 -0
  45. data/test/dummy/app/models/user.rb +3 -0
  46. data/test/dummy/app/views/application/index.html.erb +1 -0
  47. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  48. data/test/dummy/config.ru +4 -0
  49. data/test/dummy/config/application.rb +49 -0
  50. data/test/dummy/config/boot.rb +11 -0
  51. data/test/dummy/config/database.yml.sample +38 -0
  52. data/test/dummy/config/environment.rb +5 -0
  53. data/test/dummy/config/environments/development.rb +27 -0
  54. data/test/dummy/config/environments/production.rb +51 -0
  55. data/test/dummy/config/environments/test.rb +34 -0
  56. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  57. data/test/dummy/config/initializers/inflections.rb +10 -0
  58. data/test/dummy/config/initializers/innkeeper.rb +4 -0
  59. data/test/dummy/config/initializers/mime_types.rb +5 -0
  60. data/test/dummy/config/initializers/secret_token.rb +7 -0
  61. data/test/dummy/config/initializers/session_store.rb +8 -0
  62. data/test/dummy/config/locales/en.yml +5 -0
  63. data/test/dummy/config/routes.rb +3 -0
  64. data/test/dummy/db/schema.rb +19 -0
  65. data/test/dummy/db/seeds.rb +5 -0
  66. data/test/dummy/db/seeds/import.rb +5 -0
  67. data/test/dummy/public/404.html +26 -0
  68. data/test/dummy/public/422.html +26 -0
  69. data/test/dummy/public/500.html +26 -0
  70. data/test/dummy/public/favicon.ico +0 -0
  71. data/test/dummy/public/stylesheets/.gitkeep +0 -0
  72. data/test/dummy/script/rails +6 -0
  73. data/test/excluded_models_test.rb +32 -0
  74. data/test/generic_elevator_test.rb +63 -0
  75. data/test/host_hash_elevator_test.rb +42 -0
  76. data/test/innkeeper_test.rb +96 -0
  77. data/test/mocks/adapter_mock.rb +11 -0
  78. data/test/multithreading_test.rb +37 -0
  79. data/test/mysql2_adapter_test.rb +17 -0
  80. data/test/postgresql_adapter_test.rb +39 -0
  81. data/test/railtie_test.rb +31 -0
  82. data/test/rake_task_test.rb +57 -0
  83. data/test/resolver_test.rb +21 -0
  84. data/test/shared/shared_adapter_tests.rb +95 -0
  85. data/test/subdomain_elevator_test.rb +75 -0
  86. data/test/test_helper.rb +24 -0
  87. metadata +325 -0
@@ -0,0 +1 @@
1
+ <h1>Index!!</h1>
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= stylesheet_link_tag :all %>
6
+ <%= javascript_include_tag :defaults %>
7
+ <%= csrf_meta_tag %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run Dummy::Application
@@ -0,0 +1,49 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ require "active_model/railtie"
4
+ require "active_record/railtie"
5
+ require "action_controller/railtie"
6
+ require "action_view/railtie"
7
+ require "action_mailer/railtie"
8
+
9
+ Bundler.require
10
+ require "innkeeper"
11
+
12
+ module Dummy
13
+ class Application < Rails::Application
14
+ # Settings in config/environments/* take precedence over those specified here.
15
+ # Application configuration should go into files in config/initializers
16
+ # -- all .rb files in that directory are automatically loaded.
17
+ require 'innkeeper/elevators/subdomain'
18
+ require 'innkeeper/elevators/domain'
19
+
20
+ config.middleware.use Innkeeper::Elevators::Subdomain
21
+
22
+ # Custom directories with classes and modules you want to be autoloadable.
23
+ config.autoload_paths += %W(#{config.root}/lib)
24
+
25
+ # Only load the plugins named here, in the order given (default is alphabetical).
26
+ # :all can be used as a placeholder for all plugins not explicitly named.
27
+ # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
28
+
29
+ # Activate observers that should always be running.
30
+ # config.active_record.observers = :cacher, :garbage_collector, :forum_observer
31
+
32
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
33
+ # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
34
+ # config.time_zone = 'Central Time (US & Canada)'
35
+
36
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
37
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
38
+ # config.i18n.default_locale = :de
39
+
40
+ # JavaScript files you want as :defaults (application.js is always included).
41
+ # config.action_view.javascript_expansions[:defaults] = %w(jquery rails)
42
+
43
+ # Configure the default encoding used in templates for Ruby 1.9.
44
+ config.encoding = "utf-8"
45
+
46
+ # Configure sensitive parameters which will be filtered from the log file.
47
+ config.filter_parameters += [:password]
48
+ end
49
+ end
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+
3
+ gemfile = File.expand_path('../../../../Gemfile', __FILE__)
4
+
5
+ if File.exist?(gemfile)
6
+ ENV['BUNDLE_GEMFILE'] = gemfile
7
+ require 'bundler'
8
+ Bundler.setup
9
+ end
10
+
11
+ $:.unshift File.expand_path('../../../../lib', __FILE__)
@@ -0,0 +1,38 @@
1
+ # Warning: The database defined as "test" will be erased and
2
+ # re-generated from your development database when you run "rake".
3
+ # Do not set this db to the same as development or production.
4
+ <% if defined?(JRUBY_VERSION) %>
5
+ test:
6
+ adapter: postgresql
7
+ database: innkeeper_postgresql_test
8
+ username: postgres
9
+ min_messages: WARNING
10
+ driver: org.postgresql.Driver
11
+ url: jdbc:postgresql://localhost:5432/innkeeper_postgresql_test
12
+ timeout: 5000
13
+ pool: 5
14
+
15
+ development:
16
+ adapter: postgresql
17
+ database: innkeeper_postgresql_development
18
+ username: postgres
19
+ min_messages: WARNING
20
+ driver: org.postgresql.Driver
21
+ url: jdbc:postgresql://localhost:5432/innkeeper_postgresql_development
22
+ timeout: 5000
23
+ pool: 5
24
+ <% else %>
25
+ test:
26
+ adapter: postgresql
27
+ database: innkeeper_postgresql_test
28
+ min_messages: WARNING
29
+ pool: 5
30
+ timeout: 5000
31
+
32
+ development:
33
+ adapter: postgresql
34
+ database: innkeeper_postgresql_development
35
+ min_messages: WARNING
36
+ pool: 5
37
+ timeout: 5000
38
+ <% end %>
@@ -0,0 +1,5 @@
1
+ # Load the rails application
2
+ require File.expand_path('../application', __FILE__)
3
+
4
+ # Initialize the rails application
5
+ Dummy::Application.initialize!
@@ -0,0 +1,27 @@
1
+ Dummy::Application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb
3
+
4
+ # In the development environment your application's code is reloaded on
5
+ # every request. This slows down response time but is perfect for development
6
+ # since you don't have to restart the webserver when you make code changes.
7
+ config.cache_classes = false
8
+
9
+ config.eager_load = false
10
+
11
+ # Log error messages when you accidentally call methods on nil.
12
+ config.whiny_nils = true
13
+
14
+ # Show full error reports and disable caching
15
+ config.consider_all_requests_local = true
16
+ config.action_controller.perform_caching = false
17
+
18
+ # Don't care if the mailer can't send
19
+ config.action_mailer.raise_delivery_errors = false
20
+
21
+ # Print deprecation notices to the Rails logger
22
+ config.active_support.deprecation = :log
23
+
24
+ # Only use best-standards-support built into browsers
25
+ config.action_dispatch.best_standards_support = :builtin
26
+ end
27
+
@@ -0,0 +1,51 @@
1
+ Dummy::Application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb
3
+
4
+ # The production environment is meant for finished, "live" apps.
5
+ # Code is not reloaded between requests
6
+ config.cache_classes = true
7
+
8
+ config.eager_load = true
9
+
10
+ # Full error reports are disabled and caching is turned on
11
+ config.consider_all_requests_local = false
12
+ config.action_controller.perform_caching = true
13
+
14
+ # Specifies the header that your server uses for sending files
15
+ config.action_dispatch.x_sendfile_header = "X-Sendfile"
16
+
17
+ # For nginx:
18
+ # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'
19
+
20
+ # If you have no front-end server that supports something like X-Sendfile,
21
+ # just comment this out and Rails will serve the files
22
+
23
+ # See everything in the log (default is :info)
24
+ # config.log_level = :debug
25
+
26
+ # Use a different logger for distributed setups
27
+ # config.logger = SyslogLogger.new
28
+
29
+ # Use a different cache store in production
30
+ # config.cache_store = :mem_cache_store
31
+
32
+ # Disable Rails's static asset server
33
+ # In production, Apache or nginx will already do this
34
+ config.serve_static_assets = false
35
+
36
+ # Enable serving of images, stylesheets, and javascripts from an asset server
37
+ # config.action_controller.asset_host = "http://assets.example.com"
38
+
39
+ # Disable delivery errors, bad email addresses will be ignored
40
+ # config.action_mailer.raise_delivery_errors = false
41
+
42
+ # Enable threaded mode
43
+ # config.threadsafe!
44
+
45
+ # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
46
+ # the I18n.default_locale when a translation can not be found)
47
+ config.i18n.fallbacks = true
48
+
49
+ # Send deprecation notices to registered listeners
50
+ config.active_support.deprecation = :notify
51
+ end
@@ -0,0 +1,34 @@
1
+ Dummy::Application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb
3
+
4
+ # The test environment is used exclusively to run your application's
5
+ # test suite. You never need to work with it otherwise. Remember that
6
+ # your test database is "scratch space" for the test suite and is wiped
7
+ # and recreated between test runs. Don't rely on the data there!
8
+ config.cache_classes = true
9
+
10
+ config.eager_load = false
11
+
12
+ # Show full error reports and disable caching
13
+ config.consider_all_requests_local = true
14
+ config.action_controller.perform_caching = false
15
+
16
+ # Raise exceptions instead of rendering exception templates
17
+ config.action_dispatch.show_exceptions = false
18
+
19
+ # Disable request forgery protection in test environment
20
+ config.action_controller.allow_forgery_protection = false
21
+
22
+ # Tell Action Mailer not to deliver emails to the real world.
23
+ # The :test delivery method accumulates sent emails in the
24
+ # ActionMailer::Base.deliveries array.
25
+ config.action_mailer.delivery_method = :test
26
+
27
+ # Use SQL instead of Active Record's schema dumper when creating the test database.
28
+ # This is necessary if your schema can't be completely dumped by the schema dumper,
29
+ # like if you have constraints or database-specific column types
30
+ # config.active_record.schema_format = :sql
31
+
32
+ # Print deprecation notices to the stderr
33
+ config.active_support.deprecation = :stderr
34
+ end
@@ -0,0 +1,7 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces.
4
+ # Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ }
5
+
6
+ # You can also remove all the silencers if you're trying to debug a problem that might stem from framework code.
7
+ # Rails.backtrace_cleaner.remove_silencers!
@@ -0,0 +1,10 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Add new inflection rules using the following format
4
+ # (all these examples are active by default):
5
+ # ActiveSupport::Inflector.inflections do |inflect|
6
+ # inflect.plural /^(ox)$/i, '\1en'
7
+ # inflect.singular /^(ox)en/i, '\1'
8
+ # inflect.irregular 'person', 'people'
9
+ # inflect.uncountable %w( fish sheep )
10
+ # end
@@ -0,0 +1,4 @@
1
+ Innkeeper.configure do |config|
2
+ # config.excluded_models = ["Company"]
3
+ # config.tenant_names = lambda{ Company.pluck(:database) }
4
+ end
@@ -0,0 +1,5 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Add new mime types for use in respond_to blocks:
4
+ # Mime::Type.register "text/richtext", :rtf
5
+ # Mime::Type.register_alias "text/html", :iphone
@@ -0,0 +1,7 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Your secret key for verifying the integrity of signed cookies.
4
+ # If you change this key, all old signed cookies will become invalid!
5
+ # Make sure the secret is at least 30 characters and all random,
6
+ # no regular words or you'll be exposed to dictionary attacks.
7
+ Dummy::Application.config.secret_token = '7d33999a86884f74c897c98ecca4277090b69e9f23df8d74bcadd57435320a7a16de67966f9b69d62e7d5ec553bd2febbe64c721e05bc1bc1e82c7a7d2395201'
@@ -0,0 +1,8 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ Dummy::Application.config.session_store :cookie_store, :key => '_dummy_session'
4
+
5
+ # Use the database for sessions instead of the cookie-based default,
6
+ # which shouldn't be used to store highly confidential information
7
+ # (create the session table with "rails generate session_migration")
8
+ # Dummy::Application.config.session_store :active_record_store
@@ -0,0 +1,5 @@
1
+ # Sample localization file for English. Add more files in this directory for other locales.
2
+ # See http://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
3
+
4
+ en:
5
+ hello: "Hello world"
@@ -0,0 +1,3 @@
1
+ Dummy::Application.routes.draw do
2
+ root :to => 'application#index'
3
+ end
@@ -0,0 +1,19 @@
1
+ ActiveRecord::Schema.define(version: 20170619120400) do
2
+ create_table "books", force: :cascade do |t|
3
+ t.string "name"
4
+ t.integer "pages"
5
+ t.datetime "published"
6
+ end
7
+
8
+ create_table "companies", force: :cascade do |t|
9
+ t.boolean "dummy"
10
+ t.string "database"
11
+ end
12
+
13
+ create_table "users", force: :cascade do |t|
14
+ t.string "name"
15
+ t.datetime "birthdate"
16
+ t.string "sex"
17
+ t.timestamps
18
+ end
19
+ end
@@ -0,0 +1,5 @@
1
+ def create_users
2
+ 3.times {|x| User.where(name: "Some User #{x}").first_or_create! }
3
+ end
4
+
5
+ create_users
@@ -0,0 +1,5 @@
1
+ def create_users
2
+ 6.times {|x| User.where(name: "Different User #{x}").first_or_create! }
3
+ end
4
+
5
+ create_users
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The page you were looking for doesn't exist (404)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/404.html -->
21
+ <div class="dialog">
22
+ <h1>The page you were looking for doesn't exist.</h1>
23
+ <p>You may have mistyped the address or the page may have moved.</p>
24
+ </div>
25
+ </body>
26
+ </html>
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The change you wanted was rejected (422)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/422.html -->
21
+ <div class="dialog">
22
+ <h1>The change you wanted was rejected.</h1>
23
+ <p>Maybe you tried to change something you didn't have access to.</p>
24
+ </div>
25
+ </body>
26
+ </html>
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>We're sorry, but something went wrong (500)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/500.html -->
21
+ <div class="dialog">
22
+ <h1>We're sorry, but something went wrong.</h1>
23
+ <p>We've been notified about this issue and we'll take a look at it shortly.</p>
24
+ </div>
25
+ </body>
26
+ </html>
File without changes
File without changes
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3
+
4
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
5
+ require File.expand_path('../../config/boot', __FILE__)
6
+ require 'rails/commands'
@@ -0,0 +1,32 @@
1
+ require_relative 'test_helper'
2
+ require 'innkeeper/resolvers/database'
3
+
4
+ class ExcludedModelsTest < Innkeeper::Test
5
+ def setup
6
+ setup_connection("mysql")
7
+
8
+ Innkeeper.configure do |config|
9
+ config.tenant_resolver = Innkeeper::Resolvers::Database
10
+ config.excluded_models = %w(Company User)
11
+ end
12
+
13
+ super
14
+ end
15
+
16
+ def test_model_exclusions
17
+ Innkeeper::Tenant.adapter.process_excluded_models
18
+
19
+ assert_equal :_innkeeper_excluded, Company.connection_specification_name
20
+
21
+ Innkeeper::Tenant.switch(@tenant1) do
22
+ assert_tenant_is(@tenant1)
23
+ assert_tenant_is(Innkeeper.default_tenant, for_model: Company)
24
+ end
25
+ end
26
+
27
+ def test_all_excluded_models_use_same_connection_pool
28
+ Innkeeper::Tenant.adapter.process_excluded_models
29
+
30
+ assert_equal Company.connection_pool, User.connection_pool
31
+ end
32
+ end