chore(tfstated): change database state id and version id formats to uuidv7
This commit is contained in:
parent
169e2d0d9d
commit
6fd1663d8c
9 changed files with 73 additions and 57 deletions
|
@ -4,6 +4,9 @@ import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"go.n16f.net/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Atomically check the lock status of a state and lock it if unlocked. Returns
|
// Atomically check the lock status of a state and lock it if unlocked. Returns
|
||||||
|
@ -18,7 +21,11 @@ func (db *DB) SetLockOrGetExistingLock(path string, lock any) (bool, error) {
|
||||||
if lockData, err = json.Marshal(lock); err != nil {
|
if lockData, err = json.Marshal(lock); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
_, err = tx.ExecContext(db.ctx, `INSERT INTO states(path, lock) VALUES (?, json(?))`, path, lockData)
|
var stateId uuid.UUID
|
||||||
|
if err := stateId.Generate(uuid.V7); err != nil {
|
||||||
|
return fmt.Errorf("failed to generate state id: %w", err)
|
||||||
|
}
|
||||||
|
_, err = tx.ExecContext(db.ctx, `INSERT INTO states(id, path, lock) VALUES (?, ?, json(?))`, stateId, path, lockData)
|
||||||
ret = true
|
ret = true
|
||||||
return err
|
return err
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -24,7 +24,7 @@ CREATE TABLE sessions (
|
||||||
) STRICT;
|
) STRICT;
|
||||||
|
|
||||||
CREATE TABLE states (
|
CREATE TABLE states (
|
||||||
id INTEGER PRIMARY KEY,
|
id TEXT PRIMARY KEY,
|
||||||
path TEXT NOT NULL,
|
path TEXT NOT NULL,
|
||||||
lock TEXT,
|
lock TEXT,
|
||||||
created INTEGER DEFAULT (unixepoch()),
|
created INTEGER DEFAULT (unixepoch()),
|
||||||
|
@ -33,9 +33,9 @@ CREATE TABLE states (
|
||||||
CREATE UNIQUE INDEX states_path on states(path);
|
CREATE UNIQUE INDEX states_path on states(path);
|
||||||
|
|
||||||
CREATE TABLE versions (
|
CREATE TABLE versions (
|
||||||
id INTEGER PRIMARY KEY,
|
id TEXT PRIMARY KEY,
|
||||||
account_id TEXT NOT NULL,
|
account_id TEXT NOT NULL,
|
||||||
state_id INTEGER,
|
state_id TEXT,
|
||||||
data BLOB,
|
data BLOB,
|
||||||
lock TEXT,
|
lock TEXT,
|
||||||
created INTEGER DEFAULT (unixepoch()),
|
created INTEGER DEFAULT (unixepoch()),
|
||||||
|
|
|
@ -9,6 +9,7 @@ import (
|
||||||
|
|
||||||
"git.adyxax.org/adyxax/tfstated/pkg/model"
|
"git.adyxax.org/adyxax/tfstated/pkg/model"
|
||||||
"github.com/mattn/go-sqlite3"
|
"github.com/mattn/go-sqlite3"
|
||||||
|
"go.n16f.net/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (db *DB) CreateState(path string, accountId string, data []byte) (*model.Version, error) {
|
func (db *DB) CreateState(path string, accountId string, data []byte) (*model.Version, error) {
|
||||||
|
@ -16,11 +17,21 @@ func (db *DB) CreateState(path string, accountId string, data []byte) (*model.Ve
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to encrypt state data: %w", err)
|
return nil, fmt.Errorf("failed to encrypt state data: %w", err)
|
||||||
}
|
}
|
||||||
|
var stateId uuid.UUID
|
||||||
|
if err := stateId.Generate(uuid.V7); err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to generate state id: %w", err)
|
||||||
|
}
|
||||||
|
var versionId uuid.UUID
|
||||||
|
if err := versionId.Generate(uuid.V7); err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to generate version id: %w", err)
|
||||||
|
}
|
||||||
version := &model.Version{
|
version := &model.Version{
|
||||||
AccountId: accountId,
|
AccountId: accountId,
|
||||||
|
Id: versionId,
|
||||||
|
StateId: stateId,
|
||||||
}
|
}
|
||||||
return version, db.WithTransaction(func(tx *sql.Tx) error {
|
return version, db.WithTransaction(func(tx *sql.Tx) error {
|
||||||
result, err := tx.ExecContext(db.ctx, `INSERT INTO states(path) VALUES (?)`, path)
|
_, err := tx.ExecContext(db.ctx, `INSERT INTO states(id, path) VALUES (?, ?)`, stateId, path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
var sqliteErr sqlite3.Error
|
var sqliteErr sqlite3.Error
|
||||||
if errors.As(err, &sqliteErr) {
|
if errors.As(err, &sqliteErr) {
|
||||||
|
@ -31,25 +42,16 @@ func (db *DB) CreateState(path string, accountId string, data []byte) (*model.Ve
|
||||||
}
|
}
|
||||||
return fmt.Errorf("failed to insert new state: %w", err)
|
return fmt.Errorf("failed to insert new state: %w", err)
|
||||||
}
|
}
|
||||||
stateId, err := result.LastInsertId()
|
_, err = tx.ExecContext(db.ctx,
|
||||||
if err != nil {
|
`INSERT INTO versions(id, account_id, data, state_id)
|
||||||
return fmt.Errorf("failed to get last insert id for new state: %w", err)
|
VALUES (:id, :accountID, :data, :stateID)`,
|
||||||
}
|
|
||||||
version.StateId = int(stateId)
|
|
||||||
result, err = tx.ExecContext(db.ctx,
|
|
||||||
`INSERT INTO versions(account_id, data, state_id)
|
|
||||||
VALUES (:accountID, :data, :stateID)`,
|
|
||||||
sql.Named("accountID", accountId),
|
sql.Named("accountID", accountId),
|
||||||
sql.Named("data", encryptedData),
|
sql.Named("data", encryptedData),
|
||||||
|
sql.Named("id", versionId),
|
||||||
sql.Named("stateID", stateId))
|
sql.Named("stateID", stateId))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to insert new state version: %w", err)
|
return fmt.Errorf("failed to insert new state version: %w", err)
|
||||||
}
|
}
|
||||||
versionId, err := result.LastInsertId()
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to get last insert id for new version of the state: %w", err)
|
|
||||||
}
|
|
||||||
version.Id = int(versionId)
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -89,7 +91,7 @@ func (db *DB) GetState(path string) ([]byte, error) {
|
||||||
return db.dataEncryptionKey.DecryptAES256(encryptedData)
|
return db.dataEncryptionKey.DecryptAES256(encryptedData)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *DB) LoadStateById(stateId int) (*model.State, error) {
|
func (db *DB) LoadStateById(stateId uuid.UUID) (*model.State, error) {
|
||||||
state := model.State{
|
state := model.State{
|
||||||
Id: stateId,
|
Id: stateId,
|
||||||
}
|
}
|
||||||
|
@ -104,7 +106,7 @@ func (db *DB) LoadStateById(stateId int) (*model.State, error) {
|
||||||
if errors.Is(err, sql.ErrNoRows) {
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
return nil, fmt.Errorf("failed to load state id %d from database: %w", stateId, err)
|
return nil, fmt.Errorf("failed to load state id %s from database: %w", stateId, err)
|
||||||
}
|
}
|
||||||
state.Created = time.Unix(created, 0)
|
state.Created = time.Unix(created, 0)
|
||||||
state.Updated = time.Unix(updated, 0)
|
state.Updated = time.Unix(updated, 0)
|
||||||
|
@ -139,8 +141,8 @@ func (db *DB) LoadStates() ([]model.State, error) {
|
||||||
return states, nil
|
return states, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// returns true in case of id mismatch
|
// returns true in case of lock mismatch
|
||||||
func (db *DB) SetState(path string, accountID string, data []byte, lockID string) (bool, error) {
|
func (db *DB) SetState(path string, accountId string, data []byte, lock string) (bool, error) {
|
||||||
encryptedData, err := db.dataEncryptionKey.EncryptAES256(data)
|
encryptedData, err := db.dataEncryptionKey.EncryptAES256(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, fmt.Errorf("failed to encrypt state data: %w", err)
|
return false, fmt.Errorf("failed to encrypt state data: %w", err)
|
||||||
|
@ -148,45 +150,50 @@ func (db *DB) SetState(path string, accountID string, data []byte, lockID string
|
||||||
ret := false
|
ret := false
|
||||||
return ret, db.WithTransaction(func(tx *sql.Tx) error {
|
return ret, db.WithTransaction(func(tx *sql.Tx) error {
|
||||||
var (
|
var (
|
||||||
stateID int64
|
stateId string
|
||||||
lockData []byte
|
lockData []byte
|
||||||
)
|
)
|
||||||
if err = tx.QueryRowContext(db.ctx, `SELECT id, lock->>'ID' FROM states WHERE path = ?;`, path).Scan(&stateID, &lockData); err != nil {
|
if err = tx.QueryRowContext(db.ctx, `SELECT id, lock->>'ID' FROM states WHERE path = ?;`, path).Scan(&stateId, &lockData); err != nil {
|
||||||
if errors.Is(err, sql.ErrNoRows) {
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
var result sql.Result
|
var stateUUID uuid.UUID
|
||||||
result, err = tx.ExecContext(db.ctx, `INSERT INTO states(path) VALUES (?)`, path)
|
if err := stateUUID.Generate(uuid.V7); err != nil {
|
||||||
|
return fmt.Errorf("failed to generate state id: %w", err)
|
||||||
|
}
|
||||||
|
_, err = tx.ExecContext(db.ctx, `INSERT INTO states(id, path) VALUES (?, ?)`, stateUUID, path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to insert new state: %w", err)
|
return fmt.Errorf("failed to insert new state: %w", err)
|
||||||
}
|
}
|
||||||
stateID, err = result.LastInsertId()
|
stateId = stateUUID.String()
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to get last insert id for new state: %w", err)
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if lockID != "" && slices.Compare([]byte(lockID), lockData) != 0 {
|
if lock != "" && slices.Compare([]byte(lock), lockData) != 0 {
|
||||||
err = fmt.Errorf("failed to update state, lock ID does not match")
|
err = fmt.Errorf("failed to update state: lock ID mismatch")
|
||||||
ret = true
|
ret = true
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
var versionId uuid.UUID
|
||||||
|
if err := versionId.Generate(uuid.V7); err != nil {
|
||||||
|
return fmt.Errorf("failed to generate version id: %w", err)
|
||||||
|
}
|
||||||
_, err = tx.ExecContext(db.ctx,
|
_, err = tx.ExecContext(db.ctx,
|
||||||
`INSERT INTO versions(account_id, state_id, data, lock)
|
`INSERT INTO versions(id, account_id, state_id, data, lock)
|
||||||
SELECT :accountID, :stateID, :data, lock
|
SELECT :versionId, :accountId, :stateId, :data, lock
|
||||||
FROM states
|
FROM states
|
||||||
WHERE states.id = :stateID;`,
|
WHERE states.id = :stateId;`,
|
||||||
sql.Named("accountID", accountID),
|
sql.Named("accountId", accountId),
|
||||||
sql.Named("stateID", stateID),
|
sql.Named("data", encryptedData),
|
||||||
sql.Named("data", encryptedData))
|
sql.Named("stateId", stateId),
|
||||||
|
sql.Named("versionId", versionId))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to insert new state version: %w", err)
|
return fmt.Errorf("failed to insert new state version: %w", err)
|
||||||
}
|
}
|
||||||
_, err = tx.ExecContext(db.ctx,
|
_, err = tx.ExecContext(db.ctx,
|
||||||
`UPDATE states SET updated = ? WHERE id = ?;`,
|
`UPDATE states SET updated = ? WHERE id = ?;`,
|
||||||
time.Now().UTC().Unix(),
|
time.Now().UTC().Unix(),
|
||||||
stateID)
|
stateId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to touch updated for state: %w", err)
|
return fmt.Errorf("failed to touch updated for state: %w", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,9 +7,10 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.adyxax.org/adyxax/tfstated/pkg/model"
|
"git.adyxax.org/adyxax/tfstated/pkg/model"
|
||||||
|
"go.n16f.net/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (db *DB) LoadVersionById(id int) (*model.Version, error) {
|
func (db *DB) LoadVersionById(id uuid.UUID) (*model.Version, error) {
|
||||||
version := model.Version{
|
version := model.Version{
|
||||||
Id: id,
|
Id: id,
|
||||||
}
|
}
|
||||||
|
@ -29,12 +30,12 @@ func (db *DB) LoadVersionById(id int) (*model.Version, error) {
|
||||||
if errors.Is(err, sql.ErrNoRows) {
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
return nil, fmt.Errorf("failed to load version id %d from database: %w", id, err)
|
return nil, fmt.Errorf("failed to load version id %s from database: %w", id, err)
|
||||||
}
|
}
|
||||||
version.Created = time.Unix(created, 0)
|
version.Created = time.Unix(created, 0)
|
||||||
version.Data, err = db.dataEncryptionKey.DecryptAES256(encryptedData)
|
version.Data, err = db.dataEncryptionKey.DecryptAES256(encryptedData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to decrypt version %d data: %w", id, err)
|
return nil, fmt.Errorf("failed to decrypt version %s data: %w", id, err)
|
||||||
}
|
}
|
||||||
return &version, nil
|
return &version, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,11 +2,13 @@ package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"go.n16f.net/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
type State struct {
|
type State struct {
|
||||||
Created time.Time
|
Created time.Time
|
||||||
Id int
|
Id uuid.UUID
|
||||||
Lock *string
|
Lock *string
|
||||||
Path string
|
Path string
|
||||||
Updated time.Time
|
Updated time.Time
|
||||||
|
|
|
@ -3,13 +3,15 @@ package model
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"go.n16f.net/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Version struct {
|
type Version struct {
|
||||||
AccountId string
|
AccountId string
|
||||||
Created time.Time
|
Created time.Time
|
||||||
Data json.RawMessage
|
Data json.RawMessage
|
||||||
Id int
|
Id uuid.UUID
|
||||||
Lock *string
|
Lock *string
|
||||||
StateId int
|
StateId uuid.UUID
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,10 +3,10 @@ package webui
|
||||||
import (
|
import (
|
||||||
"html/template"
|
"html/template"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"git.adyxax.org/adyxax/tfstated/pkg/database"
|
"git.adyxax.org/adyxax/tfstated/pkg/database"
|
||||||
"git.adyxax.org/adyxax/tfstated/pkg/model"
|
"git.adyxax.org/adyxax/tfstated/pkg/model"
|
||||||
|
"go.n16f.net/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
var stateTemplate = template.Must(template.ParseFS(htmlFS, "html/base.html", "html/state.html"))
|
var stateTemplate = template.Must(template.ParseFS(htmlFS, "html/base.html", "html/state.html"))
|
||||||
|
@ -19,9 +19,8 @@ func handleStateGET(db *database.DB) http.Handler {
|
||||||
Versions []model.Version
|
Versions []model.Version
|
||||||
}
|
}
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
stateIdStr := r.PathValue("id")
|
var stateId uuid.UUID
|
||||||
stateId, err := strconv.Atoi(stateIdStr)
|
if err := stateId.Parse(r.PathValue("id")); err != nil {
|
||||||
if err != nil {
|
|
||||||
errorResponse(w, http.StatusBadRequest, err)
|
errorResponse(w, http.StatusBadRequest, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,6 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"path"
|
"path"
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"git.adyxax.org/adyxax/tfstated/pkg/database"
|
"git.adyxax.org/adyxax/tfstated/pkg/database"
|
||||||
"git.adyxax.org/adyxax/tfstated/pkg/model"
|
"git.adyxax.org/adyxax/tfstated/pkg/model"
|
||||||
|
@ -87,7 +86,7 @@ func handleStatesPOST(db *database.DB) http.Handler {
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
destination := path.Join("/version", strconv.Itoa(version.Id))
|
destination := path.Join("/version", version.Id.String())
|
||||||
http.Redirect(w, r, destination, http.StatusFound)
|
http.Redirect(w, r, destination, http.StatusFound)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
package webui
|
package webui
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"html/template"
|
"html/template"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"path"
|
||||||
|
|
||||||
"git.adyxax.org/adyxax/tfstated/pkg/database"
|
"git.adyxax.org/adyxax/tfstated/pkg/database"
|
||||||
"git.adyxax.org/adyxax/tfstated/pkg/model"
|
"git.adyxax.org/adyxax/tfstated/pkg/model"
|
||||||
|
"go.n16f.net/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
var versionTemplate = template.Must(template.ParseFS(htmlFS, "html/base.html", "html/version.html"))
|
var versionTemplate = template.Must(template.ParseFS(htmlFS, "html/base.html", "html/version.html"))
|
||||||
|
@ -21,9 +21,8 @@ func handleVersionGET(db *database.DB) http.Handler {
|
||||||
VersionData string
|
VersionData string
|
||||||
}
|
}
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
versionIdStr := r.PathValue("id")
|
var versionId uuid.UUID
|
||||||
versionId, err := strconv.Atoi(versionIdStr)
|
if err := versionId.Parse(r.PathValue("id")); err != nil {
|
||||||
if err != nil {
|
|
||||||
errorResponse(w, http.StatusBadRequest, err)
|
errorResponse(w, http.StatusBadRequest, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -49,7 +48,7 @@ func handleVersionGET(db *database.DB) http.Handler {
|
||||||
versionData := string(version.Data[:])
|
versionData := string(version.Data[:])
|
||||||
render(w, versionTemplate, http.StatusOK, VersionsData{
|
render(w, versionTemplate, http.StatusOK, VersionsData{
|
||||||
Page: makePage(r, &Page{
|
Page: makePage(r, &Page{
|
||||||
Precedent: fmt.Sprintf("/state/%d", state.Id),
|
Precedent: path.Join("/state/", state.Id.String()),
|
||||||
Section: "states",
|
Section: "states",
|
||||||
Title: state.Path,
|
Title: state.Path,
|
||||||
}),
|
}),
|
||||||
|
|
Loading…
Add table
Reference in a new issue