"ON UPDATE CURRENT_TIMESTAMP" not come into effect
dramonliang9527 opened this issue · 0 comments
dramonliang9527 commented
go: 1.20.5
github.com/dolthub/go-mysql-server v0.18.1
github.com/go-sql-driver/mysql v1.8.1
github.com/jmoiron/sqlx v1.4.0
code:
import (
"fmt"
"time"
sqle "github.com/dolthub/go-mysql-server"
"github.com/dolthub/go-mysql-server/memory"
"github.com/dolthub/go-mysql-server/server"
"github.com/dolthub/go-mysql-server/sql"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
"github.com/wencan/fastrest/restutils"
)
var (
dbName = "mydb"
address = "localhost"
port = 3306
)
type Record struct {
ID int64 `json:"id" db:"id"`
Title string `json:"name" db:"title"`
CreateTime time.Time `json:"create_time" db:"create_time"`
UpdateTime time.Time `json:"update_time" db:"update_time"`
}
func main() {
ctx := sql.NewEmptyContext()
db := memory.NewDatabase(dbName)
db.EnablePrimaryKeyIndexes()
provider := memory.NewDBProvider(db)
engine := sqle.NewDefault(provider)
config := server.Config{
Protocol: "tcp",
Address: fmt.Sprintf("%s:%d", address, port),
}
s, err := server.NewServer(config, engine, memory.NewSessionBuilder(provider), nil)
if err != nil {
panic(err)
}
go func() {
err = s.Start()
if err != nil {
panic(err)
}
}()
defer s.Close()
dbx, err := sqlx.Open("mysql", "tcp(localhost:3306)/mydb?parseTime=true&loc=Asia%2FShanghai")
if err != nil {
panic(err)
}
defer dbx.Close()
// create table
_, err = dbx.ExecContext(ctx, `CREATE TABLE testTable (
id bigint NOT NULL AUTO_INCREMENT COMMENT 'id',
title varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '',
create_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
update_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;`,
)
if err != nil {
panic(err)
}
// insert
_, err = dbx.ExecContext(ctx, `INSERT INTO testTable (title) VALUES (?)`, "title 1")
if err != nil {
panic(err)
}
// select
var providers []*Record
err = dbx.SelectContext(
ctx,
&providers,
`SELECT id, title, create_time, update_time FROM testTable`,
)
if err != nil {
panic(err)
}
fmt.Println(restutils.JsonString(providers))
// update
time.Sleep(time.Second * 5)
_, err = dbx.ExecContext(ctx, `UPDATE testTable set title = ? where id = ?`, "title 2", 1)
if err != nil {
panic(err)
}
// select
err = dbx.SelectContext(
ctx,
&providers,
`SELECT id, title, create_time, update_time FROM testTable`,
)
if err != nil {
panic(err)
}
fmt.Println(restutils.JsonString(providers))
}
output:
[{"id":13,"name":"title 1","create_time":"2024-05-20T15:23:41+08:00","update_time":"2024-05-20T15:23:41+08:00"}]
expected:
[{"id":13,"name":"title 1","create_time":"2024-05-20T15:23:41+08:00","update_time":"2024-05-20T15:23:46+08:00"}]