Sha256: 1f445c2921eec2eec7bbe29444411f332882c0b88c7d7a3a1d20fdb53417035a

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

Contents

require 'rubygems'
require 'bundler/setup'

ENV['RACK_ENV'] = 'test'

require 'slavery'

ActiveRecord::Base.configurations = {
  'test'            =>  { 'adapter' => 'sqlite3', 'database' => 'test_db' },
  'test_slave'      =>  { 'adapter' => 'sqlite3', 'database' => 'test_slave_db' },
  'test_slave_url'  =>  'postgres://root:@localhost:5432/test_slave'
}

# Prepare databases
class User < ActiveRecord::Base
  has_many :items
end

class Item < ActiveRecord::Base
  belongs_to :user
end

class Seeder
  def run
    # Populate on master
    connect(:test)
    create_tables
    User.create
    User.create
    User.first.items.create

    # Populate on slave, emulating replication lag
    connect(:test_slave)
    create_tables
    User.create

    # Reconnect to master
    connect(:test)
  end

  def create_tables
    ActiveRecord::Base.connection.create_table :users, force: true
    ActiveRecord::Base.connection.create_table :items, force: true do |t|
      t.references :user
    end
  end

  def connect(env)
    ActiveRecord::Base.establish_connection(env)
  end
end

Seeder.new.run

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
slavery-2.1.1 spec/spec_helper.rb