CommonAlterTableSqlBuilder implements AlterTableSqlBuilder { @Override public SqlRequest build(AlterRequest parameter) { RDBTableMetadata newTable = parameter.getNewTable(); RDBTableMetadata oldTable = parameter.getOldTable(); DefaultBatchSqlRequest batch = new DefaultBatchSqlRequest(); for (RDBColumnMetadata oldColumn : oldTable.getColumns()) { RDBColumnMetadata newColumn = newTable.getColumn(oldColumn.getName()).orElse(null); if (newColumn == null) { if (parameter.isAllowDrop()) { appendDropColumnSql(batch, oldColumn); } continue; } if (parameter.isAllowAlter() && !oldColumn.isPrimaryKey() && oldColumn.ddlModifiable(newColumn)) { appendAlterColumnSql(batch, oldColumn, newColumn); } if (newColumn.getComment() != null && !newColumn.getComment().equals(oldColumn.getComment())) { appendAddColumnCommentSql(batch, newColumn); } } for (RDBColumnMetadata newColumn : newTable.getColumns()) { if (!oldTable.getColumn(newColumn.getName()).isPresent()) { appendAddColumnSql(batch, newColumn); appendAddColumnCommentSql(batch, newColumn); } } for (RDBIndexMetadata index : newTable.getIndexes()) { if (index.isPrimaryKey()) { continue; } RDBIndexMetadata oldIndex = oldTable.getIndex(index.getName()).orElse(null); if (oldIndex == null) { appendAddIndexSql(batch, newTable, index); continue; } if (index.isChanged(oldIndex)) { appendDropIndexSql(batch, newTable, index); appendAddIndexSql(batch, newTable, index); } } return batch; }  @Override SqlRequest build(AlterRequest parameter); static final CommonAlterTableSqlBuilder INSTANCE; }
@Test public void testIndex() { RDBSchemaMetadata schema = MetadataHelper.createMockSchema(); CommonAlterTableSqlBuilder builder = new CommonAlterTableSqlBuilder(); RDBTableMetadata old = schema.getTable("test").orElseThrow(NullPointerException::new); RDBTableMetadata copy = old.clone(); { RDBIndexMetadata index = new RDBIndexMetadata(); index.setName("test_index"); index.setAlias("test"); index.setColumns(Collections.singletonList(RDBIndexMetadata.IndexColumn.of("name", RDBIndexMetadata.IndexSort.asc))); copy.addIndex(index); SqlRequest sqlRequest = builder.build(AlterRequest.builder() .oldTable(old) .newTable(copy) .build()); Assert.assertTrue(sqlRequest.isNotEmpty()); } }
@Test public void test() { RDBSchemaMetadata schema = MetadataHelper.createMockSchema(); CommonAlterTableSqlBuilder builder = new CommonAlterTableSqlBuilder(); RDBTableMetadata old = schema.getTable("test").orElseThrow(NullPointerException::new); RDBTableMetadata copy = old.clone(); { RDBColumnMetadata test = new RDBColumnMetadata(); test.setName("test"); test.setJdbcType(JDBCType.VARCHAR, String.class); test.setLength(32); test.setComment("test"); copy.addColumn(test); SqlRequest sql = builder.build(AlterRequest.builder() .oldTable(old) .newTable(copy) .build()); System.out.println(sql); List<SqlRequest> sqlList = ((BatchSqlRequest) sql).getBatch(); Assert.assertEquals(sqlList.size(), 1); Assert.assertEquals(sql.getSql(), "alter table PUBLIC.test add \"TEST\" varchar(32)"); Assert.assertEquals(sqlList.get(0).getSql(), "comment on column test.\"TEST\" is 'test'"); } { copy = old.clone(); copy.getColumn("name").ifPresent(name -> name.setLength(200)); SqlRequest sql = builder.build(AlterRequest.builder() .allowAlter(true) .oldTable(old) .newTable(copy) .build()); Assert.assertEquals(sql.getSql(), "alter table PUBLIC.test modify \"NAME\" varchar(200) null"); } { copy = old.clone(); copy.removeColumn("name"); SqlRequest sql = builder.build(AlterRequest.builder() .oldTable(old) .newTable(copy) .allowDrop(true) .build()); Assert.assertEquals(sql.getSql(), "alter table PUBLIC.test drop column \"NAME\""); } }