MySQL: Fix account_group_members_audit removed_on to be NULL

MySQL created this column as NOT NULL with a default timestamp
of "NOW" even though we didn't ask for that when we created our
table.  Its a documented "feature" of the MySQL 5.0 software[1].

The latest gwtorm snapshot knows how to declare the column to
be NULL DEFAULT NULL, but won't upgrade the existing column as
it doesn't know how to modify column types on the fly.  We also
need to set all values back to NULL once the column is ready.

[1] http://dev.mysql.com/doc/refman/5.0/en/timestamp.html

Bug: issue 419
Change-Id: I2cabb83e66fa9b4b248aad312b9498530b969901
Signed-off-by: Shawn O. Pearce <sop@google.com>
Reviewed-by: Nico Sallembien <nsallembien@google.com>
This commit is contained in:
Shawn O. Pearce
2010-01-27 17:44:48 -08:00
parent d9d7fe02b0
commit 0df4ba0ad0
2 changed files with 48 additions and 1 deletions

View File

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

View File

@@ -0,0 +1,47 @@
// Copyright (C) 2010 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.ReviewDb;
import com.google.gwtorm.jdbc.JdbcSchema;
import com.google.gwtorm.schema.sql.DialectMySQL;
import com.google.inject.Inject;
import com.google.inject.Provider;
import java.sql.SQLException;
import java.sql.Statement;
class Schema_26 extends SchemaVersion {
@Inject
Schema_26(Provider<Schema_25> prior) {
super(prior);
}
@Override
protected void migrateData(ReviewDb db) throws SQLException {
if (((JdbcSchema) db).getDialect() instanceof DialectMySQL) {
Statement stmt = ((JdbcSchema) db).getConnection().createStatement();
try {
stmt.execute("ALTER TABLE account_group_members_audit" //
+ " MODIFY removed_on TIMESTAMP NULL DEFAULT NULL");
stmt.execute("UPDATE account_group_members_audit" //
+ " SET removed_on = NULL" //
+ " WHERE removed_by IS NULL;");
} finally {
stmt.close();
}
}
}
}