Commits

tbrugz committed fe5c279

sqldiff: refactorings & comments added to DBIdentifiableDiff

  • Participants
  • Parent commits 515d8cf

Comments (0)

Files changed (3)

src_diff/tbrugz/sqldiff/model/DBIdentifiableDiff.java

 import tbrugz.sqldump.dbmodel.DBIdentifiable;
 import tbrugz.sqldump.dbmodel.DBObjectType;
 
+/*
+ * XXX option to change 'new:' & 'old:' xtra comments?
+ */
 public class DBIdentifiableDiff implements Diff, Comparable<DBIdentifiableDiff> {
 	final ChangeType changeType;
 	final DBIdentifiable ident;
+	final DBIdentifiable previousIdent;
 	final String ownerTableName;
+	
+	static boolean addComments = true;
 
-	public DBIdentifiableDiff(ChangeType changeType, DBIdentifiable ident, String ownerTableName) {
+	public DBIdentifiableDiff(ChangeType changeType, DBIdentifiable previousIdent, DBIdentifiable ident, String ownerTableName) {
 		this.changeType = changeType;
+		this.previousIdent = previousIdent;
 		this.ident = ident;
 		this.ownerTableName = ownerTableName;
 	}
 
-	public DBIdentifiableDiff(ChangeType changeType, DBIdentifiable ident) {
-		this(changeType, ident, null);
+	public DBIdentifiableDiff(ChangeType changeType, DBIdentifiable previousIdent, DBIdentifiable ident) {
+		this(changeType, previousIdent, ident, null);
 	}
 	
 	@Override
 	@Override
 	public String getDiff() {
 		switch(changeType) {
-			case ADD: return (ownerTableName!=null?"alter table "+ownerTableName+" add ":"")+ident.getDefinition(true).trim();
+			case ADD: return (ownerTableName!=null?"alter table "+ownerTableName+" add ":"")
+					+ ident.getDefinition(true).trim()
+					+ (addComments?getComment(previousIdent, "old: "):"");
 			//case ALTER:  return "ALTER "+ident.getDefinition(true);
 			//case RENAME:  return "RENAME "+ident.getDefinition(true);
-			case DROP: return (ownerTableName!=null?"alter table "+ownerTableName+" ":"")+"drop "+DBIdentifiable.getType4Diff(ident)+" "+(ident.getSchemaName()!=null?ident.getSchemaName()+".":"")+ident.getName();
+			case DROP: return (ownerTableName!=null?"alter table "+ownerTableName+" ":"")+"drop "
+					+ DBIdentifiable.getType4Diff(previousIdent)+" "+(previousIdent.getSchemaName()!=null?previousIdent.getSchemaName()+".":"")+previousIdent.getName()
+					+ (addComments?getComment(ident, "new: "):"");
 		}
-		throw new RuntimeException("changetype "+changeType+" not defined on DBId.getDiff()");
+		throw new RuntimeException("changetype "+changeType+" not defined on DBIdentifiableDiff.getDiff()");
 	}
 	
 	@Override
 		if(obj instanceof DBIdentifiableDiff) {
 			DBIdentifiableDiff dbid = (DBIdentifiableDiff) obj;
 			if(changeType.equals(dbid.changeType)) {
-				return ident.equals(dbid.ident);
+				if(ident!=null) { return ident.equals(dbid.ident); }
+				else { return previousIdent.equals(dbid.previousIdent); }
 			}
 			return false;
 		}
 	public int compareTo(DBIdentifiableDiff o) {
 		int comp = changeType.compareTo(o.changeType);
 		if(comp==0) {
-			return ident.getName().compareTo(o.ident.getName());
+			return ident().getName().compareTo(o.ident().getName());
 		}
 		return comp;
 	}
 
 	@Override
 	public DBObjectType getObjectType() {
-		//return DBIdentifiable.getType(ident);
-		return DBIdentifiable.getType4Diff(ident); //XXX: getType() or getType4Diff()? '4Diff' is better for logging...
+		return DBIdentifiable.getType4Diff(ident()); //XXX: getType() or getType4Diff()? '4Diff' is better for logging...
 	}
+	
+	public DBIdentifiable ident() {
+		return ident!=null?ident:previousIdent;
+	}
+	
+	static String getComment(DBIdentifiable dbident, String comment) {
+		if(dbident==null) return "";
+		return "\n/* "+comment
+				+ DBIdentifiable.getType4Diff(dbident)+" "
+				+ (dbident.getSchemaName()!=null?dbident.getSchemaName()+".":"")+dbident.getName()
+				+ " */";
+	}
+	
 }

src_diff/tbrugz/sqldiff/model/SchemaDiff.java

 import tbrugz.sqldump.dbmodel.FK;
 import tbrugz.sqldump.dbmodel.SchemaModel;
 import tbrugz.sqldump.dbmodel.Table;
+import tbrugz.sqldump.dbmodel.TableType;
 import tbrugz.sqldump.util.CategorizedOut;
 
 //XXX: should SchemaDiff implement Diff?
 		if(modelOrig.getTables().size()>0) {
 			
 		for(Table tOrig: modelOrig.getTables()) {
+			if(! tOrig.getType().equals(TableType.TABLE)) continue;
+			
 			Table tNew = (Table) findDBObjectBySchemaAndName(modelNew.getTables(), tOrig.getSchemaName(), tOrig.getName());
 			if(tNew==null) {
 				//if new table doesn't exist, drop old
 		//TODO: executables: do not dump extra ";"
 		log.info("output other diffs...");
 		for(DBIdentifiableDiff dbid: dbidDiffs) {
-			switch(DBIdentifiable.getType(dbid.ident)) {
+			DBIdentifiable dbident = dbid.ident();
+			switch(DBIdentifiable.getType(dbident)) {
 			case EXECUTABLE:
 			case TRIGGER:
-				out.categorizedOut(dbid.getDiff()+"\n", dbid.ident.getSchemaName(), DBIdentifiable.getType4Diff(dbid.ident).toString());
+				out.categorizedOut(dbid.getDiff()+"\n", dbident.getSchemaName(), DBIdentifiable.getType4Diff(dbident).toString());
 				break;
 			default:
-				out.categorizedOut(dbid.getDiff()+";\n", dbid.ident.getSchemaName(), DBIdentifiable.getType4Diff(dbid.ident).toString());
+				out.categorizedOut(dbid.getDiff()+";\n", dbident.getSchemaName(), DBIdentifiable.getType4Diff(dbident).toString());
 			}
 		}
 	} 

src_diff/tbrugz/sqldiff/model/TableDiff.java

 		diffs(DBObjectType.CONSTRAINT, dbiddiffs, origTable.getConstraints(), newTable.getConstraints(), origTable.getName(), newTable.getName());
 		//FIXedME: schemaname dumps as null
 		for(int i=0;i<dbiddiffs.size();i++) {
-			dbiddiffs.get(i).ident.setSchemaName(newTable.getSchemaName());
+			dbiddiffs.get(i).ident().setSchemaName(newTable.getSchemaName());
 		}
 		diffs.addAll(dbiddiffs);
 		
 				newDBObjectsThatExistsInOrigModel.add(cNew);
 				if(!cOrig.equals(cNew)) {
 					log.debug("drop/add "+objType+": orig: "+cOrig+" new: "+cNew);
-					diffs.add(new DBIdentifiableDiff(ChangeType.DROP, cOrig, origOwnerTableName));
-					diffs.add(new DBIdentifiableDiff(ChangeType.ADD, cNew, newOwnerTableName));
+					diffs.add(new DBIdentifiableDiff(ChangeType.DROP, cOrig, cNew, origOwnerTableName));
+					diffs.add(new DBIdentifiableDiff(ChangeType.ADD, cOrig, cNew, newOwnerTableName));
 				}
 			}
 			else {
 				log.debug("drop "+objType+": orig: "+cOrig);
-				diffs.add(new DBIdentifiableDiff(ChangeType.DROP, cOrig, origOwnerTableName));
+				diffs.add(new DBIdentifiableDiff(ChangeType.DROP, cOrig, null, origOwnerTableName));
 			}
 		}
 		for(DBIdentifiable cNew: listNew) {
 			if(newDBObjectsThatExistsInOrigModel.contains(cNew)) { continue; }
 			log.debug("add "+objType+": new: "+cNew);
-			diffs.add(new DBIdentifiableDiff(ChangeType.ADD, cNew, newOwnerTableName));
+			diffs.add(new DBIdentifiableDiff(ChangeType.ADD, null, cNew, newOwnerTableName));
 		}
 	}