2023-12-03 13:31:29 +08:00
|
|
|
package postgres
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/usememos/memos/store"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (d *DB) FindMigrationHistoryList(ctx context.Context, _ *store.FindMigrationHistory) ([]*store.MigrationHistory, error) {
|
2024-01-05 21:27:16 +08:00
|
|
|
query := "SELECT version, created_ts FROM migration_history ORDER BY created_ts DESC"
|
|
|
|
rows, err := d.db.QueryContext(ctx, query)
|
2023-12-03 13:31:29 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
list := make([]*store.MigrationHistory, 0)
|
|
|
|
for rows.Next() {
|
|
|
|
var migrationHistory store.MigrationHistory
|
2024-01-05 21:27:16 +08:00
|
|
|
if err := rows.Scan(
|
|
|
|
&migrationHistory.Version,
|
|
|
|
&migrationHistory.CreatedTs,
|
|
|
|
); err != nil {
|
2023-12-03 13:31:29 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
2024-01-05 21:27:16 +08:00
|
|
|
|
2023-12-03 13:31:29 +08:00
|
|
|
list = append(list, &migrationHistory)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return list, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DB) UpsertMigrationHistory(ctx context.Context, upsert *store.UpsertMigrationHistory) (*store.MigrationHistory, error) {
|
2024-01-05 21:27:16 +08:00
|
|
|
stmt := `
|
|
|
|
INSERT INTO migration_history (
|
|
|
|
version
|
|
|
|
)
|
|
|
|
VALUES ($1)
|
|
|
|
ON CONFLICT(version) DO UPDATE
|
|
|
|
SET
|
|
|
|
version=EXCLUDED.version
|
|
|
|
RETURNING version, created_ts
|
|
|
|
`
|
2023-12-03 13:31:29 +08:00
|
|
|
var migrationHistory store.MigrationHistory
|
2024-01-05 21:27:16 +08:00
|
|
|
if err := d.db.QueryRowContext(ctx, stmt, upsert.Version).Scan(
|
|
|
|
&migrationHistory.Version,
|
|
|
|
&migrationHistory.CreatedTs,
|
|
|
|
); err != nil {
|
2023-12-03 13:31:29 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &migrationHistory, nil
|
|
|
|
}
|