-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathsetup.rb
56 lines (48 loc) · 1.23 KB
/
setup.rb
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
require 'rubygems'
require 'bundler/setup'
require 'active_record'
require 'active_record/migration'
require 'benchmark'
require 'acts_as_sane_tree'
require 'minitest/autorun'
ActiveRecord::Base.establish_connection(
:adapter => 'postgresql',
:database => 'tree_test',
:username => 'postgres'
)
class Node < ActiveRecord::Base
acts_as_sane_tree
validates_uniqueness_of :name
validates_uniqueness_of :parent_id, :scope => :id
end
class NodeSetup < ActiveRecord::Migration
class << self
def up
create_table :nodes do |t|
t.text :name
t.integer :parent_id
end
add_index :nodes, [:parent_id, :id], :unique => true
end
end
end
# Quick and dirty database scrubbing
if(Node.table_exists?)
ActiveRecord::Base.connection.execute "drop schema public cascade"
ActiveRecord::Base.connection.execute "create schema public"
end
NodeSetup.up
# Create three root nodes with 50 descendants
# Descendants should branch randomly
nodes = []
3.times do |i|
nodes[i] = []
parent = Node.create(:name => "root_#{i}")
50.times do |j|
node = Node.new(:name => "node_#{i}_#{j}")
_parent = nodes[i][rand(nodes[i].size)] || parent
node.parent_id = _parent.id
node.save
nodes[i] << node
end
end