diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 74f2b2287fc0eeedda85cde5a81f8facc95d6189..f60ea9838bafea05e1c1f855e00c3fb8b2aef1ec 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,7 @@ +* Create a directory for sqlite3 file if not present on the system. + + *Richard Schneeman* + * Removed redundant override of `xml` column definition for PG, in order to use `xml` column type instead of `text`. diff --git a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb index 7d940fe1c9078b26fd8edd9ea015ceb81e64727e..16306a78cfecafefcda4f5ebbd1d12506d452352 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb @@ -17,12 +17,14 @@ def sqlite3_connection(config) # Allow database path relative to Rails.root, but only if # the database path is not the special path that tells # Sqlite to build a database only in memory. - if defined?(Rails.root) && ':memory:' != config[:database] - config[:database] = File.expand_path(config[:database], Rails.root) + if ':memory:' != config[:database] + config[:database] = Pathname.new(config[:database]) + config[:database] = config[:database].expand_path(Rails.root) if defined?(Rails.root) + config[:database].dirname.mkdir unless config[:database].dirname.directory? end db = SQLite3::Database.new( - config[:database], + config[:database].to_s, :results_as_hash => true ) diff --git a/activerecord/test/cases/adapters/sqlite3/sqlite3_create_folder_test.rb b/activerecord/test/cases/adapters/sqlite3/sqlite3_create_folder_test.rb new file mode 100644 index 0000000000000000000000000000000000000000..5a4fe6358035f40fc06cf94a42dc750050cdf18a --- /dev/null +++ b/activerecord/test/cases/adapters/sqlite3/sqlite3_create_folder_test.rb @@ -0,0 +1,21 @@ +# encoding: utf-8 +require "cases/helper" +require 'models/owner' + +module ActiveRecord + module ConnectionAdapters + class SQLite3CreateFolder < ActiveRecord::TestCase + def test_sqlite_creates_directory + Dir.mktmpdir do |dir| + dir = Pathname.new(dir) + @conn = Base.sqlite3_connection :database => dir.join("db/foo.sqlite3"), + :adapter => 'sqlite3', + :timeout => 100 + + assert Dir.exists? dir.join('db') + assert File.exist? dir.join('db/foo.sqlite3') + end + end + end + end +end