From 53aefdec91e4e26b2fbd1a8dc39817ecabfe6125 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Sun, 18 Nov 2012 23:46:33 -0200 Subject: Fix rails db command with sqlite3 database When using sqlite3 it was attempting to find the database file based on Rails.root, the problem is that Rails.root is not always present because we try to first manually load "config/database.yml" instead of loading the entire app, to make "rails db" faster. This means that when we're in the root path of the app, calling "rails db" won't allow us to use Rails.root, making the command fail for sqlite3 with the error: ./rails/commands/dbconsole.rb:62:in `start': undefined method `root' for Rails:Module (NoMethodError) The fix is to simply not pass any dir string to File.expand_path, which will make it use the current directory of the process as base, or the root path of the app, which is what we want. When we are in any other subdirectory, calling "rails db" should work just fine, because "config/database.yml" won't be found, thus "rails db" will fallback to loading the app, making Rails.root available. Closes #8257. --- railties/lib/rails/commands/dbconsole.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'railties/lib/rails') diff --git a/railties/lib/rails/commands/dbconsole.rb b/railties/lib/rails/commands/dbconsole.rb index 90359d1c08..d614328e52 100644 --- a/railties/lib/rails/commands/dbconsole.rb +++ b/railties/lib/rails/commands/dbconsole.rb @@ -6,7 +6,7 @@ require 'rbconfig' module Rails class DBConsole attr_reader :config, :arguments - + def self.start new.start end @@ -59,7 +59,7 @@ module Rails args << "-#{options['mode']}" if options['mode'] args << "-header" if options['header'] - args << File.expand_path(config['database'], Rails.root) + args << File.expand_path(config['database'], Rails.respond_to?(:root) ? Rails.root : nil) find_cmd_and_exec('sqlite3', *args) @@ -108,7 +108,7 @@ module Rails def parse_arguments(arguments) options = {} - + OptionParser.new do |opt| opt.banner = "Usage: rails dbconsole [environment] [options]" opt.on("-p", "--include-password", "Automatically provide the password from database.yml") do |v| @@ -123,7 +123,7 @@ module Rails opt.on("--header") do |h| options['header'] = h end - + opt.on("-h", "--help", "Show this help message.") do puts opt exit @@ -142,7 +142,7 @@ module Rails env = arguments.first options[:environment] = %w(production development test).detect {|e| e =~ /^#{env}/} || env end - + options end -- cgit v1.2.3