-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigrate.rb
49 lines (38 loc) · 1.31 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
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? :repos
DB.create_table :repos do
primary_key :id
Fixnum :user_id, :null => false
String :json, :null => false, :text => true
DateTime :created_at, :null => false
index :created_at
DateTime :updated_at
end
DB.create_table :apps do
primary_key :id
foreign_key :repo_id, :repos, :null => false
index :repo_id
Fixnum :user_id, :null => false
String :json, :null => false, :text => true
String :name, :null => false
DateTime :created_at, :null => false
index :created_at
DateTime :updated_at
end
# Install the origin market
require 'open-uri'
require 'json'
data = open('http://protonet-apps.github.com/apps.json').read
DB[:repos] << {:user_id => 0, :json => data, :created_at => Time.now}
# If we're in production, assume we're already fully installed (for now)
if ENV['RACK_ENV'] == 'production'
data = open('./manifest.json').read
DB[:apps] << {:repo_id => 1, :user_id => 0, :json => data, :name => 'market', :created_at => Time.now}
end
end