function SchemaTest::testGeneratedColumnDefinitionRecoveryFailure

Tests introspecting a table whose generated column cannot be recovered.

File

core/modules/sqlite/tests/src/Kernel/sqlite/SchemaTest.php, line 144

Class

SchemaTest
Tests schema API for the SQLite driver.

Namespace

Drupal\Tests\sqlite\Kernel\sqlite

Code

public function testGeneratedColumnDefinitionRecoveryFailure() : void {
  $tableName = 'test_generated_column_recovery';
  $this->schema
    ->createTable($tableName, (new Table(name: $tableName, columns: [
    Column::int(name: 'age'),
    Column::generated(name: 'double_age', type: ColumnType::Int, storage: GeneratedColumnStorage::Virtual, expression: new GeneratedColumnExpression('[age] * 2')),
  ]))->toArray());
  // A table rebuilt from a description missing a generated column would drop
  // that column, so introspection must fail instead. Every table the schema
  // API creates can be recovered, so the failure has to be simulated by
  // overriding the recovery to find nothing.
  $schema = new class ($this->connection) extends Schema {
    
    /**
     * {@inheritdoc}
     */
    protected function getColumnDefinitions(string $schema, string $table) : array {
      return [];
    }

};
  $this->expectException(SchemaException::class);
  $this->expectExceptionMessage("Unable to recover the definition of generated column double_age");
  // Schema::fieldExists() introspects the whole table, so it trips on
  // 'double_age' no matter which column it is asked about.
  $schema->fieldExists($tableName, 'age');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.