Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix data-integrity: timestamp convert to datetime scenario #969

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
4 changes: 3 additions & 1 deletion go/logic/inspect.go
Expand Up @@ -185,7 +185,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.SetConvertTimestampToDatetime(column.Name, "+00:00", this.migrationContext.ApplierTimeZone)
}
if column.Name == mappedColumn.Name && column.Type == sql.EnumColumnType && mappedColumn.Charset != "" {
this.migrationContext.MappedSharedColumns.SetEnumToTextConversion(column.Name)
Expand Down
4 changes: 2 additions & 2 deletions go/sql/builder.go
Expand Up @@ -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.enumToTextConversion {
token = fmt.Sprintf("ELT(?, %s)", column.EnumValues)
} else if column.Type == JSONColumnType {
Expand Down Expand Up @@ -109,7 +109,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.enumToTextConversion {
setToken = fmt.Sprintf("%s=ELT(?, %s)", EscapeName(column.Name), column.EnumValues)
} else if column.Type == JSONColumnType {
Expand Down
11 changes: 8 additions & 3 deletions go/sql/types.go
Expand Up @@ -29,7 +29,8 @@ const (
const maxMediumintUnsigned int32 = 16777215

type TimezoneConversion struct {
ToTimezone string
FromTimezone string
ToTimezone string
}

type Column struct {
Expand Down Expand Up @@ -191,8 +192,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 {
Expand Down