Core\Db\Plan\Step::reason
$step->reason(): stringTells you why one step of a schema plan is there, in a sentence a person reads.
Core\Db\Plan\Step::reason returns one written sentence about this step: what it changes, what it can cost, and why it is graded the way it is. A step that adds a nullable column says that every existing row already has its value. A step that drops a table says that every row in it would go, and that the plan reports the drop instead of running it.
The sentence is written by Novis, not by your program. It is meant for a person who has to approve a release or read a deployment log. Print it, log it, or put it in the message a release tool shows. Do not test what words it contains: a later version of Novis may explain the same step better.
Every step carries a reason, including the steps a plan will never run. Reading it changes nothing and asks the database nothing, because the sentence was written when the plan was computed.
The examples read the reason of a single step, tell a person why a deployment stopped, and write the release notes for whoever approves the change.
Return value
Returns string. The sentence. It is written for the same database as the SQL. So it gives the reason for that database, such as a table rewrite that this server does and another does not.
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 program may open the `notes` database.[capabilities.db]connect = ["notes"]See why a plan has the step it has
<?nvs// Shows why a plan contains the step it contains. `Core\Db\Plan\Step::reason`// returns one sentence about that step, written for a person to read.
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'], ], ],]);
// The database is empty, so the plan has one step: create the table. Novis// writes the sentence, so print it and do not test its words.foreach ($schema->planAgainst($db)->steps() as Step $step) { echo $step->reason(), "\n";}
$db->close();A new table holds no rows: nothing to lose, nothing to validate, nothing to block.Tell a person why the deployment stopped
<?nvs// Shows how a deployment tells a person why it stopped. A step that is not// `Safe` waits for somebody to decide, and its reason is the whole message.
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 autoincrement, title varchar(200) not null)', []);
var $schema = Schema::fromArray([ 'tables' => [ [ 'name' => 'notes', 'columns' => [ ['name' => 'id', 'type' => 'int64', 'identity' => true], ['name' => 'title', 'type' => 'text(200)'], ['name' => 'author', 'type' => 'text(80)'], ], 'primary_key' => ['id'], ], [ 'name' => 'tags', 'columns' => [ ['name' => 'note_id', 'type' => 'int64'], ['name' => 'tag', 'type' => 'text(40)'], ], 'primary_key' => ['note_id', 'tag'], ], ],]);
uint $ran = 0;uint $waiting = 0;foreach ($schema->planAgainst($db)->steps() as Step $step) { if ($step->grade() == Step::Grade::Safe) { $db->execute($step->sql(), []); $ran = $ran + 1; continue; } // The operator gets the sentence and never has to read the SQL. echo 'this deployment needs you: ', $step->reason(), "\n"; $waiting = $waiting + 1;}
echo $ran, ' step ran, ', $waiting, " step waits for a person\n";
$db->close();this deployment needs you: A NOT NULL column with no default has no value for the rows that are already there, so it fails outright on a table that is not empty. SQLite cannot express this alter, so it is a create-copy-drop-rename rebuild, which copies every row — destructive unconditionally, whatever the change would have cost elsewhere.1 step ran, 1 step waits for a personWrite the release notes for an approval
<?nvs// Shows how a release asks a person to approve a database change. The notes// are the reasons, numbered, and there is no SQL in them.
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 autoincrement, title varchar(200) not null)', []);$db->execute('create table notes_archive (id integer primary key)', []);
var $schema = Schema::fromArray([ 'tables' => [ [ 'name' => 'notes', 'columns' => [ ['name' => 'id', 'type' => 'int64', 'identity' => true], ['name' => 'title', 'type' => 'text(200)'], ['name' => 'body', 'type' => 'text(4000)', 'null' => true], ], 'primary_key' => ['id'], ], ],]);
// One numbered line per step. The reasons say what the release costs, so the// person who approves it does not need the statements.uint $at = 0;foreach ($schema->planAgainst($db)->steps() as Step $step) { $at = $at + 1; echo $at, '. ', $step->reason(), "\n";}
$db->close();1. A nullable column with no default is a catalog write on all four: every existing row already has its value, and it is null.2. 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.