You are here

function database_test_schema in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/system/tests/modules/database_test/database_test.install \database_test_schema()
  2. 7 modules/simpletest/tests/database_test.install \database_test_schema()

Implements hook_schema().

The database tests use the database API which depends on schema information for certain operations on certain databases. Therefore, the schema must actually be declared in a normal module like any other, not directly in the test file.

File

core/modules/system/tests/modules/database_test/database_test.install, line 16
Install, update and uninstall functions for the database_test module.

Code

function database_test_schema() {
  $schema['test'] = [
    'description' => 'Basic test table for the database unit tests.',
    'fields' => [
      'id' => [
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ],
      'name' => [
        'description' => "A person's name",
        'type' => 'varchar_ascii',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'binary' => TRUE,
      ],
      'age' => [
        'description' => "The person's age",
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ],
      'job' => [
        'description' => "The person's job",
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => 'Undefined',
      ],
    ],
    'primary key' => [
      'id',
    ],
    'unique keys' => [
      'name' => [
        'name',
      ],
    ],
    'indexes' => [
      'ages' => [
        'age',
      ],
    ],
  ];
  $schema['test_classtype'] = [
    'description' => 'A duplicate version of the test table, used for fetch_style PDO::FETCH_CLASSTYPE tests.',
    'fields' => [
      'classname' => [
        'description' => "A custom class name",
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ],
      'name' => [
        'description' => "A person's name",
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ],
      'age' => [
        'description' => "The person's age",
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ],
      'job' => [
        'description' => "The person's job",
        'type' => 'varchar_ascii',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ],
    ],
    'primary key' => [
      'job',
    ],
    'indexes' => [
      'ages' => [
        'age',
      ],
    ],
  ];

  // This is an alternate version of the same table that is structured the same
  // but has a non-serial Primary Key.
  $schema['test_people'] = [
    'description' => 'A duplicate version of the test table, used for additional tests.',
    'fields' => [
      'name' => [
        'description' => "A person's name",
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ],
      'age' => [
        'description' => "The person's age",
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ],
      'job' => [
        'description' => "The person's job",
        'type' => 'varchar_ascii',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ],
    ],
    'primary key' => [
      'job',
    ],
    'indexes' => [
      'ages' => [
        'age',
      ],
    ],
  ];
  $schema['test_people_copy'] = [
    'description' => 'A duplicate version of the test_people table, used for additional tests.',
    'fields' => [
      'name' => [
        'description' => "A person's name",
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ],
      'age' => [
        'description' => "The person's age",
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ],
      'job' => [
        'description' => "The person's job",
        'type' => 'varchar_ascii',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ],
    ],
    'primary key' => [
      'job',
    ],
    'indexes' => [
      'ages' => [
        'age',
      ],
    ],
  ];
  $schema['test_one_blob'] = [
    'description' => 'A simple table including a BLOB field for testing BLOB behavior.',
    'fields' => [
      'id' => [
        'description' => 'Simple unique ID.',
        'type' => 'serial',
        'not null' => TRUE,
      ],
      'blob1' => [
        'description' => 'A BLOB field.',
        'type' => 'blob',
      ],
    ],
    'primary key' => [
      'id',
    ],
  ];
  $schema['test_two_blobs'] = [
    'description' => 'A simple test table with two BLOB fields.',
    'fields' => [
      'id' => [
        'description' => 'Simple unique ID.',
        'type' => 'serial',
        'not null' => TRUE,
      ],
      'blob1' => [
        'description' => 'A dummy BLOB field.',
        'type' => 'blob',
      ],
      'blob2' => [
        'description' => 'A second BLOB field.',
        'type' => 'blob',
      ],
    ],
    'primary key' => [
      'id',
    ],
  ];
  $schema['test_task'] = [
    'description' => 'A task list for people in the test table.',
    'fields' => [
      'tid' => [
        'description' => 'Task ID, primary key.',
        'type' => 'serial',
        'not null' => TRUE,
      ],
      'pid' => [
        'description' => 'The {test_people}.pid, foreign key for the test table.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ],
      'task' => [
        'description' => 'The task to be completed.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ],
      'priority' => [
        'description' => 'The priority of the task.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ],
    ],
    'primary key' => [
      'tid',
    ],
  ];
  $schema['test_null'] = [
    'description' => 'Basic test table for NULL value handling.',
    'fields' => [
      'id' => [
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ],
      'name' => [
        'description' => "A person's name.",
        'type' => 'varchar_ascii',
        'length' => 255,
        'not null' => FALSE,
        'default' => '',
      ],
      'age' => [
        'description' => "The person's age.",
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => FALSE,
        'default' => 0,
      ],
    ],
    'primary key' => [
      'id',
    ],
    'unique keys' => [
      'name' => [
        'name',
      ],
    ],
    'indexes' => [
      'ages' => [
        'age',
      ],
    ],
  ];
  $schema['test_serialized'] = [
    'description' => 'Basic test table for NULL value handling.',
    'fields' => [
      'id' => [
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ],
      'name' => [
        'description' => "A person's name.",
        'type' => 'varchar_ascii',
        'length' => 255,
        'not null' => FALSE,
        'default' => '',
      ],
      'info' => [
        'description' => "The person's data in serialized form.",
        'type' => 'blob',
        'serialize' => TRUE,
      ],
    ],
    'primary key' => [
      'id',
    ],
    'unique keys' => [
      'name' => [
        'name',
      ],
    ],
  ];
  $schema['test_composite_primary'] = [
    'description' => 'Basic test table with a composite primary key',
    'fields' => [
      'name' => [
        'description' => "A person's name",
        'type' => 'varchar',
        'length' => 50,
        'not null' => TRUE,
        'default' => '',
        'binary' => TRUE,
      ],
      'age' => [
        'description' => "The person's age",
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ],
      'job' => [
        'description' => "The person's job",
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => 'Undefined',
      ],
    ],
    'primary key' => [
      'name',
      'age',
    ],
  ];

  // Hopefully no-one will ever name a table 'select' but this example is a
  // reserved keyword in all supported SQL databases so it is a good test.
  $schema['select'] = [
    'description' => 'A test table with an ANSI reserved keyword as its name and one of its column names.',
    'fields' => [
      'id' => [
        'description' => 'Simple unique ID.',
        // Using a serial as an ID properly tests
        // \Drupal\Core\Database\Driver\pgsql\Upsert.
        'type' => 'serial',
        'not null' => TRUE,
      ],
      'update' => [
        'description' => 'A column with reserved name.',
        'type' => 'text',
      ],
    ],
    'primary key' => [
      'id',
    ],
  ];
  $schema['virtual'] = [
    'description' => 'A simple table with reserved name in MySQL 8.',
    'fields' => [
      'id' => [
        'description' => 'Simple unique ID.',
        'type' => 'int',
        'not null' => TRUE,
      ],
      'function' => [
        'description' => 'A column with reserved name in MySQL 8.',
        'type' => 'text',
      ],
    ],
    'primary key' => [
      'id',
    ],
  ];
  $schema['TEST_UPPERCASE'] = $schema['test'];
  return $schema;
}