@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()); } }
@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; }
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; } }
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; }  }
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); }
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; }