Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[70] moved methods to module
Browse files Browse the repository at this point in the history
rpbaltazar committed Jun 24, 2020

Verified

This commit was signed with the committer’s verified signature.
pradyunsg Pradyun Gedam
1 parent 9531971 commit 004806b
Showing 1 changed file with 13 additions and 40 deletions.
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)
@@ -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)
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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

0 comments on commit 004806b

Please sign in to comment.