aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/aaa_create_tables_test.rb
blob: 10ac9c525d698620639bf1ac5c46e13beb24aa3b (plain) (blame)
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
# The filename begins with "aaa" to ensure this is the first test.
require 'abstract_unit'

class CreateTablesTest < Test::Unit::TestCase
  self.use_transactional_fixtures = false

  def setup
    @base_path = "#{File.dirname(__FILE__)}/fixtures/db_definitions"
  end

  def test_drop_and_create_main_tables
    recreate ActiveRecord::Base
    assert true
  end

  def test_load_schema
    eval(File.read("#{File.dirname(__FILE__)}/fixtures/db_definitions/schema.rb"))
    assert true
  end
  
  def test_drop_and_create_courses_table
    recreate Course, '2'
    assert true
  end

  private
    def recreate(base, suffix = nil)
      connection = base.connection
      adapter_name = connection.adapter_name.downcase + suffix.to_s
      execute_sql_file "#{@base_path}/#{adapter_name}.drop.sql", connection
      execute_sql_file "#{@base_path}/#{adapter_name}.sql", connection
    end

    def execute_sql_file(path, connection)
      File.read(path).split(';').each_with_index do |sql, i|
        begin
          connection.execute("\n\n-- statement ##{i}\n#{sql}\n") unless sql.blank?
        rescue ActiveRecord::StatementInvalid
          #$stderr.puts "warning: #{$!}"
        end
      end
    end
end