wt.vc
Class MigrateControlBranches
java.lang.Object
wt.vc.MigrateControlBranches
- All Implemented Interfaces:
- JavaMigrator
- public class MigrateControlBranches
- extends Object
- implements JavaMigrator
Method Summary |
boolean |
runMigration(DirectiveServices directive_services)
main method for initial testing of migrator and associated sql without actually performing the upgrade. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
VERBOSE
private static boolean VERBOSE
IDA2A2
private static final String IDA2A2
- See Also:
- Constant Field Values
BRANCH_ID
private static final String BRANCH_ID
- See Also:
- Constant Field Values
LATEST
private static final String LATEST
- See Also:
- Constant Field Values
TEMP_TABLE_ID_COLUMN_NAME
private static final String TEMP_TABLE_ID_COLUMN_NAME
- See Also:
- Constant Field Values
MigrateControlBranches
public MigrateControlBranches()
runMigration
public boolean runMigration(DirectiveServices directive_services)
throws Throwable
- main method for initial testing of migrator and associated sql without actually performing the upgrade.
If you need to test this you can uncomment this code and execute it and it should return useful output
about the which rows from which tables in the database need updating and what the update sql would look
like to perform the update.
public static void main(String[] args) throws Throwable {
long startTime = System.currentTimeMillis();
PreparedStatement statement;
java.sql.DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
Connection connection = java.sql.DriverManager.getConnection("jdbc:oracle:thin:@dbmachine:1526:dbinstance", "db_username", "db_password");
ClassInfo iteratedClassInfo = WTIntrospector.getClassInfo(Iterated.class);
ClassInfo[] iteratedDescendantsInfos = iteratedClassInfo.getDescendentInfos();
ClassInfo controlBranchClassInfo = WTIntrospector.getClassInfo(ControlBranch.class);
String controlBranchTableName = controlBranchClassInfo.getDatabaseInfo().getBaseTableInfo().getTablename();
String cbIdentifierColumnName = controlBranchClassInfo.getDatabaseInfo().getBaseTableInfo().getColumnDescriptor(IDA2A2).getColumnName();
String tempTableName = controlBranchTableName + "_tmp";
// Create a temporary table to find the dead ControlBranch objects and populate it with the data from
// the existing ControlBranch table.
String create_sql = "CREATE TABLE " + tempTableName + "(" + TEMP_TABLE_ID_COLUMN_NAME + " NUMBER)";
if (VERBOSE)
System.out.println(create_sql);
statement = connection.prepareStatement(create_sql);
statement.execute();
// Copy into the temporary table from the existing ControlBranch table.
String insert_sql = "INSERT INTO " + tempTableName + " (SELECT " + cbIdentifierColumnName + " FROM " + controlBranchTableName + ")";
statement = connection.prepareStatement(insert_sql);
int existing_branches = statement.executeUpdate();
if (VERBOSE) {
System.out.println(insert_sql);
System.out.println("Copied " + existing_branches + " existing ControlBranch objects to temp table.");
}
// Loop through the Iterated descendants and delete the existing valid control branch
// objects from the temporary table based on what is found in each concrete table.
for (int i = 0; i < iteratedDescendantsInfos.length; i++) {
// If the current descendent in not concrete or is not persistable ignore it.
if (!iteratedDescendantsInfos[i].isConcrete() || !iteratedDescendantsInfos[i].isPersistable())
continue;
String concreteIteratedClassTableName = iteratedDescendantsInfos[i].getDatabaseInfo().getBaseTableInfo().getTablename();
String branchIdIterationInfoColumnName = iteratedDescendantsInfos[i].getDatabaseInfo().getBaseTableInfo().getColumnDescriptor(BRANCH_ID).getColumnName();
String latestIterationColumnName = iteratedDescendantsInfos[i].getDatabaseInfo().getBaseTableInfo().getColumnDescriptor(LATEST).getColumnName();
String delete_sql = "DELETE FROM " + tempTableName + " WHERE " + TEMP_TABLE_ID_COLUMN_NAME + " IN (SELECT " +
branchIdIterationInfoColumnName + " FROM " + concreteIteratedClassTableName + " WHERE " +
latestIterationColumnName + "=1)";
if (VERBOSE)
System.out.println(delete_sql);
statement = connection.prepareStatement(delete_sql);
statement.executeUpdate();
}
// What is left in the temporary table is the identifiers of the dead control branches.
// Remove them from the real table.
String cleanup_sql = "DELETE FROM " + controlBranchTableName + " WHERE " + cbIdentifierColumnName + " IN (SELECT "+
TEMP_TABLE_ID_COLUMN_NAME + " FROM " + tempTableName + ")";
statement = connection.prepareStatement(cleanup_sql);
int dead_branches = statement.executeUpdate();
if (VERBOSE) {
System.out.println(cleanup_sql);
System.out.println("Cleaned up " + dead_branches + " orphaned ControlBranch objects.");
}
// Lastly, drop the temporary table that was created.
String drop_sql = "DROP TABLE " + tempTableName;
if (VERBOSE)
System.out.println(drop_sql);
statement = connection.prepareStatement(drop_sql);
statement.execute();
connection.commit();
if (VERBOSE) {
long endTime = System.currentTimeMillis();
long totalTime = endTime - startTime;
double minutes = (totalTime/60000.00);
System.out.println("Operation took: " + minutes + " minutes.");
}
System.exit(0);
}
- Specified by:
runMigration
in interface JavaMigrator
- Parameters:
directive_services
- provide access to database, logging, and versioning information
- Throws:
Throwable
- declared so that migrators can let exceptions contibuting to failure pass out of the method. The migrator runner will handle these by logging them.