Class: SportDb::CreateDb

Inherits:
Object
  • Object
show all
Defined in:
lib/sportdb/schema.rb

Instance Method Summary collapse

Instance Method Details

#upObject



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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/sportdb/schema.rb', line 6

def up
  ActiveRecord::Schema.define do

create_table :teams do |t|
  t.string  :key,   null: false   # import/export key
  t.string  :title, null: false
  t.string  :title2
  t.string  :code     # make it not null?  - three letter code (short title)
  t.string  :synonyms  # comma separated list of synonyms
  t.references :country,   null: false
  t.references :city     # NB: city is optional (should be required for clubs e.g. non-national teams)
  t.boolean  :club,     null: false, default: false  # is it a club (not a national team)?

  t.integer :since     # founding year
  t.string  :address
  t.string  :web

  ### fix: remove and add virtual attribute in model instead
  t.boolean  :national, null: false, default: false  # is it a national selection team (not a club)?
  t.timestamps
end

add_index :teams, :key, unique: true


create_table :grounds do |t|
  t.string     :key,      null: false   # import/export key
  t.string     :title,    null: false 
  t.string     :synonyms   # comma separated list of synonyms
  
  t.references :country,  null: false
  t.references :city     # todo: make city required ???

  t.integer :since     # founding year
  t.integer :capacity  # attentence capacity e.g. 10_000 or 50_000 etc.
  t.string  :address


  ### fix/todo: add since/founded/opened/build attrib  eg. 2011 or 1987
  ##   - add capacity e.g. 40_000
  ##  fix: add address !!!! etc

  ## add region ??? or just use region from city ?? 

  t.timestamps
end

add_index :grounds, :key, unique: true


create_table :persons do |t|    # use people ? instead of persons (person/persons makes it easier?)
  t.string      :key,      null: false   # import/export key
  t.string      :name,     null: false
  t.string      :synonyms  # comma separated list of synonyms
  t.string      :code       # three letter code (short title)

  ## todo: add gender flag (male/female -man/lady  how?)
  t.date        :born_at     # optional date of birth (birthday)
  ## todo: add country of birth  might not be the same as nationality

  t.references  :city
  t.references  :region
  t.references  :country,   null: false

  t.references  :nationality, null: false  # by default assume same as country of birth (see above)

  t.timestamps
end

# join table: person+game(team1+team2+event(season+league))
create_table :goals do |t|
  t.references  :person,   null: false
  t.references  :game,     null: false
  t.integer   :minute
  t.integer   :offset    # e.g. 45' +3 or 90' +2
  t.integer   :score1
  t.integer   :score2
  
  ## type of goal (penalty, owngoal)
  t.boolean   :penalty,   null: false, default: false
  t.boolean   :owngoal,   null: false, default: false  # de: Eigentor -> # todo: find better name?

  t.timestamps
end


create_table :tracks do |t|    # e.g. Formula 1 circuits or Apline Ski resorts/slops/pistes
  t.string      :key,       null: false   # import/export key
  t.string      :title,     null: false
  t.string      :synonyms   # comma separated list of synonyms
  t.string      :code       # three letter code (short title)

  t.references  :city
  t.references  :region
  t.references  :country,   null: false
  t.timestamps
end


# join table -> event(season+league)+track
create_table :races do |t|     # e.g. Formula 1 race (Grand Prix Monaco) or Alpine Ski race (Downhill Lake Louise)
  t.references :track,    null: false
  t.references :event,    null: false
  t.integer    :pos,      null: false   # Race #1,#2,#3,#4 etc.

  ## todo: add auto-created key (for import/export) e.g.

  t.datetime   :start_at
  t.timestamps
end

create_table :runs do |t|
  t.references :race,     null: false
  t.integer    :pos,      null: false

  t.datetime   :start_at
  t.timestamps
end

# join table -> race+person or run+person
create_table :records do |t|   # use TimeRecord? instead of simple record
 t.references :race  # optional either race or run references
 t.references :run
 t.references :person,  null: false
 t.integer    :pos      # 1,2,3, etc or 0
 t.boolean    :completed,  null: false, default: true    # completed race - find better name?
 t.string     :state   # find a better name? e.g. retired, e.g.
 t.string     :comment  #   find a better name ?  e.g.  collision damage (if ret) for formula 1
 t.time       :time
 t.string     :timeline   # e.g. + 0:45.343   or +1 lap
 t.integer    :laps      # laps counter for formula1

 t.timestamps
end


# join table -> person+team+event(season+league)
create_table :rosters do |t|   # use squads as an alternative name? why? why not??
  t.references :person,  null: false
  t.references :team,    null: false
  t.references :event      # make required?
  t.integer    :pos,      null: false

  t.timestamps
end


create_table :events do |t|
  t.string      :key,      null: false   # import/export key
  t.references  :league,   null: false
  t.references  :season,   null: false
  t.date        :start_at, null: false    # NB: only use date (w/o time)
  t.date        :end_at   # make it required???  # NB: only use date (w/o time)
  t.boolean     :team3,    null: false, default: true   ## e.g. Champions League has no 3rd place (only 1st and 2nd/final)

  #### track 1-n sources (from repos)  - # todo move to its own table later
  ## NB: relative to event.yml - use mapper to "resolve" to full path w/ repo; use league+season keys
  t.string      :sources    #  e.g. cup or bl,bl_ii   # NB: for now store all in on string separated by comma
  t.string      :config  # e.g. cup or bl #  e.g assumes cup.yml, bl.yml etc. for now


  t.timestamps
