Commits

tbrugz committed 9dfd092

sqldiff: better column diff, some refactoring

Comments (0)

Files changed (4)

src_diff/tbrugz/sqldiff/model/DBIdentifiableDiff.java

 import tbrugz.sqldump.dbmodel.DBObjectType;
 
 public class DBIdentifiableDiff implements Diff, Comparable<DBIdentifiableDiff> {
-	ChangeType changeType;
-	DBIdentifiable ident;
-	String ownerTableName;
+	final ChangeType changeType;
+	final DBIdentifiable ident;
+	final String ownerTableName;
 
 	public DBIdentifiableDiff(ChangeType changeType, DBIdentifiable ident, String ownerTableName) {
 		this.changeType = changeType;

src_diff/tbrugz/sqldiff/model/SchemaDiff.java

 	static Log log = LogFactory.getLog(SchemaDiff.class);
 
 	//XXX: should be List<>?
-	Set<TableDiff> tableDiffs = new TreeSet<TableDiff>();
-	Set<TableColumnDiff> columnDiffs = new TreeSet<TableColumnDiff>();
-	Set<DBIdentifiableDiff> dbidDiffs = new TreeSet<DBIdentifiableDiff>();
+	final Set<TableDiff> tableDiffs = new TreeSet<TableDiff>();
+	final Set<TableColumnDiff> columnDiffs = new TreeSet<TableColumnDiff>();
+	final Set<DBIdentifiableDiff> dbidDiffs = new TreeSet<DBIdentifiableDiff>();
 
 	public static DBObject findDBObjectBySchemaAndName(Collection<? extends DBObject> col, String schemaName, String name) {
 		for(DBObject obj: col) {
 		return null;
 	}
 
+	@SuppressWarnings("unchecked")
 	public static <T extends DBIdentifiable> T getDBIdentifiableByTypeSchemaAndName(Collection<? extends DBIdentifiable> dbids, DBObjectType type, String schemaName, String name) {
 		for(DBIdentifiable d: dbids) {
 			if(type.equals(DBIdentifiable.getType4Diff(d)) 

src_diff/tbrugz/sqldiff/model/TableColumnDiff.java

 import tbrugz.sqldump.dbmodel.Table;
 
 public class TableColumnDiff extends DBObject implements Diff {
-	ChangeType type; //ADD, ALTER, RENAME, DROP;
-	Column column;
+	private static final long serialVersionUID = 1L;
 	
-	//XXX: instead of renameFrom, add "Column from, to"?
-	String renameFrom;
+	final ChangeType type; //ADD, ALTER, RENAME, DROP;
+	final Column column;
+	final Column previousColumn;
+	
+	static boolean addComments = true;
 
-	public TableColumnDiff(ChangeType changeType, Table table, Column newColumn) {
+	public TableColumnDiff(ChangeType changeType, Table table, Column oldColumn, Column newColumn) {
 		this.type = changeType;
 		this.setName(table.getName());
 		this.setSchemaName(table.getSchemaName());
 		this.column = newColumn;
+		this.previousColumn = oldColumn;
 	}
 	
 	@Override
 				colChange = "add column "+Column.getColumnDesc(column); break; //COLUMN "+column.name+" "+column.type;
 			case ALTER:
 				//XXX: option: rename old, create new, update new from old, drop old
-				colChange = "alter column "+Column.getColumnDesc(column); break; //COLUMN "+column.name+" "+column.type; break;
+				colChange = "alter column "+Column.getColumnDesc(column);
+				if(addComments) {
+					colChange += " /* from: "+Column.getColumnDesc(previousColumn)+" */";
+				}
+				break; //COLUMN "+column.name+" "+column.type; break;
 			case RENAME:
-				colChange = "rename column "+renameFrom+" TO "+column.getName(); break;
+				//colChange = "rename column "+(previousColumn!=null?previousColumn.getName():"[unknown]")+" TO "+column.getName(); break;
+				colChange = "rename column "+previousColumn.getName()+" TO "+column.getName(); break;
 			case DROP:
-				colChange = "drop column "+column.getName(); break;
+				colChange = "drop column "+previousColumn.getName(); break;
 		}
 		return "alter table "+(getSchemaName()!=null?getSchemaName()+".":"")+getName()+" "+colChange;
 	}
 				TableColumnDiff tcd = (TableColumnDiff) o;
 				comp = type.compareTo(tcd.type);
 				if(comp==0) {
+					if(column==null || tcd.column==null) return 0;
 					return column.getName().compareTo(tcd.column.getName());
 				}
 			}

src_diff/tbrugz/sqldiff/model/TableDiff.java

 public class TableDiff implements Diff, Comparable<TableDiff> {
 	static Log log = LogFactory.getLog(TableDiff.class);
 
-	ChangeType diffType; //ADD, ALTER, RENAME, DROP;
-	String renameFrom;
-	Table table;
+	final ChangeType diffType; //ADD, ALTER, RENAME, DROP;
+	final String renameFrom;
+	final Table table;
 
 	public TableDiff(ChangeType changeType, Table table) {
 		this.diffType = changeType;
 		this.table = table;
+		this.renameFrom = null; //XXX: add constructor parameter?
 	}
 	
 	@Override
 			Column cNew = SchemaDiff.getDBIdentifiableByTypeSchemaAndName(newTable.getColumns(), DBObjectType.COLUMN, origTable.getSchemaName(), cOrig.getName());
 			if(cNew!=null) {
 				newColumnsThatExistsInOrigModel.add(cNew);
-				boolean equal = cNew.equals(cOrig);
+				//boolean equal = cNew.equals(cOrig);
+				boolean equal = Column.getColumnDesc(cOrig).equals(Column.getColumnDesc(cNew));
 				if(!equal) {
 					//alter column
 					log.debug("alter column: orig: "+cOrig+" new: "+cNew);
-					TableColumnDiff tcd = new TableColumnDiff(ChangeType.ALTER, newTable, cNew);
+					TableColumnDiff tcd = new TableColumnDiff(ChangeType.ALTER, newTable, cOrig, cNew);
 					diffs.add(tcd);
 				}
 				//else {
 			}
 			else {
 				log.debug("drop column: orig: "+cOrig);
-				TableColumnDiff tcd = new TableColumnDiff(ChangeType.DROP, origTable, cOrig);
+				TableColumnDiff tcd = new TableColumnDiff(ChangeType.DROP, origTable, cOrig, null);
 				diffs.add(tcd);
 			}
 		}
 		for(Column cNew: newTable.getColumns()) {
 			if(newColumnsThatExistsInOrigModel.contains(cNew)) { continue; }
 			log.debug("add column: new: "+cNew);
-			TableColumnDiff tcd = new TableColumnDiff(ChangeType.ADD, newTable, cNew);
+			TableColumnDiff tcd = new TableColumnDiff(ChangeType.ADD, newTable, null, cNew);
 			diffs.add(tcd);
 		}