March 2008 Archives

I recently wanted to set up a quick daemon process that used ActiveRecord outside the rails framework. I also wanted it to use sqlite, just to keep install/dependencies simple. I found not so good documentation on how to do this... and after a few missteps it turns out it's not that hard. The only gem dependencies are activerecord and sqlite3-ruby. You'll also need sqlite working. Code follows
require 'rubygems'
require 'sqlite3'
require 'activerecord'

# connect to database.  This will create one if it doesn't exist
MY_DB_NAME = ".my.db"
MY_DB = SQLite3::Database.new(MY_DB_NAME)

# get active record set up
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => MY_DB_NAME)

# create your AR class
class Update < ActiveRecord::Base

end

# do a quick pseudo migration.  This should only get executed on the first run
if !Update.table_exists?
  ActiveRecord::Base.connection.create_table(:updates) do |t|
    t.column :account_name, :string
    t.column :last_update_time, :timestamp
    t.column :last_update_id, :integer
  end
end

Contact

Send mail to mark dot mcbride at gmail dot com

Pages

Powered by Movable Type 4.1

About this Archive

This page is an archive of entries from March 2008 listed from newest to oldest.

January 2008 is the previous archive.

April 2008 is the next archive.

Find recent content on the main index or look in the archives to find all content.