From cc263ff010a6620e6257a7a433dc2a3138f492e9 Mon Sep 17 00:00:00 2001 From: Jesus Federico Date: Tue, 7 May 2024 15:07:07 -0400 Subject: [PATCH] LTI-366: complementary fixes for issue with codes already used error (#311) * LTI-366 remove uniqueness from codes in Room table * LTI-366: updated schema with last migration * LTI-366: hot-fix and followup * LTI-366: remove uniqueness validation from model --- app/controllers/rooms_controller.rb | 7 ++++--- app/models/room.rb | 1 - ...0507174455_remove_unique_index_from_rooms.rb | 17 +++++++++++++++++ db/schema.rb | 4 ++-- 4 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 db/migrate/20240507174455_remove_unique_index_from_rooms.rb diff --git a/app/controllers/rooms_controller.rb b/app/controllers/rooms_controller.rb index b865718c..5c5e9a28 100644 --- a/app/controllers/rooms_controller.rb +++ b/app/controllers/rooms_controller.rb @@ -322,6 +322,7 @@ def set_launch def launch_room(launch_params, tenant) handler = room_handler(launch_params, tenant) handler_legacy = launch_params['custom_params']['custom_handler_legacy'].presence + code = SecureRandom.alphanumeric(10) ## Any launch. @room = Room.find_by(handler: handler, tenant: tenant) @@ -331,7 +332,7 @@ def launch_room(launch_params, tenant) if handler_legacy.nil? ## Regular launch logger.debug('This is a Regular launch...') - @room = Room.create(launch_room_params) + @room = Room.create(launch_room_params.merge({ code: code, shared_code: code })) logger.debug(@room.errors.full_messages) if @room.errors.any? return end @@ -351,7 +352,7 @@ def launch_room(launch_params, tenant) logger.debug("Room #{@room.id} updated with fetched parameters...") && return if @room else # Create - @room = Room.create(fetched_room_params.merge({ code: '', shared_code: '' })) + @room = Room.create(fetched_room_params.merge({ code: code, shared_code: code })) if @room.persisted? logger.debug("Room #{@room.id} created with fetched parameters...") && return if @room else @@ -366,7 +367,7 @@ def launch_room(launch_params, tenant) return unless Rails.configuration.handler_legacy_new_room_enabled logger.debug('It will attempt to create a Room with passed parameters even though a handler_legacy was passed...') - @room = Room.create(launch_room_params) + @room = Room.create(launch_room_params.merge({ code: code, shared_code: code })) end def launch_user(launch_params) diff --git a/app/models/room.rb b/app/models/room.rb index cb720c29..a6093476 100644 --- a/app/models/room.rb +++ b/app/models/room.rb @@ -17,7 +17,6 @@ # with BigBlueButton; if not, see . class Room < ApplicationRecord before_save :default_values - validates :code, uniqueness: true store_accessor :settings, [:lockSettingsDisableCam, :lockSettingsDisableMic, :lockSettingsDisablePrivateChat, :lockSettingsDisablePublicChat, :lockSettingsDisableNote] store_accessor :settings, %i[waitForModerator allModerators guestPolicy record autoStartRecording allowStartStopRecording] diff --git a/db/migrate/20240507174455_remove_unique_index_from_rooms.rb b/db/migrate/20240507174455_remove_unique_index_from_rooms.rb new file mode 100644 index 00000000..b0d90a34 --- /dev/null +++ b/db/migrate/20240507174455_remove_unique_index_from_rooms.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +class RemoveUniqueIndexFromRooms < ActiveRecord::Migration[6.1] + def up + # Remove the existing unique index + remove_index(:rooms, :code, unique: true, if_exists: true) + # Add a non-unique index + add_index(:rooms, :code, name: 'index_rooms_on_code') + end + + def down + # Remove the non-unique index + remove_index(:rooms, name: 'index_rooms_on_code', if_exists: true) + # Re-add the unique index + add_index(:rooms, :code, unique: true, name: 'index_rooms_on_code_unique') + end +end diff --git a/db/schema.rb b/db/schema.rb index b006187f..d6d96840 100644 --- 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.define(version: 2023_11_02_145703) do +ActiveRecord::Schema.define(version: 2024_05_07_174455) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -35,7 +35,7 @@ t.string "code" t.string "shared_code" t.boolean "use_shared_code" - t.index ["code"], name: "index_rooms_on_code", unique: true + t.index ["code"], name: "index_rooms_on_code" t.index ["tenant", "handler"], name: "index_rooms_on_tenant_and_handler", unique: true t.index ["tenant", "handler_legacy"], name: "index_rooms_on_tenant_and_handler_legacy", unique: true end