/** * Returns a {@link com.datastax.driver.core.BatchStatement} with all the alter statements necessary to modify an existing table. * <p> * Note: Columns will only be added or modified, not dropped. * * @param existing the existing table to be modified * @param desired the desired end result * @return a set of statements to modify an existing table */ public static List<AlterTable> alter(TableMetadata existing, CreateTable desired) { List<AlterTable> results = new ArrayList<>(); for (BuiltTableStatement.Column column : desired.getColumns()) { ColumnMetadata columnMetadata = existing.getColumn(column.getName()); if (columnMetadata == null) { results.add(alter(desired.getKeyspace(), desired.getTable()).addColumn(column.getName(), column.getType())); } else if (!columnMetadata.getType().toString().equalsIgnoreCase(column.getType())) { if (columnMetadata.isStatic()) { throw new IllegalArgumentException("A static column cannot have its type modified"); } results.add(alter(desired.getKeyspace(), desired.getTable()).alterColumn(column.getName(), column.getType())); } } return results; }
/** * Returns a {@link com.datastax.driver.core.BatchStatement} with all the alter statements necessary to modify an existing table. * <p> * Note: Columns will only be added or modified, not dropped. * * @param existing the existing table to be modified * @param desired the desired end result * @return a set of statements to modify an existing table */ public static List<AlterTable> alter(AbstractTableMetadata existing, CreateTable desired) { List<AlterTable> results = new ArrayList<>(); for (BuiltTableStatement.Column column : desired.getColumns()) { ColumnMetadata columnMetadata = existing.getColumn(column.getName()); if (columnMetadata == null) { results.add(alter(desired.getKeyspace(), desired.getTable()).addColumn(column.getName(), column.getType())); } else if (!columnMetadata.getType().toString().equalsIgnoreCase(column.getType())) { if (columnMetadata.isStatic()) { throw new IllegalArgumentException("A static column cannot have its type modified"); } results.add(alter(desired.getKeyspace(), desired.getTable()).alterColumn(column.getName(), column.getType())); } } return results; }
/** * Returns a {@link com.datastax.driver.core.BatchStatement} with all the alter statements necessary to modify an existing table. * <p> * Note: Columns will only be added or modified, not dropped. * * @param existing the existing table to be modified * @param desired the desired end result * @return a set of statements to modify an existing table */ public static List<AlterTable> alter(AbstractTableMetadata existing, CreateTable desired) { List<AlterTable> results = new ArrayList<>(); for (BuiltTableStatement.Column column : desired.getColumns()) { ColumnMetadata columnMetadata = existing.getColumn(column.getName()); if (columnMetadata == null) { results.add(alter(desired.getKeyspace(), desired.getTable()).addColumn(column.getName(), column.getType())); } else if (!columnMetadata.getType().toString().equalsIgnoreCase(column.getType())) { if (columnMetadata.isStatic()) { throw new IllegalArgumentException("A static column cannot have its type modified"); } results.add(alter(desired.getKeyspace(), desired.getTable()).alterColumn(column.getName(), column.getType())); } } return results; }
@Test public void testAdd() throws Exception { AlterTable alter = TableBuilder.alter("test_keyspace", "test_table") .addColumn("col1", "text"); String cql = alter.getQueryString(); assertEquals("ALTER TABLE test_keyspace.test_table ADD col1 text", cql); }