penthouse 0.6.0 → 0.7.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: 0632eea067e1cf85d45c912212bfa613d7c34566
4
- data.tar.gz: 60a49d25ab6339cb567a4815b53df28fc6b30127
3
+ metadata.gz: 5c2f7032744a162f8d7841fb997536f71ed30c74
4
+ data.tar.gz: 74dc6c8881eaa3b869f0ad078217632a8037bed6
5
5
  SHA512:
6
- metadata.gz: 6e60e2fe6d903e61317ed5e4930d793dded2bbdb93c7762516a477ec18a9de783dfb1690cf22952ffab141acae16b54357a616a51754e79d117b36d75a268c80
7
- data.tar.gz: 7d53645c0a228a3cd62c6854e7ede31d3a56b40e5d12f04d18758358c9e38fcec3a7716a2e6637f9ba6908c8f93220e02f4a2077a6f030e901d2ca98bec7abde
6
+ metadata.gz: 776a192ee3c89480cd2fe9124aa65bba125d3d97c716dffcd0300b462d42cf400cc5f014836007608f8fa8ec0f7b32daf2aabba84d67ffb1e2250ce705f83bfd
7
+ data.tar.gz: 8e2ce6d50bdb1cac77fdec1bd97a9135344fd98d3a83acedc5148a0bd9f8ce8f8dae19f8a2784bb77420849f39914781cc8863fcc91b9624edf178c80e9eae26
@@ -1,56 +1,120 @@
1
- # shamelessly copied & adjusted from Apartment
2
- # @see https://github.com/influitive/apartment/blob/master/lib/apartment/migrator.rb
3
- # overrides Octopus's auto-switching to shards
4
- # @see https://github.com/thiagopradi/octopus/blob/master/lib/octopus/migration.rb
1
+ require 'active_record'
2
+ require 'set'
3
+ require 'active_support/core_ext/module/aliasing'
4
+ require 'active_support/core_ext/array/wrap'
5
5
 
6
6
  module Penthouse
7
- module Migrator
7
+ module Migration
8
+ def self.included(base)
9
+ base.alias_method_chain :announce, :penthouse
10
+ end
11
+
12
+ def announce_with_penthouse(message)
13
+ announce_without_penthouse("#{message} - #{current_tenant}")
14
+ end
8
15
 
9
- extend self
16
+ def current_tenant
17
+ "Tenant: #{Penthouse.tenant || '*** global ***'}"
18
+ end
19
+ end
20
+ end
10
21
 
11
- # Migrate to latest version
12
- # @param tenant_identifier [String, Symbol] the identifier for the tenant to switch to
13
- # @return [void]
14
- def migrate(tenant_identifier, version)
15
- Penthouse.switch(tenant_identifier) do
16
- if migrator.respond_to?(:migrate_without_octopus)
17
- migrator.migrate_without_octopus(migrator.migrations_paths, version)
18
- else
19
- migrator.migrate(migrator.migrations_paths, version)
22
+ module Penthouse
23
+ module Migrator
24
+ def self.included(base)
25
+ base.extend(ClassMethods)
26
+
27
+ base.class_eval do
28
+ class << self
29
+ alias_method_chain :migrate, :penthouse
30
+ alias_method_chain :up, :penthouse
31
+ alias_method_chain :down, :penthouse
32
+ alias_method_chain :run, :penthouse
20
33
  end
21
34
  end
22
35
  end
23
36
 
24
- # Migrate up/down to a specific version
25
- # @param tenant_identifier [String, Symbol] the identifier for the tenant to switch to
26
- # @param version [Integer] the version number to migrate up or down to
27
- # @return [void]
28
- def run(direction, tenant_identifier, version)
29
- Penthouse.switch(tenant_identifier) do
30
- if migrator.respond_to?(:run_without_octopus)
31
- migrator.run_without_octopus(direction, migrator.migrations_paths, version)
32
- else
33
- migrator.run(direction, migrator.migrations_paths, version)
37
+ module ClassMethods
38
+ def migrate_with_penthouse(migrations_paths, target_version = nil, &block)
39
+ unless Penthouse.configuration.migrate_tenants?
40
+ if defined?(:migrate_without_octopus)
41
+ return migrate_without_octopus(migrations_paths, target_version, &block)
42
+ else
43
+ return migrate_without_penthouse(migrations_paths, target_version, &block)
44
+ end
45
+ end
46
+
47
+ Penthouse.each_tenant(tenant_identifiers: tenants_to_migrate) do
48
+ if defined?(:migrate_without_octopus)
49
+ migrate_without_octopus(migrations_paths, target_version, &block)
50
+ else
51
+ migrate_without_penthouse(migrations_paths, target_version, &block)
52
+ end
34
53
  end
35
54
  end
36
- end
37
55
 
