2022-05-22 00:59:22 +08:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
2022-08-07 10:17:56 +08:00
|
|
|
"context"
|
2022-07-02 01:06:28 +08:00
|
|
|
"strings"
|
2022-05-22 00:59:22 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type MigrationHistory struct {
|
2022-07-02 10:47:16 +08:00
|
|
|
Version string
|
2022-05-22 00:59:22 +08:00
|
|
|
CreatedTs int64
|
2022-07-02 10:47:16 +08:00
|
|
|
}
|
|
|
|
|
2022-07-09 13:34:14 +08:00
|
|
|
type MigrationHistoryUpsert struct {
|
|
|
|
Version string
|
2022-05-22 00:59:22 +08:00
|
|
|
}
|
|
|
|
|
2022-07-02 01:06:28 +08:00
|
|
|
type MigrationHistoryFind struct {
|
2022-07-02 10:47:16 +08:00
|
|
|
Version *string
|
2022-07-02 01:06:28 +08:00
|
|
|
}
|
|
|
|
|
2023-01-07 11:49:58 +08:00
|
|
|
func (db *DB) FindMigrationHistoryList(ctx context.Context, find *MigrationHistoryFind) ([]*MigrationHistory, error) {
|
2023-03-18 22:34:22 +08:00
|
|
|
where, args := []string{"1 = 1"}, []any{}
|
2022-07-02 01:06:28 +08:00
|
|
|
|
2022-07-02 10:47:16 +08:00
|
|
|
if v := find.Version; v != nil {
|
|
|
|
where, args = append(where, "version = ?"), append(args, *v)
|
|
|
|
}
|
2022-07-02 01:06:28 +08:00
|
|
|
|
2022-08-07 10:17:56 +08:00
|
|
|
query := `
|
2022-07-02 01:06:28 +08:00
|
|
|
SELECT
|
|
|
|
version,
|
|
|
|
created_ts
|
|
|
|
FROM
|
|
|
|
migration_history
|
2022-08-07 10:17:56 +08:00
|
|
|
WHERE ` + strings.Join(where, " AND ") + `
|
2023-06-20 12:18:04 +08:00
|
|
|
ORDER BY created_ts DESC
|
2022-08-07 10:17:56 +08:00
|
|
|
`
|
2023-07-20 23:15:56 +08:00
|
|
|
rows, err := db.DBInstance.QueryContext(ctx, query, args...)
|
2022-07-02 01:06:28 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
2023-07-20 23:15:56 +08:00
|
|
|
list := make([]*MigrationHistory, 0)
|
2022-07-02 01:06:28 +08:00
|
|
|
for rows.Next() {
|
|
|
|
var migrationHistory MigrationHistory
|
|
|
|
if err := rows.Scan(
|
|
|
|
&migrationHistory.Version,
|
|
|
|
&migrationHistory.CreatedTs,
|
|
|
|
); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-20 23:15:56 +08:00
|
|
|
list = append(list, &migrationHistory)
|
2022-07-02 01:06:28 +08:00
|
|
|
}
|
|
|
|
|
2022-08-24 21:53:12 +08:00
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-20 23:15:56 +08:00
|
|
|
return list, nil
|
2022-07-02 01:06:28 +08:00
|
|
|
}
|
|
|
|
|
2023-07-20 23:15:56 +08:00
|
|
|
func (db *DB) UpsertMigrationHistory(ctx context.Context, upsert *MigrationHistoryUpsert) (*MigrationHistory, error) {
|
|
|
|
stmt := `
|
2022-05-22 00:59:22 +08:00
|
|
|
INSERT INTO migration_history (
|
2022-07-09 13:34:14 +08:00
|
|
|
version
|
2022-05-22 00:59:22 +08:00
|
|
|
)
|
2022-07-09 13:34:14 +08:00
|
|
|
VALUES (?)
|
2022-07-01 20:08:25 +08:00
|
|
|
ON CONFLICT(version) DO UPDATE
|
|
|
|
SET
|
2022-07-09 13:34:14 +08:00
|
|
|
version=EXCLUDED.version
|
|
|
|
RETURNING version, created_ts
|
2022-08-07 10:17:56 +08:00
|
|
|
`
|
2022-07-02 10:47:16 +08:00
|
|
|
var migrationHistory MigrationHistory
|
2023-07-20 23:15:56 +08:00
|
|
|
if err := db.DBInstance.QueryRowContext(ctx, stmt, 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
|
|
|
}
|