Skip to content

Commit

Permalink
[#86] ensure listed tenants are created before trying to migrate them…
Browse files Browse the repository at this point in the history
… or seed them
  • Loading branch information
rpbaltazar committed Dec 16, 2020
1 parent 6db829e commit 2087f7a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
8 changes: 8 additions & 0 deletions lib/apartment/tasks/task_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,13 @@ def self.warn_if_tenants_empty
Note that your tenants currently haven't been migrated. You'll need to run `db:migrate` to rectify this.
WARNING
end

def self.create_tenant(tenant_name)
begin
Apartment::Tenant.create(tenant_name)
rescue Apartment::TenantExists => e
puts 'Tried to create already existing tenant'
end
end
end
end
11 changes: 4 additions & 7 deletions lib/tasks/apartment.rake
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@ apartment_namespace = namespace :apartment do
Apartment::TaskHelper.warn_if_tenants_empty

Apartment::TaskHelper.tenants.each do |tenant|
begin
puts("Creating #{tenant} tenant")
Apartment::Tenant.create(tenant)
rescue Apartment::TenantExists => e
puts e.message
end
Apartment::TaskHelper.create_tenant(tenant)
end
end

Expand All @@ -36,6 +31,7 @@ apartment_namespace = namespace :apartment 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
Expand All @@ -45,11 +41,12 @@ apartment_namespace = namespace :apartment do
end

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

Apartment::TaskHelper.each_tenant do |tenant|
begin
Apartment::TaskHelper.create_tenant(tenant)
puts("Seeding #{tenant} tenant")
Apartment::Tenant.switch(tenant) do
Apartment::Tenant.seed
Expand Down

0 comments on commit 2087f7a

Please sign in to comment.