Skip to content

Core\Db\Plan\Step::Grade

What one step of a schema change can cost you, at worst.

Core\Db\Plan\Step::Grade is declared in the class Core\Db\Plan\Step. The method grade returns it.

A schema plan is the list of steps that would bring a database in line with the schema your code declares, and every step carries one of these grades. Safe means the step cannot lose data, cannot fail because of rows that are already there, and cannot hold a long lock — that is the half a deployment can run by itself, at any hour, with nobody watching. Locking keeps your data but can block writes for a long time, or fail on the rows you already have. Destructive can lose data, and every drop is in there.

In plain words: the grade is the worst that could happen, not the likely one. A step nothing has a rule for is graded up rather than down, because over-stating the risk costs one confirmation and under-stating it costs an outage.

Examples

What each grade can cost

<?nvs
// The three grades a step of a schema plan can carry. Each one says the worst
// that step could cost, which is what decides whether a deployment is allowed
// to run it with nobody watching.
class Grades
{
public static function cost(Core\Db\Plan\Step::Grade $grade): string
{
return match ($grade) {
Core\Db\Plan\Step::Grade::Safe => 'Safe: keeps your data, does not trip over rows that are already there, and holds no long lock',
Core\Db\Plan\Step::Grade::Locking => 'Locking: keeps your data, but can block writes for a long time or fail on the rows you already have',
Core\Db\Plan\Step::Grade::Destructive => 'Destructive: can lose data, and every drop is in here',
};
}
}
array<Core\Db\Plan\Step::Grade> $grades = [
Core\Db\Plan\Step::Grade::Safe,
Core\Db\Plan\Step::Grade::Locking,
Core\Db\Plan\Step::Grade::Destructive,
];
foreach ($grades as Core\Db\Plan\Step::Grade $grade) {
echo Grades::cost($grade), "\n";
}
Output
Safe: keeps your data, does not trip over rows that are already there, and holds no long lock
Locking: keeps your data, but can block writes for a long time or fail on the rows you already have
Destructive: can lose data, and every drop is in here

The half a deployment runs by itself

<?nvs
// A deployment reads the grade of every step and applies only the `Safe` steps.
// It gives every other step to a person, who watches it run.
array<{ step: string, grade: Core\Db\Plan\Step::Grade }> $plan = [
{ step: 'create table invoices', grade: Core\Db\Plan\Step::Grade::Safe },
{ step: 'add column invoices.sent_at', grade: Core\Db\Plan\Step::Grade::Safe },
{ step: 'add unique key on invoices.number', grade: Core\Db\Plan\Step::Grade::Locking },
{ step: 'drop column customers.fax', grade: Core\Db\Plan\Step::Grade::Destructive },
];
foreach ($plan as { step: string, grade: Core\Db\Plan\Step::Grade } $entry) {
if ($entry->grade == Core\Db\Plan\Step::Grade::Safe) {
echo 'apply now: ', $entry->step, "\n";
} else {
echo 'hand over: ', $entry->step, "\n";
}
}
Output
apply now: create table invoices
apply now: add column invoices.sent_at
hand over: add unique key on invoices.number
hand over: drop column customers.fax

Does this release need a maintenance window

<?nvs
// This program checks whether a release can go out at any time. The answer
// depends on the worst grade of any step in the release.
class Release
{
// `rank` gives each grade a number, so two grades can be compared.
// A higher number means a riskier step.
public static function rank(Core\Db\Plan\Step::Grade $grade): int
{
return match ($grade) {
Core\Db\Plan\Step::Grade::Safe => 0,
Core\Db\Plan\Step::Grade::Locking => 1,
Core\Db\Plan\Step::Grade::Destructive => 2,
};
}
public static function worst(array<Core\Db\Plan\Step::Grade> $plan): Core\Db\Plan\Step::Grade
{
Core\Db\Plan\Step::Grade $worst = Core\Db\Plan\Step::Grade::Safe;
foreach ($plan as Core\Db\Plan\Step::Grade $grade) {
if (Release::rank($grade) > Release::rank($worst)) {
$worst = $grade;
}
}
return $worst;
}
public static function verdict(array<Core\Db\Plan\Step::Grade> $plan): string
{
return match (Release::worst($plan)) {
Core\Db\Plan\Step::Grade::Safe => 'ship it whenever you like',
Core\Db\Plan\Step::Grade::Locking => 'book a quiet hour: writes may wait on this one',
Core\Db\Plan\Step::Grade::Destructive => 'someone has to sign this off, and take a backup first',
};
}
}
// This afternoon's release: two new columns and an index.
array<Core\Db\Plan\Step::Grade> $small = [
Core\Db\Plan\Step::Grade::Safe,
Core\Db\Plan\Step::Grade::Safe,
Core\Db\Plan\Step::Grade::Safe,
];
// Next week's release removes a column that no code reads.
array<Core\Db\Plan\Step::Grade> $cleanup = [
Core\Db\Plan\Step::Grade::Safe,
Core\Db\Plan\Step::Grade::Destructive,
Core\Db\Plan\Step::Grade::Locking,
];
echo 'this afternoon: ', Release::verdict($small), "\n";
echo 'next week: ', Release::verdict($cleanup), "\n";
Output
this afternoon: ship it whenever you like
next week: someone has to sign this off, and take a backup first