penthouse 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5fdce337d0e5a5c6662f7d2fbfd2a3f14877fc7a
4
- data.tar.gz: ca32b3019f2bdf8b105f962c8f1f40767a86cf95
3
+ metadata.gz: d0c9927dc0600cac735628cb2a5eab354fe2594b
4
+ data.tar.gz: 70140e22124a0f0a69d98e3f1503ae242a9b65c5
5
5
  SHA512:
6
- metadata.gz: fbc0c2e3951d8acd63ffe18c6a2dd2d3ea4a32b508001767ce64cfbff1c30e8d63e913f0401688c157bf88b95f0c69a977a735c4cc6f1c8c3d2433283bf644f5
7
- data.tar.gz: 2eab6ab62ea17238ab22999b6c7a0f6f8bc7228583ee06f29bef91e9e3a2676a42fc25b681d649f5b737c4b141826ca75562e07a40b2575331151be4e76d97b1
6
+ metadata.gz: b7230bf2af1ca86e70ae88dab90ebf07dbd75d34ef0b8350d865cf95957788028232d93760d472aa827ff2a9588d4d5543d8101dbec6a32f0d05637496cbd262
7
+ data.tar.gz: 6160b12b68e6380f2ecfd408e803ee242ae35cae61f26da3fab4e9a0864f91a8a7b425de923114abd582fc3b97a51082b3c8dc8d0ef0fed77581fc65f7363610
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- [ ![Codeship Status](https://codeship.com/projects/c6513ab0-cc05-0133-94c6-0666c337ff82/status?branch=master)](https://codeship.com/projects/140114) [![Code Climate](https://codeclimate.com/github/ryantownsend/penthouse/badges/gpa.svg)](https://codeclimate.com/github/ryantownsend/penthouse)
1
+ [![Codeship Status](https://codeship.com/projects/c6513ab0-cc05-0133-94c6-0666c337ff82/status?branch=master)](https://codeship.com/projects/140114) [![Code Climate](https://codeclimate.com/github/ryantownsend/penthouse/badges/gpa.svg)](https://codeclimate.com/github/ryantownsend/penthouse) [![RubyDocs](https://img.shields.io/badge/rubydocs-click_here-blue.svg)](http://www.rubydoc.info/github/ryantownsend/penthouse)
2
2
 
3
3
  Penthouse is an alternative to the excellent [Apartment gem](https://github.com/influitive/apartment) – however Penthouse is more of a framework for multi-tenancy than a library, in that it provides less out-of-the-box functionality, but should make for easier customisation.
4
4
 
data/lib/penthouse/app.rb CHANGED
@@ -27,6 +27,7 @@ module Penthouse
27
27
 
28
28
  # @param env [Hash] the environment passed from Rack
29
29
  # @raise [Penthouse::TenantNotFound] if the tenant cannot be found/switched to
30
+ # @return [void]
30
31
  def call(env)
31
32
  request = Rack::Request.new(env)
32
33
  runner.call(router.call(request)) do
@@ -8,6 +8,7 @@ module Penthouse
8
8
 
9
9
  # Migrate to latest version
10
10
  # @param tenant_identifier [String, Symbol] the identifier for the tenant to switch to
11
+ # @return [void]
11
12
  def migrate(tenant_identifier)
12
13
  Penthouse.switch(tenant_identifier) do
13
14
  version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
@@ -21,6 +22,7 @@ module Penthouse
21
22
  # Migrate up/down to a specific version
22
23
  # @param tenant_identifier [String, Symbol] the identifier for the tenant to switch to
23
24
  # @param version [Integer] the version number to migrate up or down to
25
+ # @return [void]
24
26
  def run(direction, tenant_identifier, version)
25
27
  Penthouse.switch(tenant_identifier) do
26
28
  ActiveRecord::Migrator.run(direction, ActiveRecord::Migrator.migrations_paths, version)
@@ -30,6 +32,7 @@ module Penthouse
30
32
  # rollback latest migration `step` number of times
31
33
  # @param tenant_identifier [String, Symbol] the identifier for the tenant to switch to
32
34
  # @param step [Integer] how many migrations to rollback by
35
+ # @return [void]
33
36
  def rollback(tenant_identifier, step = 1)
34
37
  Penthouse.switch(tenant_identifier) do
35
38
  ActiveRecord::Migrator.rollback(ActiveRecord::Migrator.migrations_paths, step)
@@ -0,0 +1,11 @@
1
+ require 'rails'
2
+
3
+ module Penthouse
4
+ class Railtie < Rails::Railtie
5
+ # Ensure rake tasks are loaded
6
+ rake_tasks do
7
+ load 'tasks/penthouse.rake'
8
+ require 'penthouse/tasks/enhancements' if Apartment.db_migrate_tenants
9
+ end
10
+ end
11
+ end
@@ -13,6 +13,7 @@ module Penthouse
13
13
 
14
14
  # @param tenant_identifier [String, Symbol] The identifier for the tenant
15
15
  # @param block [Block] The code to execute within the tenant
16
+ # @return [void]
16
17
  # @raise [Penthouse::TenantNotFound] if the tenant cannot be switched to
17
18
  def self.call(tenant_identifier, &block)
18
19
  load_tenant(tenant_identifier).call do |tenant|
@@ -21,16 +21,19 @@ module Penthouse
21
21
  # @abstract placeholder for the relevant tenant-switching code
22
22
  # @param block [Block] The code to execute within the tenant
23
23
  # @yield [BaseTenant] The current tenant instance
24
+ # @return [void]
24
25
  def call(&block)
25
26
  raise NotImplementedError
26
27
  end
27
28
 
28
29
  # @abstract creates the tenant data store
30
+ # @return [void]
29
31
  def create(*)
30
32
  raise NotImplementedError
31
33
  end
32
34
 
33
35
  # @abstract deletes the tenant data store
36
+ # @return [void]
34
37
  def delete(*)
35
38
  raise NotImplementedError
36
39
  end
@@ -12,15 +12,11 @@ module Penthouse
12
12
  module Migratable
13
13
 
14
14
  # @param db_schema_file [String] a path to the DB schema file to load, defaults to Penthouse.configuration.db_schema_file
15
+ # @return [void]
15
16
  def migrate(db_schema_file: Penthouse.configuration.db_schema_file)
16
17
  if File.exist?(db_schema_file)
17
- # turn off logging schemas
18
- ActiveRecord::Schema.verbose = false
19
18
  # run the migrations within this schema
20
- call do
21
- puts ActiveRecord::Base.connection.schema_search_path
22
- load(db_schema_file)
23
- end
19
+ call { load(db_schema_file) }
24
20
  else
25
21
  raise ArgumentError, "#{db_schema_file} does not exist"
26
22
  end
@@ -18,6 +18,7 @@ module Penthouse
18
18
  # with the tenant name
19
19
  # @param block [Block] The code to execute within the schema
20
20
  # @yield [SchemaTenant] The current tenant instance
21
+ # @return [void]
21
22
  def call(&block)
22
23
  Octopus.using(:master) do
23
24
  super
@@ -26,18 +27,21 @@ module Penthouse
26
27
 
27
28
  # creates the tenant schema within the master shard
28
29
  # @see Penthouse::Tenants::SchemaTenant#create
30
+ # @return [void]
29
31
  def create(*)
30
32
  call { super }
31
33
  end
32
34
 
33
35
  # drops the tenant schema within the master shard
34
36
  # @see Penthouse::Tenants::SchemaTenant#delete
37
+ # @return [void]
35
38
  def delete(*)
36
39
  call { super }
37
40
  end
38
41
 
39
42
  # returns whether or not the schema exists
40
43
  # @see Penthouse::Tenants::SchemaTenant#exists?
44
+ # @return [Boolean] whether or not the schema exists in the master shard
41
45
  def exists?(*)
42
46
  call { super }
43
47
  end
@@ -29,6 +29,7 @@ module Penthouse
29
29
  # switches to the relevant Octopus shard, and processes the block
30
30
  # @param block [Block] The code to execute within the connection to the shard
31
31
  # @yield [ShardTenant] The current tenant instance
32
+ # @return [void]
32
33
  def call(&block)
33
34
  Octopus.using(shard) do
34
35
  block.yield(self)
@@ -32,6 +32,7 @@ module Penthouse
32
32
  # afterwards, regardless of whether an exception occurs
33
33
  # @param block [Block] The code to execute within the schema
34
34
  # @yield [SchemaTenant] The current tenant instance
35
+ # @return [void]
35
36
  def call(&block)
36
37
  begin
37
38
  # set the search path to include the tenant
@@ -46,6 +47,7 @@ module Penthouse
46
47
  # creates the tenant schema
47
48
  # @param run_migrations [Boolean] whether or not to run migrations, defaults to Penthouse.configuration.migrate_tenants?
48
49
  # @param db_schema_file [String] a path to the DB schema file to load, defaults to Penthouse.configuration.db_schema_file
50
+ # @return [void]
49
51
  def create(run_migrations: Penthouse.configuration.migrate_tenants?, db_schema_file: Penthouse.configuration.db_schema_file)
50
52
  sql = ActiveRecord::Base.send(:sanitize_sql_array, ["create schema if not exists %s", tenant_schema])
51
53
  ActiveRecord::Base.connection.exec_query(sql, 'Create Schema')
@@ -56,6 +58,7 @@ module Penthouse
56
58
 
57
59
  # drops the tenant schema
58
60
  # @param force [Boolean] whether or not to drop the schema if not empty, defaults to true
61
+ # @return [void]
59
62
  def delete(force: true)
60
63
  sql = ActiveRecord::Base.send(:sanitize_sql_array, ["drop schema if exists %s %s", tenant_schema, force ? 'cascade' : 'restrict'])
61
64
  ActiveRecord::Base.connection.exec_query(sql, 'Delete Schema')
@@ -1,3 +1,3 @@
1
1
  module Penthouse
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
data/lib/penthouse.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require "penthouse/railtie" if defined?(Rails)
1
2
  require "penthouse/version"
2
3
  require "penthouse/configuration"
3
4
  require "penthouse/routers/base_router"
@@ -15,6 +16,7 @@ module Penthouse
15
16
 
16
17
  # Sets the currently active tenant identifier
17
18
  # @param tenant_identifier [String, Symbol] the identifier for the tenant
19
+ # @return [void]
18
20
  def tenant=(tenant_identifier)
19
21
  Thread.current[:tenant] = tenant_identifier
20
22
  end
@@ -25,6 +27,7 @@ module Penthouse
25
27
  # @param default_tenant [String, Symbol] the identifier for the tenant to return to
26
28
  # @param block [Block] the code to execute
27
29
  # @yield [String, Symbol] the identifier for the tenant
30
+ # @return [void]
28
31
  def with_tenant(tenant_identifier, default_tenant: tenant, &block)
29
32
  self.tenant = tenant_identifier
30
33
  block.yield(tenant_identifier)
@@ -37,6 +40,7 @@ module Penthouse
37
40
  # @param default_tenant [String, Symbol] the identifier for the tenant to return to
38
41
  # @param block [Block] the code to execute
39
42
  # @yield [String, Symbol] the identifier for the tenant
43
+ # @return [void]
40
44
  def each_tenant(default_tenant: tenant, runner: configuration.runner, &block)
41
45
  tenant_identifiers.each do |tenant_identifier|
42
46
  switch(tenant_identifier, runner: runner, &block)
@@ -48,6 +52,7 @@ module Penthouse
48
52
  # @param runner [Penthouse::Runners::BaseRunner] an optional runner to use, defaults to the one configured
49
53
  # @param block [Block] the code to execute
50
54
  # @yield [Penthouse::Tenants::BaseTenant] the tenant instance
55
+ # @return [void]
51
56
  def switch(tenant_identifier, runner: configuration.runner, &block)
52
57
  runner.call(tenant_identifier, &block)
53
58
  end
@@ -55,6 +60,7 @@ module Penthouse
55
60
  # Loads the tenant and creates their data store
56
61
  # @param tenant_identifier [String, Symbol] the identifier for the tenant
57
62
  # @see Penthouse::Tenants::BaseTenant#delete
63
+ # @return [void]
58
64
  def create(tenant_identifier, runner: configuration.runner, **options)
59
65
  switch(tenant_identifier, runner: runner) do |tenant|
60
66
  tenant.create(**options)
@@ -64,6 +70,7 @@ module Penthouse
64
70
  # Loads the tenant and deletes their data store
65
71
  # @param tenant_identifier [String, Symbol] the identifier for the tenant
66
72
  # @see Penthouse::Tenants::BaseTenant#delete
73
+ # @return [void]
67
74
  def delete(tenant_identifier, runner: configuration.runner, **options)
68
75
  switch(tenant_identifier, runner: runner) do |tenant|
69
76
  tenant.delete(**options)
@@ -72,6 +79,7 @@ module Penthouse
72
79
 
73
80
  # Allows you to configure the router of Penthouse
74
81
  # @yield [Penthouse::Configuration]
82
+ # @return [void]
75
83
  def configure(&block)
76
84
  # allow the configuration by the block
77
85
  block.yield(self.configuration)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: penthouse
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Townsend
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-03-15 00:00:00.000000000 Z
11
+ date: 2016-03-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -170,6 +170,7 @@ files:
170
170
  - lib/penthouse/app.rb
171
171
  - lib/penthouse/configuration.rb
172
172
  - lib/penthouse/migrator.rb
173
+ - lib/penthouse/railtie.rb
173
174
  - lib/penthouse/routers/base_router.rb
174
175
  - lib/penthouse/routers/subdomain_router.rb
175
176
  - lib/penthouse/runners/base_runner.rb