migration_history.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package mysql
  2. import (
  3. "context"
  4. "github.com/usememos/memos/store"
  5. )
  6. func (d *DB) FindMigrationHistoryList(ctx context.Context, _ *store.FindMigrationHistory) ([]*store.MigrationHistory, error) {
  7. query := "SELECT `version`, UNIX_TIMESTAMP(`created_ts`) FROM `migration_history` ORDER BY `created_ts` DESC"
  8. rows, err := d.db.QueryContext(ctx, query)
  9. if err != nil {
  10. return nil, err
  11. }
  12. defer rows.Close()
  13. list := make([]*store.MigrationHistory, 0)
  14. for rows.Next() {
  15. var migrationHistory store.MigrationHistory
  16. if err := rows.Scan(
  17. &migrationHistory.Version,
  18. &migrationHistory.CreatedTs,
  19. ); err != nil {
  20. return nil, err
  21. }
  22. list = append(list, &migrationHistory)
  23. }
  24. if err := rows.Err(); err != nil {
  25. return nil, err
  26. }
  27. return list, nil
  28. }
  29. func (d *DB) UpsertMigrationHistory(ctx context.Context, upsert *store.UpsertMigrationHistory) (*store.MigrationHistory, error) {
  30. stmt := "INSERT INTO `migration_history` (`version`) VALUES (?) ON DUPLICATE KEY UPDATE `version` = ?"
  31. _, err := d.db.ExecContext(ctx, stmt, upsert.Version, upsert.Version)
  32. if err != nil {
  33. return nil, err
  34. }
  35. var migrationHistory store.MigrationHistory
  36. stmt = "SELECT `version`, UNIX_TIMESTAMP(`created_ts`) FROM `migration_history` WHERE `version` = ?"
  37. if err := d.db.QueryRowContext(ctx, stmt, upsert.Version).Scan(
  38. &migrationHistory.Version,
  39. &migrationHistory.CreatedTs,
  40. ); err != nil {
  41. return nil, err
  42. }
  43. return &migrationHistory, nil
  44. }