Sha256: 0c24ad93252aeda256e3ed7935f01d00592298bc2dad89508d3275674606cc33

Contents?: true

Size: 1.17 KB

Versions: 5

Compression:

Stored size: 1.17 KB

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

  # Run the built-in migration
  def run_default_migration
    load File.expand_path('../../../../lib/generators/emotions/templates/migration.rb', __FILE__)
    AddEmotions.new.up
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
emotions-0.3 spec/support/macros/database_macros.rb
emotions-0.2.2 spec/support/macros/database_macros.rb
emotions-0.2.1 spec/support/macros/database_macros.rb
emotions-0.2 spec/support/macros/database_macros.rb
emotions-0.1.7 spec/support/macros/database_macros.rb