Sha256: a9545f1774c266fc52e2de9fc51e10c7a355ea1861c4463f51462f7301da0949

Contents?: true

Size: 847 Bytes

Versions: 7

Compression:

Stored size: 847 Bytes

Contents

module Specjour
  module DbScrub
    load 'Rakefile'
    extend self

    def scrub
      connect_to_database
      if pending_migrations?
        Rake::Task['db:schema: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

7 entries across 7 versions & 1 rubygems

Version Path
specjour-0.1.15 lib/specjour/db_scrub.rb
specjour-0.1.14 lib/specjour/db_scrub.rb
specjour-0.1.13 lib/specjour/db_scrub.rb
specjour-0.1.12 lib/specjour/db_scrub.rb
specjour-0.1.11 lib/specjour/db_scrub.rb
specjour-0.1.10 lib/specjour/db_scrub.rb
specjour-0.1.9 lib/specjour/db_scrub.rb