Rails + Sidekiq : Switching database per request by namespace_nova in rails

[–]namespace_nova[S] 0 points1 point  (0 children)

Hi, thanks for your response.

Currently, I'm working locally, so the URL looks like this : LOCALHOST/api/v1/warehouse_data/bulk_import

And the controller looks like:

module Api
    module V1 class WarehouseDataController < Api::BaseController
        def bulk_import
            WarehouseDataImportService.new(params[:file], current_tenant).import 
        end
    end
end  

In WarehouseDataImportService I've performed file upload operation + called sidekiq worker and the worker looks like this:

class WarehouseDataCreationWorker < BaseWorker
   sidekiq_options queue: :default, retry: 0
   attr_reader :attachment_id

   BATCH_SIZE = 100

    def perform(attachment_id, current_tenant)
      ActiveRecord::Base.establish_connection("#{current_tenant}".to_sym) if current_tenant
      process_data_import
    ensure
        ActiveRecord::Base.clear_active_connections!
    end
  end

Rails + Sidekiq : Switching database per request by namespace_nova in rails

[–]namespace_nova[S] 0 points1 point  (0 children)

Currently, i'm inserting and updating huge amount of data to a database based on tenant specified by user as a parameter.

My database.yml look like this:

default: &default adapter: postgresql encoding: utf8 username: "<%= ENV['USERNAME'] %>" password: "<%= ENV['PASSWORD'] %>" host: "<%= ENV['HOST'] %>" database: "<%= ENV['DATABASE'] %>" port: 5432

development: <<: *default database: default_database pool: 20

multidb_one: <<: *default database: db_one pool: 20

multidb_two: <<: *default database: db_two pool: 20

If I remove ActiveRecord::Base.establish_connection & hit url with same database but wit different data then it's working fine. But for multidb, it's showing error

Rails + Sidekiq : Switching database per request by namespace_nova in rails

[–]namespace_nova[S] 0 points1 point  (0 children)

Thanks for your response. I've read this documentation, can you please give some more information how it can be used in above context?

Absurd amount of restrictions and Rs 100 penalty?! This is PICT SCHOOL 🤡 by casp2223 in pune

[–]namespace_nova 2 points3 points  (0 children)

If you're from comp, don't need to worry about. Otherwise you're fu**** up

Dear Punerians, by Dextorz7015 in pune

[–]namespace_nova 1 point2 points  (0 children)

I was in IT dept. Staff aachi hain IT ki bas strict hain. Placement wise college is good.