Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add additional ETL DB Model error checking #1237

Merged
merged 1 commit into from
Feb 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions classes/ETL/DbModel/ForeignKeyConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public function __construct(
public function initialize(stdClass $config)
{
if (!isset($config->name)) {
if (!isset($config->columns) || !is_array($config->columns)) {
$this->logAndThrowException('"columns" must be an array');
}
$config->name = $this->generateForeignKeyConstraintName($config->columns);
}

Expand Down
3 changes: 3 additions & 0 deletions classes/ETL/DbModel/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ public function initialize(stdClass $config)
// Local verifications

if ( ! isset($config->name) ) {
if (!isset($config->columns) || !is_array($config->columns)) {
$this->logAndThrowException('"columns" must be an array');
}
$config->name = $this->generateIndexName($config->columns);
}

Expand Down
68 changes: 68 additions & 0 deletions tests/unit/lib/ETL/DbModel/DbModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,74 @@ public function testTableSchema()
$this->assertEquals($expected, $generated);
}

/**
* Test index initialization error.
*
* @expectedException Exception
* @expectedExceptionMessage "columns" must be an array
*/
public function testIndexInitializationError()
{
$config = (object) [
'name' => 'initialize_error',
'columns' => [
(object) [
'name' => 'column1',
'type' => 'int(11)',
'nullable' => false,
'default' => 0,
'comment' => 'This is my comment'
]
],
'indexes' => [
(object) [
'type' => 'PRIMARY'
]
]
];

$table = new Table($config);
$table->verify();
}

/**
* Test foreign key constraint initialization error.
*
* @expectedException Exception
* @expectedExceptionMessage "columns" must be an array
*/
public function testForeignKeyConstraintInitializationError()
{
$config = (object) [
'name' => 'initialize_error',
'columns' => [
(object) [
'name' => 'column1',
'type' => 'int(11)',
'nullable' => false
]
],
'indexes' => [
(object) [
'columns' => [
'column1'
]
]
],
'foreign_key_constraints' => [
(object) [
'referenced_table' => 'other_table',
'referenced_columns' => [
'id'
]
]
]
];

$table = new Table($config);
$table->verify();
}

/**
* Test table verification error
*
Expand Down