Increase tracking ID max length to 32

I need to do that because we have a legacy issue tracking system that
uses issue IDs longer than 20 characters (current max length for
tracking IDs).

Because gerrits schema upgrade process doesn't support change of column
length and there doesn't seem to be a standard (database platform
independent) way to do that from an SQL command, this change renames the
tracking_id to tracking_key to trigger creation of the new column with
new max length. The schema migration has to drop/create primary key
because the renamed column is a part of the primary key.

Change-Id: Ia23ec6db50eb16fe26c9d9570d2072c6bd6c1245
This commit is contained in:
Sasa Zivkov
2012-07-12 14:13:52 +02:00
parent 2b41c9c739
commit 766e4415b0
6 changed files with 74 additions and 13 deletions

View File

@@ -20,7 +20,7 @@ import com.google.gwtorm.client.StringKey;
/** External tracking id associated with a {@link Change} */
public final class TrackingId {
public static final int TRACKING_ID_MAX_CHAR = 20;
public static final int TRACKING_ID_MAX_CHAR = 32;
public static final int TRACKING_SYSTEM_MAX_CHAR = 10;
/** External tracking id */
@@ -80,20 +80,20 @@ public final class TrackingId {
protected Change.Id changeId;
@Column(id = 2)
protected Id trackingId;
protected Id trackingKey;
@Column(id = 3)
protected System trackingSystem;
protected Key() {
changeId = new Change.Id();
trackingId = new Id();
trackingKey = new Id();
trackingSystem = new System();
}
protected Key(final Change.Id ch, final Id id, final System s) {
changeId = ch;
trackingId = id;
trackingKey = id;
trackingSystem = s;
}
@@ -103,7 +103,7 @@ public final class TrackingId {
}
public TrackingId.Id getTrackingId() {
return trackingId;
return trackingKey;
}
public TrackingId.System getTrackingSystem() {
@@ -112,7 +112,7 @@ public final class TrackingId {
@Override
public com.google.gwtorm.client.Key<?>[] members() {
return new com.google.gwtorm.client.Key<?>[] {trackingId, trackingSystem};
return new com.google.gwtorm.client.Key<?>[] {trackingKey, trackingSystem};
}
}
@@ -140,7 +140,7 @@ public final class TrackingId {
}
public String getTrackingId() {
return key.trackingId.get();
return key.trackingKey.get();
}
public String getSystem() {

View File

@@ -29,7 +29,7 @@ public interface TrackingIdAccess extends Access<TrackingId, TrackingId.Key> {
@Query("WHERE key.changeId = ?")
ResultSet<TrackingId> byChange(Change.Id change) throws OrmException;
@Query("WHERE key.trackingId = ?")
@Query("WHERE key.trackingKey = ?")
ResultSet<TrackingId> byTrackingId(TrackingId.Id trackingId)
throws OrmException;
}

View File

@@ -146,8 +146,8 @@ ON patch_set_ancestors (ancestor_revision);
-- *********************************************************************
-- TrackingIdAccess
--
CREATE INDEX tracking_ids_byTrkId
ON tracking_ids (tracking_id);
CREATE INDEX tracking_ids_byTrkKey
ON tracking_ids (tracking_key);
-- *********************************************************************

View File

@@ -229,8 +229,8 @@ ON patch_set_ancestors (ancestor_revision);
-- *********************************************************************
-- TrackingIdAccess
--
CREATE INDEX tracking_ids_byTrkId
ON tracking_ids (tracking_id);
CREATE INDEX tracking_ids_byTrkKey
ON tracking_ids (tracking_key);
-- *********************************************************************

View File

@@ -32,7 +32,7 @@ import java.util.List;
/** A version of the database schema. */
public abstract class SchemaVersion {
/** The current schema version. */
public static final Class<Schema_69> C = Schema_69.class;
public static final Class<Schema_70> C = Schema_70.class;
public static class Module extends AbstractModule {
@Override

View File

@@ -0,0 +1,61 @@
// 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
}
}
}