A field is a property, and a property needs a column. There were three ways to get from one to the other: generate migrations by diffing a committed snapshot of the schema; keep every field in one JSON column and have no columns to manage at all; or sync the schema automatically in development and write the production migration by hand. Mainstay does the third, plus a command that compares a database with what the classes declare and fails when they differ.
Two tables for each type
Every registered type gets a table named after it, article for Article, and a sibling holding its translated fields, article_locales. The sibling is there even when nothing is translated yet, because a locale with no row is how an untranslated entry is told apart from a translated one. Both carry a site_id, and the main table a deleted_at, from the first migration: added later, either would be a migration of every table in the system.
In development: mainstay:sync
php artisan mainstay:sync compares the database with the declared types and alters it to match. It is built to be hard to run by accident:
It is off until
MAINSTAY_SCHEMA_SYNC=trueturns it on.It never runs in production, whatever the flag says. A flag copied into a production
.envby mistake is exactly the case it refuses.A renamed property reads as a dropped column and an added one, and nothing in a diff can tell the two apart. So it lists every column it would drop and asks first, and declining changes nothing at all, rather than applying the half that does not drop.
A type change that would alter stored values is asked about in the same way. Under
--forceit asks the database instead: a stored value the new type refuses stops the sync before anything is altered.Afterwards it compares again, and lists anything it could not close for you to change by hand.
In production: a migration you write
Production gets an ordinary Laravel migration. Adding the glyph field to Article is this, and nothing generates it:
Schema::table('article', function (Blueprint $table) {
$table->string('glyph', 40)->nullable();
});
In CI: mainstay:schema:check
php artisan mainstay:schema:check runs against a database built by those migrations. It builds each table as the declaration would, under a random scratch name, compares the live table with it and drops the scratch copy again, so the comparison is between what the database actually has and what it would have built, in the database's own types. A migration that forgot the column fails the build with a line naming it: article.glyph: declared, and the column does not exist.
Why not generate the migrations
The danger of syncing in development is that the development schema and the production schema drift apart, because one was reached by automatic alters and the other by hand, and nothing proves they agree. Detecting a mismatch is cheap. Describing the fix is the expensive part, and the drift check removes the danger for a fraction of the cost.
Some things stay manual regardless: no diff can tell a rename from a drop and an add, or convert data when a field's type changes.
Where no migration is needed
A page's blocks are stored as data in the entry's content, so adding a field to a block changes no column. It needs a default, or a type that reads nothing as something, such as a nullable one, because the blocks stored before it was added do not hold it.