Sha256: 27c899c3407f9bda8e8b3233418b6cf8a071ed299fdc31e3681fd67795c18c91

Contents?: true

Size: 924 Bytes

Versions: 3

Compression:

Stored size: 924 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'] || 1}..."
        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

3 entries across 3 versions & 1 rubygems

Version Path
specjour-0.2.0 lib/specjour/db_scrub.rb
specjour-0.1.18 lib/specjour/db_scrub.rb
specjour-0.1.17 lib/specjour/db_scrub.rb