17810: Fix S3 signature verification.
[arvados.git] / services / api / db / migrate / 20140817035914_add_unique_name_constraints.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 class AddUniqueNameConstraints < ActiveRecord::Migration[4.2]
6   def change
7     # Ensure uniqueness before adding constraints.
8     ["collections", "pipeline_templates", "groups"].each do |table|
9       rows = ActiveRecord::Base.connection.select_all %{
10 select uuid, owner_uuid, name from #{table} order by owner_uuid, name
11 }
12       prev = {}
13       n = 1
14       rows.each do |r|
15         if r["owner_uuid"] == prev["owner_uuid"] and !r["name"].nil? and r["name"] == prev["name"]
16           n += 1
17           ActiveRecord::Base.connection.execute %{
18 update #{table} set name='#{r["name"]} #{n}' where uuid='#{r["uuid"]}'
19 }
20         else
21           n = 1
22         end
23         prev = r
24       end
25     end
26
27     add_index(:collections, [:owner_uuid, :name], unique: true,
28               name: 'collection_owner_uuid_name_unique')
29     add_index(:pipeline_templates, [:owner_uuid, :name], unique: true,
30               name: 'pipeline_template_owner_uuid_name_unique')
31     add_index(:groups, [:owner_uuid, :name], unique: true,
32               name: 'groups_owner_uuid_name_unique')
33   end
34 end