Class: SportDb::CreateDb

Inherits:
Object
  • Object
show all
Defined in:
lib/sportdb/models/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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# File 'lib/sportdb/models/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  :name,  null: false   # "canonical" unique name

  t.string  :code       #  three letter code (short name)
  t.string  :alt_names  # comma separated list of alt names / synonyms

  t.references :country,   null: false, index: false
  t.references :city,                   index: false # note: city is optional (should be required for clubs e.g. non-national teams)
  t.references :district,               index: false # note: optional district (in city)

  ##  todo/check/fix: use team type or such e.g. club/national/etc. - why? why not?
  ###   or remove and add virtual attribute in model instead - why? why not?
  t.boolean  :club,     null: false, default: false  # is it a club (not a national team)?
  t.boolean  :national, null: false, default: false  # is it a national selection team (not a club)?

  ## todo/fix:  add team reference for a and b team!!!!


  t.integer :start_year     # founding year  -fix change to start_year / founded - why? why not?
  t.integer :end_year
  ## add more? - start_year2, end_year2 - why? why not?
  #  e.g. founded   = 1946, 2013 (refounded)
  #       dissolved = 1997

  t.string  :address
  t.string  :web

  ## todo/fix: change to gov / governing body or such!!!
  t.references :assoc,   index: false   # optional: national football assoc(iation), for example - used for national teams

  t.string      :comments

  t.timestamps
end

add_index :teams, :key, unique: true


###########
# check: use table (rename to) venues / stadiums - why? why not?
create_table :grounds do |t|
  t.string     :key,      null: false   # import/export key
  t.string     :name,     null: false
  t.string     :alt_names    # comma separated list of alt_names / synonyms

  t.references :country,  null: false,    index: false
  t.references :city,                     index: false # todo: make city required ???
  t.references :district,                 index: false # note: optional district (in city)

  t.integer :start_year     # 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


# join table: person+match(team1+team2+event(season+league))
create_table :goals do |t|
  t.references  :person,   null: false,  index: false
  t.references  :match,    null: false,  index: false
  t.references  :team,     null: false,  index: false     ##  use integer instead w/ values 1 or 2  for team1 or team2 ?? why? why not?

  t.integer   :minute
  t.integer   :offset,  null: false, default: 0    # 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


# join table -> person+team+event(season+league)
create_table :lineups do |t|   # use squads as an alternative name? why? why not??
  t.references :person,  null: false,   index: false
  t.references :team,    null: false,   index: false
  t.references :event               ,   index: false # make required?
  t.integer    :num,     # optional - jersey (t-shirt) number

  t.timestamps
end


create_table :events do |t|
  t.string      :key,        null: false   # import/export key
  t.references  :league,     null: false, index: false
  t.references  :season,     null: false, index: false
  t.date        :start_date, null: false    # note: only use date (w/o time)
  t.date        :end_date                   # note: only use date (w/o time)

  t.integer     :num       ## optional series counter e.g. World Cup No. 2, Bundesliga No. 43 etc. etc.

  ## t.boolean     :team3,    null: false, default: true   ## e.g. Champions League has no 3rd place (only 1st and 2nd/final)
  ## todo: add league/cup flag/flags  or to league itself?
  ##   or add add a tournament type field  - why? why not?

  ## 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


  #### 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, index: false  ## Note: do NOT auto-add index
  t.string     :name,     null: false
  t.integer    :pos,      null: false   ## use only for "internal" sort order (defaults to insertion order)

  t.integer    :num       ## optional match day/week number
  t.string     :key       ## optional match day/week number key (as string)

  ## add new table stage/stages for grouping rounds in group rounds and playoff rounds, for example???
  ## # "regular" season (group) matches or post-season (playoff) knockouts (k.o's)
  t.boolean    :knockout, null: false, default: false
  ## todo: add leg  (e.g. leg1, leg2, etc. why? why not?)
  t.date   :start_date   # note: only use date (w/o time)  - fix: change to start_date!!!
  t.date   :end_date     # note: only use date (w/o time)  - fix: change to end_date!!!
  t.date   :start_date2  # note: only use date (w/o time)  - fix: change to start_date!!!
  t.date   :end_date2    # note: only use date (w/o time)  - fix: change to end_date!!!

  ## add last_date/first-date(auto) - for "real" last and first dates - auto-added - why? why not?

  ## 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, index: false    ## Note: do NOT auto-add index
  t.string     :name,     null: false
  t.integer    :pos,      null: false       ## use only for "internal" sort order (defaults to insertion order)

  t.string     :key       ## optional group key e.g. A, B, C or 1, 2, etc. - use why? why not?
  t.timestamps
