The Silver Lining

Lessons & Learnings from a salesforce certified technical architect.

Posts Tagged ‘database

“Dropping” a mongohq database on Heroku

leave a comment »

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:

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

Note that the exception handler isn’t perfect and essentially all I’m doing is skipping the system collections that can’t be dropped.
Once you’ve deployed this code to the heroku servers it’s simple to execute:

heroku rake mdb:drop
Advertisements

Written by Wes

October 2, 2011 at 5:05 pm

Posted in Ruby

Tagged with , , , , ,

Unit Test Data Consistency

leave a comment »

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 the rest of this entry »

Written by Wes

July 5, 2009 at 9:28 pm

Salesforce Savepoints

with 5 comments

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 the rest of this entry »

Written by Wes

June 15, 2009 at 7:28 am

%d bloggers like this: