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