# Migrating to TableTheory v7

v7 removes one piece of magic and adds two primitives. Only the first is
breaking, and only for Go models that relied on a field *name*.

## 1. Managed lifecycle timestamps are explicit opt-in (breaking, Go only)

### What changed

Before v7 the Go runtime decided a field was a managed lifecycle timestamp by
looking at its **name**: any `time.Time` field called `CreatedAt` or `UpdatedAt`
— or matching that name case-insensitively — was stamped with the write clock on
every create/save, whether or not the model asked for it.

That was silent data loss for any model that owned such a column. A field the
application kept for its own purposes was rewritten on every put, and the only
way to keep it was to rename it. (This is exactly what downstream products did:
`CreatedAt` became `RowCreatedAt`.)

From v7 a field is managed **only** when it carries the tag:

```go
// managed: stamped by the runtime
CreatedAt time.Time `theorydb:"created_at" json:"createdAt"`
UpdatedAt time.Time `theorydb:"updated_at" json:"updatedAt"`

// not managed: ordinary attribute, stored exactly as you set it
CreatedAt time.Time `json:"createdAt"`
```

TypeScript and Python never inferred from a name, so nothing changes there:
`roles: ['created_at']` and `roles=[Role.CREATED_AT]` remain the way in.

### Who is affected

Only Go models with a `time.Time` field named `CreatedAt` / `UpdatedAt` that does
**not** carry `theorydb:"created_at"` / `theorydb:"updated_at"`. Those models now
persist the value the application sets instead of the write clock.

### How to migrate

1. **You wanted managed timestamps.** Add the tag. Nothing else changes:

   ```diff
   -  CreatedAt time.Time `json:"createdAt"`
   +  CreatedAt time.Time `theorydb:"created_at" json:"createdAt"`
   ```

2. **You own the column.** Change nothing. Your value is now persisted verbatim,
   and you can rename `RowCreatedAt` back to `CreatedAt` if you prefer.

### Finding the models

Model registration records the exact field and the tag that restores the old
behaviour:

```go
metadata, err := registry.GetMetadata(&MyModel{})
for _, warning := range metadata.Warnings {
    log.Print(warning)
}
// field CreatedAt is no longer an implicitly managed created_at timestamp;
// add theorydb:"created_at" (or rename the field) to opt in
```

The warning is emitted only for a `time.Time` field whose name matches the
retired rule and which carries no timestamp tag, so it is a precise migration
list rather than a general nag.

## 2. `created_at` is written once (all runtimes)

Managed `created_at` used to be restamped with the write clock on **every** put,
so a read-modify-write round trip silently moved an item's creation time. From
v7 a write that carries a creation time keeps it: only a write whose model holds
no creation time takes the clock.

There is nothing to change in your code, but one shape behaves differently if you
were relying on the old stamp:

```go
// Before v7 this moved created_at forward on every call.
// From v7 it is still the item's creation time.
var note Note
db.Model(&Note{PK: pk, SK: sk}).Where("PK", "=", pk).Where("SK", "=", sk).First(&note)
note.Body = "edited"
db.Model(&note).CreateOrUpdate()
```

A `PutItem` replaces the whole item, so a write cannot see the creation time it
is about to overwrite. The runtime therefore stamps the clock only when the model
being written has none, and records what it wrote back into the model. To
guarantee write-once rather than observe it, use the conditional create —
`Create()` in Go, `ifNotExists` in TypeScript — which fails when the item already
exists.

Consequence to be aware of: putting a **newly constructed** model with an empty
`created_at` over an existing item is treated as a creation. Read the item first
when you mean to update it.

## 3. New: an atomic counter and a monotonic floor

Additive, no migration required:

```go
next, err := tabletheory.Increment(ctx, db, &Usage{PK: "TENANT#1", SK: "MONTH#2026-11"}, "events", 1)
floor, err := tabletheory.Advance(ctx, db, &Tenant{PK: "TENANT#1", SK: "RETENTION"}, "floor", epochSeconds)
```

See [Atomic Primitives](../features/atomic-primitives.md).

## Version alignment

A v7 release moves every language's version together. Go consumers change their
module requirement and their imports from `github.com/theory-cloud/tabletheory/v6`
to `github.com/theory-cloud/tabletheory/v7`, then run `go get` and `go mod tidy`.
TypeScript and Python package versions move with the release; their import
specifiers do not change.

## Related

- [Lifecycle Timestamps](../features/lifecycle-timestamps.md) — the full role contract
- [Atomic Primitives](../features/atomic-primitives.md) — the two new primitives
- [Migration index](v6.md) — the previous major's notes
