-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema_sequel.rb
executable file
·146 lines (120 loc) · 3.44 KB
/
schema_sequel.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env ruby
require 'sequel'
require 'fileutils'
require 'yaml'
require 'time'
require 'mysql2'
## WARNING: THIS DELETES ALL DATA AND RE-CREATES (empty) TABLES!
## WARNING2: IF YOU REALLY WANT TO START OVER, BE SURE YOU
## RUN MIGRATIONS AS WELL AFTER RUNNING THIS FILE IN
## ORDER TO GET ALL SCHEMA CHANGES.
## (see migrations/001_add_test.rb for instructions)
basedir = File.dirname(__FILE__)
config = YAML.load_file("#{basedir}/config.yml")
if config['dbtype'] == 'mysql'
dbname = config['mysql_url'].split('/').last
_DB = Sequel.connect(config['mysql_url'])
_DB.run "drop database if exists #{dbname};"
_DB.run "create database #{dbname};"
_DB.run "use #{dbname};"
elsif config['dbtype'] == 'sqlite'
dbfile = "#{basedir}/#{config['dbname']}.sqlite3"
if File.exists? dbfile
FileUtils.rm dbfile
end
#DB = Sequel.sqlite(dbfile)
end
require_relative './db_init'
# FIXME - add null/not null, unique etc., constraints throughout
DB.create_table! :resources do
primary_key :id
String :ah_id, {:null => false, :unique => true}
String :title
String :dataprovider
String :species
Integer :taxonomyid
String :genome
String :description
TrueClass :coordinate_1_based
String :maintainer
Integer :status_id
Integer :location_prefix_id
Integer :recipe_id
String :rdataversion
Date :rdatadateadded # FIXME add index here!
Date :rdatadateremoved
end
# add users table, info about who uploaded what and when,
#... permissions.
DB.create_table! :rdatapaths do
primary_key :id
String :rdatapath
String :rdataclass
Integer :rdatasize
String :rdatamd5
Date :rdatalastmodifieddate
foreign_key :resource_id, :resources
end
DB.create_table! :input_sources do
primary_key :id
String :sourcefile
String :sourcesize # integer?
String :sourceurl
String :sourceversion
String :sourcemd5
Date :sourcelastmodifieddate
foreign_key :resource_id, :resources
end
DB.create_table! :tags do
primary_key :id
String :tag, :size => 400
foreign_key :resource_id, :resources
end
DB.create_table! :biocversions do
primary_key :id
String :biocversion
foreign_key :resource_id, :resources
end
DB.create_table! :recipes do
primary_key :id
String :recipe
String :package
end
DB.create_table! :statuses do
primary_key :id
String :status
end
DB[:statuses].insert(:status => "Unreviewed")
DB[:statuses].insert(:status => "Public")
DB[:statuses].insert(:status => "Private")
DB.create_table! :location_prefixes do
primary_key :id
String :location_prefix
end
DB.create_table! :timestamp do
DateTime :timestamp
end
DB[:timestamp].insert(:timestamp => Time.now)
# triggers
for table in DB.tables
next if table == :timestamp
for op in ['insert', 'update', 'delete']
trigger = nil
if config['dbtype'] == 'sqlite'
trigger=<<-"EOT"
create trigger #{table}_#{op} after #{op} on #{table}
begin
update timestamp set timestamp
= datetime('now', 'localtime');
end;
EOT
elsif config['dbtype'] == 'mysql'
trigger=<<-"EOT"
create trigger #{table}_#{op} after #{op}
on #{table} for each row
update timestamp set timestamp = current_timestamp();
EOT
end
DB.run trigger
end
end