2023-09-28 22:09:52 +08:00
|
|
|
package mysql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2023-11-06 08:33:31 +08:00
|
|
|
"github.com/usememos/memos/store"
|
|
|
|
)
|
2023-09-28 22:09:52 +08:00
|
|
|
|
2023-11-06 08:33:31 +08:00
|
|
|
func (d *DB) FindMigrationHistoryList(ctx context.Context, _ *store.FindMigrationHistory) ([]*store.MigrationHistory, error) {
|
2023-11-05 15:49:57 +08:00
|
|
|
query := "SELECT `version`, UNIX_TIMESTAMP(`created_ts`) FROM `migration_history` ORDER BY `created_ts` DESC"
|
|
|
|
rows, err := d.db.QueryContext(ctx, query)
|
2023-09-28 22:09:52 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
2023-11-06 08:33:31 +08:00
|
|
|
list := make([]*store.MigrationHistory, 0)
|
2023-09-28 22:09:52 +08:00
|
|
|
for rows.Next() {
|
2023-11-06 08:33:31 +08:00
|
|
|
var migrationHistory store.MigrationHistory
|
2023-09-28 22:09:52 +08:00
|
|
|
if err := rows.Scan(
|
|
|
|
&migrationHistory.Version,
|
|
|
|
&migrationHistory.CreatedTs,
|
|
|
|
); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
list = append(list, &migrationHistory)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return list, nil
|
|
|
|
}
|
|
|
|
|
2023-11-06 08:33:31 +08:00
|
|
|
func (d *DB) UpsertMigrationHistory(ctx context.Context, upsert *store.UpsertMigrationHistory) (*store.MigrationHistory, error) {
|
2023-10-07 22:56:12 +08:00
|
|
|
stmt := "INSERT INTO `migration_history` (`version`) VALUES (?) ON DUPLICATE KEY UPDATE `version` = ?"
|
2023-09-28 22:09:52 +08:00
|
|
|
_, err := d.db.ExecContext(ctx, stmt, upsert.Version, upsert.Version)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-11-06 08:33:31 +08:00
|
|
|
var migrationHistory store.MigrationHistory
|
2023-10-07 22:56:12 +08:00
|
|
|
stmt = "SELECT `version`, UNIX_TIMESTAMP(`created_ts`) FROM `migration_history` WHERE `version` = ?"
|
2023-09-28 22:09:52 +08:00
|
|
|
if err := d.db.QueryRowContext(ctx, stmt, upsert.Version).Scan(
|
|
|
|
&migrationHistory.Version,
|
|
|
|
&migrationHistory.CreatedTs,
|
|
|
|
); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &migrationHistory, nil
|
|
|
|
}
|