38
- # rollback latest migration `step` number of times
39
- # @param tenant_identifier [String, Symbol] the identifier for the tenant to switch to
40
- # @param step [Integer] how many migrations to rollback by
41
- # @return [void]
42
- def rollback(tenant_identifier, step = 1)
43
- Penthouse.switch(tenant_identifier) do
44
- if migrator.respond_to?(:rollback_without_octopus)
45
- migrator.rollback_without_octopus(migrator.migrations_paths, step)
46
- else
47
- migrator.rollback(migrator.migrations_paths, step)
56
+ def up_with_penthouse(migrations_paths, target_version = nil, &block)
57
+ unless Penthouse.configuration.migrate_tenants?
58
+ if defined?(:up_without_octopus)
59
+ return up_without_octopus(migrations_paths, target_version, &block)
60
+ else
61
+ return up_without_penthouse(migrations_paths, target_version, &block)
62
+ end
63
+ end
64
+
65
+ Penthouse.each_tenant(tenant_identifiers: tenants_to_migrate) do
66
+ if defined?(:up_without_octopus)
67
+ up_without_octopus(migrations_paths, target_version, &block)
68
+ else
69
+ up_without_penthouse(migrations_paths, target_version, &block)
70
+ end
71
+ end
72
+ end
73
+
74
+ def down_with_penthouse(migrations_paths, target_version = nil, &block)
75
+ unless Penthouse.configuration.migrate_tenants?
76
+ if defined?(:down_without_octopus)
77
+ return down_without_octopus(migrations_paths, target_version, &block)
78
+ else
79
+ return down_without_penthouse(migrations_paths, target_version, &block)
80
+ end
81
+ end
82
+
83
+ Penthouse.each_tenant(tenant_identifiers: tenants_to_migrate) do
84
+ if defined?(:down_without_octopus)
85
+ down_without_octopus(migrations_paths, target_version, &block)
86
+ else
87
+ down_without_penthouse(migrations_paths, target_version, &block)
88
+ end
89
+ end
90
+ end
91
+
92
+ def run_with_penthouse(direction, migrations_paths, target_version)
93
+ unless Penthouse.configuration.migrate_tenants?
94
+ if defined?(:run_without_octopus)
95
+ return run_without_octopus(direction, migrations_paths, target_version)
96
+ else
97
+ return run_without_penthouse(direction, migrations_paths, target_version)
98
+ end
99
+ end
100
+
101
+ Penthouse.each_tenant(tenant_identifiers: tenants_to_migrate) do
102
+ if defined?(:run_without_octopus)
103
+ run_without_octopus(direction, migrations_paths, target_version)
104
+ else
105
+ run_without_penthouse(direction, migrations_paths, target_version)
106
+ end
48
107
  end
49
108
  end
50
- end
51
109
 
52
- def migrator
53
- ActiveRecord::Migrator
110
+ def tenants_to_migrate
111
+ if (t = ENV["tenant"] || ENV["tenants"])
112
+ t.split(",").map(&:strip)
113
+ end
114
+ end
54
115
  end
55
116
  end
56
117
  end
118
+
119
+ ActiveRecord::Migration.send(:include, Penthouse::Migration)
120
+ ActiveRecord::Migrator.send(:include, Penthouse::Migrator)
@@ -7,6 +7,7 @@
7
7
 
8
8
  require_relative './base_tenant'
9
9
  require_relative './migratable'
10
+ require 'active_record'
10
11
 
11
12
  module Penthouse
12
13
  module Tenants
@@ -1,3 +1,3 @@
1
1
  module Penthouse
2
- VERSION = "0.6.0"
2
+ VERSION = "0.7.0"
3
3
  end
data/lib/penthouse.rb CHANGED
@@ -1,4 +1,4 @@
1
- require "penthouse/railtie" if defined?(Rails)
1
+ require "penthouse/migrator"
2
2
  require "penthouse/version"
3
3
  require "penthouse/configuration"
4
4
  require "penthouse/routers/base_router"
@@ -37,12 +37,13 @@ module Penthouse
37
37
 
38
38
  # Wraps Penthouse.switch and simply executes the block of code for each
39
39
  # tenant within Penthouse.tenant_identifiers
40
+ # @param tenant_identifiers [Array<String, Symbol>, nil] the array of tenants to loop through
40
41
  # @param default_tenant [String, Symbol] the identifier for the tenant to return to
41
42
  # @param block [Block] the code to execute
42
43
  # @yield [String, Symbol] the identifier for the tenant
43
44
  # @return [void]
44
- def each_tenant(default_tenant: tenant, runner: configuration.runner, &block)
45
- tenant_identifiers.each do |tenant_identifier|
45
+ def each_tenant(tenant_identifiers: nil, default_tenant: tenant, runner: configuration.runner, &block)
46
+ (tenant_identifiers || self.tenant_identifiers).each do |tenant_identifier|
46
47
  switch(tenant_identifier, runner: runner, &block)
