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

"ON UPDATE CURRENT_TIMESTAMP" not come into effect #2503

Closed
dramonliang9527 opened this issue May 20, 2024 · 0 comments
Closed

"ON UPDATE CURRENT_TIMESTAMP" not come into effect #2503

dramonliang9527 opened this issue May 20, 2024 · 0 comments

Comments

@dramonliang9527
Copy link

dramonliang9527 commented May 20, 2024

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"}]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants