Sha256: f42e4117c178fd5e9298ff6da8e2aba08c1df509d8d0a7bb425062cd0eae96df

Contents?: true

Size: 919 Bytes

Versions: 5

Compression:

Stored size: 919 Bytes

Contents

module Specjour
  module DbScrub
    load 'Rakefile'
    extend self

    def scrub
      connect_to_database
      if pending_migrations?
        puts "Migrating schema for database #{ENV['TEST_ENV_NUMBER']}..."
        Rake::Task['db:test:load'].invoke
      else
        purge_tables
      end
    end

    protected

    def connect_to_database
      connection
    rescue # assume the database doesn't exist
      Rake::Task['db:create'].invoke
    end

    def connection
      ActiveRecord::Base.connection
    end

    def purge_tables
      connection.disable_referential_integrity do
        tables_to_purge.each do |table|
          connection.delete "delete from #{table}"
        end
      end
    end

    def pending_migrations?
      ActiveRecord::Migrator.new(:up, 'db/migrate').pending_migrations.any?
    end

    def tables_to_purge
      connection.tables - ['schema_migrations']
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
specjour-0.2.5 lib/specjour/db_scrub.rb
specjour-0.2.4 lib/specjour/db_scrub.rb
specjour-0.2.3 lib/specjour/db_scrub.rb
specjour-0.2.2 lib/specjour/db_scrub.rb
specjour-0.2.1 lib/specjour/db_scrub.rb