Core\Db\Plan\Step::grade
$step->grade(): Core\Db\Plan\Step::GradeTells you what one step of a schema plan can cost, at worst.
Core\Db\Plan\Step::grade returns a Core\Db\Plan\Step::Grade case, and there are three of them. Safe cannot lose data, cannot fail on rows that are already there, and cannot hold a long lock. Locking cannot lose data, but it can fail on existing rows or block writes for a long time. Destructive can lose data. Every drop is Destructive, and so is any change SQLite has to make by copying the whole table.
The grade is the worst case, not the likely one. A change nobody wrote a rule for is graded up, because over-reporting a risk costs one confirmation and under-reporting it costs an outage.
This is what lets a program run part of a plan on its own. Core\Db\Schema::applySafe runs a plan only when every step in it is Safe, and the same decision written by hand is a comparison with ==.
The examples read the grade of each step, decide whether a deployment may run unattended, and hold the risky steps back for a maintenance window.
Return value
Returns Core\Db\Plan\Step::Grade. Safe, Locking or Destructive. The grade is the worst case, not the likely case. When there is no rule for a change, it gets the higher grade. Too high a grade costs one confirmation, and too low a grade can cost an outage.
Examples
The examples below run with this configuration file.
# The `notes` database is kept in memory. It is deleted when the program ends.[db.notes]driver = "sqlite"path = ":memory:"
# The programs may open the `notes` database and read a plan for it.[capabilities.db]connect = ["notes"]
# This example also applies its plan. Changing the tables needs the `schema` permission.[[app]]entry = "02-decide-whether-a-deployment-may-run-on-its-own.nvs"
[app.capabilities.db]connect = ["notes"]schema = ["notes"]Read what each step can cost
<?nvs// Shows what each step of a plan can cost. `Core\Db\Plan\Step::grade` returns// a `Core\Db\Plan\Step::Grade` case, so the program compares it with `==`.
use Core\Db;use Core\Db\Plan\Step;use Core\Db\Schema;
var $db = Db::connect('notes');
// A table an earlier version of this program left behind.$db->execute('create table notes_backup (id integer primary key)', []);
var $schema = Schema::fromArray([ 'tables' => [ [ 'name' => 'notes', 'columns' => [ ['name' => 'id', 'type' => 'int64', 'identity' => true], ['name' => 'title', 'type' => 'text(200)'], ], 'primary_key' => ['id'], ], ],]);
// Every step carries a grade, and `reason()` is the sentence behind it.foreach ($schema->planAgainst($db)->steps() as Step $step) { if ($step->grade() == Step::Grade::Safe) { echo 'safe: ', $step->reason(), "\n"; } elseif ($step->grade() == Step::Grade::Locking) { echo 'locking: ', $step->reason(), "\n"; } else { // Dropping a table can lose data, so it is graded `Destructive`. echo 'destructive: ', $step->reason(), "\n"; }}
$db->close();safe: A new table holds no rows: nothing to lose, nothing to validate, nothing to block.destructive: Every row in the table. Reported and never applied: a schema value describes what its author knows about, and this database holds a table it does not name.Decide whether a deployment may run on its own
<?nvs// Shows how a deployment decides whether it may change the database on its// own. It may when every step of the plan is graded `Safe`.
use Core\Db;use Core\Db\Plan\Step;use Core\Db\Schema;
var $db = Db::connect('notes');
var $schema = Schema::fromArray([ 'tables' => [ [ 'name' => 'notes', 'columns' => [ ['name' => 'id', 'type' => 'int64', 'identity' => true], ['name' => 'title', 'type' => 'text(200)'], ], 'primary_key' => ['id'], ], ],]);
uint $needsAPerson = 0;foreach ($schema->planAgainst($db)->steps() as Step $step) { if ($step->grade() != Step::Grade::Safe) { $needsAPerson = $needsAPerson + 1; }}
if ($needsAPerson == 0) { echo "every step is safe\n"; // `applySafe` makes the same check itself and throws when a step is not // `Safe`, so nothing half-applies. $schema->applySafe($db); echo "the database is up to date\n";} else { echo $needsAPerson, " steps need a person\n";}
$db->close();every step is safethe database is up to dateHold the risky steps for a maintenance window
<?nvs// Shows how a release sorts a plan into two lists: the steps the program runs// now, and the SQL a person runs in the next maintenance window.
use Core\Db;use Core\Db\Plan\Step;use Core\Db\Schema;
var $db = Db::connect('notes');
// What the last release left in the database.$db->execute('create table notes (id integer primary key, title text not null)', []);$db->execute('create table notes_2 (id integer primary key)', []);
var $schema = Schema::fromArray([ 'tables' => [ [ 'name' => 'notes', 'columns' => [ ['name' => 'id', 'type' => 'int64', 'identity' => true], ['name' => 'title', 'type' => 'text'], ], 'primary_key' => ['id'], ], [ 'name' => 'tags', 'columns' => [ ['name' => 'note_id', 'type' => 'int64'], ['name' => 'tag', 'type' => 'text(40)'], ], 'primary_key' => ['note_id', 'tag'], ], ],]);
array<string> $now = [];array<string> $window = [];foreach ($schema->planAgainst($db)->steps() as Step $step) { if ($step->grade() == Step::Grade::Safe) { $now[] = $step->sql(); } else { $window[] = $step->sql(); }}
// The steps that cannot lose data are run here, one statement at a time.foreach ($now as string $sql) { $db->execute($sql, []);}echo 'steps run: ', Core\Arr::count($now), "\n";
// The rest is written out for a person. `sql()` is the whole statement.foreach ($window as string $sql) { echo 'for the window: ', $sql, "\n";}
$db->close();steps run: 1for the window: DROP TABLE "notes_2";