diff options
author | Jamis Buck <jamis@37signals.com> | 2005-09-23 13:29:33 +0000 |
---|---|---|
committer | Jamis Buck <jamis@37signals.com> | 2005-09-23 13:29:33 +0000 |
commit | 7dc45818dc43c163700efc9896a0f3feafa31138 (patch) | |
tree | b9a263bfd2416bfbc9843b563dcc0e2565f2f877 /activerecord/test | |
parent | 436d54c3f19fc80713d975482a28a95cfa60a1e4 (diff) | |
download | rails-7dc45818dc43c163700efc9896a0f3feafa31138.tar.gz rails-7dc45818dc43c163700efc9896a0f3feafa31138.tar.bz2 rails-7dc45818dc43c163700efc9896a0f3feafa31138.zip |
Add ActiveRecord::SchemaDumper for dumping a DB schema to a pure-ruby file, making it easier to consolidate large migration lists and port database schemas between databases.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2312 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/test')
-rw-r--r-- | activerecord/test/adapter_test.rb | 38 | ||||
-rw-r--r-- | activerecord/test/ar_schema_test.rb | 31 | ||||
-rw-r--r-- | activerecord/test/migration_test.rb | 4 | ||||
-rw-r--r-- | activerecord/test/schema_dumper_test.rb | 19 |
4 files changed, 92 insertions, 0 deletions
diff --git a/activerecord/test/adapter_test.rb b/activerecord/test/adapter_test.rb new file mode 100644 index 0000000000..4496da8585 --- /dev/null +++ b/activerecord/test/adapter_test.rb @@ -0,0 +1,38 @@ +require 'abstract_unit' + +class AdapterTest < Test::Unit::TestCase + def setup + @connection = ActiveRecord::Base.connection + end + + def test_tables + if @connection.respond_to?(:tables) + tables = @connection.tables + assert tables.include?("accounts") + assert tables.include?("authors") + assert tables.include?("tasks") + assert tables.include?("topics") + else + warn "#{@connection.class} does not respond to #tables" + end + end + + def test_indexes + if @connection.respond_to?(:indexes) + indexes = @connection.indexes("accounts") + assert indexes.empty? + + @connection.add_index :accounts, :firm_id + indexes = @connection.indexes("accounts") + assert_equal "accounts", indexes.first.table + assert_equal "accounts_firm_id_index", indexes.first.name + assert !indexes.first.unique + assert_equal ["firm_id"], indexes.first.columns + else + warn "#{@connection.class} does not respond to #indexes" + end + + ensure + @connection.remove_index :accounts, :firm_id rescue nil + end +end diff --git a/activerecord/test/ar_schema_test.rb b/activerecord/test/ar_schema_test.rb new file mode 100644 index 0000000000..2b7c0c3fc7 --- /dev/null +++ b/activerecord/test/ar_schema_test.rb @@ -0,0 +1,31 @@ +require 'abstract_unit' +require "#{File.dirname(__FILE__)}/../lib/active_record/schema" + +if ActiveRecord::Base.connection.supports_migrations? + + class ActiveRecordSchemaTest < Test::Unit::TestCase + def setup + @connection = ActiveRecord::Base.connection + end + + def teardown + @connection.drop_table :fruits rescue nil + end + + def test_schema_define + ActiveRecord::Schema.define(:version => 7) do + create_table :fruits do |t| + t.column :color, :string + t.column :size, :string + t.column :texture, :string + t.column :flavor, :string + end + end + + assert_nothing_raised { @connection.select_all "SELECT * FROM fruits" } + assert_nothing_raised { @connection.select_all "SELECT * FROM schema_info" } + assert_equal 7, @connection.select_one("SELECT version FROM schema_info")['version'].to_i + end + end + +end diff --git a/activerecord/test/migration_test.rb b/activerecord/test/migration_test.rb index f98986cdfa..fa08cf4cf3 100644 --- a/activerecord/test/migration_test.rb +++ b/activerecord/test/migration_test.rb @@ -32,12 +32,16 @@ if ActiveRecord::Base.connection.supports_migrations? def test_add_index Person.connection.add_column "people", "last_name", :string + Person.connection.add_column "people", "administrator", :boolean assert_nothing_raised { Person.connection.add_index("people", "last_name") } assert_nothing_raised { Person.connection.remove_index("people", "last_name") } assert_nothing_raised { Person.connection.add_index("people", ["last_name", "first_name"]) } assert_nothing_raised { Person.connection.remove_index("people", "last_name") } + + assert_nothing_raised { Person.connection.add_index("people", %w(last_name first_name administrator), :name => "named_admin") } + assert_nothing_raised { Person.connection.remove_index("people", :name => "named_admin") } end def test_create_table_adds_id diff --git a/activerecord/test/schema_dumper_test.rb b/activerecord/test/schema_dumper_test.rb new file mode 100644 index 0000000000..e24724c9f8 --- /dev/null +++ b/activerecord/test/schema_dumper_test.rb @@ -0,0 +1,19 @@ +require 'abstract_unit' +require "#{File.dirname(__FILE__)}/../lib/active_record/schema_dumper" +require 'stringio' + +if ActiveRecord::Base.connection.respond_to?(:tables) + + class SchemaDumperTest < Test::Unit::TestCase + def test_schema_dump + stream = StringIO.new + ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream) + output = stream.string + + assert_match %r{create_table "accounts"}, output + assert_match %r{create_table "authors"}, output + assert_no_match %r{create_table "schema_info"}, output + end + end + +end |