migration_history.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package postgres
  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, 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 := `
  31. INSERT INTO migration_history (
  32. version
  33. )
  34. VALUES ($1)
  35. ON CONFLICT(version) DO UPDATE
  36. SET
  37. version=EXCLUDED.version
  38. RETURNING version, created_ts
  39. `
  40. var migrationHistory store.MigrationHistory
  41. if err := d.db.QueryRowContext(ctx, stmt, upsert.Version).Scan(
  42. &migrationHistory.Version,
  43. &migrationHistory.CreatedTs,
  44. ); err != nil {
  45. return nil, err
  46. }
  47. return &migrationHistory, nil
  48. }