Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: e4c84abdfc
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 89 lines (79 sloc) 2.498 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
require File.expand_path("#{File.dirname(__FILE__)}/../require")
Require.spec_helper!

Spec::Runner.configure do |config|
end

def db_type
  ENV['DB_TYPE'] ? ENV['DB_TYPE'] : 'mysql'
end

def article_match?(original, copy)
  copy.id.should == original.id
  copy.title.should == original.title
  copy.body.should == original.body
  if copy.respond_to?(:deleted_at)
    copy.deleted_at.strftime('%j%H%M').should == Time.now.utc.strftime('%j%H%M')
  end
end

def columns(table)
  connection.columns(table).collect(&:name)
end

def connection
  ActiveRecord::Base.connection
end

def create_records(klass=Article, values={})
  articles = []
  table = klass.table_name
  cols = columns(table)
  connection.execute("DELETE FROM #{table}")
  (1..5).collect do |x|
    vals = cols.collect do |c|
      if values.keys.include?(c.intern)
        values[c.intern] ? "'#{values[c.intern]}'" : "NULL"
      else
        case c.intern
        when :id; x
        when :deleted_at; 'NULL'
        else "'#{c.capitalize} #{x}'"
        end
      end
    end
    connection.execute(%{
INSERT INTO #{table} (#{cols.collect { |c| "#{connection.quote_column_name(c)}" }.join(', ')})
        VALUES (#{vals.join(', ')})
    })
    klass.find(x)
  end
end

def establish_test_db
  # Establish connection
  unless ActiveRecord::Base.connected?
    config = YAML::load(File.open("#{SPEC}/db/config/database.#{db_type}.yml"))
    ActiveRecord::Base.configurations = config
    ActiveRecord::Base.establish_connection(config['test'])
  end
  # Establish logger
  logger_file = File.open("#{SPEC}/db/log/test.log", 'a')
  logger_file.sync = true
  @logger = Logger.new(logger_file)
  ActiveRecord::Base.logger = @logger
  # The database should have only a simple articles table
  connection.execute("DROP TABLE IF EXISTS articles")
  connection.execute("DROP TABLE IF EXISTS archived_articles")
  connection.execute("DROP TABLE IF EXISTS schema_migrations")
  connection.create_table(:articles) do |t|
    t.string :title
    t.string :body
    t.boolean :read # break mysql w/o quotation
  end
  # Load the model
  load "#{SPEC}/db/models/article.rb"
end

def indexes
  Article.send(:archive_table_indexed_columns)
end

def migrate_up(directory='migrate')
  @old_article_columns = columns("articles")
  @old_archive_columns = columns("archived_articles")
  ActiveRecord::Migrator.migrate("#{SPEC}/db/#{directory}")
  @new_article_columns = columns("articles")
  @new_archive_columns = columns("archived_articles")
end
Something went wrong with that request. Please try again.