Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,26 @@

import com.google.cloud.teleport.v2.datastream.io.CdcJdbcIO.DataSourceConfiguration;
import com.google.cloud.teleport.v2.datastream.values.DatastreamRow;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoField;
import java.util.Locale;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** A set of Database Migration utilities to convert JSON data to DML. */
public class DatastreamToMySQLDML extends DatastreamToDML {

private static final Logger LOG = LoggerFactory.getLogger(DatastreamToMySQLDML.class);
private static final DateTimeFormatter TIMESTAMP_FORMATTER =
new DateTimeFormatterBuilder()
.appendPattern("uuuu-MM-dd HH:mm:ss")
.appendFraction(ChronoField.NANO_OF_SECOND, 0, 6, true)
.toFormatter(Locale.ROOT);

private DatastreamToMySQLDML(DataSourceConfiguration config) {
super(config);
Expand Down Expand Up @@ -69,4 +82,26 @@ public String getTargetCatalogName(DatastreamRow row) {
public String getTargetSchemaName(DatastreamRow row) {
return "";
}

@Override
public String cleanDataTypeValueSql(
String columnValue, String columnName, Map<String, String> tableSchema) {
String dataType = tableSchema.get(columnName);
if (!("TIMESTAMP".equalsIgnoreCase(dataType) || "DATETIME".equalsIgnoreCase(dataType))
|| columnValue.length() < 2
|| !columnValue.startsWith("'")
|| !columnValue.endsWith("'")) {
return columnValue;
}
try {
OffsetDateTime timestamp =
OffsetDateTime.parse(columnValue.substring(1, columnValue.length() - 1));
return "'"
+ TIMESTAMP_FORMATTER.format(timestamp.withOffsetSameInstant(ZoneOffset.UTC))
+ "'";
} catch (DateTimeParseException e) {
// Leave local timestamp literals and invalid values to the existing sink handling.
return columnValue;
}
}
Comment thread
mvanhorn marked this conversation as resolved.
Resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,70 @@ public void testGetValueSql() {
assertEquals(expectedNullByteTextContent, testNullByteSqlContent);
}

@Test
public void testMySqlTimestampLiterals() {
DatastreamToDML dml = DatastreamToMySQLDML.of(null);
Map<String, String> tableSchema = new HashMap<>();
String[][] timestamps = {
{"2025-08-01T09:15:00Z", "2025-08-01 09:15:00"},
{"2025-08-01T09:15:00.000123Z", "2025-08-01 09:15:00.000123"},
{"2025-08-01T09:15:00.123Z", "2025-08-01 09:15:00.123"},
{"2025-08-01T00:15:00+02:00", "2025-07-31 22:15:00"}
};
for (String type : Arrays.asList("TIMESTAMP", "DATETIME")) {
tableSchema.put("created_at", type);
for (String[] timestamp : timestamps) {
JsonNode rowObj = getRowObj("{\"created_at\":\"" + timestamp[0] + "\"}");
assertEquals("'" + timestamp[1] + "'", dml.getValueSql(rowObj, "created_at", tableSchema));
}
}
}
Comment thread
mvanhorn marked this conversation as resolved.
Resolved

@Test
public void testMySqlTimestampUnchangedValues() {
DatastreamToDML dml = DatastreamToMySQLDML.of(null);
Map<String, String> tableSchema = new HashMap<>();
tableSchema.put("created_at", "TIMESTAMP");
for (String value :
Arrays.asList("", "invalid", "2025-02-30T09:15:00Z", "2025-08-01 09:15:00")) {
JsonNode rowObj = getRowObj("{\"created_at\":\"" + value + "\"}");
assertEquals("'" + value + "'", dml.getValueSql(rowObj, "created_at", tableSchema));
}
assertEquals(
"null", dml.getValueSql(getRowObj("{\"created_at\":null}"), "created_at", tableSchema));
assertEquals("", dml.getValueSql(getRowObj("{}"), "created_at", tableSchema));
JsonNode rowObj = getRowObj("{\"created_at\":\"2025-08-01T09:15:00Z\",\"id\":123}");
assertEquals("123", dml.getValueSql(rowObj, "id", tableSchema));
for (String type : Arrays.asList("VARCHAR", "JSON", null)) {
tableSchema.put("created_at", type);
assertEquals("'2025-08-01T09:15:00Z'", dml.getValueSql(rowObj, "created_at", tableSchema));
}
tableSchema.clear();
assertEquals("'2025-08-01T09:15:00Z'", dml.getValueSql(rowObj, "created_at", tableSchema));
tableSchema.put("created_at", "TIMESTAMP");
assertEquals(
"'2025-08-01T09:15:00Z'",
DatastreamToPostgresDML.of(null).getValueSql(rowObj, "created_at", tableSchema));
}

@Test
public void testMySqlTimestampSqlFragmentsWithColumnCasing() {
DatastreamToDML dml = DatastreamToMySQLDML.of(null).withColumnCasing("UPPERCASE");
Map<String, String> tableSchema = new HashMap<>();
tableSchema.put("CREATED_AT", "timestamp");
JsonNode rowObj =
getRowObj(
"{\"created_at\":\"2025-08-01T09:15:00.000123Z\","
+ "\"_metadata_source_type\":\"mysql\","
+ "\"_metadata_primary_keys\":[\"created_at\"]}");
assertEquals("'2025-08-01 09:15:00.000123'", dml.getColumnsValuesSql(rowObj, tableSchema));
assertEquals(
"`CREATED_AT`='2025-08-01 09:15:00.000123'", dml.getColumnsUpdateSql(rowObj, tableSchema));
assertEquals(
"`CREATED_AT`='2025-08-01 09:15:00.000123'",
dml.getPrimaryKeyToValueFilterSql(rowObj, Arrays.asList("CREATED_AT"), tableSchema));
}

/**
* Test whether {@link DatastreamToPostgresDML#getValueSql(JsonNode, String, Map)} converts array
* data into correct integer array syntax.
Expand Down
Loading