You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: 20140817035914_add_unique_name_constraints.rb
    path of file: ./repos/arvados/services/api/db/migrate
    
    the code of the file until where you have to start completion: # Copyright (C) The Arvados Authors. All rights reserved.
#
# SPDX-License-Identifier: AGPL-3.0

class AddUniqueNameConstraints < ActiveRecord::Migration[4.2]
  def change
    # Ensure uniqueness before adding constraints.
    ["collections", "pipeline_templates", "groups"].each do |table|
      rows = ActiveRe
    
  def change
    # Ensure uniqueness before adding constraints.
    ["collections", "pipeline_templates", "groups"].each do |table|
      rows = ActiveRecord::Base.connection.select_all %{
select uuid, owner_uuid, name from #{table} order by owner_uuid, name
}
      prev = {}
      n = 1
      rows.each do |r|
        if r["owner_uuid"] == prev["owner_uuid"] and !r["name"].nil? and r["name"] == prev["name"]
          n += 1
          ActiveRecord::Base.connection.execute %{
update #{table} set name='#{r["name"]} #{n}' where uuid='#{r["uuid"]}'
}
        else
          n = 1
        end
        prev = r
      end
    end