Skip to content

test: Forge::addColumn() and null #7319

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 8, 2023
Merged
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
35 changes: 35 additions & 0 deletions tests/system/Database/Live/ForgeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,41 @@ public function testAddColumn()
$this->assertSame('username', $fieldNames[1]);
}

public function testAddColumnNull()
{
$this->forge->dropTable('forge_test_table', true);

$this->forge->addField([
'col1' => ['type' => 'VARCHAR', 'constraint' => 255],
'col2' => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
'col3' => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => false],
]);
$this->forge->createTable('forge_test_table');

$this->forge->addColumn('forge_test_table', [
'col4' => ['type' => 'VARCHAR', 'constraint' => 255],
'col5' => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
'col6' => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => false],
]);

$this->db->resetDataCache();

$col1 = $this->getMetaData('col1', 'forge_test_table');
$this->assertFalse($col1->nullable);
$col2 = $this->getMetaData('col2', 'forge_test_table');
$this->assertTrue($col2->nullable);
$col3 = $this->getMetaData('col3', 'forge_test_table');
$this->assertFalse($col3->nullable);
$col4 = $this->getMetaData('col4', 'forge_test_table');
$this->assertTrue($col4->nullable);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since $col1 is false from a logical point of view, I think $col4 should be false too. Thoughts?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #7235
I thought that, but if we change it, it is a breaking change.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, fair point but in this case, I would treat it as a bug.
API should be predictable and having a different behavior for the "same thing" is something we should avoid.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I'll be fine with documenting this and leaving it to be fixed in the next major version.

@codeigniter4/database-team any thoughts on this one?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is inconsistent to have different behavior when creating and adding, but I am a little doubtful that it is worth changing in v5.

However, if we decide to change it, I would like to include that in the documentation.

@codeigniter4/database-team Opinions?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am ambivalent.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems nobody has strong opinion.

$col5 = $this->getMetaData('col5', 'forge_test_table');
$this->assertTrue($col5->nullable);
$col6 = $this->getMetaData('col6', 'forge_test_table');
$this->assertFalse($col6->nullable);

$this->forge->dropTable('forge_test_table', true);
}

public function testAddFields()
{
$tableName = 'forge_test_fields';
Expand Down