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