Sha256: 85b9bc386fae5c7ca70aa7922107cd9c3176237ccea709a8e139af5e0d6e00e1

Contents?: true

Size: 1.91 KB

Versions: 12

Compression:

Stored size: 1.91 KB

Contents

require File.expand_path(File.dirname(__FILE__) + '/spec_helper')

describe ActiveRecord::Schema do

  let(:schema) { ActiveRecord::Schema }

  let(:connection) { ActiveRecord::Base.connection }

  context "defining with auto_index and auto_create" do

    around(:each) do |example|
      with_auto_index do
        with_auto_create do
          example.run
        end
      end
    end

    it "should pass" do
      expect { do_schema }.to_not raise_error
    end

    it "should create only explicity added indexes" do
      do_schema
      expected = SchemaDev::Rspec::Helpers.mysql? ? 2 : 1
      expect(connection.tables.collect { |table| connection.indexes(table) }.flatten.size).to eq(expected)
    end

    it "should create only explicity added foriegn keys" do
      do_schema
      expect(connection.tables.collect { |table| connection.foreign_keys(table) }.flatten.size).to eq(2)
    end

  end

  protected
  def do_schema
    define_schema do

      create_table :users, :force => true do
      end

      create_table :colors, :force => true do
      end

      create_table :shoes, :force => true do
      end

      create_table :posts, :force => true do |t|
        t.integer :user_id, :references => :users, :index => true
        t.integer :shoe_id, :references => :shoes   # should not have an index (except mysql)
        t.integer :color_id   # should not have a foreign key nor index
      end
    end
  end

  def with_auto_index(value = true)
    old_value = SchemaPlus.config.foreign_keys.auto_index
    SchemaPlus.config.foreign_keys.auto_index = value
    begin
      yield
    ensure
      SchemaPlus.config.foreign_keys.auto_index = old_value
    end
  end

  def with_auto_create(value = true)
    old_value = SchemaPlus.config.foreign_keys.auto_create
    SchemaPlus.config.foreign_keys.auto_create = value
    begin
      yield
    ensure
      SchemaPlus.config.foreign_keys.auto_create = old_value
    end
  end

end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
schema_plus-1.8.9 spec/schema_spec.rb
schema_plus-1.8.8 spec/schema_spec.rb
schema_plus-1.8.7 spec/schema_spec.rb
schema_plus-1.8.6 spec/schema_spec.rb
schema_plus-2.0.0.pre1 spec/schema_spec.rb
schema_plus-1.8.5 spec/schema_spec.rb
schema_plus-1.8.4 spec/schema_spec.rb
schema_plus-1.8.3 spec/schema_spec.rb
schema_plus-1.8.2 spec/schema_spec.rb
schema_plus-1.8.1 spec/schema_spec.rb
schema_plus-1.8.0 spec/schema_spec.rb
schema_plus-1.7.1 spec/schema_spec.rb