diff --git a/app/views/clubsite/officials/index.html.erb b/app/views/clubsite/officials/index.html.erb index 553ed95..67e56d1 100644 --- a/app/views/clubsite/officials/index.html.erb +++ b/app/views/clubsite/officials/index.html.erb @@ -41,7 +41,7 @@ <% @officials.each do |official| %> - <%= official.user&.name || 'Unknown user' %> + <%= official.user.name %> <%= form_with scope: :official, url: "/officials/edit/#{official.id}", html: { class: 'form-inline' } do |f| %> <%= f.hidden_field :user_id, value: official.user_id %> diff --git a/db/migrate/20261130120000_add_officials_user_foreign_key.rb b/db/migrate/20261130120000_add_officials_user_foreign_key.rb new file mode 100644 index 0000000..d7e9b1c --- /dev/null +++ b/db/migrate/20261130120000_add_officials_user_foreign_key.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 2f83aa5..82820a9 100755 --- a/db/schema.rb +++ b/db/schema.rb @@ -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 @@ -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 diff --git a/test/integration/clubsite_admin_test.rb b/test/integration/clubsite_admin_test.rb index aa4981b..dff66d7 100644 --- a/test/integration/clubsite_admin_test.rb +++ b/test/integration/clubsite_admin_test.rb @@ -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