diff --git a/app/controllers/results_controller.rb b/app/controllers/results_controller.rb index 5fbac60dce7..8b96fce0769 100644 --- a/app/controllers/results_controller.rb +++ b/app/controllers/results_controller.rb @@ -337,10 +337,10 @@ def records if @is_only @years_condition_competition = "AND YEAR(competitions.start_date) = #{@year}" - @years_condition_result = "AND results.year = #{@year}" + @years_condition_result = "AND results.reg_year = #{@year}" elsif @is_until @years_condition_competition = "AND YEAR(competitions.start_date) <= #{@year}" - @years_condition_result = "AND results.year <= #{@year}" + @years_condition_result = "AND results.reg_year <= #{@year}" else @years_condition_competition = "" @years_condition_result = "" diff --git a/db/migrate/20260127205619_change_month_and_day_from_concise_tables.rb b/db/migrate/20260127205619_change_month_and_day_from_concise_tables.rb new file mode 100644 index 00000000000..782c5a5128d --- /dev/null +++ b/db/migrate/20260127205619_change_month_and_day_from_concise_tables.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +class ChangeMonthAndDayFromConciseTables < ActiveRecord::Migration[8.1] + def change + change_table :concise_single_results, bulk: true do |t| + t.remove :month, type: :integer, limit: 2, default: 0, null: false, unsigned: true + t.remove :day, type: :integer, limit: 2, default: 0, null: false, unsigned: true + + t.rename :year, :reg_year + end + + change_table :concise_average_results, bulk: true do |t| + t.remove :month, type: :integer, limit: 2, default: 0, null: false, unsigned: true + t.remove :day, type: :integer, limit: 2, default: 0, null: false, unsigned: true + + t.rename :year, :reg_year + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 95490587e40..7090a7dea3b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -471,13 +471,11 @@ t.integer "average", default: 0, null: false t.string "continent_id", limit: 50, default: "", null: false t.string "country_id", limit: 50, default: "", null: false - t.integer "day", limit: 2, default: 0, null: false, unsigned: true t.string "event_id", limit: 6, default: "", null: false t.integer "id", default: 0, null: false - t.integer "month", limit: 2, default: 0, null: false, unsigned: true t.string "person_id", limit: 10, default: "", null: false + t.integer "reg_year", limit: 2, default: 0, null: false, unsigned: true t.bigint "value_and_id" - t.integer "year", limit: 2, default: 0, null: false, unsigned: true t.index ["event_id", "average"], name: "mixed_records_speedup" t.index ["event_id", "country_id", "average"], name: "regional_records_speedup" end @@ -486,13 +484,11 @@ t.integer "best", default: 0, null: false t.string "continent_id", limit: 50, default: "", null: false t.string "country_id", limit: 50, default: "", null: false - t.integer "day", limit: 2, default: 0, null: false, unsigned: true t.string "event_id", limit: 6, default: "", null: false t.integer "id", default: 0, null: false - t.integer "month", limit: 2, default: 0, null: false, unsigned: true t.string "person_id", limit: 10, default: "", null: false + t.integer "reg_year", limit: 2, default: 0, null: false, unsigned: true t.bigint "value_and_id" - t.integer "year", limit: 2, default: 0, null: false, unsigned: true t.index ["event_id", "best"], name: "mixed_records_speedup" t.index ["event_id", "country_id", "best"], name: "regional_records_speedup" end diff --git a/lib/auxiliary_data_computation.rb b/lib/auxiliary_data_computation.rb index 8544047d0a0..440e4859521 100644 --- a/lib/auxiliary_data_computation.rb +++ b/lib/auxiliary_data_computation.rb @@ -15,7 +15,7 @@ def self.compute_concise_results ].each do |field, table_name| DbHelper.with_temp_table(table_name) do |temp_table_name| ActiveRecord::Base.connection.execute <<~SQL.squish - INSERT INTO #{temp_table_name} (id, #{field}, value_and_id, person_id, event_id, country_id, continent_id, year, month, day) + INSERT INTO #{temp_table_name} (id, #{field}, value_and_id, person_id, event_id, country_id, continent_id, reg_year) SELECT results.id, #{field}, @@ -24,9 +24,7 @@ def self.compute_concise_results event_id, countries.id country_id, continent_id, - YEAR(start_date) year, - MONTH(start_date) month, - DAY(start_date) day + YEAR(start_date) reg_year FROM ( SELECT MIN(#{field} * 1000000000 + results.id) valueAndId FROM results diff --git a/lib/sanity_check_sql/15 - incorrect_records_assignment/66 - correct_records.sql b/lib/sanity_check_sql/15 - incorrect_records_assignment/66 - correct_records.sql index da8f1a35c88..568ebdf867c 100644 --- a/lib/sanity_check_sql/15 - incorrect_records_assignment/66 - correct_records.sql +++ b/lib/sanity_check_sql/15 - incorrect_records_assignment/66 - correct_records.sql @@ -10,40 +10,40 @@ WITH round_dates AS (SELECT sa.round_id, csr.event_id, MIN(csr.best) AS old_NR_single FROM concise_single_results csr - WHERE csr.year < 2019 + WHERE csr.reg_year < 2019 GROUP BY country_id, event_id), -- Fetches the NR averages of each country as of the end of the previous year. old_nr_averages AS (SELECT car.country_id, car.event_id, MIN(car.average) AS old_NR_average FROM concise_average_results car - WHERE car.year < 2019 + WHERE car.reg_year < 2019 GROUP BY country_id, event_id), -- Fetches the CR singles of each continent as of the end of the previous year. old_cr_singles AS (SELECT csr.continent_id, csr.event_id, MIN(csr.best) AS old_CR_single FROM concise_single_results csr - WHERE csr.year < 2019 + WHERE csr.reg_year < 2019 GROUP BY continent_id, event_id), -- Fetches the CR averages of each continent as of the end of the previous year. old_cr_averages AS (SELECT car.continent_id, car.event_id, MIN(car.average) AS old_CR_average FROM concise_average_results car - WHERE car.year < 2019 + WHERE car.reg_year < 2019 GROUP BY continent_id, event_id), -- Fetches WR singles as of the end of the previous year. old_wr_singles AS (SELECT csr.event_id, MIN(csr.best) AS old_WR_single FROM concise_single_results csr - WHERE csr.year < 2019 + WHERE csr.reg_year < 2019 GROUP BY event_id), -- Fetches WR averages as of the end of the previous year. old_wr_averages AS (SELECT car.event_id, MIN(car.average) AS old_WR_average FROM concise_average_results car - WHERE car.year < 2019 + WHERE car.reg_year < 2019 GROUP BY event_id), -- 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. t1 AS (SELECT r.id AS results_id, diff --git a/spec/lib/auxiliary_data_computation_spec.rb b/spec/lib/auxiliary_data_computation_spec.rb index 043a33fc231..d6ab5399153 100644 --- a/spec/lib/auxiliary_data_computation_spec.rb +++ b/spec/lib/auxiliary_data_computation_spec.rb @@ -19,10 +19,10 @@ create(:result, event_id: "222", best: 100, average: 150, competition: competition_2017, person: person) AuxiliaryDataComputation.compute_concise_results # Concise single results - concise_single_results = ActiveRecord::Base.connection.execute "SELECT event_id, person_id, year, best FROM concise_single_results" + concise_single_results = ActiveRecord::Base.connection.execute "SELECT event_id, person_id, reg_year, best FROM concise_single_results" 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]) # Concise average results - concise_average_results = ActiveRecord::Base.connection.execute "SELECT event_id, person_id, year, average FROM concise_average_results" + concise_average_results = ActiveRecord::Base.connection.execute "SELECT event_id, person_id, reg_year, average FROM concise_average_results" 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]) end @@ -32,10 +32,10 @@ create(:result, event_id: "333", best: 750, average: 850, competition: next_competition_2016, person: person) AuxiliaryDataComputation.compute_concise_results # Concise single results - concise_single_results = ActiveRecord::Base.connection.execute "SELECT event_id, person_id, country_id, year, best FROM concise_single_results" + concise_single_results = ActiveRecord::Base.connection.execute "SELECT event_id, person_id, country_id, reg_year, best FROM concise_single_results" expect(concise_single_results).to contain_exactly(["333", person.wca_id, "China", 2016, 700], ["333", person.wca_id, "Chile", 2016, 750]) # Concise average results - concise_average_results = ActiveRecord::Base.connection.execute "SELECT event_id, person_id, country_id, year, average FROM concise_average_results" + concise_average_results = ActiveRecord::Base.connection.execute "SELECT event_id, person_id, country_id, reg_year, average FROM concise_average_results" expect(concise_average_results).to contain_exactly(["333", person.wca_id, "China", 2016, 800], ["333", person.wca_id, "Chile", 2016, 850]) end end