memos/store/db/sqlite/migration_history.go

58 lines
1.3 KiB
Go
Raw Normal View History

package sqlite
2022-05-22 00:59:22 +08:00
import (
"context"
2023-11-06 08:33:31 +08:00
"github.com/usememos/memos/store"
)
2022-07-02 01:06:28 +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`, `created_ts` FROM `migration_history` ORDER BY `created_ts` DESC"
rows, err := d.db.QueryContext(ctx, query)
2022-07-02 01:06:28 +08:00
if err != nil {
return nil, err
}
defer rows.Close()
2023-11-06 08:33:31 +08:00
list := make([]*store.MigrationHistory, 0)
2022-07-02 01:06:28 +08:00
for rows.Next() {
2023-11-06 08:33:31 +08:00
var migrationHistory store.MigrationHistory
2022-07-02 01:06:28 +08:00
if err := rows.Scan(
&migrationHistory.Version,
&migrationHistory.CreatedTs,
); err != nil {
return nil, err
}
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
}
return list, nil
2022-07-02 01:06:28 +08:00
}
2023-11-06 08:33:31 +08:00
func (d *DB) UpsertMigrationHistory(ctx context.Context, upsert *store.UpsertMigrationHistory) (*store.MigrationHistory, error) {
stmt := `
2022-05-22 00:59:22 +08:00
INSERT INTO migration_history (
version
2022-05-22 00:59:22 +08:00
)
VALUES (?)
2022-07-01 20:08:25 +08:00
ON CONFLICT(version) DO UPDATE
SET
version=EXCLUDED.version
RETURNING version, created_ts
`
2023-11-06 08:33:31 +08:00
var migrationHistory store.MigrationHistory
if err := d.db.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
}