Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

root/branches/2-1-caching/activerecord/test/aaa_create_tables_test.rb

Revision 8365, 2.4 kB (checked in by bitsweat, 1 year ago)

Ruby 1.9 compat: File.exists\? -> File.exist\? en masse. References #1689 [Pratik Naik]

Line 
1 # The filename begins with "aaa" to ensure this is the first test.
2 require 'abstract_unit'
3
4 class AAACreateTablesTest < Test::Unit::TestCase
5   self.use_transactional_fixtures = false
6
7   def setup
8     @base_path = "#{File.dirname(__FILE__)}/fixtures/db_definitions"
9   end
10
11   def test_drop_and_create_main_tables
12     recreate ActiveRecord::Base unless use_migrations?
13     assert true
14   end
15
16   def test_load_schema
17     if ActiveRecord::Base.connection.supports_migrations?
18       eval(File.read("#{File.dirname(__FILE__)}/fixtures/db_definitions/schema.rb"))
19     else
20       recreate ActiveRecord::Base, '3'
21     end
22     assert true
23   end
24  
25   def test_drop_and_create_courses_table
26     if Course.connection.supports_migrations?
27       eval(File.read("#{File.dirname(__FILE__)}/fixtures/db_definitions/schema2.rb"))
28     end
29     recreate Course, '2' unless use_migrations_for_courses?
30     assert true
31   end
32
33   private
34     def use_migrations?
35       unittest_sql_filename = ActiveRecord::Base.connection.adapter_name.downcase + ".sql"
36       not File.exist? "#{@base_path}/#{unittest_sql_filename}"
37     end
38
39     def use_migrations_for_courses?
40       unittest2_sql_filename = ActiveRecord::Base.connection.adapter_name.downcase + "2.sql"
41       not File.exist? "#{@base_path}/#{unittest2_sql_filename}"
42     end
43
44     def recreate(base, suffix = nil)
45       connection = base.connection
46       adapter_name = connection.adapter_name.downcase + suffix.to_s
47       execute_sql_file "#{@base_path}/#{adapter_name}.drop.sql", connection
48       execute_sql_file "#{@base_path}/#{adapter_name}.sql", connection
49     end
50
51     def execute_sql_file(path, connection)
52       # OpenBase has a different format for sql files
53       if current_adapter?(:OpenBaseAdapter) then
54           File.read(path).split("go").each_with_index do |sql, i|
55             begin
56               # OpenBase does not support comments embedded in sql
57               connection.execute(sql,"SQL statement ##{i}") unless sql.blank?
58             rescue ActiveRecord::StatementInvalid
59               #$stderr.puts "warning: #{$!}"
60             end
61           end
62       else
63         File.read(path).split(';').each_with_index do |sql, i|
64           begin
65             connection.execute("\n\n-- statement ##{i}\n#{sql}\n") unless sql.blank?
66           rescue ActiveRecord::StatementInvalid
67             #$stderr.puts "warning: #{$!}"
68           end
69         end
70       end
71     end
72 end
Note: See TracBrowser for help on using the browser.