views:

30

answers:

2

Hi everybody.

My rails app. uses mysql database and I need to generate .sqlite3.databases. Is it possible to use activerecord and rails models for it? We are trying now to use models namespaced by Remote:: module but by this way we can't start concurrent generators.

A: 

Yep - ActiveRecord does support sqlite3 databases. You can set your database.yml file up as follows:

# SQLite version 3.x
#   gem install sqlite3-ruby (not necessary on OS X Leopard)
development:
  adapter: sqlite3
  database: db/development.sqlite3
  pool: 5
  timeout: 5000

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  adapter: sqlite3
  database: db/test.sqlite3
  pool: 5
  timeout: 5000

production:
  adapter: sqlite3
  database: db/production.sqlite3
  pool: 5
  timeout: 5000

Note that you will need to symlink your database files when deploying in order to avoid losing your DB.

Mr. Matt
Most of data is stored in mysql database. I need to generate temporary sqlite databases for other projects.
SMiX
+1  A: 

In your remote models, you want to connect to a separate database using #establish_connection:

# config/database.yml
remote_development:
  adapter: sqlite3
  database: db/development.sqlite3

remote_production:
  adapter: sqlite3
  database: /usr/local/remote/myapp.sqlite3


# app/models/remote_model.rb
class RemoteModel < ActiveRecord::Base
  establish_connection "remote_#{Rails.env}"
  self.abstract_class = true
end


# app/models/remote_user.rb
class RemoteUser < RemoteModel
end

Note the abstract_class setter: this means the class in question doesn't have an underlying table: it's used for configuration purposes only.

François Beausoleil
I need to create different sqlite databases. Generation processes may be concurrent. I need dynamic database names...
SMiX
establish_connection also accepts a Hash, thus you can say "RemoteModel.send(:establish_connection, :adapter => "sqlite3", :database => Rails.root + "tmp/#{Time.to_i}.sqlite3")
François Beausoleil