Standalone ActiveRecord and SQLite3

| | Comments () | TrackBacks (0)
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

0 TrackBacks

Listed below are links to blogs that reference this entry: Standalone ActiveRecord and SQLite3.

TrackBack URL for this entry: http://www.themcwongs.com/cgi-bin/mt/mt-tb.cgi/6

Comments

Contact

Send mail to mark dot mcbride at gmail dot com

Pages

Powered by Movable Type 4.1

About this Entry

This page contains a single entry by McWong published on March 5, 2008 3:50 PM.

Scaling and Deploying Rails: It's not Java. Or PHP. was the previous entry in this blog.

Twitter, Jabber, Stability - Should Twitter be more ejabberd and less rails? is the next entry in this blog.

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