20640: Fix test for table_name override / no searchable columns.
[arvados.git] / services / api / lib / can_be_an_owner.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 # Protect referential integrity of owner_uuid columns in other tables
6 # that can refer to the uuid column in this table.
7
8 module CanBeAnOwner
9
10   def self.included(base)
11     base.extend(ClassMethods)
12
13     # Rails' "has_many" can prevent us from destroying the owner
14     # record when other objects refer to it.
15     ActiveRecord::Base.connection.tables.each do |t|
16       next if t == base.table_name
17       next if t.in?([
18                       'schema_migrations',
19                       'permission_refresh_lock',
20                       'ar_internal_metadata',
21                       'commit_ancestors',
22                       'commits',
23                       'humans',
24                       'jobs',
25                       'job_tasks',
26                       'keep_disks',
27                       'materialized_permissions',
28                       'nodes',
29                       'pipeline_instances',
30                       'pipeline_templates',
31                       'repositories',
32                       'specimens',
33                       'traits',
34                     ])
35       klass = t.classify.constantize
36       next unless klass and 'owner_uuid'.in?(klass.columns.collect(&:name))
37       base.has_many(t.to_sym,
38                     foreign_key: 'owner_uuid',
39                     primary_key: 'uuid',
40                     dependent: :restrict_with_exception)
41     end
42     # We need custom protection for changing an owner's primary
43     # key. (Apart from this restriction, admins are allowed to change
44     # UUIDs.)
45     base.validate :restrict_uuid_change_breaking_associations
46   end
47
48   module ClassMethods
49     def install_view(type)
50       conn = ActiveRecord::Base.connection
51       transaction do
52         # Check whether the temporary view has already been created
53         # during this connection. If not, create it.
54         conn.exec_query "SAVEPOINT check_#{type}_view"
55         begin
56           conn.exec_query("SELECT 1 FROM #{type}_view LIMIT 0")
57         rescue
58           conn.exec_query "ROLLBACK TO SAVEPOINT check_#{type}_view"
59           sql = File.read(Rails.root.join("lib", "create_#{type}_view.sql"))
60           conn.exec_query(sql)
61         ensure
62           conn.exec_query "RELEASE SAVEPOINT check_#{type}_view"
63         end
64       end
65     end
66   end
67
68   def descendant_project_uuids
69     self.class.install_view('ancestor')
70     ActiveRecord::Base.connection.
71       exec_query('SELECT ancestor_view.uuid
72                   FROM ancestor_view
73                   LEFT JOIN groups ON groups.uuid=ancestor_view.uuid
74                   WHERE ancestor_uuid = $1 AND groups.group_class = $2',
75                   # "name" arg is a query label that appears in logs:
76                   "descendant_project_uuids for #{self.uuid}",
77                   # "binds" arg is an array of [col_id, value] for '$1' vars:
78                   [self.uuid, 'project'],
79                   ).rows.map do |project_uuid,|
80       project_uuid
81     end
82   end
83
84   protected
85
86   def restrict_uuid_change_breaking_associations
87     return true if new_record? or not uuid_changed?
88
89     # Check for objects that have my old uuid listed as their owner.
90     self.class.reflect_on_all_associations(:has_many).each do |assoc|
91       next unless assoc.foreign_key == 'owner_uuid'
92       if assoc.klass.where(owner_uuid: uuid_was).any?
93         errors.add(:uuid,
94                    "cannot be changed on a #{self.class} that owns objects")
95         return false
96       end
97     end
98
99     # if I owned myself before, I'll just continue to own myself with
100     # my new uuid.
101     if owner_uuid == uuid_was
102       self.owner_uuid = uuid
103     end
104   end
105 end