Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

* use Rails::Railtie

* added migrate option
  • Loading branch information...
commit 8a26672a6644c9366ff0ea06549c84e47eab17b6 1 parent 2142c29
@stepahn authored
View
22 README.md
@@ -4,21 +4,18 @@ A simple fix to run your Rails tests with sqlite. From the example at
http://blog.seagul.co.uk/articles/2006/02/08/in-memory-sqlite-database-for-rails-testing
-Install the gem:
+Add the gem to your bundle by adding
```ruby
- gem install memory_test_fix
+ gem 'memory_test_fix'
```
-In your config/environments.rb, use:
+to your `Gemfile`
-```ruby
- config.gem 'memory_test_fix'
-```
In your database.yml, use:
-```ruby
+```yaml
test:
adapter: sqlite3
database: ":memory:"
@@ -28,13 +25,22 @@ It runs much faster!
You can also adjust the verbosity of the output:
-```ruby
+```yaml
test:
adapter: sqlite3
database: ":memory:"
verbosity: silent
```
+To use rails migrations instead of loading `db/schema.rb`
+
+```yaml
+ test:
+ adapter: sqlite3
+ database: ":memory:"
+ migrate: true
+```
+
You can also use this with other (testing) environments, not just 'test'.
## Rails Versions
View
1  init.rb
@@ -1 +0,0 @@
-require File.dirname(__FILE__) + "/rails/init"
View
50 lib/memory_test_fix.rb
@@ -1,48 +1,2 @@
-module MemoryTestFix
- def self.in_memory_database?
- in_memory? && sqlite3?
- end
-
- def self.in_memory?
- configuration[:database] == ':memory:' || configuration[:dbfile] == ':memory:'
- end
-
- def self.sqlite3?
- configuration[:adapter] == 'sqlite3'
- end
-
- def self.configuration
- ActiveRecord::Base.connection_config
- end
-
- def self.verbosity
- configuration[:verbosity]
- end
-
- def self.inform_using_in_memory
- puts "Creating sqlite :memory: database"
- end
-
- def self.init_schema
- if in_memory_database?
- load_schema = lambda {
- load "#{Rails.root}/db/schema.rb" # use db agnostic schema by default
- # ActiveRecord::Migrator.up('db/migrate') # use migrations
- }
- case verbosity
- when "silent"
- silence_stream(STDOUT, &load_schema)
- when "quiet"
- inform_using_in_memory
- silence_stream(STDOUT, &load_schema)
- else
- inform_using_in_memory
- load_schema.call
- end
- end
- end
-end
-
-ActiveSupport.on_load(:before_eager_load) do
- MemoryTestFix.init_schema
-end
+require 'memory_test_fix/schema_loader'
+require 'memory_test_fix/railtie.rb' if defined?(Rails)
View
7 lib/memory_test_fix/railtie.rb
@@ -0,0 +1,7 @@
+module MemoryTestFix
+ class Railtie < ::Rails::Railtie
+ config.before_eager_load do
+ MemoryTestFix::SchemaLoader.init_schema
+ end
+ end
+end
View
58 lib/memory_test_fix/schema_loader.rb
@@ -0,0 +1,58 @@
+module MemoryTestFix
+ module SchemaLoader
+ def self.in_memory_database?
+ in_memory? && sqlite3?
+ end
+
+ def self.in_memory?
+ configuration[:database] == ':memory:' || configuration[:dbfile] == ':memory:'
+ end
+
+ def self.sqlite3?
+ configuration[:adapter] == 'sqlite3'
+ end
+
+ def self.configuration
+ ActiveRecord::Base.connection_config
+ end
+
+ def self.verbosity
+ configuration[:verbosity]
+ end
+
+ def self.migrate
+ configuration[:migrate] == true
+ end
+
+ def self.inform_using_in_memory
+ puts "Creating sqlite :memory: database"
+ end
+
+ def self.load_schema
+ if migrate
+ lambda {
+ ActiveRecord::Migrator.up('db/migrate') # use migrations
+ }
+ else
+ lambda {
+ load "#{Rails.root}/db/schema.rb" # use db agnostic schema by default
+ }
+ end
+ end
+
+ def self.init_schema
+ if in_memory_database?
+ case verbosity
+ when "silent"
+ silence_stream(STDOUT, &load_schema)
+ when "quiet"
+ inform_using_in_memory
+ silence_stream(STDOUT, &load_schema)
+ else
+ inform_using_in_memory
+ load_schema.call
+ end
+ end
+ end
+ end
+end
View
5 memory_test_fix.gemspec
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = "memory_test_fix"
- s.version = "0.2.1"
+ s.version = "0.2.2"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Chris Roos", "Geoffrey Grosenbach", "Kakutani Shintaro",
@@ -9,8 +9,9 @@ Gem::Specification.new do |s|
s.summary = "Makes SQLite3 memory tests possible by preloading the schema."
s.homepage = "http://wiki.github.com/mvz/memory_test_fix"
s.require_paths = ["lib"]
- s.files = ["init.rb", "rails/init.rb", "lib/memory_test_fix.rb", "README.md", "memory_test_fix.gemspec"]
+ s.files = ["lib/memory_test_fix.rb", "lib/memory_test_fix/railtie.rb", "lib/memory_test_fix/schema_loader.rb", "README.md", "memory_test_fix.gemspec"]
s.has_rdoc = true
s.extra_rdoc_files = ['README.md']
s.rubygems_version = %q{1.2.0}
+ s.add_runtime_dependency('railties', '>= 3.0.0')
end
View
2  rails/init.rb
@@ -1,2 +0,0 @@
-
-require 'memory_test_fix'
Please sign in to comment.
Something went wrong with that request. Please try again.