Merge branch '18691-freeze-project'
[arvados.git] / services / api / db / migrate / 20220301155729_frozen_groups.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 require '20200501150153_permission_table_constants'
6
7 class FrozenGroups < ActiveRecord::Migration[5.0]
8   def up
9     create_table :frozen_groups, :id => false do |t|
10       t.string :uuid
11     end
12     add_index :frozen_groups, :uuid, :unique => true
13
14     ActiveRecord::Base.connection.execute %{
15 create or replace function project_subtree_with_is_frozen (starting_uuid varchar(27), starting_is_frozen boolean)
16 returns table (uuid varchar(27), is_frozen boolean)
17 STABLE
18 language SQL
19 as $$
20 WITH RECURSIVE
21   project_subtree(uuid, is_frozen) as (
22     values (starting_uuid, starting_is_frozen)
23     union
24     select groups.uuid, project_subtree.is_frozen or groups.frozen_by_uuid is not null
25       from groups join project_subtree on project_subtree.uuid = groups.owner_uuid
26   )
27   select uuid, is_frozen from project_subtree;
28 $$;
29 }
30
31     # Initialize the table. After this, it is updated incrementally.
32     # See app/models/group.rb#update_frozen_groups
33     refresh_frozen
34   end
35
36   def down
37     drop_table :frozen_groups
38   end
39 end