File: core.rb

package info (click to toggle)
ruby-activerecord-nulldb-adapter 0.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 248 kB
  • sloc: ruby: 833; makefile: 3
file content (42 lines) | stat: -rw-r--r-- 1,055 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
require 'active_support'
require 'active_support/deprecation'
require 'active_record/connection_adapters/nulldb_adapter'

module NullDB
  LEGACY_ACTIVERECORD = 
    Gem::Version.new(ActiveRecord::VERSION::STRING) < Gem::Version.new('4.2.0')

  class Configuration < Struct.new(:project_root); end

  class << self
    def configure
      @configuration = Configuration.new.tap {|c| yield c}
    end

    def configuration
      if @configuration.nil?
        raise "NullDB not configured. Require a framework, ex 'nulldb/rails'"
      end

      @configuration
    end

    def nullify(options={})
      begin
        @prev_connection = ActiveRecord::Base.connection_pool.try(:spec)
      rescue ActiveRecord::ConnectionNotEstablished
      end
      ActiveRecord::Base.establish_connection(options.merge(:adapter => :nulldb))
    end

    def restore
      if @prev_connection
        ActiveRecord::Base.establish_connection(@prev_connection.config)
      end
    end

    def checkpoint
      ActiveRecord::Base.connection.checkpoint!
    end
  end
end