21700: Install Bundler system-wide in Rails postinst
[arvados.git] / services / api / db / migrate / 20230821000000_priority_update_fix.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 class PriorityUpdateFix < ActiveRecord::Migration[5.2]
6   def up
7     ActiveRecord::Base.connection.execute %{
8 CREATE OR REPLACE FUNCTION container_priority(for_container_uuid character varying, inherited bigint, inherited_from character varying) returns bigint
9     LANGUAGE sql
10     AS $$
11 /* Determine the priority of an individual container.
12    The "inherited" priority comes from the path we followed from the root, the parent container
13    priority hasn't been updated in the table yet but we need to behave it like it has been.
14 */
15 select coalesce(max(case when containers.uuid = inherited_from then inherited
16                          when containers.priority is not NULL then containers.priority
17                          else container_requests.priority * 1125899906842624::bigint - (extract(epoch from container_requests.created_at)*1000)::bigint
18                     end), 0) from
19     container_requests left outer join containers on container_requests.requesting_container_uuid = containers.uuid
20     where container_requests.container_uuid = for_container_uuid and
21           container_requests.state = 'Committed' and
22           container_requests.priority > 0 and
23           container_requests.owner_uuid not in (select group_uuid from trashed_groups);
24 $$;
25 }
26   end
27
28   def down
29   end
30 end