Rake task to migrate tenants on their schema's on multiple darabases

33 views Asked by At

i'm iterating on my tenants list in apartment and running the migration under each tenant i discovered that the migration task running only one time in my case just for the first tenant


namespace :db do
  desc 'Run migrations or rollback for all tenants in a multi-server setup'

  task :multi_server, [:task_to_run] => :environment do |_, args|
    task_to_run = args[:task_to_run]

    unless task_to_run.present?
      puts 'Please specify a task to run (e.g., db:migrate, db:rollback, etc.)'
      exit(1)
    end

    Apartment::tenants_with_config.each do |tenant_name, db_config|
      if db_config.present?
        if db_config['database'] == 'primary'
          puts "Running '#{task_to_run}' for the primary database directly."
          # Rake::Task[task_to_run].reenable # Re-enable the task
          Rake::Task[task_to_run].invoke # Invoke the task again
          puts "#{task_to_run.capitalize} completed for the primary database."
        else
          begin
            # Establish the connection to the tenant's database using Apartment's configuration
            ActiveRecord::Base.establish_connection(db_config)

            # Log that the connection is successfully established
            puts '======================================='
            puts ActiveRecord::Base.connection.current_database
            # Log tenant-specific information
            puts '======================================='
            # Switch to the tenant's schema (assuming you're using PostgreSQL schemas)
            # Apartment::Tenant.switch(tenant_name) do
            #   puts "Switched to schema for tenant: #{tenant_name}"
            # Run the specified task for this tenant
            puts "Running '#{task_to_run} ' for tenant: #{tenant_name}"
            # output = %x{RAILS_ENV=develzopment rails db:migrate 2>&1} # Run and capture output

            # Load Rake tasks dynamically for the current tenant

           puts  Rake::Task[task_to_run].invoke
           puts Rake::Task[task_to_run].reenable


            #system('RAILS_ENV=development bundle exec rake db:migrate --trace')
            puts "#{task_to_run.capitalize} completed for tenant: #{tenant_name}"
          rescue StandardError => e
            puts "Error establishing connection for tenant: #{tenant_name}"
            puts "Error message: #{e.message}"
          ensure
            # Reset the connection to the primary database after running the task
            ActiveRecord::Base.establish_connection(:primary)
          end
        end
      else
        puts "No database configuration found for tenant: #{tenant_name}"
      end
    end
  end
end

console

the migration task running only one time in my case just for the first tenant while i was expecting that it will run for each tenant on their database

0

There are 0 answers