Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/views/clubsite/officials/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<tbody>
<% @officials.each do |official| %>
<tr>
<td><%= official.user&.name || 'Unknown user' %></td>
<td><%= official.user.name %></td>
<td>
<%= form_with scope: :official, url: "/officials/edit/#{official.id}", html: { class: 'form-inline' } do |f| %>
<%= f.hidden_field :user_id, value: official.user_id %>
Expand Down
32 changes: 32 additions & 0 deletions db/migrate/20261130120000_add_officials_user_foreign_key.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Stops officials pointing at users that no longer exist. Deleting a user who
# still has officials fails instead of removing their certifications: merge
# moves them to the other account, and account deletion removes them first.
#
# Reassign or remove the officials without a user before running this. They may
# be real certifications, so this doesn't remove them.
#
# Production's tables predate schema.rb and differ from it: officials is MyISAM,
# which ignores foreign keys, and its user_id is signed while users.id is
# unsigned. Both need to match before InnoDB can enforce the constraint. This can
# be run again if it stops partway, as MariaDB can't roll back schema changes.
class AddOfficialsUserForeignKey < ActiveRecord::Migration[7.2]
def up
orphans = select_values(<<~SQL)
SELECT officials.id FROM officials
LEFT JOIN users ON users.id = officials.user_id
WHERE users.id IS NULL
SQL
raise "Officials without a user, reassign or remove them first: #{orphans.join(', ')}" if orphans.any?

execute 'ALTER TABLE officials ENGINE=InnoDB'
# Signed in databases created from schema.rb, unsigned in production
user_id_type = columns(:users).find { |column| column.name == 'id' }.sql_type
execute "ALTER TABLE officials MODIFY user_id #{user_id_type} NOT NULL"
add_foreign_key :officials, :users, name: 'officials_ibfk_1'
end

# Leaves officials on InnoDB, with user_id matching users.id
def down
remove_foreign_key :officials, name: 'officials_ibfk_1'
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.2].define(version: 2026_10_21_210000) do
ActiveRecord::Schema[7.2].define(version: 2026_11_30_120000) do
create_table "club_categories", id: :integer, charset: "utf8mb3", collation: "utf8mb3_unicode_ci", force: :cascade do |t|
t.string "name"
end
Expand Down Expand Up @@ -271,6 +271,7 @@
add_foreign_key "maps", "clubs", name: "maps_ibfk_2"
add_foreign_key "maps", "map_standards", name: "maps_ibfk_1", on_delete: :nullify
add_foreign_key "memberships", "users", name: "memberships_ibfk_1", on_delete: :cascade
add_foreign_key "officials", "users", name: "officials_ibfk_1"
add_foreign_key "organizers", "events", name: "organizers_ibfk_3", on_delete: :cascade
add_foreign_key "organizers", "roles", name: "organizers_ibfk_2"
add_foreign_key "organizers", "users", name: "organizers_ibfk_1", on_delete: :cascade
Expand Down
9 changes: 3 additions & 6 deletions test/integration/clubsite_admin_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -321,12 +321,9 @@ class ClubsiteAdminTest < ActionDispatch::IntegrationTest
assert_select 'input.simple-person-picker[data-user-id-target="#OfficialUserId"]'
end

test 'officials index renders an official whose user no longer exists' do
officials(:member_level_one).update_columns(user_id: User.maximum(:id) + 1)
sign_in users(:webmaster)
get '/officials'
assert_response :success
assert_select 'td', text: 'Unknown user'
test 'a user with certifications cannot be deleted without moving them' do
assert_raises(ActiveRecord::InvalidForeignKey) { users(:member).delete }
assert Official.exists?(officials(:member_level_one).id)
end

test 'officials index redirects the executive' do
Expand Down