Core\Db\Plan::steps
$plan->steps(): array<Core\Db\Plan\Step>Gives you every step of a plan, in the order they must run.
A plan is the difference between a schema you wrote and a database as it is now. Core\Db\Schema::planAgainst computes that difference, and Core\Db\Plan::steps is how you read it. Each step is a Core\Db\Plan\Step. It says what the change can cost, why it is there, and the complete SQL it is.
The list is a document. Reading it asks the database nothing, and reading it twice gives the same steps. An empty list means the database already matches the schema.
Steps that will never run are in the list too. A table the database has and the schema does not declare is reported, together with the SQL that would drop it, and nothing drops it for you.
The examples show what a schema would change, how to tell that a database is up to date, and how a deployment stops before a step that needs a person.
Return value
Returns array<Core\Db\Plan\Step>. The steps. An empty array means the database already matches the schema. No apply runs a step whose isRefused() is true.
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-tell-that-the-database-is-up-to-date.nvs"
[app.capabilities.db]connect = ["notes"]schema = ["notes"]See what a schema would change
<?nvs// Shows what a schema would change in a database. `Core\Db\Plan::steps` gives// you the list of changes, and each one says why it is there.
use Core\Db;use Core\Db\Plan\Step;use Core\Db\Schema;
var $db = Db::connect('notes');
// What an earlier version of this program left in the database.$db->execute('create table notes (id integer primary key)', []);
// The tables this program needs now. One of them is missing a column, and the// other is not there at all.var $schema = Schema::fromArray([ 'tables' => [ [ 'name' => 'notes', 'columns' => [ ['name' => 'id', 'type' => 'int64', 'identity' => true], ['name' => 'title', 'type' => 'text(200)', 'null' => true], ], 'primary_key' => ['id'], ], [ 'name' => 'tags', 'columns' => [ ['name' => 'note_id', 'type' => 'int64'], ['name' => 'tag', 'type' => 'text(40)'], ], 'primary_key' => ['note_id', 'tag'], ], ],]);
// Planning reads the database. It changes nothing.var $plan = $schema->planAgainst($db);
echo Core\Arr::count($plan->steps()), " steps\n";
// The steps come in the order they must run.foreach ($plan->steps() as Step $step) { echo '- ', $step->reason(), "\n";}
$db->close();2 steps- A nullable column with no default is a catalog write on all four: every existing row already has its value, and it is null.- A new table holds no rows: nothing to lose, nothing to validate, nothing to block.Tell that the database is up to date
<?nvs// Shows how a program tells that a database is up to date. When nothing is// left to change, `Core\Db\Plan::steps` gives back an empty array.
use Core\Db;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 table is missing, so the plan has one step.echo Core\Arr::count($schema->planAgainst($db)->steps()), " before\n";
// `applySafe` runs the steps that cannot lose data.$schema->applySafe($db);
// The database now matches the schema. The new plan is empty, and that is how// a program knows there is nothing to do.echo Core\Arr::count($schema->planAgainst($db)->steps()), " after\n";
$db->close();1 before0 afterStop a deployment before a risky step
<?nvs// Shows how a deployment reads a plan before it changes anything. Steps that// can lose data or lock a table are printed for a person to run by hand.
use Core\Db;use Core\Db\Plan\Step;use Core\Db\Schema;
var $db = Db::connect('notes');
// What an earlier version of this program left in the database.$db->execute('create table notes (id integer primary key, title text not null)', []);$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'], ], 'primary_key' => ['id'], ], [ 'name' => 'tags', 'columns' => [ ['name' => 'note_id', 'type' => 'int64'], ['name' => 'tag', 'type' => 'text(40)'], ], 'primary_key' => ['note_id', 'tag'], ], ],]);
var $plan = $schema->planAgainst($db);
uint $safe = 0;uint $risky = 0;foreach ($plan->steps() as Step $step) { if ($step->grade() == Step::Grade::Safe) { $safe = $safe + 1; } else { // `sql()` is the whole statement, so a person can run it as it stands. $risky = $risky + 1; echo 'by hand: ', $step->sql(), "\n"; }}
echo $safe, ' safe, ', $risky, " for a person\n";
$db->close();by hand: DROP TABLE "notes_backup";1 safe, 1 for a person