2 require 'models/course'
4 class FirebirdMigrationTest
< ActiveRecord
::TestCase
5 self.use_transactional_fixtures
= false
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)
14 @connection.drop_table
:foo rescue nil
15 @connection.execute("DROP DOMAIN D_BOOLEAN") rescue nil
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
24 assert
!sequence_exists
?('foo_seq')
25 assert sequence_exists
?('foo_custom_seq')
27 assert_nothing_raised
{ @connection.drop_table(:foo, :sequence => 'foo_custom_seq') }
28 assert
!sequence_exists
?('foo_custom_seq')
30 FireRuby
::Generator.new('foo_custom_seq', @fireruby_connection).drop
rescue nil
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
39 assert
!sequence_exists
?('foo_seq')
40 assert_nothing_raised
{ @connection.drop_table
:foo }
42 assert_nothing_raised
do
43 @connection.create_table(:foo, :id => false) do |f
|
44 f
.column
:bar, :string
47 assert
!sequence_exists
?('foo_seq')
48 assert_nothing_raised
{ @connection.drop_table
:foo }
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
59 assert boolean_domain_exists
?
62 def test_add_boolean_column
63 assert
!boolean_domain_exists
?
64 @connection.create_table
:foo do |f
|
65 f
.column
:bar, :string
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
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
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
84 def test_rename_table_with_data_and_index
85 @connection.create_table
:foo do |f
|
86 f
.column
:baz, :string, :limit => 50
88 100.times
{ |i
| @connection.execute
"INSERT INTO foo VALUES (GEN_ID(foo_seq, 1), 'record #{i+1}')" }
89 @connection.add_index
:foo, :baz
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"]
98 @connection.drop_table
:bar rescue nil
101 def test_renaming_table_with_fk_constraint_raises_error
102 @connection.create_table
:parent do |p
|
103 p
.column
:name, :string
105 @connection.create_table
:child do |c
|
106 c
.column
:parent_id, :integer
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 }
111 @connection.drop_table
:child rescue nil
112 @connection.drop_table
:descendant rescue nil
113 @connection.drop_table
:parent rescue nil
117 def boolean_domain_exists
?
118 !@connection.select_one("SELECT 1 FROM rdb$fields WHERE rdb$field_name = 'D_BOOLEAN'").nil?
121 def sequence_exists
?(sequence_name
)
122 FireRuby
::Generator.exists
?(sequence_name
, @fireruby_connection)