Re: setting up the SQLite database
Philippe Monnet <
ruby@...>
2012-05-21 01:58:15 GMT
I personally always use a config/database.yml file as I like to keep
the configuration out of the code.
Plus it makes it easier to switch between development and production
configuration.
E.g. for Heroku:
dbconfig = YAML.load(File.read('config/database.yml'))
# On Heroku the production database,yml is configured
automatically
environment = ENV['DATABASE_URL'] ? 'production' :
'development'
Camping::Models::Base.establish_connection
dbconfig[environment]
On 5/15/2012 11:39 AM, Dave Everitt wrote:
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-GrnCvJ7WPxnNLxjTenLetw@public.gmane.org
http://rubyforge.org/mailman/listinfo/camping-list
_______________________________________________
Camping-list mailing list
Camping-list@...
http://rubyforge.org/mailman/listinfo/camping-list