-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate.rb
66 lines (52 loc) · 2.54 KB
/
migrate.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
57
58
59
60
61
62
63
64
65
unless Object.const_defined? 'DB'
raise 'Not given a database to migrate' unless ENV.key? 'DATABASE_URL'
require 'sequel'
require 'logger'
DB = Sequel.connect ENV['DATABASE_URL']
DB.loggers << Logger.new(STDOUT) if ENV['RACK_ENV'] != 'production'
end
unless DB.table_exists?(:pages) && DB.table_exists?(:versions)
DB.create_table :pages do
primary_key :id
String :name, :null => false, :unique => true
constraint(:name_min_length) { length(name) > 0 }
DateTime :created_at, :null => false
index :created_at
end
DB.create_table :versions do
primary_key :id
foreign_key :page_id, :pages, :null => false
index :page_id
Fixnum :user_id, :null => false
index :user_id
String :title, :null => false
constraint(:title_min_length) { length(title) > 0 }
String :body, :null => false, :text => true
constraint(:body_min_length) { length(body) > 0 }
DateTime :created_at, :null => false
index :created_at
end
DB.alter_table :pages do
add_foreign_key :version_id, :versions
add_index :version_id
end
# Some test data
if ENV['RACK_ENV'] != 'production'
['home', 'api', 'ui', 'db'].each do |seed|
DB[:pages] << {:name => seed, :created_at => Time.now}
end
DB[:versions].multi_insert([
{:page_id => 1, :user_id => 1, :title => 'Home', :body => 'Welcome to the wiki homepage.', :created_at => Time.now},
{:page_id => 2, :user_id => 1, :title => 'API docs', :body => 'iframes all the way, man!', :created_at => Time.now},
{:page_id => 2, :user_id => 2, :title => 'API docs', :body => 'binary all the way, man!', :created_at => Time.now+1},
{:page_id => 2, :user_id => 1, :title => 'API docs', :body => 'RESTful all the way, man!', :created_at => Time.now+2},
{:page_id => 3, :user_id => 1, :title => 'UI spec', :body => 'It\'s a GUI interface in Visual Basic that will be capable of tracking an IP address.', :created_at => Time.now},
{:page_id => 3, :user_id => 2, :title => 'UI spec', :body => 'It\'s a GUI interface in Visual Basic that\'s capable of tracking an IP address.', :created_at => Time.now+1},
{:page_id => 4, :user_id => 3, :title => 'DB schema', :body => 'There\'s multiple tables', :created_at => Time.now},
])
DB[:pages].where(:id => 1).update(:version_id => 1)
DB[:pages].where(:id => 2).update(:version_id => 4)
DB[:pages].where(:id => 3).update(:version_id => 6)
DB[:pages].where(:id => 4).update(:version_id => 7)
end
end