1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
6 require 'kind_and_etag'
8 class BackwardCompatibilityForUserRepositories < ActiveRecord::Migration[4.2]
9 include CurrentApiClient
11 class ArvadosModel < ActiveRecord::Base
12 self.abstract_class = true
13 extend HasUuid::ClassMethods
14 include CurrentApiClient
16 before_create do |record|
17 record.uuid ||= record.class.generate_uuid
18 record.owner_uuid ||= system_user_uuid
20 serialize :properties, Hash
23 # Clean up the name of the stub model class so we generate correct UUIDs.
24 super.rpartition("::").last
28 class Log < ArvadosModel
29 def self.log_for(thing, age="old")
30 { "#{age}_etag" => thing.etag,
31 "#{age}_attributes" => thing.attributes,
35 def self.log_create(thing)
36 new_log("create", thing, log_for(thing, "new"))
39 def self.log_update(thing, start_state)
40 new_log("update", thing, start_state.merge(log_for(thing, "new")))
43 def self.log_destroy(thing)
44 new_log("destroy", thing, log_for(thing, "old"))
49 def self.new_log(event_type, thing, properties)
50 create!(event_type: event_type,
52 object_uuid: thing.uuid,
53 object_owner_uuid: thing.owner_uuid,
54 properties: properties)
58 class Link < ArvadosModel
61 class Repository < ArvadosModel
65 remove_index :repositories, name: "repositories_search_index"
66 add_index(:repositories, %w(uuid owner_uuid modified_by_client_uuid
67 modified_by_user_uuid name),
68 name: "repositories_search_index")
69 remove_column :repositories, :fetch_url
70 remove_column :repositories, :push_url
72 [Link, Log, Repository].each { |m| m.reset_column_information }
73 Repository.where("owner_uuid != ?", system_user_uuid).find_each do |repo|
75 tail_uuid: repo.owner_uuid,
76 link_class: "permission", name: "can_manage", head_uuid: repo.uuid,
78 if Link.where(link_attrs).first.nil?
79 manage_link = Link.create!(link_attrs)
80 Log.log_create(manage_link)
82 start_log = Log.log_for(repo)
83 repo.owner_uuid = system_user_uuid
85 Log.log_update(repo, start_log)
90 raise ActiveRecord::IrreversibleMigration.
91 new("can't restore prior fetch and push URLs")