Skip to content

Core\Db\Plan\Step::sql

$step->sql(): string

Gives you the complete SQL one step of a schema plan is.

Core\Db\Plan\Step::sql returns the statement the step would run, written for the database this plan was computed against. It is never shortened and never summarised. A program can print it for a person to review, save it to a file, or run it with Core\Db::execute as it stands.

A few steps are more than one statement. SQLite has to rebuild a table to change a column, which is four statements, and sql() returns them joined by a newline. Core\Db::execute runs one statement per call, so a step of that kind is one for a person rather than for a program.

Steps the plan will never run carry their SQL too. A table the database has and the schema does not name is reported with the statement that would drop it, and nothing drops it for you. Running that statement is a decision a program makes for itself.

The examples read the statement a step is, run one step by hand, and write a whole plan out for a review.

Return value

Returns string. The statements, one per line. Most steps are one statement, and you can pass it directly to Core\Db::execute. SQLite's table rebuild is four statements. A person pastes those, and a program does not run them.

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 program may open the `notes` database.
[capabilities.db]
connect = ["notes"]

Read the statement a step is

<?nvs
// Shows the SQL one step of a plan is. `Core\Db\Plan\Step::sql` gives you the
// whole statement, written for the database the plan was computed against.
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 table is missing, so the plan has one step: the statement that creates
// it. Nothing is changed by reading this.
foreach ($schema->planAgainst($db)->steps() as Step $step) {
echo $step->sql(), "\n";
}
$db->close();
Output
CREATE TABLE "notes" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT,
"title" VARCHAR(200) NOT NULL
);

Run one step yourself

<?nvs
// Shows how a program runs one step of a plan itself. The statement is
// complete, so `Core\Db\Connection::execute` runs it as it stands.
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'],
],
],
]);
var $steps = $schema->planAgainst($db)->steps();
echo Core\Arr::count($steps), " step to run\n";
// `execute` runs one statement per call. This step is one statement, so it
// can be handed over as it is.
$db->execute($steps[0]->sql(), []);
// The database now matches the schema, so the next plan has nothing in it.
echo Core\Arr::count($schema->planAgainst($db)->steps()), " steps left\n";
$db->close();
Output
1 step to run
0 steps left

Write the plan out for a review

<?nvs
// Shows how a release writes its plan out for a person to review. Each step
// becomes the reason it is there and then the statement itself.
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],
],
'primary_key' => ['id'],
],
],
]);
// The steps the plan will never run carry their SQL too, so a review sees all
// of it.
foreach ($schema->planAgainst($db)->steps() as Step $step) {
echo '-- ', $step->reason(), "\n";
echo $step->sql(), "\n";
}
$db->close();
Output
-- A new table holds no rows: nothing to lose, nothing to validate, nothing to block.
CREATE TABLE "notes" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT
);
-- 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.
DROP TABLE "notes_backup";