Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions doc/database-migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

OpenMOS stores its data in PostgreSQL only. The original MongoDB store was
removed outright (MOS-34) rather than kept alongside or behind a switch.
Until the PostgreSQL store lands, the server runs on the in-memory reference
store (`internal/repository/memory`), so data is not persisted.
The PostgreSQL repositories exist (`internal/store/postgres`, MOS-14), but
until the server is switched over (MOS-16) it runs on the in-memory
reference store (`internal/repository/memory`), so data is not persisted.

## Why

Expand All @@ -21,7 +22,7 @@ store (`internal/repository/memory`), so data is not persisted.
| --- | --- | --- |
| Driver and pool | [pgx v5](https://github.com/jackc/pgx) (`pgxpool`) | Actively maintained, native PostgreSQL types |
| Migrations | [goose v3](https://github.com/pressly/goose) | Plain SQL files embedded in the binary |
| Integration tests | [testcontainers-go](https://github.com/testcontainers/testcontainers-go) | Runs a real PostgreSQL in tests and CI |
| Integration tests | PostgreSQL service container in CI, Docker Compose locally | Tests read `OPENMOS_TEST_DATABASE_URL`; chosen over testcontainers-go to keep Docker out of the test binary |
| Database | PostgreSQL 17 | Local development via Docker Compose |

## Schema principles
Expand All @@ -45,7 +46,12 @@ Tables: `running_orders`, `stories`, `items`, `mos_objects`.
- `internal/db` and the MongoDB repositories are gone; a new `internal/store/postgres`
package provides the pool, transactions and the four repositories.
- `internal/repository`: interfaces stay, gaining composite keys and ordering
operations. Shared errors live in `errors.go` and grow with each change.
operations (`Move`, `Swap`, `ReplaceAll`). The ordering rules are shared
helpers in `order.go`, so both stores behave alike. Shared errors live in
`errors.go` and grow with each change.
- The PostgreSQL repositories use hand-written pgx queries. Operations that
assign positions lock the parent row first, so concurrent appends and moves
on one running order or story run one at a time.
- Services run each MOS message in one transaction, and running order updates
use version-checked writes (optimistic locking).
- Configuration: the old `mongo` block is gone; a new `postgres` block, with
Expand Down
10 changes: 7 additions & 3 deletions doc/developer-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ OpenMOS follows a **Clean Layered Architecture** with clear separation of concer
| **Server** | `internal/server/` | TCP connections, client management, heartbeat monitoring |
| **Service** | `internal/service/` | Business logic, MOS operations, event publishing |
| **Repository** | `internal/repository/` | Data access abstraction, CRUD operations |
| **Store** | `internal/store/postgres/` | PostgreSQL connection pool and schema migrations (repositories to follow) |
| **Store** | `internal/store/postgres/` | PostgreSQL connection pool, schema migrations and repositories |
| **Model** | `internal/model/` | Domain entities and value objects |
| **XML** | `internal/xml/` | Message parsing and serialization |
| **Events** | `internal/events/` | Pub-sub event bus for real-time notifications |
Expand Down Expand Up @@ -83,12 +83,15 @@ OpenMOS/
│ │ ├── repository/
│ │ │ ├── repository.go # Interface definitions
│ │ │ ├── errors.go # Shared errors (ErrNotFound, ...)
│ │ │ ├── order.go # ID checks and shared ordering rules
│ │ │ ├── memory/ # In-memory reference implementation
│ │ │ └── repotest/ # Contract suite for every implementation
│ │ │
│ │ ├── store/postgres/
│ │ │ ├── migrate.go # Goose migrations (embedded SQL)
│ │ │ ├── pool.go # pgx connection pool, TLS rules
│ │ │ ├── repository.go # PostgreSQL repositories (New), error mapping
│ │ │ ├── runningorder.go, story.go, item.go, object.go
│ │ │ └── migrations/ # Schema migrations (00001_init.sql, ...)
│ │ │
│ │ ├── server/
Expand Down Expand Up @@ -162,7 +165,7 @@ RunningOrder (1) ──► (N) Story ──► (N) Item ──► MOSObject
2. **Heartbeat Monitoring**: Client heartbeat is tracked; timeout triggers disconnection
3. **Message Reception**: XML messages are parsed and validated
4. **Service Processing**: Business logic handles operations (create/update/replace)
5. **Storage**: Changes are stored through the repository interfaces (in memory until the PostgreSQL store lands)
5. **Storage**: Changes are stored through the repository interfaces (in memory until the server switches to PostgreSQL in MOS-16)
6. **Event Publishing**: Service publishes events to the event bus
7. **Client Notification**: Connected clients receive real-time updates via subscriptions

Expand Down Expand Up @@ -336,7 +339,8 @@ sentry:
### Testing
- Write unit tests for service and repository layers
- Every repository implementation must pass the contract suite:
call `repotest.Run` from its tests (see `repository/memory`)
call `repotest.Run` from its tests (see `repository/memory` and
`store/postgres/repository_test.go`)
- Use interfaces to enable mocking
- Test XML parsing with example messages from `/res/`
- PostgreSQL tests (`internal/store/postgres`) need a throwaway database in
Expand Down
87 changes: 44 additions & 43 deletions src/internal/model/mos.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,52 @@ import (
"time"
)

// The model follows the MOS keys: an ID is unique only within its parent, so
// stories and items carry their parents' IDs, and objects their MOS device.
// Order is the position among siblings and is managed by the repositories.
// Durations of items and objects are counts of samples at TimeBase samples
// per second (MOS objTB, e.g. 50 for PAL or 59.94 for NTSC).

// MOSObject represents the lowest level media object in the MOS hierarchy
type MOSObject struct {
ID string `json:"id"` // Unique MOS Object ID
ObjectType string `json:"objectType"` // Type of object (e.g., VIDEO, AUDIO, GRAPHIC)
Slug string `json:"slug"` // Human-readable name
Duration int `json:"duration"` // Duration in seconds
TimeBase int `json:"timeBase,omitempty"`
Status StatusType `json:"status"`
ObjectID string `json:"objectID,omitempty"`
MediaID string `json:"mediaID,omitempty"`
MosAbstract string `json:"mosAbstract,omitempty"`
Metadata map[string]string `json:"metadata,omitempty"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
MosID string `json:"mosID"` // MOS device that owns the object
ID string `json:"id"` // objID, unique within MosID
ObjectType string `json:"objectType"` // STILL, AUDIO or VIDEO; empty if unknown
Slug string `json:"slug"` // Human-readable name
Duration int64 `json:"duration"` // objDur, in samples
TimeBase float64 `json:"timeBase,omitempty"`
Status StatusType `json:"status"`
Abstract string `json:"abstract,omitempty"` // mosAbstract
Metadata map[string]string `json:"metadata,omitempty"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}

// Item represents a single item within a story
type Item struct {
ID string `json:"id"` // Unique Item ID
ObjectID string `json:"objectID,omitempty"` // Reference to MOS Object
Slug string `json:"slug"`
Duration int `json:"duration"` // Duration in seconds
EditorialDuration int `json:"editorialDuration,omitempty"`
TimeBase int `json:"timeBase,omitempty"`
Status StatusType `json:"status"`
Order int `json:"order"` // Order within the story
StoryID string `json:"storyID"` // Parent story ID
Metadata map[string]string `json:"metadata,omitempty"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
RunningOrderID string `json:"runningOrderID"` // Parent running order
StoryID string `json:"storyID"` // Parent story
ID string `json:"id"` // itemID, unique within its story
ObjectID string `json:"objectID,omitempty"` // Referenced MOS object (objID)
MosID string `json:"mosID,omitempty"` // MOS device of the referenced object
Slug string `json:"slug"`
Duration int64 `json:"duration"` // itemEdDur, in samples
TimeBase float64 `json:"timeBase,omitempty"`
Status StatusType `json:"status"`
Order int `json:"order"` // Position within the story
Metadata map[string]string `json:"metadata,omitempty"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}

// Story represents a story in the running order (collection of items)
type Story struct {
ID string `json:"id"` // Unique Story ID
RunningOrderID string `json:"runningOrderID"` // Parent running order
ID string `json:"id"` // storyID, unique within its running order
Slug string `json:"slug"`
Number string `json:"number,omitempty"`
Duration int `json:"duration"` // Duration in seconds
Status StatusType `json:"status"`
Order int `json:"order"` // Order within the running order
PreviousID string `json:"previousID,omitempty"` // Previous story ID for linked list
NextID string `json:"nextID,omitempty"` // Next story ID for linked list
Order int `json:"order"` // Position within the running order
Presenter string `json:"presenter,omitempty"`
Metadata map[string]string `json:"metadata,omitempty"`
CreatedAt time.Time `json:"createdAt"`
Expand All @@ -55,18 +58,16 @@ type Story struct {

// RunningOrder represents the top-level running order (collection of stories)
type RunningOrder struct {
ID string `json:"id"` // Unique Running Order ID
MosID string `json:"mosID"` // MOS ID for this running order
Slug string `json:"slug"`
Status StatusType `json:"status"`
Duration int `json:"duration"` // Total duration in seconds
FirstStoryID string `json:"firstStoryID,omitempty"` // First story ID for linked list
LastStoryID string `json:"lastStoryID,omitempty"` // Last story ID for linked list
AirTime *time.Time `json:"airTime,omitempty"`
Channel string `json:"channel,omitempty"`
Metadata map[string]string `json:"metadata,omitempty"`
Version int `json:"version"`
CreatedBy string `json:"createdBy,omitempty"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
ID string `json:"id"` // roID
MosID string `json:"mosID"` // MOS ID for this running order
Slug string `json:"slug"`
Status StatusType `json:"status"`
Duration int `json:"duration"` // roEdDur, in seconds
AirTime *time.Time `json:"airTime,omitempty"` // roEdStart
Channel string `json:"channel,omitempty"`
Metadata map[string]string `json:"metadata,omitempty"`
Version int `json:"version"`
CreatedBy string `json:"createdBy,omitempty"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
7 changes: 6 additions & 1 deletion src/internal/repository/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ var (
// refers to, does not exist.
ErrNotFound = errors.New("not found")

// ErrAlreadyExists means Create was called with an ID that is in use.
// ErrAlreadyExists means Create was called with a key that is in use.
ErrAlreadyExists = errors.New("already exists")

// ErrVersionConflict means an update was based on a stale version of a
// running order.
ErrVersionConflict = errors.New("version conflict")

// ErrInvalid means the request itself is malformed: an empty or too long
// ID, a value outside its MOS limits, or an ordering request that names
// the same record twice.
ErrInvalid = errors.New("invalid")
)
Loading
Loading