Skip to content

Commit

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

**Implemented enhancements:**

- Add config for handling tenant creation on db:migrate - #140

**Fixed bugs:**

**Closed issues:**

- Tenant exists errors on migrate task on 2.8.0 [#136](#136)
- Add Rails 6.1 to build matrix #144
  • Loading branch information
rpbaltazar authored Jan 21, 2021
2 parents d97d55b + 6ce4113 commit ac0fbb2
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ workflows:
matrix:
parameters:
ruby_version: ["ruby:2.6-buster", "ruby:2.7-buster"]
gemfile: ["gemfiles/rails_5_2.gemfile", "gemfiles/rails_6_0.gemfile"]
gemfile: ["gemfiles/rails_5_2.gemfile", "gemfiles/rails_6_0.gemfile", "gemfiles/rails_6_1.gemfile"]

rubocop:
jobs:
Expand Down
18 changes: 15 additions & 3 deletions Appraisals
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,26 @@ appraise 'rails-6-0' do
end
end

appraise 'rails-6-1' do
gem 'rails', '~> 6.1.0'
platforms :ruby do
gem 'sqlite3', '~> 1.4'
end
platforms :jruby do
gem 'activerecord-jdbc-adapter', '~> 61.0'
gem 'activerecord-jdbcpostgresql-adapter', '~> 61.0'
gem 'activerecord-jdbcmysql-adapter', '~> 61.0'
end
end

appraise 'rails-master' do
gem 'rails', git: 'https://github.com/rails/rails.git'
platforms :ruby do
gem 'sqlite3', '~> 1.4'
end
platforms :jruby do
gem 'activerecord-jdbc-adapter', '~> 52.0'
gem 'activerecord-jdbcpostgresql-adapter', '~> 52.0'
gem 'activerecord-jdbcmysql-adapter', '~> 52.0'
gem 'activerecord-jdbc-adapter', '~> 61.0'
gem 'activerecord-jdbcpostgresql-adapter', '~> 61.0'
gem 'activerecord-jdbcmysql-adapter', '~> 61.0'
end
end
23 changes: 23 additions & 0 deletions gemfiles/rails_6_1.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# This file was generated by Appraisal

source "http://rubygems.org"

gem "rails", "~> 6.1.0"
gem "rubocop"

group :local do
gem "guard-rspec", "~> 4.2"
gem "pry"
end

platforms :ruby do
gem "sqlite3", "~> 1.4"
end

platforms :jruby do
gem "activerecord-jdbc-adapter", "~> 61.0"
gem "activerecord-jdbcpostgresql-adapter", "~> 61.0"
gem "activerecord-jdbcmysql-adapter", "~> 61.0"
end

gemspec path: "../"
17 changes: 16 additions & 1 deletion lib/apartment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class << self

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

attr_accessor(*ACCESSOR_METHODS)
Expand Down Expand Up @@ -72,6 +72,21 @@ def db_migrate_tenants
@db_migrate_tenants = true
end

# How to handle tenant missing on db:migrate
# defaults to :rescue_exception
# available options: rescue_exception, raise_exception, create_tenant
def db_migrate_tenant_missing_strategy
valid = %i[rescue_exception raise_exception create_tenant]
value = @db_migrate_tenant_missing_strategy || :rescue_exception

return value if valid.include?(value)

key_name = 'config.db_migrate_tenant_missing_strategy'
opt_names = valid.join(', ')

raise ApartmentError, "Option #{value} not valid for `#{key_name}`. Use one of #{opt_names}"
end

# Default to empty array
def excluded_models
@excluded_models || []
Expand Down
12 changes: 12 additions & 0 deletions lib/apartment/tasks/task_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,17 @@ def self.create_tenant(tenant_name)
rescue Apartment::TenantExists => e
puts "Tried to create already existing tenant: #{e}"
end

def self.migrate_tenant(tenant_name)
strategy = Apartment.db_migrate_tenant_missing_strategy
create_tenant(tenant_name) if strategy == :create_tenant

puts("Migrating #{tenant_name} tenant")
Apartment::Migrator.migrate tenant_name
rescue Apartment::TenantNotFound => e
raise e if strategy == :raise_exception

puts e.message
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.8.1'
VERSION = '2.9.0'
end
8 changes: 1 addition & 7 deletions lib/tasks/apartment.rake
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,7 @@ apartment_namespace = namespace :apartment do
task :migrate do
Apartment::TaskHelper.warn_if_tenants_empty
Apartment::TaskHelper.each_tenant do |tenant|
begin
Apartment::TaskHelper.create_tenant(tenant)
puts("Migrating #{tenant} tenant")
Apartment::Migrator.migrate tenant
rescue Apartment::TenantNotFound => e
puts e.message
end
Apartment::TaskHelper.migrate_tenant(tenant)
end
end

Expand Down

0 comments on commit ac0fbb2

Please sign in to comment.