3
3
import datastax .astra .migrate .MigrateDataType ;
4
4
import datastax .astra .migrate .properties .KnownProperties ;
5
5
import datastax .astra .migrate .properties .PropertyHelper ;
6
- import org .apache .commons .lang .StringUtils ;
7
6
import org .slf4j .Logger ;
8
7
import org .slf4j .LoggerFactory ;
9
8
@@ -17,10 +16,16 @@ public class ConstantColumns extends AbstractFeature {
17
16
public enum Property {
18
17
COLUMN_NAMES ,
19
18
COLUMN_TYPES ,
20
- COLUMN_VALUES ,
21
- TARGET_PRIMARY_TYPES_WITHOUT_CONSTANT
19
+ COLUMN_VALUES
22
20
}
23
21
22
+ public enum Function {
23
+ TARGET_PK_WITHOUT_CONSTANTS ,
24
+ TEST_FUNCTION
25
+ }
26
+
27
+ private boolean valid = true ;
28
+
24
29
@ Override
25
30
public boolean initialize (PropertyHelper propertyHelper ) {
26
31
List <String > columnNames = propertyHelper .getStringList (KnownProperties .CONSTANT_COLUMN_NAMES );
@@ -34,17 +39,26 @@ public boolean initialize(PropertyHelper propertyHelper) {
34
39
propertyHelper .getString (KnownProperties .CONSTANT_COLUMN_SPLIT_REGEX ));
35
40
putStringList (Property .COLUMN_VALUES , columnValues );
36
41
37
- List <MigrateDataType > targetPrimaryKeyTypesWithoutConstantColumns =
38
- targetPrimaryKeyTypesWithoutConstantColumns (
39
- columnNames ,
40
- propertyHelper .getMigrationTypeList (KnownProperties .TARGET_PRIMARY_KEY_TYPES ),
41
- propertyHelper .getStringList (KnownProperties .TARGET_PRIMARY_KEY ));
42
- putMigrateDataTypeList (Property .TARGET_PRIMARY_TYPES_WITHOUT_CONSTANT , targetPrimaryKeyTypesWithoutConstantColumns );
43
-
44
42
isInitialized = true ;
45
- if (!isValid (propertyHelper )) return false ;
46
- isEnabled = null !=columnNames && !columnNames .isEmpty ();
47
- return true ;
43
+ valid = isValid (propertyHelper );
44
+ isEnabled = valid && null !=columnNames && !columnNames .isEmpty ();
45
+ return valid ;
46
+ }
47
+
48
+ @ Override
49
+ public Object featureFunction (Enum <?> function , Object ... args ) {
50
+ switch ((Function ) function ) {
51
+ case TARGET_PK_WITHOUT_CONSTANTS :
52
+ // args[] should be List<MigrateDataType> targetPrimaryKeyTypes, List<String> targetPrimaryKeyNames
53
+ if (null ==args || args .length !=2 || null ==args [0 ] || null ==args [1 ])
54
+ throw new IllegalArgumentException ("Expected 2 not-null arguments, got " + (null ==args ? "1" : args .length ));
55
+ if (!(args [0 ] instanceof List <?>) || ((List <?>) args [0 ]).isEmpty () || !(((List <?>) args [0 ]).get (0 ) instanceof MigrateDataType ))
56
+ throw new IllegalArgumentException ("First argument should be a non-empty List<MigrateDataType>, got " + args [0 ]);
57
+ if (!(args [1 ] instanceof List <?>) || ((List <?>) args [1 ]).isEmpty () || !(((List <?>) args [1 ]).get (0 ) instanceof String ))
58
+ throw new IllegalArgumentException ("Second argument should be a non-empty List<String>, got " + args [1 ]);
59
+ return targetPrimaryKeyTypesWithoutConstantColumns ((List <MigrateDataType >)args [0 ], (List <String >)args [1 ]);
60
+ }
61
+ return null ;
48
62
}
49
63
50
64
private List <String > columnValues (String columnValueString , String regexString ) {
@@ -61,21 +75,17 @@ private List<String> columnValues(String columnValueString, String regexString)
61
75
return columnValues ;
62
76
}
63
77
64
- private List <MigrateDataType > targetPrimaryKeyTypesWithoutConstantColumns (List <String > columnNames , List <MigrateDataType > targetPrimaryKeyTypes , List <String > targetPrimaryKeyNames ) {
78
+ private List <MigrateDataType > targetPrimaryKeyTypesWithoutConstantColumns (List <MigrateDataType > targetPrimaryKeyTypes , List <String > targetPrimaryKeyNames ) {
79
+ if (!isEnabled ) return targetPrimaryKeyTypes ;
80
+ if (!valid ) return null ;
81
+
82
+ // As this is valid, we know that the column names, types, and values are all the same size
83
+ List <String > columnNames = getRawStringList (Property .COLUMN_NAMES );
84
+
65
85
List <MigrateDataType > rtn = new ArrayList <>();
66
- if (null !=columnNames && !columnNames .isEmpty ()) {
67
- if (null ==targetPrimaryKeyTypes || null ==targetPrimaryKeyNames ) {
68
- if (null ==targetPrimaryKeyTypes )
69
- logger .error ("Target primary key types are not specified in property {}" , KnownProperties .TARGET_PRIMARY_KEY_TYPES );
70
- if (null ==targetPrimaryKeyNames )
71
- logger .error ("Target primary key names are not specified in property {}" , KnownProperties .TARGET_PRIMARY_KEY );
72
- }
73
- else {
74
- for (String keyName : targetPrimaryKeyNames ) {
75
- if (!columnNames .contains (keyName )) {
76
- rtn .add (targetPrimaryKeyTypes .get (targetPrimaryKeyNames .indexOf (keyName )));
77
- }
78
- }
86
+ for (String keyName : targetPrimaryKeyNames ) {
87
+ if (!columnNames .contains (keyName )) {
88
+ rtn .add (targetPrimaryKeyTypes .get (targetPrimaryKeyNames .indexOf (keyName )));
79
89
}
80
90
}
81
91
return rtn ;
@@ -85,7 +95,6 @@ private boolean isValid(PropertyHelper propertyHelper) {
85
95
List <String > columnNames = getRawStringList (Property .COLUMN_NAMES );
86
96
List <MigrateDataType > columnTypes = getRawMigrateDataTypeList (Property .COLUMN_TYPES );
87
97
List <String > columnValues = getRawStringList (Property .COLUMN_VALUES );
88
- List <MigrateDataType > targetPrimaryKeyTypesWithoutConstantColumns = getRawMigrateDataTypeList (Property .TARGET_PRIMARY_TYPES_WITHOUT_CONSTANT );
89
98
90
99
boolean haveColumnNames = null !=columnNames && !columnNames .isEmpty ();
91
100
boolean haveColumnTypes = null !=columnTypes && !columnTypes .isEmpty ();
@@ -116,11 +125,6 @@ private boolean isValid(PropertyHelper propertyHelper) {
116
125
valid = false ;
117
126
}
118
127
119
- if (null ==targetPrimaryKeyTypesWithoutConstantColumns || targetPrimaryKeyTypesWithoutConstantColumns .isEmpty ()) {
120
- logger .warn ("There are no primary key columns specified in property {} that are not constant columns. This may be intentional, but it is unusual."
121
- , KnownProperties .TARGET_PRIMARY_KEY );
122
- }
123
-
124
128
return valid ;
125
129
}
126
130
}
0 commit comments