Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #11763 from schneems/schneems/create-sqlite3db

Create sqlite3 directory if not present
Conflicts:
	activerecord/CHANGELOG.md
  • Loading branch information...
commit 550da78bc200391c189d04283969572371af8031 1 parent aee5fe9
@josevalim josevalim authored rafaelfranca committed
View
4 activerecord/CHANGELOG.md
@@ -1,5 +1,9 @@
## unreleased ##
+* 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`.
View
8 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
)
View
21 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
Please sign in to comment.
Something went wrong with that request. Please try again.