feat: implement license platform business flows and delivery tooling
Add admin and customer interfaces, orders, signed activation and leases, protected package delivery, keyring verification, migrations, security controls, tests and deployment documentation. Co-Authored-By: Craft Agent <agents-noreply@craft.do>
This commit is contained in:
89
backend/internal/migrations/runner.go
Normal file
89
backend/internal/migrations/runner.go
Normal file
@@ -0,0 +1,89 @@
|
||||
package migrations
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
|
||||
"github.com/golang-migrate/migrate/v4"
|
||||
_ "github.com/golang-migrate/migrate/v4/database/mysql"
|
||||
_ "github.com/golang-migrate/migrate/v4/source/file"
|
||||
)
|
||||
|
||||
var ErrDirtyDatabase = errors.New("database migration is dirty; inspect and force the last known good version")
|
||||
|
||||
type Runner struct {
|
||||
migrate *migrate.Migrate
|
||||
}
|
||||
|
||||
func Open(dsn, directory string) (*Runner, error) {
|
||||
if dsn == "" {
|
||||
return nil, errors.New("migration DSN is required")
|
||||
}
|
||||
absolute, err := filepath.Abs(directory)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("resolve migration directory: %w", err)
|
||||
}
|
||||
if _, err := os.Stat(absolute); err != nil {
|
||||
return nil, fmt.Errorf("migration directory: %w", err)
|
||||
}
|
||||
sourceURL := (&url.URL{Scheme: "file", Path: filepath.ToSlash(absolute)}).String()
|
||||
m, err := migrate.New(sourceURL, dsn)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("open migration database: %w", err)
|
||||
}
|
||||
return &Runner{migrate: m}, nil
|
||||
}
|
||||
|
||||
func (r *Runner) Close() error {
|
||||
sourceErr, databaseErr := r.migrate.Close()
|
||||
if sourceErr != nil {
|
||||
return sourceErr
|
||||
}
|
||||
return databaseErr
|
||||
}
|
||||
|
||||
func (r *Runner) Up() error {
|
||||
if err := r.migrate.Up(); err != nil && !errors.Is(err, migrate.ErrNoChange) {
|
||||
return r.wrap(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Runner) Down() error {
|
||||
if err := r.migrate.Down(); err != nil && !errors.Is(err, migrate.ErrNoChange) {
|
||||
return r.wrap(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Runner) Version() (uint, bool, error) {
|
||||
return r.migrate.Version()
|
||||
}
|
||||
|
||||
// Force is deliberately explicit: it only repairs migration metadata after an
|
||||
// operator has inspected the failed DDL and selected a known-good version.
|
||||
func (r *Runner) Force(version int) error {
|
||||
if version < -1 {
|
||||
return fmt.Errorf("migration version must be >= -1: %d", version)
|
||||
}
|
||||
return r.migrate.Force(version)
|
||||
}
|
||||
|
||||
func (r *Runner) wrap(err error) error {
|
||||
if _, dirty, versionErr := r.migrate.Version(); versionErr == nil && dirty {
|
||||
return fmt.Errorf("%w: version=%s: %v", ErrDirtyDatabase, strconv.FormatUint(uint64(versionFrom(r.migrate)), 10), err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func versionFrom(m *migrate.Migrate) uint {
|
||||
version, _, err := m.Version()
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
return version
|
||||
}
|
||||
83
backend/internal/migrations/v6_integration_test.go
Normal file
83
backend/internal/migrations/v6_integration_test.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package migrations
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
migrate "github.com/golang-migrate/migrate/v4"
|
||||
_ "github.com/golang-migrate/migrate/v4/database/mysql"
|
||||
_ "github.com/golang-migrate/migrate/v4/source/file"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func TestV6Rehearsal(t *testing.T) {
|
||||
dsn := os.Getenv("MIGRATION_REHEARSAL_DSN")
|
||||
if dsn == "" {
|
||||
t.Skip("isolated rehearsal DSN required")
|
||||
}
|
||||
directory, err := filepath.Abs("../../migrations")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
m, err := migrate.New((&url.URL{Scheme: "file", Path: filepath.ToSlash(directory)}).String(), "mysql://"+dsn+"?multiStatements=true")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer m.Close()
|
||||
if err = m.Migrate(5); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
db, err := sql.Open("mysql", dsn)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
statements := []string{
|
||||
"INSERT INTO admins(id,email,password_hash) VALUES(1,'rehearsal@example.invalid','not-a-login-hash')",
|
||||
"INSERT INTO customers(id,name,contact_email) VALUES(1,'rehearsal','rehearsal@example.invalid')",
|
||||
"INSERT INTO products(id,code,name,description) VALUES(1,'rehearsal','rehearsal','')",
|
||||
"INSERT INTO licenses(id,license_key,customer_id,product_id,starts_at,expires_at,idempotency_key,issued_by) VALUES(1,REPEAT('a',32),1,1,UTC_TIMESTAMP(),DATE_ADD(UTC_TIMESTAMP(),INTERVAL 1 YEAR),'rehearsal',1)",
|
||||
"INSERT INTO license_instances(id,instance_id,license_id,customer_id,product_id,secret_hash,created_by) VALUES(1,'00000000-0000-4000-8000-000000000001',1,1,1,UNHEX(REPEAT('00',32)),1)",
|
||||
"INSERT INTO activation_operations(instance_id,actor_admin_id,idempotency_key,request_fingerprint,response) VALUES(1,1,'rehearsal',UNHEX(REPEAT('00',32)),JSON_OBJECT('fixture',true))",
|
||||
"INSERT INTO audit_events(actor_admin_id,action,resource_type,resource_id,details) VALUES(1,'fixture','license_instance',1,JSON_OBJECT('fixture',true))",
|
||||
}
|
||||
for _, s := range statements {
|
||||
if _, err = db.Exec(s); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if err = m.Migrate(6); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = db.Exec("INSERT INTO heartbeat_operations(instance_id,idempotency_key,request_fingerprint,response,state) VALUES(1,'fixture',UNHEX(REPEAT('00',32)),JSON_OBJECT('fixture',true),'active')"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = db.Exec("INSERT INTO instance_state_operations(instance_id,actor_admin_id,idempotency_key,request_fingerprint,response) VALUES(1,1,'fixture',UNHEX(REPEAT('00',32)),JSON_OBJECT('fixture',true))"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = m.Steps(-1); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, table := range []string{"admins", "customers", "products", "licenses", "license_instances", "activation_operations", "audit_events"} {
|
||||
var n int
|
||||
if err = db.QueryRow("SELECT COUNT(*) FROM " + table).Scan(&n); err != nil || n != 1 {
|
||||
t.Fatalf("old table %s n=%d err=%v", table, n, err)
|
||||
}
|
||||
}
|
||||
if err = m.Steps(1); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, table := range []string{"heartbeat_operations", "instance_state_operations"} {
|
||||
var n int
|
||||
if err = db.QueryRow("SELECT COUNT(*) FROM " + table).Scan(&n); err != nil || n != 0 {
|
||||
t.Fatalf("v6 table %s n=%d err=%v", table, n, err)
|
||||
}
|
||||
}
|
||||
v, dirty, err := m.Version()
|
||||
if err != nil || v != 6 || dirty {
|
||||
t.Fatalf("version=%d dirty=%v err=%v", v, dirty, err)
|
||||
}
|
||||
t.Log("005 -> 006 -> 005 -> 006 clean; seven pre-v6 fixture rows preserved; v6 operation history and lease/freeze fields intentionally removed by down")
|
||||
}
|
||||
Reference in New Issue
Block a user