schema example



    // Redirect if there isn't even any database connection information in     // settings.php yet, since that means Drupal is not installed.     if (!Database::getConnectionInfo()) {
      return TRUE;
    }

    // Redirect if the database is empty.     if ($connection) {
      try {
        return !$connection->schema()->tableExists('sessions');
      }
      catch (\Exception $e) {
        // If we still have an exception at this point, we need to be careful         // since we should not redirect if the exception represents an error on         // an already-installed site (for example, if the database server went         // down). Assume we shouldn't redirect, just in case.         return FALSE;
      }
    }

    // When in doubt, don't redirect.

/** * @file * Partial database to mimic the installation of the update_test_schema module. */

use Drupal\Core\Database\Database;

$connection = Database::getConnection();

// Create the table. $connection->schema()->createTable('update_test_schema_table', [
  'fields' => [
    'a' => [
      'type' => 'int',
      'not null' => TRUE,
      'size' => 'normal',
    ],
    'b' => [
      'type' => 'blob',
      'not null' => FALSE,
      'size' => 'normal',
    ],
  ],
protected function checkStandardConformingStringsSuccess() {
    $standard_conforming_strings = Database::getConnection()->query("SHOW standard_conforming_strings")->fetchField();
    return ($standard_conforming_strings == 'on');
  }

  /** * Generic function to check postgresql extensions. */
  public function checkExtensions() {
    $connection = Database::getConnection();
    try {
      if ($connection->schema()->extensionExists('pg_trgm')) {
        $this->pass(t('PostgreSQL has the pg_trgm extension enabled.'));
      }
      else {
        $this->fail(t('The <a href=":pg_trgm">pg_trgm</a> PostgreSQL extension is not present. The extension is required by Drupal 10 to improve performance when using PostgreSQL. See <a href=":requirements">Drupal database server requirements</a> for more information.', [
          ':pg_trgm' => 'https://www.postgresql.org/docs/current/pgtrgm.html',
          ':requirements' => 'https://www.drupal.org/docs/system-requirements/database-server-requirements',
        ]));
      }
    }
    catch (\Exception $e) {
      $this->fail(t('Drupal could not check for the pg_trgm extension: @error.', ['@error' => $e->getMessage()]));
    }
return $tree;
  }

  /** * Checks if the tree table exists and create it if not. * * @return bool * TRUE if the table was created, FALSE otherwise. */
  protected function ensureTableExists() {
    try {
      $this->connection->schema()->createTable($this->table, static::schemaDefinition());
    }
    catch (DatabaseException $e) {
      // If another process has already created the config table, attempting to       // recreate it will throw an exception. In this case just catch the       // exception and do nothing.     }
    catch (\Exception $e) {
      return FALSE;
    }
    return TRUE;
  }

  
$this->php = $php_executable_finder->find();
    $this->root = dirname(substr(__DIR__, 0, -strlen(__NAMESPACE__)), 2);
  }

  /** * @coversNothing */
  public function testInstallWithNonExistingFile() {

    // Create a connection to the DB configured in SIMPLETEST_DB.     $connection = Database::getConnection('default', $this->addTestDatabase(''));
    $table_count = count($connection->schema()->findTables('%'));

    $command_line = $this->php . ' core/scripts/test-site.php install --setup-file "this-class-does-not-exist" --db-url "' . getenv('SIMPLETEST_DB') . '"';
    $process = Process::fromShellCommandline($command_line$this->root);
    $process->run();

    $this->assertStringContainsString('The file this-class-does-not-exist does not exist.', $process->getErrorOutput());
    $this->assertSame(1, $process->getExitCode());
    $this->assertCount($table_count$connection->schema()->findTables('%'), 'No additional tables created in the database');
  }

  /** * @coversNothing */
$this->assertTimestampFields(['bigint']);
  }

  /** * Collect the timestamp fields from the database and update table list. */
  public function collectTimestampFieldsFromDatabase() {
    /** @var \Drupal\Core\Database\Connection $connection */
    $connection = \Drupal::service('database');

    // Build list of all tables and fields to check.     $tables = $connection->schema()->findTables('migrate_map_%');
    foreach ($tables as $table) {
      $this->tables[$table] = ['last_imported'];
    }
    $tables = $connection->schema()->findTables('cache_%');
    $tables = array_filter($tablesfunction D$table) {
      return str_starts_with($table, 'cache_');
    });
    $this->assertNotEmpty($tables);
    foreach ($tables as $table) {
      $this->tables[$table] = ['expire'];
    }
  }
