Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Resolves #70] rake tasks define methods on main #75

Merged
merged 3 commits into from
Jun 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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_schema]
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
53 changes: 13 additions & 40 deletions lib/tasks/apartment.rake
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# frozen_string_literal: true

require 'apartment/migrator'
require 'apartment/tasks/task_helper'
require 'parallel'

apartment_namespace = namespace :apartment do
desc 'Create all tenants'
task :create do
tenants.each do |tenant|
Apartment::TaskHelper.tenants.each do |tenant|
begin
puts("Creating #{tenant} tenant")
Apartment::Tenant.create(tenant)
Expand All @@ -18,7 +19,7 @@ apartment_namespace = namespace :apartment do

desc 'Drop all tenants'
task :drop do
tenants.each do |tenant|
Apartment::TaskHelper.tenants.each do |tenant|
begin
puts("Dropping #{tenant} tenant")
Apartment::Tenant.drop(tenant)
Expand All @@ -30,8 +31,8 @@ apartment_namespace = namespace :apartment do

desc 'Migrate all tenants'
task :migrate do
warn_if_tenants_empty
each_tenant do |tenant|
Apartment::TaskHelper.warn_if_tenants_empty
Apartment::TaskHelper.each_tenant do |tenant|
begin
puts("Migrating #{tenant} tenant")
Apartment::Migrator.migrate tenant
Expand All @@ -43,9 +44,9 @@ apartment_namespace = namespace :apartment do

desc 'Seed all tenants'
task :seed do
warn_if_tenants_empty
Apartment::TaskHelper.warn_if_tenants_empty

each_tenant do |tenant|
Apartment::TaskHelper.each_tenant do |tenant|
begin
puts("Seeding #{tenant} tenant")
Apartment::Tenant.switch(tenant) do
Expand All @@ -59,11 +60,11 @@ apartment_namespace = namespace :apartment do

desc 'Rolls the migration back to the previous version (specify steps w/ STEP=n) across all tenants.'
task :rollback do
warn_if_tenants_empty
Apartment::TaskHelper.warn_if_tenants_empty

step = ENV['STEP'] ? ENV['STEP'].to_i : 1

each_tenant do |tenant|
Apartment::TaskHelper.each_tenant do |tenant|
begin
puts("Rolling back #{tenant} tenant")
Apartment::Migrator.rollback tenant, step
Expand All @@ -76,12 +77,12 @@ apartment_namespace = namespace :apartment do
namespace :migrate do
desc 'Runs the "up" for a given migration VERSION across all tenants.'
task :up do
warn_if_tenants_empty
Apartment::TaskHelper.warn_if_tenants_empty

version = ENV['VERSION'] ? ENV['VERSION'].to_i : nil
raise 'VERSION is required' unless version

each_tenant do |tenant|
Apartment::TaskHelper.each_tenant do |tenant|
begin
puts("Migrating #{tenant} tenant up")
Apartment::Migrator.run :up, tenant, version
Expand All @@ -93,12 +94,12 @@ apartment_namespace = namespace :apartment do

desc 'Runs the "down" for a given migration VERSION across all tenants.'
task :down do
warn_if_tenants_empty
Apartment::TaskHelper.warn_if_tenants_empty

version = ENV['VERSION'] ? ENV['VERSION'].to_i : nil
raise 'VERSION is required' unless version

each_tenant do |tenant|
Apartment::TaskHelper.each_tenant do |tenant|
begin
puts("Migrating #{tenant} tenant down")
Apartment::Migrator.run :down, tenant, version
Expand All @@ -119,32 +120,4 @@ apartment_namespace = namespace :apartment do
end
end
end

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

def tenants_without_default
tenants - [Apartment.default_schema]
end

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

def warn_if_tenants_empty
return unless 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:

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
end
end