Module: MigrationValidators::Spec::Support::DB

Defined in:
lib/migration_validators/spec/support/db.rb

Instance Method Summary collapse

Instance Method Details

#chg_table(table_name = :test_table, &block) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/migration_validators/spec/support/db.rb', line 42

def chg_table table_name = :test_table, &block
  migrate do
    change_table(table_name) do |t|
      yield t
    end
  end

  table(table_name)
end

#dbObject



14
15
16
# File 'lib/migration_validators/spec/support/db.rb', line 14

def db
  ::ActiveRecord::Base.connection 
end

#migrate(&block) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/migration_validators/spec/support/db.rb', line 18

def migrate &block
  migration_class = Class.new(::ActiveRecord::Migration) do
    def self.up
      up_migrate 
    end
  end

  migration_class.class.instance_eval  do
    define_method :up_migrate, &block
  end

  migration_class.migrate(:up)
end

#new_table(table_name = :test_table, &block) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/migration_validators/spec/support/db.rb', line 32

def new_table table_name = :test_table, &block
  migrate do
    create_table(table_name) do |t|
      yield t
    end
  end

  table(table_name)
end

#table(table_name) ⇒ Object



52
53
54
# File 'lib/migration_validators/spec/support/db.rb', line 52

def table table_name
  MigrationValidators::Spec::Support::TableWrapper.new(table_name, db)
end

#use_db(config) ⇒ Object



5
6
7
8
# File 'lib/migration_validators/spec/support/db.rb', line 5

def use_db config
  ::ActiveRecord::Base.remove_connection if ::ActiveRecord::Base.connected?
  ::ActiveRecord::Base.establish_connection config
end

#use_memory_dbObject



10
11
12
# File 'lib/migration_validators/spec/support/db.rb', line 10

def use_memory_db
  use_db :adapter => "sqlite3", :database => ":memory:"
end