ActiveRecord + sqlite + in memory db + without rails

Send to friend

For a current data crunching project I’m working on, it makes sense to build a db in memory at the moment. Doing it in memory allowed me to cut the run time by about 75%. I’m sure I could get it down further, but atm this is good enough, and was v. easy to setup.

I started with a yaml config file, probably unnecessary, but I have some other db configs in there too, so makes sense for me.

1
2
3
4

adapter: sqlite3
database: ":memory:"

Then make a simple env file.

1
2
3
4
5
6
7
8

require 'activerecord'
dbconf = YAML::load(File.open('config/database.yml'))  
ActiveRecord::Base.establish_connection(dbconf) 
ActiveRecord::Base.logger = Logger.new(File.open('log/database.log', 'a')) 

ActiveRecord::Migrator.up('db/migrate') 

And a migration.

1
2
3
4
5
6
7
8
9
10
11
12

class CreateLogTables < ActiveRecord::Migration
  def self.up
    create_table :accounts do |t|
                        t.integer :account_id
    end

  end
end

#ETC

And some simple models.

1
2
3
4
5
6
7
8
9
10
11
12
13
14

class Account < ActiveRecord::Base
        has_many :sessions
end

class Session < ActiveRecord::Base
        belongs_to :user
        has_many :actions
end

class Action < ActiveRecord::Base
        belongs_to :session
end

  • Technology: