Core\Db\Plan\Step::isRefused
$step->isRefused(): boolTells you whether a plan reports one step instead of running it.
Core\Db\Plan\Step::isRefused returns true for a step the plan will never run for you. That is always a step that would delete something: a table, a column or a key the database has and your schema value does not name. The step is in the plan, with the statement that would do it, and the decision is yours.
A schema value describes what its author knows about. Another program, another release or a person may own that table, so deleting it because one schema does not name it would lose data nobody asked to lose. Core\Db\Schema::applySafe and applyIncludingRisky both leave these steps alone.
true therefore means: read this and decide. Your program can print the step, run its statement with Core\Db\Connection::execute, or do nothing at all.
The examples separate the steps a plan runs from the ones it reports, delete a table on purpose, and warn an operator about what an earlier release left behind.
Return value
Returns bool. true for a step that no apply ever runs. Something missing from the schema is never deleted. A database is often shared with a queue, a reporting view or tables an administrator added. A program that wants the drop runs this step's sql().
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 = "03-warn-an-operator-about-what-a-release-left-behind.nvs"
[app.capabilities.db]connect = ["notes"]schema = ["notes"]See which steps a plan will not run
<?nvs// Shows which steps of a plan are reports. `Core\Db\Plan\Step::isRefused`// returns `true` for a step the plan will never run for you.
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'], ], ],]);
uint $toRun = 0;uint $reported = 0;foreach ($schema->planAgainst($db)->steps() as Step $step) { if ($step->isRefused()) { $reported = $reported + 1; echo 'reported: ', $step->reason(), "\n"; } else { $toRun = $toRun + 1; }}
echo $toRun, ' step to run, ', $reported, " step reported\n";
$db->close();reported: 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.1 step to run, 1 step reportedDelete a table the schema no longer names
<?nvs// Shows how a program deletes a table its schema no longer names. The plan// never does this for you, so the program runs the statement itself.
use Core\Db;use Core\Db\Plan\Step;use Core\Db\Schema;
var $db = Db::connect('notes');
// The table this program still uses, and one it stopped using last year.$db->execute('create table notes (id integer primary key autoincrement, title varchar(200) not null)', []);$db->execute('create table notes_2024 (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'], ], ],]);
foreach ($schema->planAgainst($db)->steps() as Step $step) { if (!$step->isRefused()) { continue; } // This program owns that table and wants it gone. Running the statement // deletes every row in it. $db->execute($step->sql(), []); echo "deleted a table\n";}
// The database now matches the schema, so the next plan is empty.echo Core\Arr::count($schema->planAgainst($db)->steps()), " steps left\n";
$db->close();deleted a table0 steps leftWarn an operator about what a release left behind
<?nvs// Shows what a deployment does with the steps it may not run: it applies the// rest, then asks a person about 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_import (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'], ], ],]);
// `applySafe` runs the steps it may and leaves every reported step where it is.$schema->applySafe($db);echo "the release is applied\n";
// What is left is the deployment's message to a person.foreach ($schema->planAgainst($db)->steps() as Step $step) { if ($step->isRefused()) { echo 'please decide: ', $step->reason(), "\n"; }}
$db->close();the release is appliedplease decide: 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.