Switch to prefixed IDs and tell agents which Mac a message came from
This commit is contained in:
+9
-15
@@ -2,13 +2,12 @@ package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"database/sql"
|
||||
"embed"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -29,12 +28,13 @@ type migration struct {
|
||||
key string
|
||||
name string
|
||||
sql string
|
||||
run func(*sql.Tx) error
|
||||
run func(tx *sql.Tx, dataDir string) error
|
||||
}
|
||||
|
||||
var goMigrations = map[string]func(*sql.Tx) error{
|
||||
var goMigrations = map[string]func(tx *sql.Tx, dataDir string) error{
|
||||
"071-workflow-steps": migrateWorkflowSteps,
|
||||
"078-normalize-workflow-definitions": migrateWorkflowDefinitions,
|
||||
"085-prefix-ids": migratePrefixIDs,
|
||||
}
|
||||
|
||||
func loadMigrations() ([]migration, error) {
|
||||
@@ -89,7 +89,7 @@ func Open(path string, portFirst int, onEvent func([]api.Event)) (*Store, error)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = migrate(db)
|
||||
err = migrate(db, filepath.Dir(path))
|
||||
if err != nil {
|
||||
db.Close()
|
||||
return nil, err
|
||||
@@ -98,7 +98,7 @@ func Open(path string, portFirst int, onEvent func([]api.Event)) (*Store, error)
|
||||
return &Store{db: db, path: path, portFirst: portFirst, onEvent: onEvent}, nil
|
||||
}
|
||||
|
||||
func migrate(db *sql.DB) error {
|
||||
func migrate(db *sql.DB, dataDir string) error {
|
||||
migrations, err := loadMigrations()
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -119,7 +119,7 @@ func migrate(db *sql.DB) error {
|
||||
continue
|
||||
}
|
||||
|
||||
err = applyMigration(db, m)
|
||||
err = applyMigration(db, m, dataDir)
|
||||
if err != nil {
|
||||
return fmt.Errorf("migration %s: %w", m.name, err)
|
||||
}
|
||||
@@ -147,7 +147,7 @@ func appliedMigrations(db *sql.DB) (map[string]bool, error) {
|
||||
return applied, rows.Err()
|
||||
}
|
||||
|
||||
func applyMigration(db *sql.DB, m migration) error {
|
||||
func applyMigration(db *sql.DB, m migration, dataDir string) error {
|
||||
ctx := context.Background()
|
||||
conn, err := db.Conn(ctx)
|
||||
if err != nil {
|
||||
@@ -168,7 +168,7 @@ func applyMigration(db *sql.DB, m migration) error {
|
||||
defer tx.Rollback()
|
||||
|
||||
if m.run != nil {
|
||||
err = m.run(tx)
|
||||
err = m.run(tx, dataDir)
|
||||
} else {
|
||||
_, err = tx.Exec(m.sql)
|
||||
}
|
||||
@@ -218,12 +218,6 @@ func (s *Store) Path() string {
|
||||
return s.path
|
||||
}
|
||||
|
||||
func NewID() string {
|
||||
b := make([]byte, 6)
|
||||
rand.Read(b)
|
||||
return hex.EncodeToString(b)
|
||||
}
|
||||
|
||||
func (s *Store) EnsureUser(ctx context.Context, login, displayName string) (api.User, error) {
|
||||
_, err := s.db.ExecContext(ctx, `
|
||||
INSERT INTO users (login, display_name, created_at) VALUES (?, ?, ?)
|
||||
|
||||
Reference in New Issue
Block a user