Skip to content

Core\Db\Schema::applyIncludingRisky

$schema->applyIncludingRisky(Core\Db\Connection $connection): void$schema->applyIncludingRisky(
    Core\Db\Connection $connection,
): void

Runs every step of the plan, including the ones that can lose data or hold a lock.

Core\Db\Schema::applyIncludingRisky plans the schema against the database and runs the whole plan. Core\Db\Schema::applySafe is the same method with one check in front of it, and that check refuses a plan holding a step which is not safe. This method runs that step.

The name is the point. Somebody reading the call sees what is being accepted, and a search across a project finds every place where it was accepted. There is no option to pass: the two methods are the choice itself.

It needs the db.schema capability for the connection's block, the same grant applySafe needs.

A table the database has and the schema does not describe is still never dropped. Accepting a risk is not the same as accepting data loss.

The examples below run a change applySafe refuses, read the plan before deciding to run it, and show that a table you never declared is kept.

Parameters

  • $connectionCore\Db\ConnectionThe database to change. It works as in applySafe, and needs the same db.schema capability.

Return value

Returns void. Nothing. A reported drop still does not run, because a risky change is not the same as data loss. The step has the SQL for the drop if a program wants to run it.

Errors

  • LogicErrorThe connection has no block to grant db.schema for. It is also thrown for every error that planAgainst throws.
  • RuntimeErrorThe db.schema capability is not granted for this connection's block.

Examples

The examples below run with this configuration file.

nvs.toml
# 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 change its tables.
# Changing the tables needs the `schema` permission.
[capabilities.db]
connect = ["notes"]
schema = ["notes"]

Run a change that applysafe refuses

<?nvs
// Shows the change `applySafe` will not make. `applyIncludingRisky` runs the
// same plan, and the name at the call site is what says so.
use Core\Db;
use Core\Db\Schema;
var $db = Db::connect('notes');
$db->execute('create table notes (id integer not null primary key autoincrement, title varchar(200) not null)', []);
// A shorter column. SQLite makes that change by copying the whole table, so
// the step can lose data.
var $schema = Schema::fromArray([
'tables' => [
[
'name' => 'notes',
'columns' => [
['name' => 'id', 'type' => 'int64', 'identity' => true],
['name' => 'title', 'type' => 'text(50)'],
],
'primary_key' => ['id'],
],
],
]);
// This prints `applySafe refused`.
try {
$schema->applySafe($db);
echo "applySafe ran it\n";
} catch (LogicError $error) {
echo "applySafe refused\n";
}
$schema->applyIncludingRisky($db);
// The database matches the schema now, so nothing is left. This prints
// `steps left: 0`.
echo 'steps left: ', Core\Arr::count($schema->planAgainst($db)->steps()), "\n";
$db->close();
Output
applySafe refused
steps left: 0

Read the plan before you run it

<?nvs
// Shows the steps a deployment takes before it accepts a risky change: plan
// first, print what each step will do, then run the plan.
use Core\Db;
use Core\Db\Plan\Step;
use Core\Db\Schema;
var $db = Db::connect('notes');
$db->execute('create table notes (id integer not null primary key autoincrement, title varchar(200) not null)', []);
$db->execute("insert into notes (title) values ('a note')", []);
var $schema = Schema::fromArray([
'tables' => [
[
'name' => 'notes',
'columns' => [
['name' => 'id', 'type' => 'int64', 'identity' => true],
['name' => 'title', 'type' => 'text(50)'],
],
'primary_key' => ['id'],
],
],
]);
// `isRefused()` is `true` for a step no apply ever runs. The rest are the
// steps this call is about to make. This prints `to run: 1`.
uint $toRun = 0;
foreach ($schema->planAgainst($db)->steps() as Step $step) {
if ($step->isRefused() == false) {
$toRun = $toRun + 1;
if ($step->grade() == Step::Grade::Safe) {
echo "step is safe\n";
} else {
echo "step needs applyIncludingRisky\n";
}
}
}
echo 'to run: ', $toRun, "\n";
$schema->applyIncludingRisky($db);
// The row was copied into the new table. This prints `rows: 1`.
echo 'rows: ', $db->query('select id from notes', [])->count(), "\n";
$db->close();
Output
step needs applyIncludingRisky
to run: 1
rows: 1

A table you never declared is kept

<?nvs
// Shows what happens to a table the schema says nothing about. It is reported
// in the plan and never dropped, and this method does not drop it either.
use Core\Db;
use Core\Db\Plan\Step;
use Core\Db\Schema;
var $db = Db::connect('notes');
// A table another program owns, in the same database.
$db->execute('create table sessions (id integer not null)', []);
$db->execute('insert into sessions (id) values (7)', []);
var $schema = Schema::fromArray([
'tables' => [
[
'name' => 'notes',
'columns' => [
['name' => 'id', 'type' => 'int64', 'identity' => true],
['name' => 'title', 'type' => 'text(200)'],
],
'primary_key' => ['id'],
],
],
]);
$schema->applyIncludingRisky($db);
// The row is still there. This prints `sessions rows: 1`.
echo 'sessions rows: ', $db->query('select id from sessions', [])->count(), "\n";
// The plan still carries the report, so you can see the difference and decide
// what to do about it yourself. This prints `reported: 1`.
uint $reported = 0;
foreach ($schema->planAgainst($db)->steps() as Step $step) {
if ($step->isRefused()) {
$reported = $reported + 1;
}
}
echo 'reported: ', $reported, "\n";
$db->close();
Output
sessions rows: 1
reported: 1