go-mysqldump icon indicating copy to clipboard operation
go-mysqldump copied to clipboard

Date value is not correct in sql dump file

Open neerajbg opened this issue 4 years ago • 1 comments

For some reasons, the sql file is not having correct date time for the date field .

The resulting sql file shows date time as '&{2021-08-05 00:00:00 +0000 UTC %!s(bool=true)}'

Do I need to do any specific setting for this behaviour. When I restore, it sets date time as 0000-00-00.

Kindly suggest what is the fix for this.

Neeraj

neerajbg avatar Aug 06 '21 16:08 neerajbg

Hello, I also experienced this issue. Here's how I fixed it:

Disclaimer: not a good implementation, just a quick fix for a private project

Before dump.go, line 481, insert:

case *sql.NullTime:
	if s.Valid {
		if s.Time.Format(time.TimeOnly) == "00:00:00" {
			fmt.Fprintf(&b, "'%s'", s.Time.Format(time.DateOnly))
		} else {
			fmt.Fprintf(&b, "'%s'", s.Time.Format(time.DateTime))
		}
	} else {
		b.WriteString(nullType)
	}

Not sure if this works for all database-types. Tested with mariadb 10.5.15.

modelD-svg avatar May 05 '23 09:05 modelD-svg