“Dropping” a mongohq database on Heroku

I’ve used inverted commas around the word ‘dropping’ because it doesn’t look like you can drop a mongohq database – what you can do however is drop all the collections in the database. I was doing this through the add-on interface in heroku but that got tired quickly, and after some googling it looked like the solution was to write a custom rake task and drop it into lib/tasks/; the file can be named anything with a ‘.rb’ extension. The code follows: [code language=”ruby”] namespace :mdb do desc ‘Drops all the collections for the database for the current Rails.env’ task :drop => :environment do if ENV[‘MONGOHQ_URL’] uri = URI.parse(ENV[‘MONGOHQ_URL’]) conn = Mongo::Connection.from_uri(ENV[‘MONGOHQ_URL’]) DB = conn.db(uri.path.gsub(/^\//, ”)) DB.collections.each do |collection| begin collection.drop rescue puts "Can’t drop: " + collection.name end end end end end [/code] Note that the exception handler isn’t perfect and essentially all I’m doing is skipping the system collections …

Read more

Unit Test Data Consistency

I’m sure every Apex developer has had their Developer Org data interfere with their unit testing. Or perhaps you have coded unit tests that function perfectly within your Developer Org but when deployed to another Org fail because of a different, partial or empty database.

Of course as a developer this type on inconsistency within your work environment is extremely counter-productive. Initially I developed a methodology that did the job but wasn’t nearly as concise as I would have liked. More recently however I’ve developed a technical solution that is universal as well as quick to implement.

Read more

Salesforce Savepoints

Transaction control is an important part of any system that interacts with a database and Salesforce has neat ways of implementing said control.

Anyone that’s worked with SQL databases will be familiar with savepoints and rolling back, and Salesforce has implemented similar constructs. For those who haven’t heard of these terms wikipedia describes them as

savepoint is a way of implementing subtransactions (also known as nested transactions) within a relational database management system by indicating a point within a transaction that can be “rolled back to” without affecting any work done in the transaction before the savepoint was created.

rollback is an operation which returns the database to some previous state.

As with most things, the importance of these two features is best demonstrated using examples.

Read more