// Copyright (C) 2012 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.server.schema;

import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gwtorm.jdbc.JdbcSchema;
import com.google.gwtorm.schema.sql.DialectPostgreSQL;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
import com.google.inject.Provider;

import java.sql.SQLException;
import java.sql.Statement;

public class Schema_70 extends SchemaVersion {
  @Inject
  protected Schema_70(Provider<Schema_69> prior) {
    super(prior);
  }

  @Override
  protected void migrateData(ReviewDb db, UpdateUI ui) throws OrmException,
      SQLException {
    Statement stmt = ((JdbcSchema) db).getConnection().createStatement();
    try {
      stmt.executeUpdate("UPDATE tracking_ids SET tracking_key = tracking_id");
      execute(stmt, "DROP INDEX tracking_ids_byTrkId");
      if (((JdbcSchema) db).getDialect() instanceof DialectPostgreSQL) {
        execute(stmt, "ALTER TABLE tracking_ids DROP CONSTRAINT tracking_ids_pkey");
      } else {
        execute(stmt, "ALTER TABLE tracking_ids DROP PRIMARY KEY");
      }
      stmt.execute("ALTER TABLE tracking_ids"
          + " ADD PRIMARY KEY (change_id, tracking_key, tracking_system)");
      stmt.execute("CREATE INDEX tracking_ids_byTrkKey"
          + " ON tracking_ids (tracking_key)");
    } finally {
      stmt.close();
    }
  }

  private static final void execute(Statement stmt, String command) {
    try {
      stmt.execute(command);
    } catch (SQLException e) {
      // ignore
    }
  }
}

@@ -666,1454 +666,184 @@
 gle.
-gerrit.reviewdb.server.ReviewDb;%0Aimport com.google.gwtorm.jdbc.JdbcSchema;%0Aimport com.google.gwtorm.schema.sql.DialectPostgreSQL;%0Aimport com.google.gwtorm.server.OrmException;%0Aimport com.google.inject.Inject;%0Aimport com.google.inject.Provider;%0A%0Aimport java.sql.SQLException;%0Aimport java.sql.Statement;%0A%0Apublic class Schema_70 extends SchemaVersion %7B%0A  @Inject%0A  protected Schema_70(Provider%3CSchema_69%3E prior) %7B%0A    super(prior);%0A  %7D%0A%0A  @Override%0A  protected void migrateData(ReviewDb db, UpdateUI ui) throws OrmException,%0A      SQLException %7B%0A    Statement stmt = ((JdbcSchema) db).getConnection().createStatement();%0A    try %7B%0A      stmt.executeUpdate(%22UPDATE tracking_ids SET tracking_key = tracking_id%22);%0A      execute(stmt, %22DROP INDEX tracking_ids_byTrkId%22);%0A      if (((JdbcSchema) db).getDialect() instanceof DialectPostgreSQL) %7B%0A        execute(stmt, %22ALTER TABLE tracking_ids DROP CONSTRAINT tracking_ids_pkey%22);%0A      %7D else %7B%0A        execute(stmt, %22ALTER TABLE tracking_ids DROP PRIMARY KEY%22);%0A      %7D%0A      stmt.execute(%22ALTER TABLE tracking_ids%22%0A          + %22 ADD PRIMARY KEY (change_id, tracking_key, tracking_system)%22);%0A      stmt.execute(%22CREATE INDEX tracking_ids_byTrkKey%22%0A          + %22 ON tracking_ids (tracking_key)%22);%0A    %7D finally %7B%0A      stmt.close();%0A    %7D%0A  %7D%0A%0A  private static final void execute(Statement stmt, String command) %7B%0A    try %7B%0A      stmt.execute(command);%0A    %7D catch (SQLException e) %7B%0A      // ignore%0A    %7D
+inject.Inject;%0Aimport com.google.inject.Provider;%0A%0Apublic class Schema_70 extends SchemaVersion %7B%0A  @Inject%0A  protected Schema_70(Provider%3CSchema_69%3E prior) %7B%0A    super(prior);
 %0A  %7D

Clean left over data migration after removal of TrackingIds table