Module: MigrationTestHelper

Defined in:
lib/migration_test_helper.rb

Defined Under Namespace

Modules: Connection Classes: Schema, Table

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.migration_dirObject



27
28
29
# File 'lib/migration_test_helper.rb', line 27

def self.migration_dir
  @migration_dir || File.expand_path(RAILS_ROOT + '/db/migrate')
end

.migration_dir=(new_dir) ⇒ Object

sets the directory in which the migrations reside to be run with migrate



34
35
36
# File 'lib/migration_test_helper.rb', line 34

def self.migration_dir=(new_dir)
  @migration_dir = new_dir
end

Instance Method Details

#assert_schema {|schema| ... } ⇒ Object

verifies the schema exactly matches the one specified (schema_info does not have to be specified)

assert_schema do |s|
  s.table :dogs do |t|
    t.column :id,   :integer, :default => 2
    t.column :name, :string
  end
end

Yields:

  • (schema)


48
49
50
51
52
# File 'lib/migration_test_helper.rb', line 48

def assert_schema
  schema = Schema.new
  yield schema
  schema.verify
end

#assert_table(name) {|table| ... } ⇒ Object

verifies a single table exactly matches the one specified

assert_table :dogs do |t|
  t.column :id,   :integer, :default => 2
  t.column :name, :string
end

Yields:

  • (table)


62
63
64
65
66
# File 'lib/migration_test_helper.rb', line 62

def assert_table(name)
  table = Table.new(name)
  yield table
  table.verify
end

#drop_all_tablesObject

drops all tables in the database



71
72
73
74
75
# File 'lib/migration_test_helper.rb', line 71

def drop_all_tables
  ActiveRecord::Base.connection.tables.each do |table|
    ActiveRecord::Base.connection.drop_table(table)
  end
end

#migrate(opts = {}) ⇒ Object

executes your migrations

migrate                # same as rake db:migrate

Options are:

  • :version - version to migrate to (same as VERSION=.. option to rake db:migrate)

  • :verbose - print migration status messages to STDOUT, defaults to false



86
87
88
89
90
91
92
93
# File 'lib/migration_test_helper.rb', line 86

def migrate(opts={})
  old_verbose = ActiveRecord::Migration.verbose
  ActiveRecord::Migration.verbose = opts[:verbose].nil? ? false : opts[:verbose]
  version = opts[:version] ? opts[:version].to_i : nil
  ActiveRecord::Migrator.migrate(MigrationTestHelper.migration_dir, version)
ensure
  ActiveRecord::Migration.verbose = old_verbose
end