Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into feature/multiple_…
Browse files Browse the repository at this point in the history
…schemas
  • Loading branch information
ryanswood committed Oct 20, 2020
2 parents 99cb83e + e3cc79b commit 390bede
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 22 deletions.
11 changes: 11 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# 2.2.1
* June 19, 2019

## Added
- #566: IGNORE_EMPTY_TENANTS environment variable to ignore empty tenants
warning. [Pysis868]

## Fixed
- #586: Ignore `CREATE SCHEMA public` statement in pg dump [artemave]
- #549: Fix Postgres schema creation with dump SQL [ancorcruz]

# 2.2.0
* April 14, 2018

Expand Down
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,31 @@ end

See [apartment-sidekiq](https://github.com/influitive/apartment-sidekiq) or [apartment-activejob](https://github.com/influitive/apartment-activejob).

## Callbacks

You can execute callbacks when switching between tenants or creating a new one, Apartment provides the following callbacks:

- before_create
- after_create
- before_switch
- after_switch

You can register a callback using [ActiveSupport::Callbacks](https://api.rubyonrails.org/classes/ActiveSupport/Callbacks.html) the following way:

```ruby
require 'apartment/adapters/abstract_adapter'

module Apartment
module Adapters
class AbstractAdapter
set_callback :switch, :before do |object|
...
end
end
end
end
```

## Contributing

* In both `spec/dummy/config` and `spec/config`, you will see `database.yml.sample` files
Expand Down
38 changes: 19 additions & 19 deletions lib/apartment/adapters/abstract_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,25 @@ def seed_data
end
alias_method :seed, :seed_data

# Prepend the environment if configured and the environment isn't already there
#
# @param {String} tenant Database name
# @return {String} tenant name with Rails environment *optionally* prepended
#
def environmentify(tenant)
unless tenant.include?(Rails.env)
if Apartment.prepend_environment
"#{Rails.env}_#{tenant}"
elsif Apartment.append_environment
"#{tenant}_#{Rails.env}"
else
tenant
end
else
tenant
end
end

protected

def process_excluded_model(excluded_model)
Expand Down Expand Up @@ -163,25 +182,6 @@ def connect_to_new(tenant)
raise_connect_error!(tenant, exception)
end

# Prepend the environment if configured and the environment isn't already there
#
# @param {String} tenant Database name
# @return {String} tenant name with Rails environment *optionally* prepended
#
def environmentify(tenant)
unless tenant.include?(Rails.env)
if Apartment.prepend_environment
"#{Rails.env}_#{tenant}"
elsif Apartment.append_environment
"#{tenant}_#{Rails.env}"
else
tenant
end
else
tenant
end
end

# Import the database schema
#
def import_database_schema
Expand Down
3 changes: 3 additions & 0 deletions lib/apartment/adapters/postgresql_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ class PostgresqlSchemaFromSqlAdapter < PostgresqlSchemaAdapter
/SET lock_timeout/i, # new in postgresql 9.3
/SET row_security/i, # new in postgresql 9.5
/SET idle_in_transaction_session_timeout/i, # new in postgresql 9.6
/CREATE SCHEMA public/i,
/COMMENT ON SCHEMA public/i,

]

def import_database_schema
Expand Down
2 changes: 1 addition & 1 deletion lib/apartment/tenant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Tenant
extend self
extend Forwardable

def_delegators :adapter, :create, :drop, :switch, :switch!, :current, :each, :reset, :set_callback, :seed, :current_tenant, :default_tenant
def_delegators :adapter, :create, :drop, :switch, :switch!, :current, :each, :reset, :set_callback, :seed, :current_tenant, :default_tenant, :environmentify

attr_writer :config

Expand Down
2 changes: 1 addition & 1 deletion lib/apartment/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Apartment
VERSION = "2.2.0"
VERSION = "2.2.1"
end
2 changes: 1 addition & 1 deletion lib/tasks/apartment.rake
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ apartment_namespace = namespace :apartment do
end

def warn_if_tenants_empty
if tenants.empty?
if tenants.empty? && ENV['IGNORE_EMPTY_TENANTS'] != "true"
puts <<-WARNING
[WARNING] - The list of tenants to migrate appears to be empty. This could mean a few things:
Expand Down

0 comments on commit 390bede

Please sign in to comment.