47
48
  end
48
49
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: penthouse
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Townsend
@@ -170,7 +170,6 @@ files:
170
170
  - lib/penthouse/app.rb
171
171
  - lib/penthouse/configuration.rb
172
172
  - lib/penthouse/migrator.rb
173
- - lib/penthouse/railtie.rb
174
173
  - lib/penthouse/routers/base_router.rb
175
174
  - lib/penthouse/routers/subdomain_router.rb
176
175
  - lib/penthouse/runners/base_runner.rb
@@ -186,7 +185,6 @@ files:
186
185
  - lib/penthouse/tenants/octopus_shard_tenant.rb
187
186
  - lib/penthouse/tenants/schema_tenant.rb
188
187
  - lib/penthouse/version.rb
189
- - lib/tasks/penthouse.rake
190
188
  - penthouse.gemspec
191
189
  homepage: https://github.com/ryantownsend/penthouse
192
190
  licenses: []
@@ -1,11 +0,0 @@
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'
9
- end
10
- end
11
- end
@@ -1,96 +0,0 @@
1
- require 'penthouse/migrator'
2
-
3
- penthouse_namespace = namespace :penthouse do
4
-
5
- desc "Migrate all tenants to latest version"
6
- task :migrate do
7
- warn_if_tenants_empty
8
-
9
- version = ENV['VERSION'] ? ENV['VERSION'].to_i : nil
10
-
11
- tenant_identifiers.each do |tenant_identifier|
12
- begin
13
- puts("Migrating #{tenant_identifier || '***global***'} tenant")
14
- Penthouse::Migrator.migrate(tenant_identifier, version)
15
- rescue Penthouse::TenantNotFound => e
16
- puts e.message
17
- end
18
- end
19
- end
20
-
21
- desc "Rolls the migration back to the previous version (specify steps w/ STEP=n) across all tenants."
22
- task :rollback do
23
- warn_if_tenants_empty
24
-
25
- step = ENV['STEP'] ? ENV['STEP'].to_i : 1
26
-
27
- tenant_identifiers.each do |tenant_identifier|
28
- begin
29
- puts("Rolling back #{tenant_identifier || '***global***'} tenant")
30
- Penthouse::Migrator.rollback(tenant_identifier, step)
31
- rescue Penthouse::TenantNotFound => e
32
- puts e.message
33
- end
34
- end
35
- end
36
-
37
- namespace :migrate do
38
- desc 'Runs the "up" for a given migration VERSION across all tenants.'
39
- task :up do
40
- warn_if_tenants_empty
41
-
42
- version = ENV['VERSION'] ? ENV['VERSION'].to_i : nil
43
- raise 'VERSION is required' unless version
44
-
45
- tenant_identifiers.each do |tenant_identifier|
46
- begin
47
- puts("Migrating #{tenant_identifier || '***global***'} tenant up")
48
- Penthouse::Migrator.run(:up, tenant_identifier, version)
49
- rescue Penthouse::TenantNotFound => e
50
- puts e.message
51
- end
52
- end
53
- end
54
-
55
- desc 'Runs the "down" for a given migration VERSION across all tenants.'
56
- task :down do
57
- warn_if_tenants_empty
58
-
59
- version = ENV['VERSION'] ? ENV['VERSION'].to_i : nil
60
- raise 'VERSION is required' unless version
61
-
62
- tenant_identifiers.each do |tenant_identifier|
63
- begin
64
- puts("Migrating #{tenant_identifier || '***global***'} tenant down")
65
- Penthouse::Migrator.run(:down, tenant_identifier, version)
66
- rescue Penthouse::TenantNotFound => e
67
- puts e.message
68
- end
69
- end
70
- end
71
-
72
- desc 'Rolls back the tenant one migration and re-migrate up (options: STEP=x, VERSION=x).'
73
- task :redo do
74
- if ENV['VERSION']
75
- penthouse_namespace['migrate:down'].invoke
76
- penthouse_namespace['migrate:up'].invoke
77
- else
78
- penthouse_namespace['rollback'].invoke
79
- penthouse_namespace['migrate'].invoke
80
- end
81
- end
82
- end
83
-
84
- def tenant_identifiers
85
- Penthouse.tenant_identifiers
86
- end
87
-
88
- def warn_if_tenants_empty
89
- if tenant_identifiers.empty?
90
- puts <<-WARNING
91
- [WARNING] - The list of tenants to migrate appears to be empty. This could mean you've not created any.
92
- Note that your tenants currently haven't been migrated. You'll need to run `db:migrate` to rectify this.
93
- WARNING
94
- end
95
- end
96
- end