public
Description: Fork of DataMapper 0.3 with patches to fix major show-stopping bugs
Homepage:
Clone URL: git://github.com/cardmagic/dm-works.git
cardmagic (author)
Thu Apr 24 15:12:22 -0700 2008
commit  4c6160d17c3cfceee66bb7a8cf6cd11d2016af2f
tree    53f374194d738f279b78d3c970312a018d007a27
parent  93ad19c37c9a978525ea260483ba7ecfb8877184
dm-works / environment.rb
100644 62 lines (49 sloc) 2.106 kb
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
unless defined?(INITIAL_CLASSES)
  # Require the DataMapper, and a Mock Adapter.
  require File.dirname(__FILE__) + '/lib/data_mapper'
  require File.dirname(__FILE__) + '/spec/mock_adapter'
 
  adapter = ENV['ADAPTER'] || 'sqlite3'
 
  configuration_options = {
    :adapter => adapter,
    :database => (ENV['DATABASE'] || 'data_mapper_1').dup
  }
 
  # Prepare the log path, and remove the existing spec.log
  require 'fileutils'
 
  if ENV['LOG_NAME']
    log_path = nil
    
    if ENV['LOG_NAME'] != 'STDOUT'
      FileUtils::mkdir_p(File.dirname(__FILE__) + '/log')
      log_path = File.dirname(__FILE__) + "/log/#{ENV['LOG_NAME']}.log"
      FileUtils::rm log_path if File.exists?(log_path)
    else
      log_path = 'STDOUT'
    end
    
    configuration_options.merge!(:log_stream => log_path, :log_level => Logger::DEBUG)
  end
 
  case adapter
    when 'postgresql' then
      configuration_options[:username] = ENV['USERNAME'] || 'postgres'
    when 'mysql' then
      configuration_options[:username] = 'root'
    when 'sqlite3' then
      unless configuration_options[:database] == ':memory:'
        configuration_options[:database] << '.db'
      end
  end
 
  load_models = lambda do
    Dir[File.dirname(__FILE__) + '/spec/models/*.rb'].sort.each { |path| load path }
  end
 
  secondary_configuration_options = configuration_options.dup
  secondary_configuration_options.merge!(:database => (adapter == 'sqlite3' ? 'data_mapper_2.db' : 'data_mapper_2'))
  
  DataMapper::Database.setup(configuration_options)
  DataMapper::Database.setup(:secondary, secondary_configuration_options)
  DataMapper::Database.setup(:mock, :adapter => MockAdapter)
 
  [:default, :secondary, :mock].each { |name| database(name) { load_models.call } }
 
  # Reset the test database.
  unless ENV['AUTO_MIGRATE'] == 'false'
    [:default, :secondary].each { |name| database(name) { DataMapper::Persistence.auto_migrate! } }
  end
 
  # Save the initial database layout so we can put everything back together
  # after auto migrations testing
  INITIAL_CLASSES = Array.new(DataMapper::Persistence.subclasses.to_a)
end