end

add_index :events, :key, unique: true


create_table :rounds do |t|
  t.references :event,    null: false
  t.string     :title,    null: false
  t.string     :title2
  t.integer    :pos,      null: false
  ## add new table stage/stages for grouping rounds in group rounds and playoff rounds, for example???
  ## # "regular" season (group) games or post-season (playoff) knockouts (k.o's)
  t.boolean    :knockout, null: false, default: false
  t.date   :start_at, null: false     # NB: only use date (w/o time)
  t.date   :end_at    # todo: make it required e.g. :null => false    # NB: only use date (w/o time)

  ## auto-added flag (e.g. start_at n end_at dates got calculated)
  ##  if auto-added flag is false - do NOT auto-update start_at, end_at etc.
  t.boolean    :auto, null: false, default: true

  t.timestamps
end

add_index :rounds, :event_id  # fk event_id index


create_table :groups do |t|     # Teamgruppe (zB Gruppe A, Gruppe B, etc.)
  t.references :event,    null: false
  t.string     :title,    null: false
  t.integer    :pos,      null: false
  t.timestamps
end

add_index :groups, :event_id  # fk event_id index


create_table :games do |t|
  t.string     :key          # import/export key
  t.references :round,    null: false
  t.integer    :pos,      null: false
  t.references :group      # note: group is optional
  t.references :team1,    null: false
  t.references :team2,    null: false

  t.datetime   :play_at,   null: false
  t.boolean    :postponed, null: false, default: false
  t.datetime   :play_at_v2   # optional old date (when postponed)
  t.datetime   :play_at_v3   # optional odl date (when postponed twice)

  t.references :ground    # optional - stadium (lets you get city,region,country,etc)
  t.references :city      # optional - convenience for ground.city_id ???


  t.boolean    :knockout, null: false, default: false
  t.boolean    :home,     null: false, default: true    # is team1 play at home (that is, at its home stadium)
  t.integer    :score1
  t.integer    :score2
  t.integer    :score1et  # extratime - team 1 (opt)
  t.integer    :score2et  # extratime - team 2 (opt)
  t.integer    :score1p   # penalty  - team 1 (opt)
  t.integer    :score2p   # penalty  - team 2 (opt) elfmeter (opt)
  t.integer    :score1i   # half time / first third (opt)
  t.integer    :score2i   # half time - team 2
  t.integer    :score1ii  # second third (opt)
  t.integer    :score2ii  # second third - team2 (opt)
  t.references :next_game  # for hinspiel bei rueckspiel in knockout game
  t.references :prev_game

  t.integer    :winner      # 1,2,0,nil  calculate on save  - "real" winner (after 90 or extra time or penalty, aggregated first+second leg?)
  t.integer    :winner90    # 1,2,0,nil  calculate on save  - winner after 90 mins (or regugular play time depending on sport - add alias or find  a better name!) 

  t.timestamps
end

add_index :games, :key,  unique: true
add_index :games, :round_id      # fk round_id index
add_index :games, :group_id      # fk group_id index
add_index :games, :next_game_id  # fk next_game_id index
add_index :games, :prev_game_id  # fk next_game_id index



# todo: remove id from join table (without extra fields)? why?? why not??
create_table :events_teams do |t|
  t.references :event, null: false
  t.references :team,  null: false
  t.timestamps
end

add_index :events_teams, [:event_id,:team_id], unique: true
add_index :events_teams, :event_id


# todo: remove id from join table (without extra fields)? why?? why not??
create_table :events_grounds do |t|
  t.references :event,   null: false
  t.references :ground,  null: false
  t.timestamps
end

add_index :events_grounds, [:event_id,:ground_id], unique: true
add_index :events_grounds, :event_id



create_table :groups_teams do |t|
  t.references :group, null: false
  t.references :team,  null: false
  t.timestamps
end

add_index :groups_teams, [:group_id,:team_id], unique: true
add_index :groups_teams, :group_id


### todo: add models and some seed data

create_table :seasons do |t|  ## also used for years
  t.string :key,   null: false
  t.string :title, null: false   # e.g. 2011/12, 2012/13 ### what to do w/ 2012? for world cup etc?
  t.timestamps
end

create_table :leagues do |t|  ## also for cups/conferences/tournaments/world series/etc.
  t.string     :key,   null: false
  t.string     :title, null: false     # e.g. Premier League, Deutsche Bundesliga, World Cup, Champions League, etc.
  t.references :country   ##  optional for now ,   :null => false   ### todo: create "virtual" country for international leagues e.g. use int? or world (ww?)/europe (eu)/etc. similar? already taken??
 
  ## fix: rename to :clubs from :club
  t.boolean    :club,          null: false, default: false  # club teams or national teams?
  ## todo: add t.boolean  :national flag? for national teams?
  ## t.boolean  :international, :null => false, :default => false  # national league or international?
  ## t.boolean  :cup     ## or regular season league??
  t.timestamps
end

create_table :badges do |t|
  t.references  :team,  null: false
  ## todo/fix: use event insead of league+season ??
  ## t.references  :event, :null => false   # event => league+season
  t.references  :league, null: false
  t.references  :season, null: false
  t.string      :title,  null: false   # Meister, Weltmeister, Europameister, Cupsieger, Vize-Meister, Aufsteiger, Absteiger, etc.
  t.timestamps
end

  end  # Schema.define
end