Skip to content

Commit 2d79400

Browse files
authored
[Concise 1/3] Remove month and day columns from concise results (#13455)
* Remove month and day columns from concise results * Rename year to reg_year * Fix column rename in Records Sanity Check * Rename column in impromptu test case SQL
1 parent a30bee0 commit 2d79400

File tree

6 files changed

+35
-22
lines changed

6 files changed

+35
-22
lines changed

app/controllers/results_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,10 +337,10 @@ def records
337337

338338
if @is_only
339339
@years_condition_competition = "AND YEAR(competitions.start_date) = #{@year}"
340-
@years_condition_result = "AND results.year = #{@year}"
340+
@years_condition_result = "AND results.reg_year = #{@year}"
341341
elsif @is_until
342342
@years_condition_competition = "AND YEAR(competitions.start_date) <= #{@year}"
343-
@years_condition_result = "AND results.year <= #{@year}"
343+
@years_condition_result = "AND results.reg_year <= #{@year}"
344344
else
345345
@years_condition_competition = ""
346346
@years_condition_result = ""
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# frozen_string_literal: true
2+
3+
class ChangeMonthAndDayFromConciseTables < ActiveRecord::Migration[8.1]
4+
def change
5+
change_table :concise_single_results, bulk: true do |t|
6+
t.remove :month, type: :integer, limit: 2, default: 0, null: false, unsigned: true
7+
t.remove :day, type: :integer, limit: 2, default: 0, null: false, unsigned: true
8+
9+
t.rename :year, :reg_year
10+
end
11+
12+
change_table :concise_average_results, bulk: true do |t|
13+
t.remove :month, type: :integer, limit: 2, default: 0, null: false, unsigned: true
14+
t.remove :day, type: :integer, limit: 2, default: 0, null: false, unsigned: true
15+
16+
t.rename :year, :reg_year
17+
end
18+
end
19+
end

db/schema.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -471,13 +471,11 @@
471471
t.integer "average", default: 0, null: false
472472
t.string "continent_id", limit: 50, default: "", null: false
473473
t.string "country_id", limit: 50, default: "", null: false
474-
t.integer "day", limit: 2, default: 0, null: false, unsigned: true
475474
t.string "event_id", limit: 6, default: "", null: false
476475
t.integer "id", default: 0, null: false
477-
t.integer "month", limit: 2, default: 0, null: false, unsigned: true
478476
t.string "person_id", limit: 10, default: "", null: false
477+
t.integer "reg_year", limit: 2, default: 0, null: false, unsigned: true
479478
t.bigint "value_and_id"
480-
t.integer "year", limit: 2, default: 0, null: false, unsigned: true
481479
t.index ["event_id", "average"], name: "mixed_records_speedup"
482480
t.index ["event_id", "country_id", "average"], name: "regional_records_speedup"
483481
end
@@ -486,13 +484,11 @@
486484
t.integer "best", default: 0, null: false
487485
t.string "continent_id", limit: 50, default: "", null: false
488486
t.string "country_id", limit: 50, default: "", null: false
489-
t.integer "day", limit: 2, default: 0, null: false, unsigned: true
490487
t.string "event_id", limit: 6, default: "", null: false
491488
t.integer "id", default: 0, null: false
492-
t.integer "month", limit: 2, default: 0, null: false, unsigned: true
493489
t.string "person_id", limit: 10, default: "", null: false
490+
t.integer "reg_year", limit: 2, default: 0, null: false, unsigned: true
494491
t.bigint "value_and_id"
495-
t.integer "year", limit: 2, default: 0, null: false, unsigned: true
496492
t.index ["event_id", "best"], name: "mixed_records_speedup"
497493
t.index ["event_id", "country_id", "best"], name: "regional_records_speedup"
498494
end

lib/auxiliary_data_computation.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def self.compute_concise_results
1515
].each do |field, table_name|
1616
DbHelper.with_temp_table(table_name) do |temp_table_name|
1717
ActiveRecord::Base.connection.execute <<~SQL.squish
18-
INSERT INTO #{temp_table_name} (id, #{field}, value_and_id, person_id, event_id, country_id, continent_id, year, month, day)
18+
INSERT INTO #{temp_table_name} (id, #{field}, value_and_id, person_id, event_id, country_id, continent_id, reg_year)
1919
SELECT
2020
results.id,
2121
#{field},
@@ -24,9 +24,7 @@ def self.compute_concise_results
2424
event_id,
2525
countries.id country_id,
2626
continent_id,
27-
YEAR(start_date) year,
28-
MONTH(start_date) month,
29-
DAY(start_date) day
27+
YEAR(start_date) reg_year
3028
FROM (
3129
SELECT MIN(#{field} * 1000000000 + results.id) valueAndId
3230
FROM results

lib/sanity_check_sql/15 - incorrect_records_assignment/66 - correct_records.sql

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,40 +10,40 @@ WITH round_dates AS (SELECT sa.round_id,
1010
csr.event_id,
1111
MIN(csr.best) AS old_NR_single
1212
FROM concise_single_results csr
13-
WHERE csr.year < 2019
13+
WHERE csr.reg_year < 2019
1414
GROUP BY country_id, event_id),
1515
-- Fetches the NR averages of each country as of the end of the previous year.
1616
old_nr_averages AS (SELECT car.country_id,
1717
car.event_id,
1818
MIN(car.average) AS old_NR_average
1919
FROM concise_average_results car
20-
WHERE car.year < 2019
20+
WHERE car.reg_year < 2019
2121
GROUP BY country_id, event_id),
2222
-- Fetches the CR singles of each continent as of the end of the previous year.
2323
old_cr_singles AS (SELECT csr.continent_id,
2424
csr.event_id,
2525
MIN(csr.best) AS old_CR_single
2626
FROM concise_single_results csr
27-
WHERE csr.year < 2019
27+
WHERE csr.reg_year < 2019
2828
GROUP BY continent_id, event_id),
2929
-- Fetches the CR averages of each continent as of the end of the previous year.
3030
old_cr_averages AS (SELECT car.continent_id,
3131
car.event_id,
3232
MIN(car.average) AS old_CR_average
3333
FROM concise_average_results car
34-
WHERE car.year < 2019
34+
WHERE car.reg_year < 2019
3535
GROUP BY continent_id, event_id),
3636
-- Fetches WR singles as of the end of the previous year.
3737
old_wr_singles AS (SELECT csr.event_id,
3838
MIN(csr.best) AS old_WR_single
3939
FROM concise_single_results csr
40-
WHERE csr.year < 2019
40+
WHERE csr.reg_year < 2019
4141
GROUP BY event_id),
4242
-- Fetches WR averages as of the end of the previous year.
4343
old_wr_averages AS (SELECT car.event_id,
4444
MIN(car.average) AS old_WR_average
4545
FROM concise_average_results car
46-
WHERE car.year < 2019
46+
WHERE car.reg_year < 2019
4747
GROUP BY event_id),
4848
-- Joins round date to results table and filters out rows that are not <= NRs from previous years. Assigns ranking 1 to each single or average that is the best for that country for that day.
4949
t1 AS (SELECT r.id AS results_id,

spec/lib/auxiliary_data_computation_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
create(:result, event_id: "222", best: 100, average: 150, competition: competition_2017, person: person)
2020
AuxiliaryDataComputation.compute_concise_results
2121
# Concise single results
22-
concise_single_results = ActiveRecord::Base.connection.execute "SELECT event_id, person_id, year, best FROM concise_single_results"
22+
concise_single_results = ActiveRecord::Base.connection.execute "SELECT event_id, person_id, reg_year, best FROM concise_single_results"
2323
expect(concise_single_results).to contain_exactly(["333", person.wca_id, 2016, 700], ["333", person.wca_id, 2017, 800], ["222", person.wca_id, 2017, 100])
2424
# Concise average results
25-
concise_average_results = ActiveRecord::Base.connection.execute "SELECT event_id, person_id, year, average FROM concise_average_results"
25+
concise_average_results = ActiveRecord::Base.connection.execute "SELECT event_id, person_id, reg_year, average FROM concise_average_results"
2626
expect(concise_average_results).to contain_exactly(["333", person.wca_id, 2016, 800], ["333", person.wca_id, 2017, 900], ["222", person.wca_id, 2017, 150])
2727
end
2828

@@ -32,10 +32,10 @@
3232
create(:result, event_id: "333", best: 750, average: 850, competition: next_competition_2016, person: person)
3333
AuxiliaryDataComputation.compute_concise_results
3434
# Concise single results
35-
concise_single_results = ActiveRecord::Base.connection.execute "SELECT event_id, person_id, country_id, year, best FROM concise_single_results"
35+
concise_single_results = ActiveRecord::Base.connection.execute "SELECT event_id, person_id, country_id, reg_year, best FROM concise_single_results"
3636
expect(concise_single_results).to contain_exactly(["333", person.wca_id, "China", 2016, 700], ["333", person.wca_id, "Chile", 2016, 750])
3737
# Concise average results
38-
concise_average_results = ActiveRecord::Base.connection.execute "SELECT event_id, person_id, country_id, year, average FROM concise_average_results"
38+
concise_average_results = ActiveRecord::Base.connection.execute "SELECT event_id, person_id, country_id, reg_year, average FROM concise_average_results"
3939
expect(concise_average_results).to contain_exactly(["333", person.wca_id, "China", 2016, 800], ["333", person.wca_id, "Chile", 2016, 850])
4040
end
4141
end

0 commit comments

Comments
 (0)