22
22
23
23
import com .aliyun .fastmodel .compare .impl .table .ColumnCompare ;
24
24
import com .aliyun .fastmodel .core .tree .BaseStatement ;
25
+ import com .aliyun .fastmodel .core .tree .Comment ;
25
26
import com .aliyun .fastmodel .core .tree .Property ;
26
27
import com .aliyun .fastmodel .core .tree .QualifiedName ;
27
28
import com .aliyun .fastmodel .core .tree .datatype .BaseDataType ;
30
31
import com .aliyun .fastmodel .core .tree .datatype .GenericDataType ;
31
32
import com .aliyun .fastmodel .core .tree .datatype .TypeParameter ;
32
33
import com .aliyun .fastmodel .core .tree .expr .Identifier ;
34
+ import com .aliyun .fastmodel .core .tree .expr .literal .StringLiteral ;
33
35
import com .aliyun .fastmodel .core .tree .statement .constants .ColumnCategory ;
34
36
import com .aliyun .fastmodel .core .tree .statement .constants .ColumnPropertyDefaultKey ;
37
+ import com .aliyun .fastmodel .core .tree .statement .table .ChangeCol ;
38
+ import com .aliyun .fastmodel .core .tree .statement .table .ChangeCol .ChangeType ;
35
39
import com .aliyun .fastmodel .core .tree .statement .table .ColumnDefinition ;
36
40
import com .aliyun .fastmodel .core .tree .statement .table .CreateTable ;
37
41
import com .aliyun .fastmodel .core .tree .util .DataTypeUtil ;
41
45
import org .junit .Test ;
42
46
43
47
import static org .junit .Assert .assertEquals ;
48
+ import static org .junit .Assert .assertFalse ;
49
+ import static org .junit .Assert .assertTrue ;
44
50
45
51
/**
46
52
* ColumnCompareTest
@@ -163,7 +169,7 @@ public void compareOrder() {
163
169
164
170
List <BaseStatement > baseStatementList = columnCompare .compareTableElement (before , after );
165
171
String join = Joiner .on ("\n " ).join (baseStatementList );
166
- assertEquals (join , "ALTER TABLE dim_shop CHANGE COLUMN c2 c2 BIGINT FIRST" );
172
+ assertEquals (join , "ALTER TABLE dim_shop CHANGE COLUMN c2 c2 BIGINT FIRST" );
167
173
}
168
174
169
175
@ Test
@@ -194,7 +200,6 @@ public void testCompareNode2() {
194
200
.columns (beforeColumns )
195
201
.tableName (QualifiedName .of ("dim_shop" )).build ();
196
202
197
-
198
203
CreateTable after = CreateTable .builder ()
199
204
.columns (afterColumns )
200
205
.tableName (QualifiedName .of ("dim_shop" )).build ();
@@ -209,7 +214,6 @@ public void testCompareNode2() {
209
214
+ "ALTER TABLE dim_shop CHANGE COLUMN c c BIGINT AFTER d" );
210
215
}
211
216
212
-
213
217
private static ColumnDefinition getColumn (String colName ) {
214
218
return ColumnDefinition .builder ().colName (new Identifier (colName ))
215
219
.dataType (DataTypeUtil .simpleType (DataTypeEnums .BIGINT ))
@@ -344,5 +348,75 @@ public void testComparePropertiesBeforeNull() {
344
348
assertEquals (1 , baseStatementList .size ());
345
349
String join = Joiner .on ("\n " ).join (baseStatementList );
346
350
assertEquals (join , "ALTER TABLE dim_shop CHANGE COLUMN c1 c1 BIGINT WITH ('code_table'='a')" );
351
+ BaseStatement baseStatement = baseStatementList .get (0 );
352
+ ChangeCol changeCol = (ChangeCol )baseStatement ;
353
+ boolean change = changeCol .change (ChangeType .COLUMN_PROPERTIES );
354
+ assertTrue (change );
355
+ change = changeCol .change (ChangeType .COMMENT );
356
+ assertFalse (change );
357
+ }
358
+
359
+ @ Test
360
+ public void testCompareDefaultValue () {
361
+ ColumnDefinition columnDefinition = ColumnDefinition .builder ()
362
+ .colName (new Identifier ("c1" ))
363
+ .defaultValue (new StringLiteral ("abc" ))
364
+ .dataType (DataTypeUtil .simpleType (DataTypeEnums .BIGINT ))
365
+ .build ();
366
+ ColumnDefinition columnDefinition2 = ColumnDefinition .builder ()
367
+ .colName (new Identifier ("c1" ))
368
+ .dataType (DataTypeUtil .simpleType (DataTypeEnums .BIGINT ))
369
+ .defaultValue (new StringLiteral ("bcd" ))
370
+ .build ();
371
+ CreateTable before = CreateTable .builder ()
372
+ .columns (ImmutableList .of (columnDefinition ))
373
+ .tableName (QualifiedName .of ("dim_shop" )).build ();
374
+
375
+ CreateTable after = CreateTable .builder ()
376
+ .columns (ImmutableList .of (columnDefinition2 ))
377
+ .tableName (QualifiedName .of ("dim_shop" )).build ();
378
+
379
+ List <BaseStatement > baseStatementList = columnCompare .compareTableElement (before , after );
380
+ assertEquals (1 , baseStatementList .size ());
381
+ String join = Joiner .on ("\n " ).join (baseStatementList );
382
+ assertEquals (join , "ALTER TABLE dim_shop CHANGE COLUMN c1 c1 BIGINT DEFAULT 'bcd'" );
383
+ BaseStatement baseStatement = baseStatementList .get (0 );
384
+ assertTrue (baseStatement instanceof ChangeCol );
385
+ ChangeCol changeCol = (ChangeCol )baseStatement ;
386
+ boolean change = changeCol .change (ChangeType .DEFAULT_VALUE );
387
+ assertTrue (change );
388
+ change = changeCol .change (ChangeType .COMMENT );
389
+ assertFalse (change );
390
+ }
391
+
392
+ @ Test
393
+ public void testCompareComment () {
394
+ ColumnDefinition columnDefinition = ColumnDefinition .builder ()
395
+ .colName (new Identifier ("c1" ))
396
+ .comment (new Comment ("abc" ))
397
+ .dataType (DataTypeUtil .simpleType (DataTypeEnums .BIGINT ))
398
+ .build ();
399
+ ColumnDefinition columnDefinition2 = ColumnDefinition .builder ()
400
+ .colName (new Identifier ("c1" ))
401
+ .comment (new Comment ("bcd" ))
402
+ .dataType (DataTypeUtil .simpleType (DataTypeEnums .BIGINT ))
403
+ .build ();
404
+ CreateTable before = CreateTable .builder ()
405
+ .columns (ImmutableList .of (columnDefinition ))
406
+ .tableName (QualifiedName .of ("dim_shop" )).build ();
407
+
408
+ CreateTable after = CreateTable .builder ()
409
+ .columns (ImmutableList .of (columnDefinition2 ))
410
+ .tableName (QualifiedName .of ("dim_shop" )).build ();
411
+
412
+ List <BaseStatement > baseStatementList = columnCompare .compareTableElement (before , after );
413
+ assertEquals (1 , baseStatementList .size ());
414
+ String join = Joiner .on ("\n " ).join (baseStatementList );
415
+ assertEquals (join , "ALTER TABLE dim_shop CHANGE COLUMN c1 c1 BIGINT COMMENT 'bcd'" );
416
+ BaseStatement baseStatement = baseStatementList .get (0 );
417
+ assertTrue (baseStatement instanceof ChangeCol );
418
+ ChangeCol changeCol = (ChangeCol )baseStatement ;
419
+ boolean change = changeCol .change (ChangeType .COMMENT );
420
+ assertTrue (change );
347
421
}
348
422
}
0 commit comments