Skip to main content

Posts

Showing posts from December, 2007

How to access ActiveRecord & Migrations outside of Ruby on Rails

Rails migrations are an extremely handy way of capturing changes to your database and rolling them back if they are incorrect.

Manipulating or querying a database is a pretty useful tool and ActiveRecord turns your tables into objects so I looked into how to access these features outside of a Rails environment.

I wanted to be able to either specify the database connection details directly in the script if I was running it outside of a Rails application, or to be able to bind into the Rails application's database.yml database configuration file.

To run it as a standalone script with the database details described inside the script run:ruby play_with_the_database.rbTo run it within the context of a Rails application place the script inside a new directory called db/script and run:ruby play_with_the_database.rb developmentIf you want to run the above script against the production database run:ruby play_with_the_database.rb productionI'll show you the ruby code that I came up with an…

htop - a great alternative to top for Ubuntu Linux

I colleague recommended that I install htop the other day. It's an alternative to top which offers colour (ooh), a tree sort view (aah), and the ability to kill off processes within htop itself (very cool).

To install it, simply run:sudo aptitude install htopAnd to run it, simply type:htopThis is what it looks like running (with the tree view selected):



And here are some of the help options:



Technorati Tags: , , , , ,

Upgrading to Ubuntu 7.10 (Gutsy Gibbon) causes snmpd errors

When Gutsy Gibbon was released back in October I was quick to upgrade my home Ubuntu server to this new release. The upgrade went smoothly apart from one failure - a strange error message when upgrading the SNMP support:
Starting network management services:invoke-rc.d: initscript snmpd, action "start" failed.
dpkg: error processing snmpd (--configure):
subprocess post-installation script returned error exit status 1I didn't understand the error message and googling for it at the time brought up no solutions. I checked and found that snmpd was running successfully and all appeared fine. I recently applied some more updates and received the same error message so I investigated it again.

This time I found the reason, the SNMP package upgrade scripts don't stop snmpd before they perform the upgrade, so you need to stop snmpd before you update.

This is how I did it:$ sudo /etc/init.d/snmpd stop
Stopping network management services: snmpd snmptrapd.
$ sudo dpkg --con…

How to rollback Rails database migrations

When you perform database migrations in Rails you use the following command:rake db:migrateIf you want to roll back your change to need need to find out what version your database is currently at and then roll back to a previous version:rake db:migrate VERSION=<version to roll back to>I've never liked this way to roll back, seems to much like hard work to me so I found a migration rollback script on the programmingishard site and simplified so that it only rollback by one version:namespace :db do namespace :migrate do desc "Rollback the database schema to the previous version" task :rollback => :environment do previous_version = ActiveRecord::Migrator.current_version.to_i - 1 ActiveRecord::Migrator.migrate("db/migrate/", previous_version) puts "Schema rolled back to previous verison (#{previous_version})." end end endTo use this scriptlet, copy and paste the code into a file called db_rollback.rake and place …