Class SchemaLoader
Create test database schema from one or more SQL dump files.
This class can be useful to create test database schema when your schema is managed by tools external to your CakePHP application.
It is not well suited for applications/plugins that need to support multiple database platforms. You should use migrations for that instead.
Method Summary
-
loadInternalFile() public
Load and apply CakePHP schema file.
-
loadSqlFiles() public
Load and apply schema sql file, or an array of files.
Method Detail
loadInternalFile() ¶ public
loadInternalFile(string $file, string $connectionName = 'test'): void
Load and apply CakePHP schema file.
This method will process the array returned by $file
and treat
the contents as a list of table schema.
An example table is:
return [
'articles' => [
'columns' => [
'id' => [
'type' => 'integer',
],
'author_id' => [
'type' => 'integer',
'null' => true,
],
'title' => [
'type' => 'string',
'null' => true,
],
'body' => 'text',
'published' => [
'type' => 'string',
'length' => 1,
'default' => 'N',
],
],
'constraints' => [
'primary' => [
'type' => 'primary',
'columns' => [
'id',
],
],
],
],
];
This schema format can be useful for plugins that want to include tables to test against but don't need to include production ready schema via migrations. Applications should favour using migrations or SQL dump files over this format for ease of maintenance.
A more complete example can be found in tests/schema.php
.
Parameters
-
string
$file Schema file
-
string
$connectionName optional Connection name
Returns
void
Throws
InvalidArgumentException
For missing table name(s).
loadSqlFiles() ¶ public
loadSqlFiles(list<string>|string $paths, string $connectionName = 'test', bool $dropTables = true, bool $truncateTables = false): void
Load and apply schema sql file, or an array of files.
Parameters
-
list<string>|string
$paths Schema files to load
-
string
$connectionName optional Connection name
-
bool
$dropTables optional Drop all tables prior to loading schema files
-
bool
$truncateTables optional Truncate all tables after loading schema files
Returns
void