Skip to content

Commit

Permalink
Merge pull request #77 from rails-on-services/development
Browse files Browse the repository at this point in the history
Prepare Release - 2.7.0

# Enhancements

- [Resolves #70] Rake tasks define methods on main - #75
- Add database and schema to active record log. Configurable, defaults to false to keep current behavior - #55

# Bugfixes

- [Fixes #61] Fix database create in mysql - #76

# Chores

- Remove depracated tld_length config option: tld_length was removed in influitive#309, this configuration option doesn't have any effect now. - #72
- Using [diffend.io proxy](https://diffend.io) to safely check required gems
- Added [story branch](https://github.com/story-branch/story_branch) to the configuration
- Using travis-ci to run rubocop as well, replacing github actions: github actions do not work in fork's PRs
  • Loading branch information
rpbaltazar authored Jun 26, 2020
2 parents 5a84b77 + 4c44f0c commit c51f826
Show file tree
Hide file tree
Showing 22 changed files with 198 additions and 98 deletions.
22 changes: 0 additions & 22 deletions .github/workflows/.rubocop-linter.yml

This file was deleted.

1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ inherit_gem:
AllCops:
Exclude:
- 'gemfiles/**/*.gemfile'
- 'gemfiles/vendor/**/*'

Style/WordArray:
Exclude:
Expand Down
1 change: 1 addition & 0 deletions .story_branch.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
---
tracker: github
issue_placement: beginning
project_id:
- rails-on-services/apartment
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,15 @@ before_install:
- sudo /etc/init.d/mysql stop
- sudo /etc/init.d/postgresql stop
- docker-compose up -d

env:
RUBY_GC_MALLOC_LIMIT: 90000000
RUBY_GC_HEAP_FREE_SLOTS: 200000
jobs:
include:
- name: Rubocop Lint
script: gem install perx-rubocop && rubocop

allow_failures:
- rvm: ruby-head
- rvm: jruby-head
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

source 'http://rubygems.org'
source 'https://my.diffend.io/protect/gems'

gemspec

Expand Down
57 changes: 57 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,60 @@
# 2.6.1

## Enhancements
- N/a

## Bugfixes
- [Resolves influitive#607] Avoid early connection
- <https://github.com/rails-on-services/apartment/pull/39>
- <https://github.com/rails-on-services/apartment/pull/53>
- <https://github.com/rails-on-services/apartment/pull/51>
- [Resolves #52] Rake db:setup tries to seed non existent tenant - <https://github.com/rails-on-services/apartment/pull/54>
- [Resolves #56] DB rollback uses second last migration - <https://github.com/rails-on-services/apartment/pull/57>

## Chores
- N/a

# 2.6.0

## Enhancements
- [Resolves #26] Support configuration for skip checking of schema existence before switching
- [Resolves #41] Add tenant info to console boot

## Bugfixes
- [Resolves #37] Custom Console deprecation warning
- [Resolves #42] After switch callback not working with nil argument

## Chores
- Updated github actions configuration to run on PRs as well

# 2.5.0

## Enhancements
- [Resolves #6] Adds support for rails 6.1
- [Resolves #27] Adds support to not rely on set search path, but instead prepends the schema name to the table name when using postgresql with schemas.
- [Resolves #35] Cache keys are now tenant dependent

## Bugfixes
- [Resolves #27] Manually switching connection between read and write forgets the schema

## Chores
- [Resolves #31] Add latest ruby versions to test matrix

# 2.4.0

## Enhancements
- [Resolves #14] Add console info about tenants and fast switches #17
- Skip init if we're running webpacker:compile #18

## Bugfixes
- Don't crash when no database connection is present #16
- Rescuing ActiveRecord::NoDatabaseError when dropping tenants #19

## Chores
- Rakefile should use mysql port from configuration #5
- [Resolves #9] Cleanup rubocop todo #8
- Cleanup travis matrix #23

# 2.3.0
* January 3, 2020

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -368,12 +368,12 @@ Enable this option with:
config.use_sql = true
```

### Providing a Different default_schema
### Providing a Different default_tenant

By default, ActiveRecord will use `"$user", public` as the default `schema_search_path`. This can be modified if you wish to use a different default schema be setting:

```ruby
config.default_schema = "some_other_schema"
config.default_tenant = "some_other_schema"
```

With that set, all excluded models will use this schema as the table name prefix instead of `public` and `reset` on `Apartment::Tenant` will return to this schema as well.
Expand Down Expand Up @@ -446,7 +446,7 @@ schema_search_path: "public,shared_extensions"
...
```

This would be for a config with `default_schema` set to `public` and `persistent_schemas` set to `['shared_extensions']`. **Note**: This only works on Heroku with [Rails 4.1+](https://devcenter.heroku.com/changelog-items/426). For apps that use older Rails versions hosted on Heroku, the only way to properly setup is to start with a fresh PostgreSQL instance:
This would be for a config with `default_tenant` set to `public` and `persistent_schemas` set to `['shared_extensions']`. **Note**: This only works on Heroku with [Rails 4.1+](https://devcenter.heroku.com/changelog-items/426). For apps that use older Rails versions hosted on Heroku, the only way to properly setup is to start with a fresh PostgreSQL instance:

1. Append `?schema_search_path=public,hstore` to your `DATABASE_URL` environment variable, by this you don't have to revise the `database.yml` file (which is impossible since Heroku regenerates a completely different and immutable `database.yml` of its own on each deploy)
2. Run `heroku pg:psql` from your command line
Expand Down
1 change: 0 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,5 @@

Quick TODOs

1. `default_tenant` should be up to the adapter, not the Apartment class, deprecate `default_schema`
2. deprecation.rb rescues everything, we have a hard dependency on ActiveSupport so this is unnecessary
3.
19 changes: 9 additions & 10 deletions lib/apartment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

# require_relative 'apartment/arel/visitors/postgresql'

require_relative 'apartment/active_record/log_subscriber'
require_relative 'apartment/active_record/connection_handling' if ActiveRecord.version.release >= Gem::Version.new('6.0')

if ActiveRecord.version.release >= Gem::Version.new('6.1')
Expand All @@ -19,12 +20,12 @@ module Apartment
class << self
extend Forwardable

ACCESSOR_METHODS = %i[use_schemas use_sql seed_after_create prepend_environment
append_environment with_multi_server_setup tenant_presence_check].freeze
ACCESSOR_METHODS = %i[use_schemas use_sql seed_after_create prepend_environment default_tenant
append_environment with_multi_server_setup tenant_presence_check active_record_log].freeze

WRITER_METHODS = %i[tenant_names database_schema_file excluded_models
default_schema persistent_schemas connection_class
tld_length db_migrate_tenants seed_data_file
persistent_schemas connection_class
db_migrate_tenants seed_data_file
parallel_migration_threads pg_excluded_names].freeze

attr_accessor(*ACCESSOR_METHODS)
Expand Down Expand Up @@ -53,6 +54,10 @@ def tenants_with_config
extract_tenant_config
end

def tld_length=(_)
Apartment::Deprecation.warn('`config.tld_length` have no effect because it was removed in https://github.com/influitive/apartment/pull/309')
end

def db_config_for(tenant)
(tenants_with_config[tenant] || connection_config)
end
Expand All @@ -70,15 +75,9 @@ def excluded_models
@excluded_models || []
end

def default_schema
@default_schema || 'public' # TODO: 'public' is postgres specific
end

def parallel_migration_threads
@parallel_migration_threads || 0
end
alias default_tenant default_schema
alias default_tenant= default_schema=

def persistent_schemas
@persistent_schemas || []
Expand Down
41 changes: 41 additions & 0 deletions lib/apartment/active_record/log_subscriber.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true

module ActiveRecord
class LogSubscriber
def apartment_log
return unless Apartment.active_record_log

database = color("[#{Apartment.connection.current_database}] ", ActiveSupport::LogSubscriber::MAGENTA, true)
schema = nil
schema = color("[#{Apartment.connection.schema_search_path.tr('"', '')}] ", ActiveSupport::LogSubscriber::YELLOW, true) unless Apartment.connection.schema_search_path.nil?
"#{database}#{schema}"
end

def payload_binds(binds, type_casted_binds)
return unless (binds || []).empty?

casted_params = type_casted_binds(type_casted_binds)
binds = ' ' + binds.zip(casted_params).map { |attr, value| render_bind(attr, value) }.inspect
binds
end

def sql(event)
self.class.runtime += event.duration
return unless logger.debug?

payload = event.payload

return if IGNORE_PAYLOAD_NAMES.include?(payload[:name])

name = "#{payload[:name]} (#{event.duration.round(1)}ms)"
name = "CACHE #{name}" if payload[:cached]
sql = payload[:sql]
binds = payload_binds(payload[:binds], payload[:type_casted_binds])

name = colorize_payload_name(name, payload[:name])
sql = color(sql, sql_color(sql), true) if colorize_logging

debug " #{apartment_log}#{name} #{sql}#{binds}"
end
end
end
1 change: 0 additions & 1 deletion lib/apartment/adapters/abstract_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ def current
def default_tenant
@default_tenant || Apartment.default_tenant
end
alias default_schema default_tenant # TODO: deprecate default_schema

# Drop the tenant
#
Expand Down
2 changes: 2 additions & 0 deletions lib/apartment/adapters/mysql2_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ def initialize(config)
# Reset current tenant to the default_tenant
#
def reset
return unless default_tenant

Apartment.connection.execute "use `#{default_tenant}`"
end

Expand Down
4 changes: 4 additions & 0 deletions lib/apartment/adapters/postgresql_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ def initialize(config)
reset
end

def default_tenant
@default_tenant = Apartment.default_tenant || 'public'
end

# Reset schema search path to the default schema_search_path
#
# @return {String} default schema search path
Expand Down
1 change: 1 addition & 0 deletions lib/apartment/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Railtie < Rails::Railtie
config.prepend_environment = false
config.append_environment = false
config.tenant_presence_check = true
config.active_record_log = false
end

ActiveRecord::Migrator.migrations_paths = Rails.application.paths['db/migrate'].to_a
Expand Down
35 changes: 35 additions & 0 deletions lib/apartment/tasks/task_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

module Apartment
module TaskHelper
def self.each_tenant(&block)
Parallel.each(tenants_without_default, in_threads: Apartment.parallel_migration_threads) do |tenant|
block.call(tenant)
end
end

def self.tenants_without_default
tenants - [Apartment.default_tenant]
end

def self.tenants
ENV['DB'] ? ENV['DB'].split(',').map(&:strip) : Apartment.tenant_names || []
end

def self.warn_if_tenants_empty
return unless tenants.empty? && ENV['IGNORE_EMPTY_TENANTS'] != 'true'

# rubocop:disable Rails/Output
puts <<-WARNING
[WARNING] - The list of tenants to migrate appears to be empty. This could mean a few things:
1. You may not have created any, in which case you can ignore this message
2. You've run `apartment:migrate` directly without loading the Rails environment
* `apartment:migrate` is now deprecated. Tenants will automatically be migrated with `db:migrate`
Note that your tenants currently haven't been migrated. You'll need to run `db:migrate` to rectify this.
WARNING
# rubocop:enable Rails/Output
end
end
end
2 changes: 1 addition & 1 deletion lib/apartment/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Apartment
VERSION = '2.6.1'
VERSION = '2.7.0'
end
5 changes: 5 additions & 0 deletions lib/generators/apartment/install/templates/apartment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@
# the new tenant
#
# config.pg_excluded_names = ["uuid_generate_v4"]

# Specifies whether the database and schema (when using PostgreSQL schemas) will prepend in ActiveRecord log.
# Uncomment the line below if you want to enable this behavior.
#
# config.active_record_log = true
end

# Setup a custom Tenant switching middleware. The Proc should return the name of the Tenant that
Expand Down
Loading

0 comments on commit c51f826

Please sign in to comment.