Capture state of Rails test database

Ever want to explore the state of the database during a Rails test run? I was recently writing a test to create a seam in some complex legacy code and needed this. Dropping this code in does the job in MySQL:

ActiveRecord::Base.connection.tables.each do |table_name|
ActiveRecord::Base.connection.execute("select * from #{table_name} into outfile '/tmp/datadump-#{table_name}.outfile'")
end

(or see this gist)

That creates a series of tab-separated files in /tmp/. Then LOAD DATA INFILE queries can bring the data back in, or in my case I used Sequel Pro’s import command configured for tab-separated input.

Get PHP APC cache uptime in seconds

I just posted about getting Apache uptime. For what I’m seeking, I need to compare the system uptime, Apache’s uptime, and the uptime of the PHP APC opcode cache.

Here’s code for a PHP page that will simply output the number of seconds the APC cache has been up.

<?php

$limited = true;
$info = apc_cache_info('', $limited);
print_r(time() - $info['start_time']);

?>

Apache uptime in seconds

I’m looking at some interesting anomalies on a cPanel VPS I administer, and wanted to add Apache uptime graphing. Building on this ServerFault solution, here is a way to get the number of seconds Apache has been up. Whitespace/line breaks added for clarity.

ps -eo comm,etime |
grep httpd |
ruby -ne 'sec, min, hour, day = $_.split(/ /).last.chomp.split(/[-:]/).reverse;
          p day.to_i*24*60*60 + hour.to_i*60*60 + min.to_i*60 + sec.to_i' |
sort -nr |
head -n1


And without the whitespace, for ease of copy/pasting:

ps -eo comm,etime | grep httpd | ruby -ne 'sec, min, hour, day = $_.split(/ /).last.chomp.split(/[-:]/).reverse; p day.to_i*24*60*60 + hour.to_i*60*60 + min.to_i*60 + sec.to_i' | sort -nr | head -n1