memos/store/db/migration_history.go

41 lines
660 B
Go
Raw Normal View History

2022-05-22 00:59:22 +08:00
package db
import (
2022-07-01 20:08:25 +08:00
"database/sql"
2022-05-22 00:59:22 +08:00
)
type MigrationHistory struct {
CreatedTs int64
Version string
}
2022-07-01 20:08:25 +08:00
func upsertMigrationHistory(db *sql.DB, version string) (*MigrationHistory, error) {
row, err := db.Query(`
2022-05-22 00:59:22 +08:00
INSERT INTO migration_history (
2022-05-22 09:29:34 +08:00
version
2022-05-22 00:59:22 +08:00
)
2022-05-22 09:29:34 +08:00
VALUES (?)
2022-07-01 20:08:25 +08:00
ON CONFLICT(version) DO UPDATE
SET
version=EXCLUDED.version
RETURNING version, created_ts
2022-05-22 00:59:22 +08:00
`,
version,
)
if err != nil {
2022-07-01 20:08:25 +08:00
return nil, err
2022-05-22 00:59:22 +08:00
}
2022-07-01 20:08:25 +08:00
defer row.Close()
row.Next()
migrationHistory := MigrationHistory{}
if err := row.Scan(
&migrationHistory.Version,
&migrationHistory.CreatedTs,
); err != nil {
return nil, err
2022-05-22 00:59:22 +08:00
}
2022-07-01 20:08:25 +08:00
return &migrationHistory, nil
2022-05-22 00:59:22 +08:00
}