Froze rails gems
[depot.git] / vendor / rails / activerecord / test / cases / migration_test_firebird.rb
1 require "cases/helper"
2 require 'models/course'
3
4 class FirebirdMigrationTest < ActiveRecord::TestCase
5 self.use_transactional_fixtures = false
6
7 def setup
8 # using Course connection for tests -- need a db that doesn't already have a BOOLEAN domain
9 @connection = Course.connection
10 @fireruby_connection = @connection.instance_variable_get(:@connection)
11 end
12
13 def teardown
14 @connection.drop_table :foo rescue nil
15 @connection.execute("DROP DOMAIN D_BOOLEAN") rescue nil
16 end
17
18 def test_create_table_with_custom_sequence_name
19 assert_nothing_raised do
20 @connection.create_table(:foo, :sequence => 'foo_custom_seq') do |f|
21 f.column :bar, :string
22 end
23 end
24 assert !sequence_exists?('foo_seq')
25 assert sequence_exists?('foo_custom_seq')
26
27 assert_nothing_raised { @connection.drop_table(:foo, :sequence => 'foo_custom_seq') }
28 assert !sequence_exists?('foo_custom_seq')
29 ensure
30 FireRuby::Generator.new('foo_custom_seq', @fireruby_connection).drop rescue nil
31 end
32
33 def test_create_table_without_sequence
34 assert_nothing_raised do
35 @connection.create_table(:foo, :sequence => false) do |f|
36 f.column :bar, :string
37 end
38 end
39 assert !sequence_exists?('foo_seq')
40 assert_nothing_raised { @connection.drop_table :foo }
41
42 assert_nothing_raised do
43 @connection.create_table(:foo, :id => false) do |f|
44 f.column :bar, :string
45 end
46 end
47 assert !sequence_exists?('foo_seq')
48 assert_nothing_raised { @connection.drop_table :foo }
49 end
50
51 def test_create_table_with_boolean_column
52 assert !boolean_domain_exists?
53 assert_nothing_raised do
54 @connection.create_table :foo do |f|
55 f.column :bar, :string
56 f.column :baz, :boolean
57 end
58 end
59 assert boolean_domain_exists?
60 end
61
62 def test_add_boolean_column
63 assert !boolean_domain_exists?
64 @connection.create_table :foo do |f|
65 f.column :bar, :string
66 end
67
68 assert_nothing_raised { @connection.add_column :foo, :baz, :boolean }
69 assert boolean_domain_exists?
70 assert_equal :boolean, @connection.columns(:foo).find { |c| c.name == "baz" }.type
71 end
72
73 def test_change_column_to_boolean
74 assert !boolean_domain_exists?
75 # Manually create table with a SMALLINT column, which can be changed to a BOOLEAN
76 @connection.execute "CREATE TABLE foo (bar SMALLINT)"
77 assert_equal :integer, @connection.columns(:foo).find { |c| c.name == "bar" }.type
78
79 assert_nothing_raised { @connection.change_column :foo, :bar, :boolean }
80 assert boolean_domain_exists?
81 assert_equal :boolean, @connection.columns(:foo).find { |c| c.name == "bar" }.type
82 end
83
84 def test_rename_table_with_data_and_index
85 @connection.create_table :foo do |f|
86 f.column :baz, :string, :limit => 50
87 end
88 100.times { |i| @connection.execute "INSERT INTO foo VALUES (GEN_ID(foo_seq, 1), 'record #{i+1}')" }
89 @connection.add_index :foo, :baz
90
91 assert_nothing_raised { @connection.rename_table :foo, :bar }
92 assert !@connection.tables.include?("foo")
93 assert @connection.tables.include?("bar")
94 assert_equal "index_bar_on_baz", @connection.indexes("bar").first.name
95 assert_equal 100, FireRuby::Generator.new("bar_seq", @fireruby_connection).last
96 assert_equal 100, @connection.select_one("SELECT COUNT(*) FROM bar")["count"]
97 ensure
98 @connection.drop_table :bar rescue nil
99 end
100
101 def test_renaming_table_with_fk_constraint_raises_error
102 @connection.create_table :parent do |p|
103 p.column :name, :string
104 end
105 @connection.create_table :child do |c|
106 c.column :parent_id, :integer
107 end
108 @connection.execute "ALTER TABLE child ADD CONSTRAINT fk_child_parent FOREIGN KEY(parent_id) REFERENCES parent(id)"
109 assert_raise(ActiveRecord::ActiveRecordError) { @connection.rename_table :child, :descendant }
110 ensure
111 @connection.drop_table :child rescue nil
112 @connection.drop_table :descendant rescue nil
113 @connection.drop_table :parent rescue nil
114 end
115
116 private
117 def boolean_domain_exists?
118 !@connection.select_one("SELECT 1 FROM rdb$fields WHERE rdb$field_name = 'D_BOOLEAN'").nil?
119 end
120
121 def sequence_exists?(sequence_name)
122 FireRuby::Generator.exists?(sequence_name, @fireruby_connection)
123 end
124 end