diff options
author | Eike Stepper | 2013-04-10 05:44:06 +0000 |
---|---|---|
committer | Eike Stepper | 2013-04-10 05:44:06 +0000 |
commit | 3b76805b60dc8b72903a0c597bbbbb1c266bff0a (patch) | |
tree | eddd0109ff3f2567ea9871a37b5b4ff58577e69d | |
parent | 0590c495a3fc53eb38dce25001c0ed2da7c80b0a (diff) | |
download | cdo-3b76805b60dc8b72903a0c597bbbbb1c266bff0a.tar.gz cdo-3b76805b60dc8b72903a0c597bbbbb1c266bff0a.tar.xz cdo-3b76805b60dc8b72903a0c597bbbbb1c266bff0a.zip |
[404047] DB-Migration from 4.1 to 4.2 failed
https://bugs.eclipse.org/bugs/show_bug.cgi?id=404047
-rw-r--r-- | plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/DBUtil.java | 94 | ||||
-rw-r--r-- | plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/ddl/DBIndex.java | 25 |
2 files changed, 72 insertions, 47 deletions
diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/DBUtil.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/DBUtil.java index e5aa6c30cd..2678d75db8 100644 --- a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/DBUtil.java +++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/DBUtil.java @@ -11,8 +11,6 @@ package org.eclipse.net4j.db; import org.eclipse.net4j.db.ddl.IDBField; -import org.eclipse.net4j.db.ddl.IDBIndex; -import org.eclipse.net4j.db.ddl.IDBIndexField; import org.eclipse.net4j.db.ddl.IDBNamedElement; import org.eclipse.net4j.db.ddl.IDBSchema; import org.eclipse.net4j.db.ddl.IDBTable; @@ -274,10 +272,15 @@ public final class DBUtil { try { - fixNullableIndexColumns(adapter, connection, schema); + Set<IDBField> nullableIndexFields = DBIndex.NULLABLE_INDEX_FIELDS.get(); + if (nullableIndexFields != null && !nullableIndexFields.isEmpty()) + { + fixNullableIndexFields(adapter, connection, nullableIndexFields); + } } finally { + DBIndex.NULLABLE_INDEX_FIELDS.remove(); DBIndex.FIX_NULLABLE_INDEX_COLUMNS.remove(); } } @@ -286,60 +289,46 @@ public final class DBUtil return schema; } - private static void fixNullableIndexColumns(IDBAdapter adapter, Connection connection, IDBSchema schema) + private static void fixNullableIndexFields(IDBAdapter adapter, Connection connection, + Set<IDBField> nullableIndexFields) { - Statement statement = null; StringBuilder builder = new StringBuilder(); + builder.append("The internal schema migration has fixed the following nullable index columns:"); + builder.append(StringUtil.NL); + + boolean autoCommit = false; + Statement statement = null; try { - for (IDBTable table : schema.getTables()) - { - for (IDBIndex index : table.getIndices()) - { - if (index.getType() != IDBIndex.Type.NON_UNIQUE) - { - for (IDBIndexField indexField : index.getIndexFields()) - { - IDBField field = indexField.getField(); - boolean nullable = !field.isNotNull(); - if (nullable) - { - field.setNotNull(true); + autoCommit = setAutoCommit(connection, false); + statement = connection.createStatement(); - if (statement == null) - { - statement = connection.createStatement(); - builder.append("The internal schema migration has fixed the following nullable index columns:"); - builder.append(StringUtil.NL); - } + for (IDBField field : nullableIndexFields) + { + field.setNotNull(true); - String sql = adapter.sqlModifyField(field); - builder.append("- "); - builder.append(sql); - builder.append(StringUtil.NL); + String sql = adapter.sqlModifyField(field); + statement.execute(sql); - statement.execute(sql); - } - } - } - } + builder.append("- "); + builder.append(sql); + builder.append(StringUtil.NL); } - if (statement != null) - { - connection.commit(); - OM.LOG.info(builder.toString()); - } + connection.commit(); + OM.LOG.info(builder.toString()); } catch (SQLException ex) { + OM.LOG.error(ex); rollback(connection); throw new DBException(ex); } finally { - DBUtil.close(statement); + close(statement); + setAutoCommit(connection, autoCommit); } } @@ -463,6 +452,27 @@ public final class DBUtil return null; } + /** + * @since 4.2 + */ + public static boolean setAutoCommit(Connection connection, boolean autoCommit) + { + try + { + if (connection.getAutoCommit() != autoCommit) + { + connection.setAutoCommit(autoCommit); + return !autoCommit; + } + + return autoCommit; + } + catch (SQLException ex) + { + throw new DBException(ex); + } + } + private static void rollback(Connection connection) { try @@ -588,7 +598,7 @@ public final class DBUtil try { statement = connection.createStatement(); - for (String tableName : DBUtil.getAllTableNames(connection, dbName)) + for (String tableName : getAllTableNames(connection, dbName)) { String sql = "DROP TABLE " + tableName; //$NON-NLS-1$ trace(sql); @@ -609,7 +619,7 @@ public final class DBUtil } finally { - DBUtil.close(statement); + close(statement); } return exceptions; @@ -870,7 +880,7 @@ public final class DBUtil */ public static int update(PreparedStatement stmt, boolean exactlyOne) throws SQLException { - if (DBUtil.isTracerEnabled()) + if (isTracerEnabled()) { trace(stmt.toString()); } diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/ddl/DBIndex.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/ddl/DBIndex.java index 31ea531b4a..c770d73078 100644 --- a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/ddl/DBIndex.java +++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/ddl/DBIndex.java @@ -29,8 +29,10 @@ import java.io.IOException; import java.io.Writer; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashSet; import java.util.Iterator; import java.util.List; +import java.util.Set; /** * @author Eike Stepper @@ -39,6 +41,8 @@ public class DBIndex extends DBSchemaElement implements InternalDBIndex { public static final ThreadLocal<Boolean> FIX_NULLABLE_INDEX_COLUMNS = new InheritableThreadLocal<Boolean>(); + public static final ThreadLocal<Set<IDBField>> NULLABLE_INDEX_FIELDS = new InheritableThreadLocal<Set<IDBField>>(); + private static final boolean DISABLE_NULLABLE_CHECK = Boolean.parseBoolean(OMPlatform.INSTANCE.getProperty( "org.eclipse.net4j.db.DisableNullableCheck", "true")); @@ -118,12 +122,23 @@ public class DBIndex extends DBSchemaElement implements InternalDBIndex { assertUnlocked(); - if (type != Type.NON_UNIQUE && !field.isNotNull() && !DISABLE_NULLABLE_CHECK - && FIX_NULLABLE_INDEX_COLUMNS.get() != Boolean.TRUE) + if (type != Type.NON_UNIQUE && !field.isNotNull()) { - Exception constructionStackTrace = ((InternalDBField)field).getConstructionStackTrace(); - throw new DBException( - "Index field is nullable: " + field + " (to disable this check run with '-Dorg.eclipse.net4j.db.DisableNullableCheck=true')", constructionStackTrace); //$NON-NLS-1$ + if (!DISABLE_NULLABLE_CHECK && FIX_NULLABLE_INDEX_COLUMNS.get() != Boolean.TRUE) + { + Exception constructionStackTrace = ((InternalDBField)field).getConstructionStackTrace(); + throw new DBException( + "Index field is nullable: " + field + " (to disable this check run with '-Dorg.eclipse.net4j.db.DisableNullableCheck=true')", constructionStackTrace); //$NON-NLS-1$ + } + + Set<IDBField> nullableIndexFields = NULLABLE_INDEX_FIELDS.get(); + if (nullableIndexFields == null) + { + nullableIndexFields = new HashSet<IDBField>(); + NULLABLE_INDEX_FIELDS.set(nullableIndexFields); + } + + nullableIndexFields.add(field); } if (field.getTable() != table) |