aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/migration_test_firebird.rb
blob: 9d4b80c839920cb3ab3a4013e80086037c78867c (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
require 'abstract_unit'
require 'fixtures/course'

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

  def setup
    # using Course connection for tests -- need a db that doesn't already have a BOOLEAN domain
    @connection = Course.connection
    @fireruby_connection = @connection.instance_variable_get(:@connection)
  end

  def teardown
    @connection.drop_table :foo rescue nil
    @connection.execute("DROP DOMAIN D_BOOLEAN") rescue nil
  end

  def test_create_table_with_custom_sequence_name
    assert_nothing_raised do
      @connection.create_table(:foo, :sequence => 'foo_custom_seq') do |f|
        f.column :bar, :string
      end
    end
    assert !sequence_exists?('foo_seq')
    assert sequence_exists?('foo_custom_seq')

    assert_nothing_raised { @connection.drop_table(:foo, :sequence => 'foo_custom_seq') }
    assert !sequence_exists?('foo_custom_seq')
  ensure
    FireRuby::Generator.new('foo_custom_seq', @fireruby_connection).drop rescue nil
  end

  def test_create_table_without_sequence
    assert_nothing_raised do
      @connection.create_table(:foo, :sequence => false) do |f|
        f.column :bar, :string
      end
    end
    assert !sequence_exists?('foo_seq')
    assert_nothing_raised { @connection.drop_table :foo }

    assert_nothing_raised do
      @connection.create_table(:foo, :id => false) do |f|
        f.column :bar, :string
      end
    end
    assert !sequence_exists?('foo_seq')
    assert_nothing_raised { @connection.drop_table :foo }
  end

  def test_create_table_with_boolean_column
    assert !boolean_domain_exists?
    assert_nothing_raised do
      @connection.create_table :foo do |f|
        f.column :bar, :string
        f.column :baz, :boolean
      end
    end
    assert boolean_domain_exists?
  end

  def test_add_boolean_column
    assert !boolean_domain_exists?
    @connection.create_table :foo do |f|
      f.column :bar, :string
    end

    assert_nothing_raised { @connection.add_column :foo, :baz, :boolean }
    assert boolean_domain_exists?
    assert_equal :boolean, @connection.columns(:foo).find { |c| c.name == "baz" }.type
  end

  def test_change_column_to_boolean
    assert !boolean_domain_exists?
    # Manually create table with a SMALLINT column, which can be changed to a BOOLEAN
    @connection.execute "CREATE TABLE foo (bar SMALLINT)"
    assert_equal :integer, @connection.columns(:foo).find { |c| c.name == "bar" }.type

    assert_nothing_raised { @connection.change_column :foo, :bar, :boolean }
    assert boolean_domain_exists?
    assert_equal :boolean, @connection.columns(:foo).find { |c| c.name == "bar" }.type
  end

  def test_rename_table_with_data_and_index
    @connection.create_table :foo do |f|
      f.column :baz, :string, :limit => 50
    end
    100.times { |i| @connection.execute "INSERT INTO foo VALUES (GEN_ID(foo_seq, 1), 'record #{i+1}')" }
    @connection.add_index :foo, :baz

    assert_nothing_raised { @connection.rename_table :foo, :bar }
    assert !@connection.tables.include?("foo")
    assert @connection.tables.include?("bar")
    assert_equal "bar_baz_index", @connection.indexes("bar").first.name
    assert_equal 100, FireRuby::Generator.new("bar_seq", @fireruby_connection).last
    assert_equal 100, @connection.select_one("SELECT COUNT(*) FROM bar")["count"]
  ensure
    @connection.drop_table :bar rescue nil
  end

  def test_renaming_table_with_fk_constraint_raises_error
    @connection.create_table :parent do |p|
      p.column :name, :string
    end
    @connection.create_table :child do |c|
      c.column :parent_id, :integer
    end
    @connection.execute "ALTER TABLE child ADD CONSTRAINT fk_child_parent FOREIGN KEY(parent_id) REFERENCES parent(id)"
    assert_raise(ActiveRecord::ActiveRecordError) { @connection.rename_table :child, :descendant }
  ensure
    @connection.drop_table :child rescue nil
    @connection.drop_table :descendant rescue nil
    @connection.drop_table :parent rescue nil
  end

  private
    def boolean_domain_exists?
      !@connection.select_one("SELECT 1 FROM rdb$fields WHERE rdb$field_name = 'D_BOOLEAN'").nil?
    end

    def sequence_exists?(sequence_name)
      FireRuby::Generator.exists?(sequence_name, @fireruby_connection)
    end
end