|
21 | 21 | import com.google.cloud.spanner.ResultSet; |
22 | 22 | import com.google.cloud.spanner.Statement; |
23 | 23 | import com.google.cloud.spanner.TransactionContext; |
| 24 | +import com.google.cloud.spanner.Value; |
24 | 25 | import com.google.cloud.teleport.v2.spanner.ddl.Ddl; |
| 26 | +import com.google.cloud.teleport.v2.spanner.ddl.IndexColumn; |
25 | 27 | import com.google.cloud.teleport.v2.spanner.ddl.Table; |
26 | 28 | import com.google.cloud.teleport.v2.spanner.migrations.convertors.ChangeEventSpannerConvertor; |
| 29 | +import com.google.cloud.teleport.v2.spanner.migrations.convertors.ChangeEventTypeConvertor; |
27 | 30 | import com.google.cloud.teleport.v2.spanner.migrations.exceptions.ChangeEventConvertorException; |
28 | 31 | import com.google.cloud.teleport.v2.spanner.migrations.exceptions.DroppedTableException; |
29 | 32 | import com.google.cloud.teleport.v2.spanner.migrations.exceptions.InvalidChangeEventException; |
30 | 33 | import com.google.cloud.teleport.v2.spanner.migrations.spanner.SpannerReadUtils; |
31 | 34 | import com.google.cloud.teleport.v2.templates.spanner.ShadowTableCreator; |
32 | 35 | import com.google.common.collect.ImmutableMap; |
33 | | -import java.util.Arrays; |
| 36 | +import java.util.ArrayList; |
34 | 37 | import java.util.List; |
35 | 38 | import java.util.Map; |
36 | 39 | import java.util.Set; |
@@ -107,23 +110,94 @@ protected void convertChangeEventToMutation(Ddl ddl, Ddl shadowTableDdl) |
107 | 110 | ChangeEventConvertor.convertChangeEventColumnKeysToLowerCase(changeEvent); |
108 | 111 | ChangeEventConvertor.verifySpannerSchema(ddl, changeEvent); |
109 | 112 |
|
| 113 | + boolean hasGeneratedPK = false; |
| 114 | + Table table = ddl.table(this.dataTable); |
| 115 | + if (table != null) { |
| 116 | + hasGeneratedPK = hasGeneratedPK(table); |
| 117 | + } |
| 118 | + |
110 | 119 | this.primaryKey = |
111 | 120 | ChangeEventSpannerConvertor.changeEventToPrimaryKey( |
112 | 121 | changeEvent.get(DatastreamConstants.EVENT_TABLE_NAME_KEY).asText(), |
113 | 122 | ddl, |
114 | 123 | changeEvent, |
115 | 124 | /* convertNameToLowerCase= */ true); |
116 | | - this.dataMutation = ChangeEventConvertor.changeEventToMutation(ddl, changeEvent); |
| 125 | + |
| 126 | + String changeType = getChangeType(changeEvent); |
| 127 | + boolean isDelete = DatastreamConstants.DELETE_EVENT.equalsIgnoreCase(changeType); |
| 128 | + |
| 129 | + if (hasGeneratedPK && isDelete) { |
| 130 | + // For delete events on tables with generated primary keys, we need to use DML |
| 131 | + // to delete the row. |
| 132 | + this.dataMutation = null; |
| 133 | + } else { |
| 134 | + this.dataMutation = ChangeEventConvertor.changeEventToMutation(ddl, changeEvent); |
| 135 | + } |
| 136 | + |
117 | 137 | this.shadowTableMutation = generateShadowTableMutation(ddl, shadowTableDdl); |
118 | 138 | } |
119 | 139 |
|
| 140 | + public Statement getDataDmlStatement(Ddl ddl) throws ChangeEventConvertorException { |
| 141 | + String changeType = getChangeType(changeEvent); |
| 142 | + boolean isDelete = DatastreamConstants.DELETE_EVENT.equalsIgnoreCase(changeType); |
| 143 | + if (!isDelete) { |
| 144 | + return null; |
| 145 | + } |
| 146 | + Table table = ddl.table(this.dataTable); |
| 147 | + if (table != null && hasGeneratedPK(table)) { |
| 148 | + return generateDeleteDml(table, this.dataTable, changeEvent); |
| 149 | + } |
| 150 | + return null; |
| 151 | + } |
| 152 | + |
| 153 | + private boolean hasGeneratedPK(Table table) { |
| 154 | + for (IndexColumn keyColumn : table.primaryKeys()) { |
| 155 | + if (table.column(keyColumn.name()).isGenerated()) { |
| 156 | + return true; |
| 157 | + } |
| 158 | + } |
| 159 | + return false; |
| 160 | + } |
| 161 | + |
| 162 | + private Statement generateDeleteDml(Table table, String tableName, JsonNode event) |
| 163 | + throws ChangeEventConvertorException { |
| 164 | + // TODO: Add support for PostgreSQL |
| 165 | + StringBuilder sql = new StringBuilder("DELETE FROM ").append(tableName).append(" WHERE "); |
| 166 | + Statement.Builder builder = Statement.newBuilder(""); |
| 167 | + boolean first = true; |
| 168 | + for (com.google.cloud.teleport.v2.spanner.ddl.Column column : table.columns()) { |
| 169 | + String colName = column.name(); |
| 170 | + if (column.isGenerated()) { |
| 171 | + continue; |
| 172 | + } |
| 173 | + if (!first) { |
| 174 | + sql.append(" AND "); |
| 175 | + } |
| 176 | + sql.append(colName).append(" = @").append(colName); |
| 177 | + // Bind value |
| 178 | + Value value = |
| 179 | + ChangeEventTypeConvertor.toValue(event, column.type(), colName, /* requiredField */ true); |
| 180 | + builder.bind(colName).to(value); |
| 181 | + first = false; |
| 182 | + } |
| 183 | + builder.replace(sql.toString()); |
| 184 | + return builder.build(); |
| 185 | + } |
| 186 | + |
120 | 187 | public JsonNode getChangeEvent() { |
121 | 188 | return changeEvent; |
122 | 189 | } |
123 | 190 |
|
124 | 191 | // Returns an array of data and shadow table mutations. |
125 | 192 | public Iterable<Mutation> getMutations() { |
126 | | - return Arrays.asList(dataMutation, shadowTableMutation); |
| 193 | + List<Mutation> mutations = new ArrayList<>(); |
| 194 | + if (dataMutation != null) { |
| 195 | + mutations.add(dataMutation); |
| 196 | + } |
| 197 | + if (shadowTableMutation != null) { |
| 198 | + mutations.add(shadowTableMutation); |
| 199 | + } |
| 200 | + return mutations; |
127 | 201 | } |
128 | 202 |
|
129 | 203 | // Returns the data table mutation |
@@ -176,4 +250,12 @@ public void readDataTableRowWithExclusiveLock( |
176 | 250 | // Read the row in order to acquire the lock and discard it. |
177 | 251 | resultSet.getCurrentRowAsStruct(); |
178 | 252 | } |
| 253 | + |
| 254 | + public static String getChangeType(JsonNode changeEvent) { |
| 255 | + return changeEvent.has(DatastreamConstants.EVENT_CHANGE_TYPE_KEY) |
| 256 | + ? changeEvent |
| 257 | + .get(DatastreamConstants.EVENT_CHANGE_TYPE_KEY) |
| 258 | + .asText(DatastreamConstants.EMPTY_EVENT) |
| 259 | + : DatastreamConstants.EMPTY_EVENT; |
| 260 | + } |
179 | 261 | } |
0 commit comments