I know this isn't Python, but I'd like to get a view on the 'one obvious' way to set up an SQLite (or other) database and its location per-app. I've got a bit lost with the Camping 2 changes and various code snippets I have kicking around.

1.
is it best to set up the DB creation/connection:

1.1
at the end of the app

AppName::Models::Base.establish_connection(
 :adapter => 'sqlite3',
 :database => '/path/to/my/app/myApp.db'
)
run AppName #from an old snippet

1.2
OR
like this (postgres) example [Magnus]:

def List.create
 List::Models::Base.establish_connection(
   :adapter => "postgresql",
   :username => "root",
   :password => "toor,
   :database => "list"
 )
 List::Models.create_schema
end

1.3
in a config/database.yml file [Magnus] (probably not worth it for SQLite):
---
adapter: postgresql
username: root
password: toor
database: mycampingdb

And then do:

require 'yaml'

def AppName.create
AppName ::Models ::Base.establish_connection(YAML.load(File.read("database.yml")))
 AppName::Models.create_schema
end


2.
since sqlite is the default, is it necessary to set :adapter explicitly if that's what I'm using?

def AppName.create
 AppName::Models::Base.establish_connection(
  :adapter => 'sqlite3',
  :database => '/path/to/my/app/.camping.db'
)
end


3.
Since .create is *only needed once* to set up the database (Magnus: "if you connect to a database which already has the tables, DON'T run `AppName::Models.create_schema` as this will probably delete the whole database.") what do we do with this after the first run:

def AppName.create
 AppName::Models.create_schema
end

3.1 delete it after the db is created on the first run?
3.2 check if the db already exists (best way, please)?
3.3 check like this (never understood ':assume'?):
AppName::Models.create_schema :assume => (AppName::Models:: table_name.table_exists? ? 1.0 : 0.0)
or could we do something simpler like (?):
AppName::Models.create_schema unless table_exists?(table_name)
?

4.
There's also this from a previous post (opinions please?):

"On the part of migrations ... "def self.up" and "def self.down" ... gave me errors for some reason. But ... it should be updated to "def self.change" ... that's the modern way of doing it."

DaveE

_______________________________________________
Camping-list mailing list
Camping-list@rubyforge.org
http://rubyforge.org/mailman/listinfo/camping-list

Reply via email to