Repository navigation
NTNT v0.6.4 — Safer database migrations and adoption
NTNT v0.6.4 — Safer database migrations and adoption
0.6.4 fixes ntnt db problems found while moving real projects onto it, and
reports a runtime error in an imported module against that module's file.
ntnt db on SQLite
- Time defaults. Migrations can use SQLite's date and time functions, so
DEFAULT CURRENT_TIMESTAMP,DEFAULT (datetime('now'))and anupdated_at
trigger plan, migrate and verify. They used to failplanwith
DB_SQLITE"code -1" (#414). - Refusals name what was refused. A migration statement that calls a
function outside the supported set, itself or through a trigger it fires or a
view it reads, fails withDB_SQL_UNSUPPORTED, naming the statement number
and the function, for exampleStatement 2 uses SQLite function `randomblob`. Other failing statements are numbered too. A trigger or view
body that nothing in the migration uses is checked only during inspection and
reported asDB_COVERAGE_INCOMPLETE, and an authored PRAGMA is refused before
replay withDB_REPLAY_UNSUPPORTED(#414). - Scratch settings. An empty scratch takes the target's journal mode, page
size, encoding and auto-vacuum, so a scratch made withtouchworks with any
target, including one the application has opened, whichstd/db/sqlite
connect()switches to WAL. A difference that SQLite can no longer change in
a scratch that is not empty fails withDB_SCRATCH_MISMATCH, naming each
setting and both values, and says to replace the scratch with a new empty
file. These cases used to fail withDB_SQLITE"code 1" (#413, #425). - Partial and expression indexes.
CREATE INDEX ... WHERE ..., unique or
not, and indexes on expressions such aslower(email)plan, migrate and
verify, and a predicate or expression changed outside a migration is reported
as drift. They used to leave every planunknownwith
DB_COVERAGE_INCOMPLETE(#411). - Drops that would delete child rows. A migration that drops a table which
other tables reference withON DELETE CASCADE,SET NULLorSET DEFAULT
is refused withDB_REBUILD_REQUIRED, naming those tables, unless it uses
thesqlite_rebuildsidecar. SQLite deletes every row of a dropped table
first. A rebuild without the sidecar therefore deleted or orphaned child rows
on the target while plan, migrate and verify all passed (#419). - Statement limit. On a target, a SQLite migration runs under
[databases.<name>.limits] statement_seconds(90 seconds by default) instead
of a fixed 5 seconds. A statement past the limit fails with
DB_STATEMENT_LIMIT, naming the statement and the limit. Scratch replay keeps
its fixed 5-second bound. A plan records a declared limit, so a changed limit
makes the plan stale (#429). - Long histories. Replay inspection reuses the work for objects a migration
does not change, soplan,status,verifyandmigrategrow about
linearly with the history: 400 one-table migrations plan in under half a
second instead of 28 seconds, and 1,024 in about 2 seconds instead of timing
out. Histories dense in foreign keys benefit too: a 400-tableON DELETE CASCADEchain plans in half a second. SQLite refuses programs nested more
than 1,000 trigger or foreign-key-action levels deep, so a table at the end
of a deeper chain is uncovered. SQLite operations now run on threads with an
8 MiB stack, enough for the deepest programs SQLite accepts (#421).
ntnt db on PostgreSQL
- Objects outside the managed schema. Inspection covers every schema the
migration history creates, and the objects the history creates in other
schemas (for examplepublic.audit_log), with the same coverage and drift
rules as the managed schema. An object dropped there out of band is now
reported as drift, not as a pass. Other objects in those schemas are not
inspected. A foreign key from a table the history does not track into one
it creates outside the managed schema blocks execution with
DB_EXTERNAL_DEPENDENCY, as one into a managed table does, so aDELETE
cannot cascade into rows the plan never saw (#420). - Statements outside the managed schema run as written. A pending statement
that may act outside the managed scope (a qualified name, anIN SCHEMAlist,
search_path, or SQL the parser cannot read) is flagged in the plan with the
hazardOUTSIDE_MANAGED_SCOPE, naming the statement and the schema, and the
plan can still be approved.DB_OUTSIDE_SCOPErefuses only changes to the
_ntntledger schema and to system schemas. When the history creates objects
outside the managed scope,statusandverifyread the target twice, and
reportDB_TARGET_CHANGEDinstead of comparing if it changed in between. A
saved plan whose pending migrations act outside the managed scope must be
planned again (#420). - Checks follow views across schemas.
[requires],[ensures]and
adoption checks are refused withDB_CHECK_UNSUPPORTED, before any of them
runs, when a view they can read reaches a non-builtin or volatile function
through views in any schema. Before, only views in the managed schema were
checked, so such a check could run a function that writes (#420). - Policies that call outside routines. A row-level security policy that
calls a routine outsidepg_catalogand the managed scope is now uncovered.
It used to be reported as covered (#434). - Long histories (unchanged). PostgreSQL replay still costs time in
proportion to the square of the history length; DATABASE_MIGRATIONS.md
states the current limits. #421 stays open for PostgreSQL. - Known limitation. A pending migration that replaces an object already on
the target outside the managed schema, for exampleDROP TABLE IF EXISTS public.legacyfollowed byCREATE TABLE public.legacy, is reported as drift
and cannot be planned. Create the replacement under a new name, or drop the
old object before planning (#439).
ntnt db on every engine
-
Empty migrations. A migration with no statements, such as the file
ntnt db newcreates, fails withDB_MIGRATION_EMPTY, naming it, instead of being
reported as unsupported SQL (#428). -
Coverage diagnostics name the objects.
DB_COVERAGE_INCOMPLETElists the
catalog objects outside supported inspection in a newobjectsfield, and
when replaying a migration produced them,sourcenames that migration.
Human output prints a diagnostic'sSource:andObjects:lines (#411). -
ntnt db newtakes turns. While it numbers and creates a migration,
ntnt db newholds the lock onntnt.tomlthatntnt db initalready uses,
so two runs at the same moment can no longer both create0001_.... A run
that finds the lock held stops at once withDB_NEW_CONFLICT, creates
nothing and can simply be rerun; the lock ends with the command, so there is
nothing to clean up.ntnt db init's conflict message now namesntnt db new
and the manifest (#388).- On Windows, a
statusorplanthat readsntnt.tomlduring that moment
fails withDB_INPUT_IOand can be rerun. - On NFS, a read-only
ntnt.tomlcannot be locked, sontnt db newfails
withDB_SCAFFOLD_IOnaming the file and the error.
- On Windows, a
-
Adopting an application with its own migration ledger. When
plan --adopt-throughis refused because the target holds tables the history does
not create, the advice names them. If one is the application's previous
migration ledger, it gives the commands to retire it: on PostgreSQL, move it
to a schema outside the inspected scope; on SQLite, take exclusive access in one shell, check for incoming foreign
keys, and keep the lock until the archived rows have been verified and the
original table dropped. After interruption, restart from the lock step;
never resume atDROP TABLE(#451).
DATABASE_MIGRATIONS.mdhas a new section on retiring an application's
previous ledger. Tested by adopting a copy of a real application's 33
PostgreSQL migrations (#431). -
ntnt db newkeeps the ID width. A new migration's ID is padded to the
width of the widest existing ID, so a history numbered001_to033_gets
034_, which sorts last inlsand shell globs. An empty directory still
gets four digits (#431).
ntnt db refusals say what to fix
Refusals that used one fixed message now name the file, secret, migration or
flag they are about and the cause, with advice for that cause. Codes are
unchanged, and secret values, row values and file contents are never shown.
- Inputs.
DB_INPUT_IOnames the file it could not read and why: not
found, a symbolic link, too large, changed while it was read, or the
operating system's error. This covers the manifest,--plan,--against
and migrations entries. A refused migrations entry is named relative to the
project, a sidecar that is not valid TOML gives its line and column,
DB_CHECK_UNSUPPORTEDnames the check and its phase, andDB_DUPLICATE_ID
names both files (#422). - Dotfiles and READMEs. In
migrations/, entries whose names start with
., andREADMEorREADME.<ext>in any letter case, are ignored, so a
.gitkeep, a README or an editor's swap file no longer stopsstatusand
plan. Every other file that is not a migration or sidecar is still
refused, by name (#422). - Secrets and files.
DB_SECRETnames the connection secret, its role
(target, scratch or approval) and the cause, such as an unset environment
variable, a missing[secrets.<NAME>]table orNTNT_ENV=production
disabling the env provider. A suggested[secrets]table quotes a dotted
name, so it declares that secret.DB_FILE_IDENTITYnames the secret whose SQLite
path broke a rule, and which rule, never the path. The SQLite quick start
uses"$(pwd -P)/data/app.sqlite", because$PWDkeeps a symlinked
directory, which is refused (#422). - Selections. An unknown
--targetor--databaselists the declared
names, andDB_ADOPTION_INPUTsays which adoption requirement failed (#422). - History and plans.
DB_HISTORY_EDITED,DB_HISTORY_MISSINGand
DB_HISTORY_DIVERGENTname the migration and how to restore it. A
migration merged in below the last applied ID is told to take an unused ID
above it.DB_PLAN_STALEnames the binding that changed, such as the
selected target. Advice to rename a migration also renames its.toml
sidecar, and a migration added below an adopted checkpoint is named, never
a covered one (#423). - Approval and output.
DB_APPROVAL_REQUIREDsays how to approve the plan,
instead of advising a new plan, whose digest would fail the same way.
DB_OUTPUTnames the file, and an--outfile that already exists, or
whose directory does not or passes through a file, is refused before any
replay or migration runs. A report that cannot be written completely is
removed. If saving amigratereport fails, checkprogresson stdout and
runntnt db statuson the same target before retrying: database changes
may already have committed. An approval secret the
provider cannot read is refused for that cause (#425). --help. Eachntnt dbsubcommand lists only the flags it takes.
Command lines accepted before are still accepted (#432).- SQLite errors. A failing statement gives SQLite's own message where it
holds only names (which UNIQUE or FOREIGN KEY constraint failed, a missing
column) and the result code's name, such asSQLITE_CONSTRAINT_UNIQUE. A
file that cannot be written is named by role and secret instead of being
blamed on a statement. The parser gate names the statement and why it
stopped: a syntax error near a token, the 4,096-token limit, an authored
PRAGMA or an unsupported statement.DB_BUSYsays that another connection
holds the lock and that the migration it stopped committed nothing. Earlier
migrations in the same run may have committed; rerunning the same plan
skips them (#424, #425).
Runtime errors in imported modules
- The right file and line.
ntnt runreports a runtime error raised in a
function from an imported module against that module's file and line, shows
that file's source line, and adds anote: called from main.tnt:3for each
call site that led there (note: imported from ...when the module's own
top-level code failed while it was imported). A parse error in an imported
module names the module, and an error raised while a function'srequiresor
ensuresclause is evaluated points at that clause. These errors used to name the entry file with the
module's line number, beside unrelated or empty source (#412). - Route handler error logs and the development error page still pair the
handler's file with the error's line; #257 tracks them.
Security
The lockfile updates url from 2.5.8 to 2.5.9 for RUSTSEC-2026-0592 and its dependency form_urlencoded from 1.2.2 to 1.2.3 (#450).
Upgrading
- Finish or reconcile any interrupted
migratewith 0.6.3 before upgrading.
0.6.4 cannot reconcile an attempt whose plan 0.6.3 saved when its pending
migrations act outside the managed schema, or when its applied history
creates another schema (#440).
See NTNT v0.6.3 for the previous release.
What's Changed
- 0.6.4: fix the known ntnt db issues (#414, #413, #411, #388) and imported-module error locations (#412) by @larimonious in #416
- 0.6.4 correctness: cascading SQLite drops, created PostgreSQL schemas, replay cost, statement limits, empty migrations by @larimonious in #435
- ntnt db: refusals say what to fix (0.6.4 diagnostics) by @larimonious in #445
- Protect SQLite ledger retirement from writes after archive verification by @larimonious in #451
- Prepare NTNT 0.6.4 for release by @larimonious in #452
Full Changelog: v0.6.3...v0.6.4