2022-05-22 00:59:22 +08:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
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 {
|
2022-07-02 10:47:16 +08:00
|
|
|
Version string
|
|
|
|
Statement string
|
2022-05-22 00:59:22 +08:00
|
|
|
CreatedTs int64
|
2022-07-02 10:47:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type MigrationHistoryCreate struct {
|
2022-05-22 00:59:22 +08:00
|
|
|
Version string
|
2022-07-02 10:47:16 +08:00
|
|
|
Statement 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
|
|
|
}
|
|
|
|
|
|
|
|
func findMigrationHistoryList(db *sql.DB, find *MigrationHistoryFind) ([]*MigrationHistory, error) {
|
|
|
|
where, args := []string{"1 = 1"}, []interface{}{}
|
|
|
|
|
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
|
|
|
|
|
|
|
rows, err := db.Query(`
|
|
|
|
SELECT
|
|
|
|
version,
|
|
|
|
created_ts
|
|
|
|
FROM
|
|
|
|
migration_history
|
|
|
|
WHERE `+strings.Join(where, " AND ")+`
|
|
|
|
ORDER BY created_ts DESC`,
|
|
|
|
args...,
|
|
|
|
)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
return migrationHistoryList, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func findMigrationHistory(db *sql.DB, find *MigrationHistoryFind) (*MigrationHistory, error) {
|
|
|
|
list, err := findMigrationHistoryList(db, find)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(list) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
} else {
|
|
|
|
return list[0], nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-02 10:47:16 +08:00
|
|
|
func upsertMigrationHistory(db *sql.DB, create *MigrationHistoryCreate) (*MigrationHistory, error) {
|
2022-07-01 20:08:25 +08:00
|
|
|
row, err := db.Query(`
|
2022-05-22 00:59:22 +08:00
|
|
|
INSERT INTO migration_history (
|
2022-07-02 10:47:16 +08:00
|
|
|
version,
|
|
|
|
statement
|
2022-05-22 00:59:22 +08:00
|
|
|
)
|
2022-07-02 10:47:16 +08:00
|
|
|
VALUES (?, ?)
|
2022-07-01 20:08:25 +08:00
|
|
|
ON CONFLICT(version) DO UPDATE
|
|
|
|
SET
|
2022-07-02 10:47:16 +08:00
|
|
|
version=EXCLUDED.version,
|
|
|
|
statement=EXCLUDED.statement
|
|
|
|
RETURNING version, statement, created_ts
|
2022-05-22 00:59:22 +08:00
|
|
|
`,
|
2022-07-02 10:47:16 +08:00
|
|
|
create.Version,
|
|
|
|
create.Statement,
|
2022-05-22 00:59:22 +08:00
|
|
|
)
|
|
|
|
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()
|
2022-07-02 10:47:16 +08:00
|
|
|
var migrationHistory MigrationHistory
|
2022-07-01 20:08:25 +08:00
|
|
|
if err := row.Scan(
|
|
|
|
&migrationHistory.Version,
|
2022-07-02 10:47:16 +08:00
|
|
|
&migrationHistory.Statement,
|
2022-07-01 20:08:25 +08:00
|
|
|
&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
|
|
|
}
|