diff --git a/app/models/experiment.rb b/app/models/experiment.rb index 607898720..5e459f2c2 100644 --- a/app/models/experiment.rb +++ b/app/models/experiment.rb @@ -15,6 +15,7 @@ class Experiment < ApplicationRecord before_save -> { report_elements.destroy_all }, if: -> { !new_record? && project_id_changed? } belongs_to :project, inverse_of: :experiments, touch: true + delegate :team, to: :project belongs_to :created_by, class_name: 'User' belongs_to :last_modified_by, diff --git a/app/models/my_module.rb b/app/models/my_module.rb index feb3630fc..a60aad9de 100644 --- a/app/models/my_module.rb +++ b/app/models/my_module.rb @@ -44,6 +44,7 @@ class MyModule < ApplicationRecord belongs_to :restored_by, foreign_key: 'restored_by_id', class_name: 'User', optional: true belongs_to :experiment, inverse_of: :my_modules, touch: true has_one :project, through: :experiment, autosave: false + delegate :team, to: :project belongs_to :my_module_group, inverse_of: :my_modules, optional: true belongs_to :my_module_status, optional: true belongs_to :changing_from_my_module_status, optional: true, class_name: 'MyModuleStatus' diff --git a/app/models/user_assignment.rb b/app/models/user_assignment.rb index 4f21d82e5..2e31bfd89 100644 --- a/app/models/user_assignment.rb +++ b/app/models/user_assignment.rb @@ -1,12 +1,15 @@ # frozen_string_literal: true class UserAssignment < ApplicationRecord + before_validation -> { self.team ||= (assignable.is_a?(Team) ? assignable : assignable.team) } + belongs_to :assignable, polymorphic: true, touch: true belongs_to :user_role belongs_to :user + belongs_to :team belongs_to :assigned_by, class_name: 'User', optional: true enum assigned: { automatically: 0, manually: 1 }, _suffix: true - validates :user, uniqueness: { scope: :assignable } + validates :user, uniqueness: { scope: %i(assignable team_id) } end diff --git a/db/migrate/20220621153016_add_team_reference_to_user_assignments.rb b/db/migrate/20220621153016_add_team_reference_to_user_assignments.rb new file mode 100644 index 000000000..f5749b3b5 --- /dev/null +++ b/db/migrate/20220621153016_add_team_reference_to_user_assignments.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +class AddTeamReferenceToUserAssignments < ActiveRecord::Migration[6.1] + def up + add_reference :user_assignments, :team, index: true, foreign_key: true + + UserAssignment.preload(:assignable).find_each do |user_assignment| + team = user_assignment.assignable.is_a?(Team) ? user_assignment.assignable : user_assignment.assignable.team + user_assignment.update_column(:team_id, team.id) + end + end + + def down + remove_reference :user_assignments, :team, index: true, foreign_key: true + end +end diff --git a/db/structure.sql b/db/structure.sql index 5173c750c..df7402f05 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -2834,7 +2834,8 @@ CREATE TABLE public.user_assignments ( assigned_by_id bigint, assigned integer DEFAULT 0 NOT NULL, created_at timestamp(6) without time zone NOT NULL, - updated_at timestamp(6) without time zone NOT NULL + updated_at timestamp(6) without time zone NOT NULL, + team_id bigint ); @@ -6468,6 +6469,13 @@ CREATE INDEX index_user_assignments_on_assignable ON public.user_assignments USI CREATE INDEX index_user_assignments_on_assigned_by_id ON public.user_assignments USING btree (assigned_by_id); +-- +-- Name: index_user_assignments_on_team_id; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX index_user_assignments_on_team_id ON public.user_assignments USING btree (team_id); + + -- -- Name: index_user_assignments_on_user_id; Type: INDEX; Schema: public; Owner: - -- @@ -7075,6 +7083,14 @@ ALTER TABLE ONLY public.result_assets ADD CONSTRAINT fk_rails_48803d79ff FOREIGN KEY (asset_id) REFERENCES public.assets(id); +-- +-- Name: user_assignments fk_rails_4a5764985b; Type: FK CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.user_assignments + ADD CONSTRAINT fk_rails_4a5764985b FOREIGN KEY (team_id) REFERENCES public.teams(id); + + -- -- Name: experiments fk_rails_4a63cb023e; Type: FK CONSTRAINT; Schema: public; Owner: - -- @@ -8371,6 +8387,7 @@ INSERT INTO "schema_migrations" (version) VALUES ('20220321122111'), ('20220325101011'), ('20220328164215'), -('20220516111152'); +('20220516111152'), +('20220621153016');