end

add_index :groups, :event_id  # fk event_id index


create_table :stages do |t|     # e.g. regular season, champions round, etc.
  t.references :event,    null: false, index: false    ## Note: do NOT auto-add index
  t.string     :name,     null: false
  ## todo/check: add pos for use only for "internal" sort order (defaults to insertion order)??
  t.timestamps
end

add_index :stages, :event_id  # fk event_id index



create_table :matches do |t|
  t.string     :key          # import/export key
  t.references :event,    null: false, index: false
  t.integer    :pos,      null: false       ## note: use only for "internal" sort order (defaults to insertion order)
  t.integer    :num       ## optional - "event global" match number e.g. World Cup - Match 1, Match 2, etc.
  t.references :team1,    null: false, index: false   ## Note: do NOT auto-add index
  t.references :team2,    null: false, index: false   ## Note: do NOT auto-add index

  t.references :round,                 index: false   ## Note: do NOT auto-add index
  t.references :group,                 index: false   ## Note: do NOT auto-add index  -- group is optional
  t.references :stage,                 index: false     # optional - regular seasion / champions round etc.

  ## "inline" helper keys auto-populate for easier "no-join/single-table" queries
  t.string    :team1_key
  t.string    :team2_key
  t.string    :event_key
  t.string    :round_key
  t.integer   :round_num   ## e.g. 1,2,3 for match day/match week
  t.string    :group_key
  t.string    :stage_key


  t.date      :date      # optional play date  - todo/fix: split into play_date AND play_time!!!
  ## note:  activerecord(rails) will auto-add date (2000-01-01) to hours/time!!!
  ##   work around - save a string type!!!!
  ##  e.g.  19:00 etc.
  ## t.time      :time
  t.string    :time

  
  t.boolean    :postponed, null: false, default: false
  ## t.date   :date2   # optional old date (when postponed)
  ## t.date   :date3   # optional old date (when postponed twice)

  t.string    :status     ## optional match status  - note: uses UPCASE string constants for now
                          ## e.g. CANCELLED / ABANDONED / REPLAY / AWARDED / POSTPONED / etc.


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

  ## change home to neutral - why? why not?
  t.boolean    :home,     null: false, default: true    # is team1 play at home or neutral (that is, at its home stadium)
  t.boolean    :knockout, null: false, default: false

  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_match, index: false   ## Note: do NOT auto-add index  -- for hinspiel bei rueckspiel in knockout match
  t.references :prev_match, index: false   ## Note: do NOT auto-add index

  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.string     :comments

  t.timestamps
end

add_index :matches, :key,  unique: true
add_index :matches, :event_id      # fk event_id index
add_index :matches, :round_id      # fk round_id index
add_index :matches, :group_id      # fk group_id index
add_index :matches, :next_match_id  # fk next_match_id index
add_index :matches, :prev_match_id  # fk next_match_id index
add_index :matches, :team1_id
add_index :matches, :team2_id


# todo: remove id from join table (without extra fields)? why?? why not??
create_table :events_teams do |t|
  t.references :event, null: false, index: false    ## Note: do NOT auto-add index
  t.references :team,  null: false, index: false    ## Note: do NOT auto-add index
  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 :stages_teams do |t|
  t.references :stage, null: false, index: false    ## Note: do NOT auto-add index
  t.references :team,  null: false, index: false    ## Note: do NOT auto-add index
  t.timestamps
end

add_index :stages_teams, [:stage_id, :team_id], unique: true
add_index :stages_teams, :stage_id



# todo: remove id from join table (without extra fields)? why?? why not??
create_table :events_grounds do |t|
  t.references :event,   null: false, index: false    ## Note: do NOT auto-add index
  t.references :ground,  null: false, index: false    ## Note: do NOT auto-add index
  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, index: false    ## Note: do NOT auto-add index
  t.references :team,  null: false, index: false    ## Note: do NOT auto-add index
  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 - add a boolean year true/false flag too - why? why not?
  t.string :key,   null: false
  t.string :name,  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     :name,  null: false     # e.g. Premier League, Deutsche Bundesliga, World Cup, Champions League, etc.
  t.string     :alt_names  # comma separated list of alt names / synonyms

  t.references :country,       index: false   ##  optional for now    ### 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 - why? why not?
  ## fix: rename to :intl  from :international - why? why not? shorter? better?
  ## todo/check:  flip clup to league flag? why? why not?
  t.boolean    :clubs,    null: false, default: false  # club teams or national teams?
  t.boolean    :intl,     null: false, default: false  # national league or international?
  t.boolean    :cup,      null: false, default: false  ## or regular season league?? use a tournament type field with enums - why? why not?

  t.integer    :level       ## use tier?  e.g. level 1, level 2, etc.

  t.integer    :start_year
  t.integer    :end_year

  ## todo: add t.boolean  :national flag? for national teams?
  t.timestamps
