fix data-integrity: timestamp convert to datetime scenario

This commit is contained in:
thsun 2021-05-08 13:28:34 +08:00
parent fef83af378
commit a07ddae80f
3 changed files with 12 additions and 5 deletions

View File

@ -181,7 +181,9 @@ func (this *Inspector) inspectOriginalAndGhostTables() (err error) {
column := this.migrationContext.SharedColumns.Columns()[i]
mappedColumn := this.migrationContext.MappedSharedColumns.Columns()[i]
if column.Name == mappedColumn.Name && column.Type == sql.DateTimeColumnType && mappedColumn.Type == sql.TimestampColumnType {
this.migrationContext.MappedSharedColumns.SetConvertDatetimeToTimestamp(column.Name, this.migrationContext.ApplierTimeZone)
this.migrationContext.MappedSharedColumns.SetConvertDatetimeToTimestamp(column.Name, this.migrationContext.ApplierTimeZone, "+00:00")
} else if column.Name == mappedColumn.Name && column.Type == sql.TimestampColumnType && mappedColumn.Type == sql.DateTimeColumnType {
this.migrationContext.MappedSharedColumns.SetConvertDatetimeToTimestamp(column.Name, "+00:00", this.migrationContext.ApplierTimeZone)
}
}

View File

@ -37,7 +37,7 @@ func buildColumnsPreparedValues(columns *ColumnList) []string {
for i, column := range columns.Columns() {
var token string
if column.timezoneConversion != nil {
token = fmt.Sprintf("convert_tz(?, '%s', '%s')", column.timezoneConversion.ToTimezone, "+00:00")
token = fmt.Sprintf("convert_tz(?, '%s', '%s')", column.timezoneConversion.FromTimezone, column.timezoneConversion.ToTimezone)
} else if column.Type == JSONColumnType {
token = "convert(? using utf8mb4)"
} else {
@ -107,7 +107,7 @@ func BuildSetPreparedClause(columns *ColumnList) (result string, err error) {
for _, column := range columns.Columns() {
var setToken string
if column.timezoneConversion != nil {
setToken = fmt.Sprintf("%s=convert_tz(?, '%s', '%s')", EscapeName(column.Name), column.timezoneConversion.ToTimezone, "+00:00")
setToken = fmt.Sprintf("%s=convert_tz(?, '%s', '%s')", EscapeName(column.Name), column.timezoneConversion.FromTimezone, column.timezoneConversion.ToTimezone)
} else if column.Type == JSONColumnType {
setToken = fmt.Sprintf("%s=convert(? using utf8mb4)", EscapeName(column.Name))
} else {

View File

@ -29,6 +29,7 @@ const (
const maxMediumintUnsigned int32 = 16777215
type TimezoneConversion struct {
FromTimezone string
ToTimezone string
}
@ -190,8 +191,12 @@ func (this *ColumnList) GetColumnType(columnName string) ColumnType {
return this.GetColumn(columnName).Type
}
func (this *ColumnList) SetConvertDatetimeToTimestamp(columnName string, toTimezone string) {
this.GetColumn(columnName).timezoneConversion = &TimezoneConversion{ToTimezone: toTimezone}
func (this *ColumnList) SetConvertDatetimeToTimestamp(columnName string, fromTimezone, toTimezone string) {
this.GetColumn(columnName).timezoneConversion = &TimezoneConversion{FromTimezone: fromTimezone, ToTimezone: toTimezone}
}
func (this *ColumnList) SetConvertTimestampToDatetime(columnName string, fromTimezone, toTimezone string) {
this.GetColumn(columnName).timezoneConversion = &TimezoneConversion{FromTimezone: fromTimezone, ToTimezone: toTimezone}
}
func (this *ColumnList) HasTimezoneConversion(columnName string) bool {