Skip to content

Global Replace Content in RoR

I think it’s fairly typical to have to make a global replace on your database at some time or another – updating old links, old code, ect.  And today this was something I was tasked with!  It was pretty straight forward, and these two stackoverflow questions helped me do it.  To get this working you should just need to follow the commands below, while replacing the following:

  • TABLE – The database table you’re replacing content in
  • COLUMN – The column in that table you’re replacing content in
  • BAD – The old string you want to replace
  • GOOD – The new string you want to replace BAD with
1
2
3
4
5
6
7
8
9
10
rails c
 
ActiveRecord::Base.connection.execute(%q{
    update TABLE
    set COLUMN = replace(
        COLUMN,
        'BAD',
        'GOOD'
    )
})

Export table in rails console

This is just a small twist on the very nice snippet I found from this post.  I just changed it slightly, so you can get all the attributes automatically so you can easily swap out any model.  And it will also list the column names in the first row.

1
2
3
4
5
6
7
8
9
10
11
12
require 'csv'
 
file = "#{Rails.root}/public/data.csv"
 
table = User.all;0
 
CSV.open( file, 'w' ) do |writer|
  writer << table.first.attributes.map { |a,v| a }
  table.each do |s|
    writer << s.attributes.map { |a,v| v }
  end
end

simple-zoom

Just finished pushing a very simple zoom app in GitHub, I couldn’t find a nice script that would allow the image to take up the full size of it’s parent element when zoomed in, and would handle updating the image dynamically.  So I wrote one for myself.

Get the files on GitHub.

See a preview:

Add tracking tokens to WordPress

Just had to do a search and replace to add in tracking tokens site-wide to a wordpress site.  I tried some search & replace plugins, but I found them all a bit buggy, so I opened up PHP My Admin to handle it. I had a list of links so I just ran them all like:

1
UPDATE wp_posts SET post_content = REPLACE (post_content,'http://example.com/page.html','http://example.com/page.html?UTM=token');

then ran the following, because I knew a few links already had the tracking token added:

1
UPDATE wp_posts SET post_content = REPLACE (post_content,'?UTM=token?UTM=token','?UTM=token');

And, last but not least I verified that all links where updated by running:

1
SELECT post_content FROM wp_posts WHERE (CONVERT(`post_content` USING utf8) LIKE '%%http://example.com%%' AND CONVERT(`post_content` USING utf8) NOT LIKE '%%UTM%%')

So, I’m basically just checking that every post with that domain also has the token code in it somewhere.

You can be a bit more thorough by using regex, but that should do the trick if you only have a few links to update.

UPDATE 6/2016

I just needed to do a similar task, but I needed to add a tracking token to all links to a domain, so I needed a bit of regex.  This time I found a nice search and replace plugin that supports regex. And I used these commands to first add in the token:

1
2
Search pattern: ((<a href="(http://)?(www.)?example.com/(.*?))")
Replace pattern: $1?UTM=token

This will pick up all links, weather they have http or not, and weather they have www. or not.  And again I ran this kind of replace to fix any links that already had tracking added to them:

1
2
Search pattern: ?UTM=token?UTM=token
Replace pattern: ?UTM=token

Monitoring & Understanding Server Load

I have a site whose performance could stand some improvement.  I want to be able to make sure that any changes I make are actually having an impact, so I’m going to start with setting up some monitoring tools. I found a very nice and free monitoring application called Load Average. After I got that all set I did a bit of research so I can fully understand and make the most out of the numbers I see in there.

CPU LOAD

First off, I needed to get a handle on how CPU Load is measured. If you run “cat /proc/loadavg” on a Linux server you will get a string similar to this:

1
0.02 0.03 0.00 1/437 21084

The first three numbers are the average CPU Load over 1 minutes, 5 minutes, and 15 minutes.

The lower the numbers the better, and the highest it should be is equal to the number of cores you have.  With one core it would be bad if it went over 1, but if you had 8 cores then a value of 1 wouldn’t be a problem, but 8.5 would.  To find out how many cores you have you can run “nproc.”

MEMORY USAGE

Check how much memory you’re using by running “free -m.” You’ll get a readout like this:

1
2
3
4
             total       used       free     shared    buffers     cached
Mem:         11909      10785       1124          1        234       9372
-/+ buffers/cache:       1178      10731
Swap:            0          0          0

The number you want to watch out for here is the used buffer/cache, here it’s “1178.”  That’s the memory that’s being used by the applications currently running on your server.  That number should be less then the total memory + swap memory, here’s that’s “11909.”

 

References:

http://stackoverflow.com/questions/11987495/linux-proc-loadavg
http://blog.scoutapp.com/articles/2009/07/31/understanding-load-averages
http://serverfault.com/questions/67759/how-to-understand-the-memory-usage-and-load-average-in-linux-server
http://www.cyberciti.biz/faq/linux-get-number-of-cpus-core-command/