Rails | Apartment gem | Resets AR objects when tenant switched

I ran into a weird situation today. Active Record objects stored in vars are removed when I switched from one tenant to another on the fly. This will create a weird test-failing scenario and you never know why its happening.

def switch(name)
  yield(name)
end

@customers = [1,2,3]
switch('shiva') do |name|
  puts name
  puts @customers
end

# Gives output
# shiva
# 1
# 2
# 3

 

but when I do this

@roles     = Role.all
@customers = org.branches
@list      = [1, 2]

puts 'Role count' + @roles.count.to_s
puts 'Customer count' + @customers.count.to_s
puts 'list count' + @list.count.to_s

Apartment::Tenant.switch!(org.database_name)
puts '-------'
puts 'Role count' + @roles.count.to_s
puts 'Customer count' + @customers.count.to_s
puts 'list count' + @list.count.to_s

output is

Role count2
Customer count2
list count2
-------
Role count0
Customer count0
list count2

 

Reason

# apartment-2.2.0/lib/apartment/adapters/abstract_adapter.rb
#   Switch to a new tenant
#
#   @param {String} tenant name
#
def switch!(tenant = nil)
  run_callbacks :switch do
    return reset if tenant.nil?

    connect_to_new(tenant).tap do
      Apartment.connection.clear_query_cache
    end
  end
end
Advertisements

Could not install Ruby 2.3.4 in Ubuntu 17.04 using RVM

I could not install Ruby 2.3.4 which I needed because Heroku does not support latest stable release like 2.4.0.  So my Gemfile has locked the version 2.3.4. Till now I have been manually commenting out that particular line from the Gemfile and skipping the change from Git-Commits.

Hack

I found a hack

  • install ruby 2.3 from apt-get
sudo apt-get install ruby2.3 ruby2.3-dev
  • find location of the ruby installed
which ruby2.3
# => /usr/bin/ruby2.3
  • Mount the ruby to RVM
rvm mount /usr/bin/ruby2.3 -n ruby-2.3.3

rvm list                                

rvm rubies

 * ext-ruby-2.3.3 [ x86_64 ]
   ruby-1.8.7-p371 [ x86_64 ]
   ruby-1.9.3-p551 [ x86_64 ]
=> ruby-2.4.0 [ x86_64 ]

A better option

Install Ruby from archived link

rvm mount -r https://cache.ruby-lang.org/pub/ruby/2.3/ruby-2.3.3.tar.bz2

Debugging: What is binding in Ruby?

You might have thought always whenever you wrote code binding.pry in your application to debug your application.

People often say “do binding to see whats going on”. Actually you are not doing binding but prying into the binding. So, its good to say “Please pry into that binding”.

But, what is binding in the first place? Continue reading

Developer’s checklist: Production deployment

One has to be very careful for shipping new changes/features to the production server.

When feature is said to be done

Assumption: Proper development processes have been followed

  1. Smoke Test in local machine
  2. Production Database backups
    • Generate snapshots or database-dumps
    • Store the backups in safe places
    • In case of Heroku, you will be provided with backup-tools
  3. Codebase versioning in Github / Diffusion
    • Create tags with proper naming conventions followed
    • eg V 0.1.1 or V 1.0.0 pre
  4. Deployment to production
    • Deploy the changes to the server
    • Make sure you run rake db:migrate is any migration pending
  5. Do data migrations if required
    • Already existing Production data might not co-op with your new code changes
      • reasons: there might be some data-fields manipulations or
        • new cols might have been added and data need to be filled in; data to be gathered from existing fields
    • Document the migration code you wrote for data-migrations [remember I am not taking about schema migrations]
    • eg
TransactionHistory.recurring_rewards.each do |tx|
   charge = Stripe::Charge.retrieve(tx.charge_id)
   invoice = Stripe::Invoice.retrieve(charge.invoice)
   subscription_id = invoice.subscription
   metadata = tx.transaction_additional_detail.metadata
   tx.transaction_additional_detail.update metadata: metadata.deep_merge(
                                           {
                                               subscription_id: subscription_id,
                                               invoice_id: invoice.id
                                           })
end
  1. Smoke test in production

Rails : Could not find jwt-1.5.3 in any of the sources

I have been using Capistrano for a long time for this project. This time when I tried to deploy my latest changes I stumbled upon this issue. So I documented this.

 

(Backtrace restricted to imported tasks)
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing as user@mysite.com: bundle exit status: 7
bundle stdout: Could not find jwt-1.5.3 in any of the sources
bundle stderr: Regenerating ruby-2.2.1 wrappers........
Regenerating ruby-2.2.1 wrappers........
/usr/local/rvm/gems/ruby-2.2.1/gems/bundler-1.11.2/lib/bundler/vendor/thor/lib/thor/shell/basic.rb:356: warning: Insecure world writable dir /usr/local/rvm/gems/ruby-2.2.1/bin in PATH, mode 042777

SSHKit::Command::Failed: bundle exit status: 7
bundle stdout: Could not find jwt-1.5.3 in any of the sources
bundle stderr: Regenerating ruby-2.2.1 wrappers........
Regenerating ruby-2.2.1 wrappers........
/usr/local/rvm/gems/ruby-2.2.1/gems/bundler-1.11.2/lib/bundler/vendor/thor/lib/thor/shell/basic.rb:356: warning: Insecure world writable dir /usr/local/rvm/gems/ruby-2.2.1/bin in PATH, mode 042777

Tasks: TOP => deploy:updated => bundler:install
(See full trace by running task with --trace)
The deploy has failed with an error: Exception while executing as deployer@45.79.200.151: bundle exit status: 7
bundle stdout: Could not find jwt-1.5.3 in any of the sources
bundle stderr: Regenerating ruby-2.2.1 wrappers........

 

The reason is:

https://rubygems.org/gems/jwt/versions/1.5.3

jwt 1.5.3 is yanked because of some reasons. So 1.5.3 version isn’t in repository..!

How to solve

Delete your Gemfile.lock and

$ bundle install

update your remote repo and deploy again

Sources:

http://stackoverflow.com/a/36219627/3437900

Should “have”s and “not have”s in your CV : for college graduates

Disclaimer: These techniques work well for IT graduates. Have not been tested for other disciplines. Please comment if they work for them as well.

If you have just completed you college or about to, you are at the right place. Here are some tips that will help while preparing you CV.

Definition of CV: a brief account of a person’s education, qualifications, and previous occupations, typically sent with a job application.

It is actually your summary, so make sure your CV describes you completely. Continue reading