\Drupal\strata\Restore\Plugin\Strata\Restore TruncateRestoreStrategy

Empties a table and refills it, inside one transaction.

The strategy that works everywhere. Every driver Drupal supports can truncate and insert, and a transaction makes the whole replacement atomic from a reader's point of view - on SQLite, MySQL with InnoDB and PostgreSQL alike.

What it costs. The table is empty to any reader inside the transaction's window, so this is a maintenance-mode operation on a live site. That is stated rather than worked around, because the alternative is a strategy that silently serves an empty table to real traffic.

TRUNCATE is deliberately not used. On MySQL it is DDL and commits implicitly, which would break the transaction wrapping the refill and leave a truncated table if the insert then failed. A DELETE with no condition is transactional on every driver, which matters more here than the speed difference on a table Strata is restoring row by row anyway.

Summary

Methods
Properties
Constants
id
label
describe
isSupported
unsupportedReason
restore
No public properties found
BATCH
No protected methods found
No protected properties found
No protected constants found
insert
No private properties found
No private constants found

Constant

BATCH

BATCH = 500

How many rows are inserted per statement.

A multi-row insert is far faster than one per row, and an unbounded one exceeds placeholder limits: MySQL allows 65,535 placeholders per statement, so a twenty-column table caps out around 3,200 rows.

Methods

id()

id() : string

A short lowercase identifier.

Returns

string —

For example "truncate" or "shadow_swap".

label()

label() : string

A human label for the confirm form.

Returns

string —

The label.

describe()

describe() : string

What this strategy costs the site while it runs.

Returns

string —

One sentence.

isSupported()

isSupported(\Drupal\Core\Database\Connection  $database) : bool

Whether this strategy can run on a connection.

Parameters

\Drupal\Core\Database\Connection $database

The connection.

Returns

bool —

TRUE when the driver supports what the strategy needs.

unsupportedReason()

unsupportedReason(\Drupal\Core\Database\Connection  $database) : string|null

Why the strategy cannot run here.

Parameters

\Drupal\Core\Database\Connection $database

The connection.

Returns

string|null —

The reason, or NULL when it is supported.

restore()

restore(\Drupal\Core\Database\Connection  $database, string  $table, array  $rows, array  $columns) : int

Replaces a table's contents.

Parameters

\Drupal\Core\Database\Connection $database

The connection.

string $table

The table to replace.

array $rows

The rows to leave in it. An empty list empties the table, which is a legitimate restore target and is why this does not treat it as a mistake.

array $columns

The columns to write, in order. Given explicitly so a row carrying a column the table no longer has is refused rather than silently dropped.

Returns

int —

How many rows the table now holds.

insert()

insert(\Drupal\Core\Database\Connection  $database, string  $table, list>  $rows, list  $columns) : int

Inserts the rows in batches.

Parameters

\Drupal\Core\Database\Connection $database

The connection.

string $table

The table.

list> $rows

The rows.

list $columns

The columns to write.

Throws

\RuntimeException

When a row does not carry every column.

Returns

int —

How many rows were inserted.