store.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package rintegration
  2. import (
  3. "database/sql"
  4. "time"
  5. )
  6. // Row is one row of the router_integrations table. The pusher password is
  7. // never stored here: it lives only in the on-disk environment file written by
  8. // the Manager.
  9. type Row struct {
  10. Slug string
  11. Vendor string
  12. Mode string
  13. Host string
  14. Username string
  15. Version string
  16. Asset string
  17. Arch string
  18. IntervalS int
  19. Enabled bool
  20. State string
  21. LastError string
  22. LastStartMs int64
  23. CreatedAt int64
  24. UpdatedAt int64
  25. }
  26. // Store wraps the router_integrations table (created by db.schemaSQL).
  27. type Store struct {
  28. db *sql.DB
  29. }
  30. // NewStore builds a Store over an open *sql.DB.
  31. func NewStore(db *sql.DB) *Store { return &Store{db: db} }
  32. const integrationCols = "slug, vendor, mode, host, username, version, asset, arch, interval_s, enabled, state, last_error, last_start_ms, created_at, updated_at"
  33. func scanIntegration(row interface{ Scan(...any) error }) (Row, error) {
  34. var r Row
  35. var enabled int
  36. if err := row.Scan(
  37. &r.Slug, &r.Vendor, &r.Mode, &r.Host, &r.Username, &r.Version, &r.Asset,
  38. &r.Arch, &r.IntervalS, &enabled, &r.State, &r.LastError, &r.LastStartMs,
  39. &r.CreatedAt, &r.UpdatedAt,
  40. ); err != nil {
  41. return Row{}, err
  42. }
  43. r.Enabled = enabled == 1
  44. return r, nil
  45. }
  46. // List returns all integrations ordered by creation time.
  47. func (st *Store) List() ([]Row, error) {
  48. rows, err := st.db.Query("SELECT " + integrationCols + " FROM router_integrations ORDER BY created_at ASC")
  49. if err != nil {
  50. return nil, err
  51. }
  52. defer rows.Close()
  53. out := []Row{}
  54. for rows.Next() {
  55. r, err := scanIntegration(rows)
  56. if err != nil {
  57. continue
  58. }
  59. out = append(out, r)
  60. }
  61. return out, rows.Err()
  62. }
  63. // Get returns the integration with the given slug, or ok=false.
  64. func (st *Store) Get(slug string) (Row, bool) {
  65. r, err := scanIntegration(st.db.QueryRow("SELECT "+integrationCols+" FROM router_integrations WHERE slug = ?", slug))
  66. if err != nil {
  67. return Row{}, false
  68. }
  69. return r, true
  70. }
  71. // Upsert inserts or fully updates an integration row. created_at is preserved
  72. // on conflict; every other column is replaced.
  73. func (st *Store) Upsert(r Row) error {
  74. now := time.Now().UnixMilli()
  75. enabled := 0
  76. if r.Enabled {
  77. enabled = 1
  78. }
  79. if r.CreatedAt == 0 {
  80. r.CreatedAt = now
  81. }
  82. if r.UpdatedAt == 0 {
  83. r.UpdatedAt = now
  84. }
  85. _, err := st.db.Exec(
  86. `INSERT INTO router_integrations
  87. (slug, vendor, mode, host, username, version, asset, arch, interval_s, enabled, state, last_error, last_start_ms, created_at, updated_at)
  88. VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
  89. ON CONFLICT(slug) DO UPDATE SET
  90. vendor=excluded.vendor, mode=excluded.mode, host=excluded.host,
  91. username=excluded.username, version=excluded.version, asset=excluded.asset,
  92. arch=excluded.arch, interval_s=excluded.interval_s, enabled=excluded.enabled,
  93. state=excluded.state, last_error=excluded.last_error,
  94. last_start_ms=excluded.last_start_ms, updated_at=excluded.updated_at`,
  95. r.Slug, r.Vendor, r.Mode, r.Host, r.Username, r.Version, r.Asset,
  96. r.Arch, r.IntervalS, enabled, r.State, r.LastError, r.LastStartMs,
  97. r.CreatedAt, r.UpdatedAt,
  98. )
  99. return err
  100. }
  101. // SetState updates state and last_error.
  102. func (st *Store) SetState(slug, state, lastError string) error {
  103. _, err := st.db.Exec(
  104. "UPDATE router_integrations SET state = ?, last_error = ?, updated_at = ? WHERE slug = ?",
  105. state, lastError, time.Now().UnixMilli(), slug,
  106. )
  107. return err
  108. }
  109. // SetEnabled updates the enabled flag.
  110. func (st *Store) SetEnabled(slug string, enabled bool) error {
  111. e := 0
  112. if enabled {
  113. e = 1
  114. }
  115. _, err := st.db.Exec(
  116. "UPDATE router_integrations SET enabled = ?, updated_at = ? WHERE slug = ?",
  117. e, time.Now().UnixMilli(), slug,
  118. )
  119. return err
  120. }
  121. // SetVersion records the release that is installed.
  122. func (st *Store) SetVersion(slug, version, asset, arch string) error {
  123. _, err := st.db.Exec(
  124. "UPDATE router_integrations SET version = ?, asset = ?, arch = ?, updated_at = ? WHERE slug = ?",
  125. version, asset, arch, time.Now().UnixMilli(), slug,
  126. )
  127. return err
  128. }
  129. // SetLastStart records the last successful process start timestamp (epoch ms).
  130. func (st *Store) SetLastStart(slug string, ms int64) error {
  131. _, err := st.db.Exec(
  132. "UPDATE router_integrations SET last_start_ms = ?, updated_at = ? WHERE slug = ?",
  133. ms, time.Now().UnixMilli(), slug,
  134. )
  135. return err
  136. }
  137. // Delete removes the row with the given slug.
  138. func (st *Store) Delete(slug string) error {
  139. _, err := st.db.Exec("DELETE FROM router_integrations WHERE slug = ?", slug)
  140. return err
  141. }