Sha256: c4a1237ca92838c95057c03828ad724cec704b2bacf64af5e33bd4653390d6bb

Contents?: true

Size: 957 Bytes

Versions: 3

Compression:

Stored size: 957 Bytes

Contents

require 'active_record'

class PGInitializer
  def self.initialize!
    config = {
      'adapter'   => 'postgresql',
      'encoding'  => 'unicode',
      'database'  => 'superstore_test',
      'pool'      => 5,
      'username' => 'postgres'
    }

    ActiveRecord::Base.configurations = { test: config }

    ActiveRecord::Tasks::DatabaseTasks.drop config
    ActiveRecord::Tasks::DatabaseTasks.create config
    ActiveRecord::Base.establish_connection config

    create_users_table
  end

  def self.create_users_table
    ActiveRecord::Migration.create_table :users do |t|
      t.string :special_id, null: false
      t.index :special_id, unique: true
    end
  end

  def self.table_names
    %w(users)
  end
end

PGInitializer.initialize!

module ActiveSupport
  class TestCase
    teardown do
      PGInitializer.table_names.each do |table_name|
        ActiveRecord::Base.connection.execute "TRUNCATE #{table_name}"
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
superstore-1.0.4 test/support/pg.rb
superstore-1.0.3 test/support/pg.rb
superstore-1.0.2 test/support/pg.rb