diff --git a/lib/apartment/tasks/task_helper.rb b/lib/apartment/tasks/task_helper.rb index 84257b9a..3940e8cd 100644 --- a/lib/apartment/tasks/task_helper.rb +++ b/lib/apartment/tasks/task_helper.rb @@ -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 diff --git a/lib/tasks/apartment.rake b/lib/tasks/apartment.rake index f4583074..1ea98fed 100644 --- a/lib/tasks/apartment.rake +++ b/lib/tasks/apartment.rake @@ -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 @@ -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 @@ -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