end


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


create_table :assocs do |t|
  t.string     :key,   null: false
  t.string     :name,  null: false     # e.g. Premier League, Deutsche Bundesliga, World Cup, Champions League, etc.

  t.integer    :start_year     # founding year
  t.string     :web

  ### if national assoc - has (optional) country ref
  t.references :country,       index: false   # note: optional - only used/set (required) for national assocs (or subnational too?)
  t.boolean    :national,    null: false, default: false

  ## add :world flag for FIFA? - just check if parent is null? for root assoc(s)? why? why not?
  ## add :regional flag for continental subdivision?
  ##  todo: shorten to contl and intercontl  - why? why not?
  t.boolean :continental,      null: false, default: false
  t.boolean :intercontinental, null: false, default: false  # e.g. arab football league (africa+western asia/middle east)
  t.timestamps
end

add_index :assocs, :key, unique: true



create_table :assocs_assocs do |t|
  t.references :assoc1, null: false, index: false   ## Note: do NOT auto-add index  -- parent assoc
  t.references :assoc2, null: false, index: false   ## Note: do NOT auto-add index  -- child assoc is_member_of parent assoc
  t.timestamps
end

add_index :assocs_assocs, [:assoc1_id,:assoc2_id], unique: true
add_index :assocs_assocs, :assoc1_id
add_index :assocs_assocs, :assoc2_id



############################################
# stats tables

# use tables for standings e.g group_tables? - why? why not?
#
# todo: add group_standings per round with pos diffs e.g +1,+2, -3 etc.

create_table :group_standings do |t|
  t.references  :group,    null: false,  index: false
  t.timestamps
end

### use items or lines instead of entries - why (shorter! simple plural e.g. just add s)
##  use group_table_lines/stats  - why? why not?

create_table :group_standing_entries do |t|
  t.references  :group_standing,  null: false, index: false
  t.references  :team,            null: false, index: false
  t.integer     :pos       # check/todo: use rank? -- keep/use pos only for "internal" insertation order only - why? why not?
  t.integer     :played   ## p/pld
  t.integer     :won      ## w
  t.integer     :lost     ## l
  t.integer     :drawn    ## d  or t/tied ??
  t.integer     :goals_for             # todo: find a short name - gf? why? why not?
  t.integer     :goals_against         # todo: find a shorter name - ga? why? why not?
  t.integer     :pts
  t.string      :comments
  t.timestamps
end


create_table :event_standings do |t|
  t.references  :event,   null: false, index: false
  t.timestamps
end

create_table :event_standing_entries do |t|
  t.references  :event_standing,  null: false, index: false
  t.references  :team,            null: false, index: false
  t.integer     :pos
  t.integer     :played
  t.integer     :won
  t.integer     :lost
  t.integer     :drawn
  t.integer     :goals_for             # todo: find a short name - gf? or for? why? why not?
  t.integer     :goals_against         # todo: find a shorter name - ga? or against? why? why not?
  t.integer     :pts
  t.string      :comments
  t.timestamps
end


## flex (free-style/form) standings table - lets you add as many events as you like (not bound to single event/season/etc.)
##  -use (find a better) a different name? why? why not?
create_table :alltime_standings do |t|
  t.string    :key,   null: false
  t.string    :name,  null: false
  t.timestamps
end

create_table :alltime_standing_entries do |t|
  t.references  :alltime_standing,  null: false, index: false
  t.references  :team,              null: false, index: false
  t.integer     :pos
  t.integer     :played    # todo: use a different name - why? why not?
  t.integer     :won
  t.integer     :lost
  t.integer     :drawn
  t.integer     :goals_for             # todo: find a short name - gf? why? why not?
  t.integer     :goals_against         # todo: find a shorter name - ga? why? why not?
  t.integer     :pts
  t.integer     :recs               # note: specific to alltime - stats records counter (e.g. appearance counter) - find a better name - why? why not?
  t.string      :comments
  t.timestamps
end


  end  # Schema.define
end