From aa3eee167775af7926a3baaab9698bfcaa2b2104 Mon Sep 17 00:00:00 2001 From: Matt Palmer Date: Mon, 24 Oct 2011 20:59:15 +1100 Subject: [PATCH 1/6] Add the ability to specify a path to a database file --- README.md | 13 +++++++++++++ lib/sinatra/activerecord.rb | 1 - test/sinatra_activerecord_test.rb | 10 ++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e754be8..809cbbb 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,19 @@ the following code in about anywhere and it'll work just fine: erb :foos end +### Database file locations + +Giving a path to an SQLite database via a URL is slightly painful... far too +many slashes are needed. If you want to give a relative path, then you must +use three slashes before your path name: + + sqlite:///db/foo.db + +An absolute path has *four* slashes (an extra one to indicate "absolute +path"): + + sqlite:////home/foo/db/bar.db + ### NOTE about the rip-off This Code and README.md is a heavy adaption of [rtomayko's sinatra-sequel](http://github.com/rtomayko/sinatra-sequel/) diff --git a/lib/sinatra/activerecord.rb b/lib/sinatra/activerecord.rb index af8a4d5..9768d17 100644 --- a/lib/sinatra/activerecord.rb +++ b/lib/sinatra/activerecord.rb @@ -42,7 +42,6 @@ def database_options case url.scheme when "sqlite" options[:adapter] = "sqlite3" - options[:database] = url.host when "postgres" options[:adapter] = "postgresql" end diff --git a/test/sinatra_activerecord_test.rb b/test/sinatra_activerecord_test.rb index 1bf2f26..bbd2286 100644 --- a/test/sinatra_activerecord_test.rb +++ b/test/sinatra_activerecord_test.rb @@ -1,3 +1,4 @@ +require 'test/unit' require 'sinatra/base' require 'sinatra/activerecord' @@ -32,4 +33,13 @@ def test_establishes_a_database_connection_when_set assert @app.database.respond_to? :table_exists? end + def test_db_urls_with_a_path + @app.database = 'sqlite:///test/foo.db' + assert_equal 'test/foo.db', @app.database.connection.instance_variable_get(:@config)[:database] + end + + def test_db_urls_with_absolute_path + @app.database = 'sqlite:////tmp/foo.db' + assert_equal '/tmp/foo.db', @app.database.connection.instance_variable_get(:@config)[:database] + end end From b9c96ec37aa9fc18b94dc95afd2f8274bbf09780 Mon Sep 17 00:00:00 2001 From: Matt Palmer Date: Mon, 7 Nov 2011 19:43:56 +1100 Subject: [PATCH 2/6] Newer AR versions require a call to .configurations before fixtures will work --- lib/sinatra/activerecord.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/sinatra/activerecord.rb b/lib/sinatra/activerecord.rb index 9768d17..28a90aa 100644 --- a/lib/sinatra/activerecord.rb +++ b/lib/sinatra/activerecord.rb @@ -22,7 +22,8 @@ def database @database ||= ( url = URI(database_url) ActiveRecord::Base.logger = activerecord_logger - ActiveRecord::Base.establish_connection(database_options) + ActiveRecord::Base.configurations[environment] = database_options + ActiveRecord::Base.establish_connection(environment) ActiveRecord::Base ) end From 833222789dc7129a5ba2ec98861ff5a7481d4a39 Mon Sep 17 00:00:00 2001 From: Matt Palmer Date: Mon, 7 Nov 2011 19:54:45 +1100 Subject: [PATCH 3/6] Fkkn gemspecs --- sinatra-activerecord.gemspec | 1 - 1 file changed, 1 deletion(-) diff --git a/sinatra-activerecord.gemspec b/sinatra-activerecord.gemspec index 1989fec..1d71450 100644 --- a/sinatra-activerecord.gemspec +++ b/sinatra-activerecord.gemspec @@ -24,7 +24,6 @@ Gem::Specification.new do |s| s.extra_rdoc_files = %w[README.md] s.add_dependency 'sinatra', '>= 0.9.4' - s.has_rdoc = true s.homepage = "http://github.com/rtomayko/sinatra-activerecord" s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Sinatra::ActiveRecord"] s.require_paths = %w[lib] From 8e30fc723ad7b00059e4742b6c84f323feab805c Mon Sep 17 00:00:00 2001 From: Matt Palmer Date: Mon, 7 Nov 2011 20:59:24 +1100 Subject: [PATCH 4/6] Oh FFS, where's HashWithIndifferentAccess when you need it --- lib/sinatra/activerecord.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/sinatra/activerecord.rb b/lib/sinatra/activerecord.rb index 28a90aa..ebc8c3c 100644 --- a/lib/sinatra/activerecord.rb +++ b/lib/sinatra/activerecord.rb @@ -22,8 +22,8 @@ def database @database ||= ( url = URI(database_url) ActiveRecord::Base.logger = activerecord_logger - ActiveRecord::Base.configurations[environment] = database_options - ActiveRecord::Base.establish_connection(environment) + ActiveRecord::Base.configurations[environment.to_s] = database_options + ActiveRecord::Base.establish_connection(environment.to_s) ActiveRecord::Base ) end From c54c946b3e995c1ecfa59c2d6f4526a5746c2487 Mon Sep 17 00:00:00 2001 From: Matt Palmer Date: Mon, 26 Dec 2011 15:21:33 +1100 Subject: [PATCH 5/6] Support Rails-style database.yml files, if you're into that kind of thing --- README.md | 12 ++++++++++++ lib/sinatra/activerecord.rb | 14 +++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 809cbbb..0c9c3cd 100644 --- a/README.md +++ b/README.md @@ -87,6 +87,18 @@ path"): sqlite:////home/foo/db/bar.db +### Using a YAML config file + +If you want to store your database environment configurations in a YAML +file, Rails-style, simply tell your app all about it: + + set :database_config_yaml, "/path/to/some/file.yaml" + +And SAR will load the file and configure ActiveRecord appropriately. It +will automatically key off your pre-existing `:environment` variable to +decide which database to connect to. + + ### NOTE about the rip-off This Code and README.md is a heavy adaption of [rtomayko's sinatra-sequel](http://github.com/rtomayko/sinatra-sequel/) diff --git a/lib/sinatra/activerecord.rb b/lib/sinatra/activerecord.rb index ebc8c3c..baf6c41 100644 --- a/lib/sinatra/activerecord.rb +++ b/lib/sinatra/activerecord.rb @@ -3,6 +3,7 @@ require 'sinatra/base' require 'active_record' require 'logger' +require 'yaml' module Sinatra module ActiveRecordHelper @@ -20,13 +21,21 @@ def database=(url) def database @database ||= ( - url = URI(database_url) ActiveRecord::Base.logger = activerecord_logger - ActiveRecord::Base.configurations[environment.to_s] = database_options + unless database_url.nil? + url = URI(database_url) + ActiveRecord::Base.configurations[environment.to_s] = database_options + end + ActiveRecord::Base.establish_connection(environment.to_s) ActiveRecord::Base ) end + + def database_config_yaml=(yaml) + ActiveRecord::Base.configurations = YAML.load_file(yaml) + set :database_url, nil + end protected @@ -53,7 +62,6 @@ def self.registered(app) app.set :database_url, lambda { ENV['DATABASE_URL'] || "sqlite://#{environment}.db" } app.set :database_extras, Hash.new app.set :activerecord_logger, Logger.new(STDOUT) - app.database # force connection app.helpers ActiveRecordHelper end end From bfde66130f7b085f05be630ad8fccb71543071f8 Mon Sep 17 00:00:00 2001 From: Matt Palmer Date: Mon, 26 Dec 2011 15:21:55 +1100 Subject: [PATCH 6/6] Load the local activerecord.rb, to ensure we're testing the right code --- test/foo.db | 0 test/sinatra_activerecord_test.rb | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) create mode 100644 test/foo.db diff --git a/test/foo.db b/test/foo.db new file mode 100644 index 0000000..e69de29 diff --git a/test/sinatra_activerecord_test.rb b/test/sinatra_activerecord_test.rb index bbd2286..7bc8f9c 100644 --- a/test/sinatra_activerecord_test.rb +++ b/test/sinatra_activerecord_test.rb @@ -1,6 +1,6 @@ require 'test/unit' require 'sinatra/base' -require 'sinatra/activerecord' +require File.expand_path('../../lib/sinatra/activerecord', __FILE__) class MockSinatraApp < Sinatra::Base register Sinatra::ActiveRecordExtension