Skip to content
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

test: Forge::modifyColumn() and null #7302

Merged
merged 1 commit into from
May 1, 2023

Conversation

kenjis
Copy link
Member

@kenjis kenjis commented Feb 24, 2023

Needs #7301

Description
From https://zenn.dev/naente/articles/50935eb55c14ea (CodeIgniter4 PostgreSQL always adds NOT NULL constraints when running modifyColumn())

See the current behavior: #7302 (comment)

  • add test for Forge::modifyColumn() and null

This bug was fixed in v4.3.4:

Related #7235

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@kenjis kenjis added testing Pull requests that changes tests only 4.4 database Issues or pull requests that affect the database layer labels Feb 24, 2023
@kenjis kenjis marked this pull request as draft February 24, 2023 07:16
@kenjis kenjis force-pushed the test-Forge-modifyColumn-null branch 2 times, most recently from 5d49dff to f9c953c Compare February 24, 2023 07:33
@kenjis
Copy link
Member Author

kenjis commented Feb 24, 2023

@codeigniter4/database-team
It seems Postgre, SQLSRV, SQLite3 add NOT NULL constraints when running modifyColumn().
See the test: https://github.com/codeigniter4/CodeIgniter4/pull/7302/files#diff-489cc58f8341d1fa6ca26b7bb857562d4274924d2dc691ca64e9dc609731b8eaR1285
Are these bugs or not?

@sclubricants
Copy link
Member

Looks like nullable is the default. What is the matter with this?

@kenjis
Copy link
Member Author

kenjis commented Feb 24, 2023

I did not look into all drivers, but Postgre changes to NOT NULL in all cases.
There is no way to keep nuallble. So it is clearly a bug.

Another issue is these behaviors are not consistent. As you see all tests pass
on MySQLi and OCI8.

If a dev changes DB driver, e.g. MySQLi to Postgres,
the definitiion of the table will change when running migrations.

@kenjis kenjis force-pushed the test-Forge-modifyColumn-null branch from f9c953c to a5fc127 Compare February 25, 2023 03:30
@kenjis
Copy link
Member Author

kenjis commented Feb 25, 2023

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

        $this->forge->modifyColumn('forge_test_modify', [
            'col1' => ['type' => 'VARCHAR', 'constraint' => 1],
            'col2' => ['type' => 'VARCHAR', 'constraint' => 1, 'null' => true],
            'col3' => ['type' => 'VARCHAR', 'constraint' => 1, 'null' => false],
        ]);

        $this->db->resetDataCache();

        $fields = $this->db->getFieldData('forge_test_modify');
        dd($fields);

Postgres:

array (3) [
    0 => stdClass#249 (5) (
        public 'name' -> string (4) "col1"
        public 'type' -> string (17) "character varying"
        public 'nullable' -> boolean false  // MySQLi true
        public 'default' -> null
        public 'max_length' -> string (1) "1"
    )
    1 => stdClass#74 (5) (
        public 'name' -> string (4) "col2"
        public 'type' -> string (17) "character varying"
        public 'nullable' -> boolean false  // should be true
        public 'default' -> null
        public 'max_length' -> string (1) "1"
    )
    2 => stdClass#75 (5) (
        public 'name' -> string (4) "col3"
        public 'type' -> string (17) "character varying"
        public 'nullable' -> boolean false
        public 'default' -> null
        public 'max_length' -> string (1) "1"
    )
]

SQLSRV:

array (3) [
    0 => stdClass#249 (5) (
        public 'name' -> string (4) "col1"
        public 'type' -> string (7) "varchar"
        public 'default' -> null
        public 'max_length' -> integer 1
        public 'nullable' -> boolean false  // MySQLi true
    )
    1 => stdClass#74 (5) (
        public 'name' -> string (4) "col2"
        public 'type' -> string (7) "varchar"
        public 'default' -> null
        public 'max_length' -> integer 1
        public 'nullable' -> boolean false  // should be true
    )
    2 => stdClass#75 (5) (
        public 'name' -> string (4) "col3"
        public 'type' -> string (7) "varchar"
        public 'default' -> null
        public 'max_length' -> integer 1
        public 'nullable' -> boolean false
    )
]

SQLite3:

array (3) [
    0 => stdClass#77 (6) (
        public 'name' -> string (4) "col1"
        public 'type' -> string (7) "VARCHAR"
        public 'max_length' -> null
        public 'default' -> string (2) "''"
        public 'primary_key' -> boolean false
        public 'nullable' -> boolean false  // MySQLi true
    )
    1 => stdClass#67 (6) (
        public 'name' -> string (4) "col2"
        public 'type' -> string (7) "VARCHAR"
        public 'max_length' -> null
        public 'default' -> string (4) "NULL"
        public 'primary_key' -> boolean false
        public 'nullable' -> boolean true
    )
    2 => stdClass#602 (6) (
        public 'name' -> string (4) "col3"
        public 'type' -> string (7) "VARCHAR"
        public 'max_length' -> null
        public 'default' -> string (4) "NULL"
        public 'primary_key' -> boolean false
        public 'nullable' -> boolean true  // should be false
    )
]

Not directly related, but the all max_length values are incorrect on SQLite3.

@kenjis kenjis added the bug Verified issues on the current code behavior or pull requests that will fix them label Feb 25, 2023
@kenjis kenjis force-pushed the test-Forge-modifyColumn-null branch from a5fc127 to cedb583 Compare March 1, 2023 07:00
@michalsn
Copy link
Member

michalsn commented Mar 3, 2023

Seems like a bug to me.

@kenjis kenjis force-pushed the test-Forge-modifyColumn-null branch from 5232285 to 0e86d4c Compare April 30, 2023 02:49
@kenjis kenjis removed the bug Verified issues on the current code behavior or pull requests that will fix them label Apr 30, 2023
@kenjis kenjis marked this pull request as ready for review April 30, 2023 02:51
@kenjis
Copy link
Member Author

kenjis commented Apr 30, 2023

Rebased and now this PR removes only unneeded test code.

@MGatner
Copy link
Member

MGatner commented May 1, 2023

Looks good, thanks!

@kenjis kenjis merged commit d9fd9d5 into codeigniter4:4.4 May 1, 2023
@kenjis kenjis deleted the test-Forge-modifyColumn-null branch May 1, 2023 12:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
4.4 database Issues or pull requests that affect the database layer testing Pull requests that changes tests only
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants