diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2006-07-08 17:13:21 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2006-07-08 17:13:21 +0000 |
commit | a3f459eecfec2beda2f51fb6d62eca4e5d5fbd70 (patch) | |
tree | b9aef7ab10560845ab8dc759d0540448b8a846be /activerecord/test | |
parent | 338defb48ca29148c933aee6fc34ae902bc7ba7f (diff) | |
download | rails-a3f459eecfec2beda2f51fb6d62eca4e5d5fbd70.tar.gz rails-a3f459eecfec2beda2f51fb6d62eca4e5d5fbd70.tar.bz2 rails-a3f459eecfec2beda2f51fb6d62eca4e5d5fbd70.zip |
Firebird migrations support. Closes #5337.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4594 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/test')
-rw-r--r-- | activerecord/test/fixtures/db_definitions/firebird3.drop.sql | 11 | ||||
-rw-r--r-- | activerecord/test/fixtures/db_definitions/firebird3.sql | 49 | ||||
-rw-r--r-- | activerecord/test/fixtures/db_definitions/schema.rb | 12 | ||||
-rw-r--r-- | activerecord/test/migration_test.rb | 28 | ||||
-rw-r--r-- | activerecord/test/migration_test_firebird.rb | 124 |
5 files changed, 148 insertions, 76 deletions
diff --git a/activerecord/test/fixtures/db_definitions/firebird3.drop.sql b/activerecord/test/fixtures/db_definitions/firebird3.drop.sql deleted file mode 100644 index fc654212bd..0000000000 --- a/activerecord/test/fixtures/db_definitions/firebird3.drop.sql +++ /dev/null @@ -1,11 +0,0 @@ -DROP TABLE taggings; -DROP TABLE tags; -DROP TABLE categorizations; -DROP TABLE author_addresses; -DROP TABLE author_favorites; - -DROP GENERATOR taggings_seq; -DROP GENERATOR tags_seq; -DROP GENERATOR categorizations_seq; -DROP GENERATOR author_addresses_seq; -DROP GENERATOR author_favorites_seq; diff --git a/activerecord/test/fixtures/db_definitions/firebird3.sql b/activerecord/test/fixtures/db_definitions/firebird3.sql deleted file mode 100644 index 1d8e709d29..0000000000 --- a/activerecord/test/fixtures/db_definitions/firebird3.sql +++ /dev/null @@ -1,49 +0,0 @@ -CREATE TABLE taggings ( - id BIGINT NOT NULL, - tag_id BIGINT, - super_tag_id BIGINT, - taggable_type VARCHAR(255), - taggable_id BIGINT, - PRIMARY KEY (id) -); -CREATE GENERATOR taggings_seq; -SET GENERATOR taggings_seq TO 10000; - -CREATE TABLE tags ( - id BIGINT NOT NULL, - name VARCHAR(255), - taggings_count BIGINT DEFAULT 0, - PRIMARY KEY (id) -); -CREATE GENERATOR tags_seq; -SET GENERATOR tags_seq TO 10000; - -CREATE TABLE categorizations ( - id BIGINT NOT NULL, - category_id BIGINT, - post_id BIGINT, - author_id BIGINT, - PRIMARY KEY (id) -); -CREATE GENERATOR categorizations_seq; -SET GENERATOR categorizations_seq TO 10000; - -ALTER TABLE posts ADD taggings_count BIGINT DEFAULT 0; -ALTER TABLE authors ADD author_address_id BIGINT; - -CREATE TABLE author_addresses ( - id BIGINT NOT NULL, - author_address_id BIGINT, - PRIMARY KEY (id) -); -CREATE GENERATOR author_addresses_seq; -SET GENERATOR author_addresses_seq TO 10000; - -CREATE TABLE author_favorites ( - id BIGINT NOT NULL, - author_id BIGINT, - favorite_author_id BIGINT, - PRIMARY KEY (id) -); -CREATE GENERATOR author_favorites_seq; -SET GENERATOR author_favorites_seq TO 10000; diff --git a/activerecord/test/fixtures/db_definitions/schema.rb b/activerecord/test/fixtures/db_definitions/schema.rb index 7d10fbcadf..a5f2c9dc10 100644 --- a/activerecord/test/fixtures/db_definitions/schema.rb +++ b/activerecord/test/fixtures/db_definitions/schema.rb @@ -1,5 +1,15 @@ ActiveRecord::Schema.define do + # For Firebird, set the sequence values 10000 when create_table is called; + # this prevents primary key collisions between "normally" created records + # and fixture-based (YAML) records. + if adapter_name == "Firebird" + def create_table(*args, &block) + ActiveRecord::Base.connection.create_table(*args, &block) + ActiveRecord::Base.connection.execute "SET GENERATOR #{args.first}_seq TO 10000" + end + end + create_table :taggings, :force => true do |t| t.column :tag_id, :integer t.column :super_tag_id, :integer @@ -29,4 +39,4 @@ ActiveRecord::Schema.define do t.column :author_id, :integer t.column :favorite_author_id, :integer end -end
\ No newline at end of file +end diff --git a/activerecord/test/migration_test.rb b/activerecord/test/migration_test.rb index 0429f10e5b..9514eb369d 100644 --- a/activerecord/test/migration_test.rb +++ b/activerecord/test/migration_test.rb @@ -43,13 +43,16 @@ if ActiveRecord::Base.connection.supports_migrations? Person.connection.remove_column("people", "favorite_day") rescue nil Person.connection.remove_column("people", "male") rescue nil Person.connection.remove_column("people", "administrator") rescue nil + Person.connection.remove_column("people", "first_name") rescue nil + Person.connection.add_column("people", "first_name", :string, :limit => 40) Person.reset_column_information end def test_add_index - Person.connection.add_column "people", "last_name", :string + # Limit size of last_name and key columns to support Firebird index limitations + Person.connection.add_column "people", "last_name", :string, :limit => 100 + Person.connection.add_column "people", "key", :string, :limit => 100 Person.connection.add_column "people", "administrator", :boolean - Person.connection.add_column "people", "key", :string assert_nothing_raised { Person.connection.add_index("people", "last_name") } assert_nothing_raised { Person.connection.remove_index("people", "last_name") } @@ -58,8 +61,9 @@ if ActiveRecord::Base.connection.supports_migrations? assert_nothing_raised { Person.connection.remove_index("people", "last_name") } # quoting - assert_nothing_raised { Person.connection.add_index("people", ["key"], :name => "key", :unique => true) } - assert_nothing_raised { Person.connection.remove_index("people", :name => "key", :unique => true) } + # Note: changed index name from "key" to "key_idx" since "key" is a Firebird reserved word + assert_nothing_raised { Person.connection.add_index("people", ["key"], :name => "key_idx", :unique => true) } + assert_nothing_raised { Person.connection.remove_index("people", :name => "key_idx", :unique => true) } # Sybase adapter does not support indexes on :boolean columns unless current_adapter?(:SybaseAdapter) @@ -170,14 +174,14 @@ if ActiveRecord::Base.connection.supports_migrations? end def test_add_column_not_null_with_default - Person.connection.create_table :testings, :id => false do |t| + Person.connection.create_table :testings do |t| t.column :foo, :string end - Person.connection.execute "insert into testings (foo) values ('hello')" + Person.connection.execute "insert into testings values (1, 'hello')" assert_nothing_raised {Person.connection.add_column :testings, :bar, :string, :null => false, :default => "default" } assert_raises(ActiveRecord::StatementInvalid) do - Person.connection.execute "insert into testings (foo, bar) values ('hello', NULL)" + Person.connection.execute "insert into testings values (2, 'hello', NULL)" end ensure Person.connection.drop_table :testings rescue nil @@ -294,14 +298,8 @@ if ActiveRecord::Base.connection.supports_migrations? end ActiveRecord::Base.connection.rename_table :octopuses, :octopi - assert_nothing_raised do - if current_adapter?(:OracleAdapter) - # Oracle requires the explicit sequence value for the pk - ActiveRecord::Base.connection.execute "INSERT INTO octopi (id, url) VALUES (1, 'http://www.foreverflying.com/octopus-black7.jpg')" - else - ActiveRecord::Base.connection.execute "INSERT INTO octopi (url) VALUES ('http://www.foreverflying.com/octopus-black7.jpg')" - end - end + # Using explicit id in insert for compatibility across all databases + assert_nothing_raised { ActiveRecord::Base.connection.execute "INSERT INTO octopi VALUES (1, 'http://www.foreverflying.com/octopus-black7.jpg')" } assert_equal 'http://www.foreverflying.com/octopus-black7.jpg', ActiveRecord::Base.connection.select_value("SELECT url FROM octopi WHERE id=1") diff --git a/activerecord/test/migration_test_firebird.rb b/activerecord/test/migration_test_firebird.rb new file mode 100644 index 0000000000..9d4b80c839 --- /dev/null +++ b/activerecord/test/migration_test_firebird.rb @@ -0,0 +1,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 |