Showing posts with label n00b. Show all posts
Showing posts with label n00b. Show all posts

Monday, January 18, 2016

Migrating data in Rails using Rails migrations

Put this one in the “should’ve been obvious” column…

For an embarassingly long while, when I had a feature ready for deploy that required production data be modified for a whole bunch of records – for example, downcasing all name attributes – I would write a one-off rake task to update those records, then delete that task.

Not only is this bad code since it doesn’t keep a record of what happened, it’s also tedious because it’s an extra step that must be taken by whoever deploys the code.

Luckily there’s a better way - just put the updating login in a migration!

Example:

class DowncaseName < ActiveRecord::Migration
  def up
    User.where.not(name: nil).find_each do |user|
       user.update!(name: user.name.downcase)
    end
  end
end

This example isn’t great because 1. there’s almost certainly a way to do this with pure SQL without instantiating every user record, and 2. it’s not reversible. But since I’m lazy and since it illustrates the point of the post, I’m leaving it.

Monday, November 9, 2015

Intro to SSH for relative newbies

I have an app running on an Amazon EC2 instance, and it requires an SSH key to access it. Previously, I had to access it by navigating to the folder containing my private key pem file, then running ssh -p 2222 -i private_key.pem -A ubuntu@www.example.com.

Let’s break this down a bit.
  • -p 2222 - the port that my server runs on
  • -i private_key.pem - use an Identity File (SSH key) and specify the name
  • -A - use ForwardAgent to allow my public keys to pass through to the AWS server (important if I want to be able to access Github or some other service while SSH-ed into the server)
  • ubuntu@www.example.com - username (ubuntu) and server HostName (www.example.com)
This is all fine and dandy, but it’s a pain to remember. So, my next step to simplify this was setting up a terminal alias in ~/.bash_aliases

alias my-server-ssh="cd ~ && ssh -p 2222 -i private_key.pem -A ubuntu@www.example.com"

This lets me simply run my-server-ssh from whatever directory I’m in on the terminal and automatically login to my server. However, once I have a couple more servers to juggle, it starts to get troublesome keeping track of these things. It’s also problematic if I want to use a tool like Capistrano for deployment.

Here’s where setting up the ~/.ssh/config file comes in handy. I opened this file in my text editor, and entered this info:

Host my-server
    HostName www.example.com
    Port 2222
    User ubuntu
    IdentityFile "~/.ssh/private_key.pem"
    ForwardAgent yes

Once I save, I can now use my computer’s built in SSH manager to access my server by running the command ssh my-server. Then, in a tool like Capistrano, I can plug this line into my config file:

server 'my-server', roles: %w{app web}

It will read my ~/.ssh/config file, find the configuration for host my-server and SSH in.



Big ups to Nerderati for the helpful explanation on SSH config.

Monday, October 19, 2015

Device Emulators all up in your browser

File this one under the "n00b" tag.

For checking responsiveness on mobile, Chrome (and probably Firefox) has some sweet device emulators.

Go to Developer Tools, then click the little phone icon in the far left of the Developer Tools nav
and it will let you select whatever device to see how the current page looks.

Wow. Much responsive. Very browser.