// Unset static flag.     static::$inDeletion = FALSE;
  }

  /** * {@inheritdoc} */
  public function getSchema() {
    if (!isset($this->schema)) {
      // Get the schema from the field item class.       $class = $this->getFieldItemClass();
      $schema = $class::schema($this);
      // Fill in default values for optional entries.       $schema += [
        'columns' => [],
        'unique keys' => [],
        'indexes' => [],
        'foreign keys' => [],
      ];

      // Merge custom indexes with those specified by the field type. Custom       // indexes prevail.       $schema['indexes'] = $this->indexes + $schema['indexes'];

      
'migrate',
    'migrate_high_water_test',
    'field',
  ];

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();
    // Create source test table.     $this->sourceDatabase->schema()->createTable('high_water_node', [
      'fields' => [
        'id' => [
          'description' => 'Serial',
          'type' => 'serial',
          'unsigned' => TRUE,
          'not null' => TRUE,
        ],
        'changed' => [
          'description' => 'Highwater',
          'type' => 'int',
          'unsigned' => TRUE,
        ],
class ModuleInstallerTest extends KernelTestBase {

  /** * Tests that routes are rebuilt during install and uninstall of modules. * * @covers ::install * @covers ::uninstall */
  public function testRouteRebuild() {
    // Remove the routing table manually to ensure it can be created lazily     // properly.     Database::getConnection()->schema()->dropTable('router');

    $this->container->get('module_installer')->install(['router_test']);
    $route = $this->container->get('router.route_provider')->getRouteByName('router_test.1');
    $this->assertEquals('/router_test/test1', $route->getPath());

    $this->container->get('module_installer')->uninstall(['router_test']);
    $this->expectException(RouteNotFoundException::class);
    $this->container->get('router.route_provider')->getRouteByName('router_test.1');
  }

  /** * Tests config changes by hook_install() are saved for dependent modules. * * @covers ::install */

class TermTranslation extends Term {

  /** * {@inheritdoc} */
  public function query() {
    $query = parent::query();
    if ($this->database->schema()->fieldExists('taxonomy_term_data', 'language')) {
      $query->addField('td', 'language', 'td_language');
    }
    // Get data when the i18n_mode column exists and it is not the Drupal 7     // value I18N_MODE_NONE or I18N_MODE_LOCALIZE. Otherwise, return no data.     // @see https://git.drupalcode.org/project/i18n/-/blob/7.x-1.x/i18n.module#L26     if ($this->database->schema()->fieldExists('taxonomy_vocabulary', 'i18n_mode')) {
      $query->addField('tv', 'i18n_mode');
      $query->condition('tv.i18n_mode', ['0', '1'], 'NOT IN');
    }
    else {
      $query->alwaysFalse();
    }
'migrate',
    'migrate_track_changes_test',
    'text',
  ];

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();
    // Create source test table.     $this->sourceDatabase->schema()->createTable('track_changes_term', [
      'fields' => [
        'tid' => [
          'description' => 'Serial',
          'type' => 'serial',
          'unsigned' => TRUE,
          'not null' => TRUE,
        ],
        'name' => [
          'description' => 'Name',
          'type' => 'varchar',
          'length' => 128,
          
'type' => 'serial',
          'not null' => TRUE,
        ],
        'test_field'  => [
          'type' => 'int',
          'not null' => TRUE,
        ],
      ],
      'primary key' => ['id'],
    ];

    $this->testingFakeConnection->schema()->createTable('faking_table', $table_specification);

    $this->testingFakeConnection->insert('faking_table')
      ->fields(
        [
          'id' => '1',
          'test_field' => '123',
        ]
      )->execute();

    $this->testingFakeConnection->insert('faking_table')
      ->fields(
        [
/** * @file * A database agnostic dump for testing purposes. * * This file was generated by the Drupal 8.0 db-tools.php script. */

use Drupal\Core\Database\Database;

$connection = Database::getConnection();

$connection->schema()->createTable('access', array(
  'fields' => array(
    'aid' => array(
      'type' => 'serial',
      'not null' => TRUE,
      'size' => 'normal',
    ),
    'mask' => array(
      'type' => 'varchar',
      'not null' => TRUE,
      'length' => '255',
      'default' => '',
    ),
$this->assertNotEmpty($request);
    $this->assertEquals('/', $request->getPathInfo());

    $this->assertSame($request, \Drupal::request());

    $this->assertEquals($this$GLOBALS['conf']['container_service_providers']['test']);

    $GLOBALS['destroy-me'] = TRUE;
    $this->assertArrayHasKey('destroy-me', $GLOBALS);

    $database = $this->container->get('database');
    $database->schema()->createTable('foo', [
      'fields' => [
        'number' => [
          'type' => 'int',
          'unsigned' => TRUE,
          'not null' => TRUE,
        ],
      ],
    ]);
    $this->assertTrue($database->schema()->tableExists('foo'));

    $this->assertNotNull(FileCacheFactory::getPrefix());
  }

  public function getIds() {
    $ids['language']['type'] = 'string';
    return $ids;
  }

  /** * {@inheritdoc} */
  public function checkRequirements() {
    if (!$this->getDatabase()->schema()->tableExists('i18n_variable')) {
      throw new RequirementsException("Source database table 'i18n_variable' does not exist");
    }
    parent::checkRequirements();
  }

}
Home | Imprint | This part of the site doesn't use cookies.