memos/store/db/migration_history.go

124 lines
2.5 KiB
Go
Raw Normal View History

2022-05-22 00:59:22 +08:00
package db
import (
"context"
2022-07-01 20:08:25 +08:00
"database/sql"
2022-07-02 01:06:28 +08:00
"strings"
2022-05-22 00:59:22 +08:00
)
type MigrationHistory struct {
Version string
2022-05-22 00:59:22 +08:00
CreatedTs int64
}
type MigrationHistoryUpsert struct {
Version string
2022-05-22 00:59:22 +08:00
}
2022-07-02 01:06:28 +08:00
type MigrationHistoryFind struct {
Version *string
2022-07-02 01:06:28 +08:00
}
func (db *DB) FindMigrationHistory(ctx context.Context, find *MigrationHistoryFind) (*MigrationHistory, error) {
2022-11-19 09:57:54 +08:00
tx, err := db.Db.BeginTx(ctx, nil)
if err != nil {
return nil, err
}
defer tx.Rollback()
list, err := findMigrationHistoryList(ctx, tx, find)
if err != nil {
return nil, err
}
if len(list) == 0 {
return nil, nil
}
2022-08-24 21:53:12 +08:00
migrationHistory := list[0]
return migrationHistory, nil
}
func (db *DB) UpsertMigrationHistory(ctx context.Context, upsert *MigrationHistoryUpsert) (*MigrationHistory, error) {
2022-11-19 09:57:54 +08:00
tx, err := db.Db.BeginTx(ctx, nil)
if err != nil {
return nil, err
}
defer tx.Rollback()
migrationHistory, err := upsertMigrationHistory(ctx, tx, upsert)
if err != nil {
return nil, err
}
if err := tx.Commit(); err != nil {
return nil, err
}
return migrationHistory, nil
}
func findMigrationHistoryList(ctx context.Context, tx *sql.Tx, find *MigrationHistoryFind) ([]*MigrationHistory, error) {
2022-07-02 01:06:28 +08:00
where, args := []string{"1 = 1"}, []interface{}{}
if v := find.Version; v != nil {
where, args = append(where, "version = ?"), append(args, *v)
}
2022-07-02 01:06:28 +08:00
query := `
2022-07-02 01:06:28 +08:00
SELECT
version,
created_ts
FROM
migration_history
WHERE ` + strings.Join(where, " AND ") + `
ORDER BY version DESC
`
rows, err := tx.QueryContext(ctx, query, args...)
2022-07-02 01:06:28 +08:00
if err != nil {
return nil, err
}
defer rows.Close()
migrationHistoryList := make([]*MigrationHistory, 0)
for rows.Next() {
var migrationHistory MigrationHistory
if err := rows.Scan(
&migrationHistory.Version,
&migrationHistory.CreatedTs,
); err != nil {
return nil, err
}
migrationHistoryList = append(migrationHistoryList, &migrationHistory)
}
2022-08-24 21:53:12 +08:00
if err := rows.Err(); err != nil {
return nil, err
}
2022-07-02 01:06:28 +08:00
return migrationHistoryList, nil
}
func upsertMigrationHistory(ctx context.Context, tx *sql.Tx, upsert *MigrationHistoryUpsert) (*MigrationHistory, error) {
query := `
2022-05-22 00:59:22 +08:00
INSERT INTO migration_history (
version
2022-05-22 00:59:22 +08:00
)
VALUES (?)
2022-07-01 20:08:25 +08:00
ON CONFLICT(version) DO UPDATE
SET
version=EXCLUDED.version
RETURNING version, created_ts
`
var migrationHistory MigrationHistory
2022-11-19 09:57:54 +08:00
if err := tx.QueryRowContext(ctx, query, upsert.Version).Scan(
2022-07-01 20:08:25 +08:00
&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
}