Sha256: f597c67f125e0e47f72241a82dffa4b5f259491e74c2364fbfe9e25bd42b80a6

Contents?: true

Size: 1007 Bytes

Versions: 16

Compression:

Stored size: 1007 Bytes

Contents

module DatabaseMacros
  # Run migrations in the test database
  def run_migration(&block)
    # Create a new migration class
    klass = Class.new(ActiveRecord::Migration)

    # Create a new `up` that executes the argument
    klass.send(:define_method, :up) { self.instance_exec(&block) }

    # Create a new instance of it and execute its `up` method
    klass.new.up
  end

  def self.database_file
    @database_file || File.expand_path('../test.db', __FILE__)
  end

  def setup_database
    # Make sure the test database file is gone
    cleanup_database

    # Establish the connection
    SQLite3::Database.new FileUtils.touch(DatabaseMacros.database_file).first
    ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: DatabaseMacros.database_file)

    # Silence everything
    ActiveRecord::Base.logger = ActiveRecord::Migration.verbose = false
  end

  def cleanup_database
    FileUtils.rm(DatabaseMacros.database_file) if File.exists?(DatabaseMacros.database_file)
  end
end

Version data entries

16 entries across 16 versions & 4 rubygems

Version Path
activerecord_strict_validations-0.3.1 spec/support/macros/database_macros.rb
activerecord_strict_validations-0.3 spec/support/macros/database_macros.rb
microscope-0.4.2 spec/support/macros/database_macros.rb
activerecord_mysql_strict-0.2.1 spec/support/macros/database_macros.rb
encore-0.0.3 spec/support/macros/database_macros.rb
encore-0.0.2 spec/support/macros/database_macros.rb
microscope-0.4.1 spec/support/macros/database_macros.rb
microscope-0.4 spec/support/macros/database_macros.rb
microscope-0.3 spec/support/macros/database_macros.rb
activerecord_mysql_strict-0.2 spec/support/macros/database_macros.rb
activerecord_mysql_strict-0.1.1 spec/support/macros/database_macros.rb
activerecord_mysql_strict-0.1 spec/support/macros/database_macros.rb
microscope-0.2 spec/support/macros/database_macros.rb
microscope-0.1.1 spec/support/macros/database_macros.rb
microscope-0.1 spec/support/macros/database_macros.rb
microscope-0.0.1 spec/support/macros/database_macros.rb