Versioning reference data with effective_from dates
By Flavio
Tagged: Database
Version reference data in SQLite with effective_from dates instead of overwriting rows. Keep pricing history and drive changelog notifications.
Cloud provider prices change all the time. If you store pricing in a database and update rows in place, you lose the history.
A report generated last month might show $12/mo, while today the same tier costs $18. You can’t explain or reproduce the old number anymore, and users (or auditors) will ask questions.
To keep that history, never overwrite pricing rows. Instead, append a new row with an effective_from date and query whichever row was active at the time you care about.
I built this pattern into the provider knowledge base of one of my apps. Whenever Railway raises a plan price, I insert a new tier row so old reports stay explainable.
The problem with UPDATE
The most obvious way to record a price increase is a plain UPDATE:
UPDATE plan_tiers SET base_price_cents = 2000 WHERE slug = 'pro'
This query is simple, but it’s the wrong approach for reference data.
That row had $15/mo yesterday, and now it says $20/mo. Every historical calculation that read this row looks wrong in hindsight, and you can’t answer a basic question like “what did we quote on March 3?”
The append-only pattern
With an append-only table, each pricing edit creates a new row. The old row gets a retired_at timestamp, and nothing is deleted.
Here’s what the schema looks like:
export const planTiers = sqliteTable('plan_tiers', {
id: text('id').primaryKey(),
serviceId: text('service_id').notNull(),
slug: text('slug').notNull(),
basePriceCents: integer('base_price_cents').notNull(),
effectiveFrom: integer('effective_from', { mode: 'timestamp_ms' }).notNull(),
retiredAt: integer('retired_at', { mode: 'timestamp_ms' }),
})
When Railway’s Pro tier goes from $15 to $20, you do two things:
- Set
retired_at = nowon the current active row - Insert a new row with the new price and
effective_from = now
Both rows stay in the table, with the old one retired and the new one active.
Run both writes in a D1 batch so you never end up with a retired tier and no replacement.
Querying the current price
For live recommendations, load the tiers where retired_at IS NULL:
const tiers = await db
.select()
.from(planTiers)
.where(isNull(planTiers.retiredAt))
That gives you the current snapshot, with one active row per tier slug per service.
To reconstruct what a price was at a past date, filter by effective_from instead:
SELECT *
FROM plan_tiers
WHERE service_id = ?
AND slug = 'pro'
AND effective_from <= ?
AND (retired_at IS NULL OR retired_at > ?)
ORDER BY effective_from DESC
LIMIT 1
Pass the report’s created_at as both ? values, and you’ll get the exact tier version that was in effect when the report was generated.
Changelog notifications
My app also drafts a changelog row on every tier change so pricing edits don’t hit users silently:
export const kbChanges = sqliteTable('kb_changes', {
id: text('id').primaryKey(),
type: text('type').notNull(), // 'pricing_change', 'improved_limits', ...
serviceId: text('service_id'),
tierId: text('tier_id'),
title: text('title').notNull(),
published: integer('published', { mode: 'boolean' }).default(false),
publishedAt: integer('published_at', { mode: 'timestamp_ms' }),
})
The app drafts the entry on save, then a human reviews it in the admin panel and publishes it when it’s ready.
Published entries show up on a public /changelog page and in a “what’s new” panel for users whose saved stacks reference the affected service.
Here’s how the flow works:
- An admin (or an automated agent) proposes a price change
- The code retires the old tier and inserts the new one
- The code inserts a
kb_changesrow withpublished: false - An admin publishes the entry, and affected users see “Railway Pro went from $15 to $20/mo”
Match notifications to users using the service_ids stored on their reports, so you don’t blast everyone for a change that doesn’t affect them.
Metadata-only edits
Not every edit needs a new pricing version. Renaming a tier from “Pro” to “Pro Plan” is just metadata, so you can update the active row in place.
Only create a new version when pricing fields change: base price, included quotas, overage rates, or hard limits. Compare the old and new values before writing. If dollars or limits moved, retire the old row and insert a new one; if only the display name changed, patch the existing row.
Why this beats a history table
Some teams add a separate plan_tiers_history table, and that works too.
I prefer keeping all versions in one table with effective_from and retired_at. You only need one query shape and one index, and getting the current snapshot is just WHERE retired_at IS NULL.
Either way, appending instead of overwriting keeps every saved quote explainable when you need to look back at it later.
Want me to talk about your product? You can sponsor this site.
Related posts about database: