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

Postgre & SQLSRV - Should Never Have A Field Length For TEXT #6405

Merged
merged 7 commits into from
Aug 30, 2022

Conversation

sclubricants
Copy link
Member

@sclubricants sclubricants commented Aug 21, 2022

This PR fixes an issue where a table definition in Forge has a length value for type TEXT. Postgre does not support a length for the TEXT type.

Its probably possible for a developer just to set length null when developing on Postgre. This PR is useful when using the same table definition across different DBMS.

The following query works fine for Mysql, Sqlite but fails on Postgre & SQLSRV:

        $this->forge->addColumn('actions', [
            'summary'     => ['type' => 'varchar', 'constraint' => 255],
            'description' => ['type' => 'text', 'constraint' => 255],
        ]);

https://www.postgresql.org/docs/current/datatype-character.html

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

@sclubricants sclubricants changed the title Postgre - Should Never Have A Field Length Postgre - Should Never Have A Field Length For TEXT Aug 21, 2022
@kenjis kenjis added the bug Verified issues on the current code behavior or pull requests that will fix them label Aug 21, 2022
@kenjis
Copy link
Member

kenjis commented Aug 21, 2022

Thank you for sending PR.

If this PR is a bug fix, it should go to develop branch.

@sclubricants
Copy link
Member Author

Its not a bug per se, You can assign the constraint as null and it will not cause a problem.

This PR just allows using the same table definition to work between databases.

Put another way, Forge allows you to assign an illegal constraint. This PR will just ignore such illegal constraint if exists.

SQLSRV has same issue. I can add here.

@sclubricants sclubricants changed the title Postgre - Should Never Have A Field Length For TEXT Postgre & SQLSRV - Should Never Have A Field Length For TEXT Aug 21, 2022
@kenjis kenjis added 4.3 enhancement PRs that improve existing functionalities and removed bug Verified issues on the current code behavior or pull requests that will fix them labels Aug 21, 2022
@kenjis
Copy link
Member

kenjis commented Aug 21, 2022

Okay, this PR is an enhancement.

Can you add a test to prove this enhancement?

@kenjis kenjis added the database Issues or pull requests that affect the database layer label Aug 21, 2022
@sclubricants
Copy link
Member Author

Added test and another fix for SQLSRV.

SQLSRV adds a constraint whenever a column is defined with a default value. Whenever dropColumn is called the current code drops all indexes associated with the column but it doesn't drop the default constraints. These use a slightly different command as well: Instead of DROP INDEX they use DROP CONSTRAINT. These constraint names are generated by the system and do not list in getIndexData(). There has been an attempt to name these in the modifyColumn() method however they are not named in createTable(). These are kind of annoying and I'm not sure why they don't automatically drop with the column name.

@sclubricants
Copy link
Member Author

The test demonstrates that these two simple commands can now execute successfully on all DBMS

        $result = $this->forge->addColumn('user', [
            'text_with_constraint' => ['type' => 'text', 'constraint' => 255, 'default' => ''],
        ]);

        $result = $this->forge->dropColumn('user', 'text_with_constraint');

Without this PR Postgre and SQLSRV would fail.

@MGatner
Copy link
Member

MGatner commented Aug 22, 2022

Shows what I know, I didn't think MySQL could have a length spec for TEXT 🤦‍♂️

@kenjis kenjis added the docs needed Pull requests needing documentation write-ups and/or revisions. label Aug 26, 2022
Copy link
Member

@kenjis kenjis left a comment

Choose a reason for hiding this comment

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

Can you add a short description about this enhancement in changelog?

@kenjis kenjis removed the docs needed Pull requests needing documentation write-ups and/or revisions. label Aug 30, 2022
@@ -70,6 +70,7 @@ Others
- Added new Form helper function :php:func:`validation_errors()`, :php:func:`validation_list_errors()` and :php:func:`validation_show_error()` to display Validation Errors.
- Now you can autoload helpers by **app/Config/Autoload.php**.
- ``BaseConnection::escape()`` now excludes the ``RawSql`` data type. This allows passing SQL strings into data.
- SQLSRV now automatically drops ``DEFAULT`` constraint when using ``Forge::dropColumn()``.
Copy link
Member

Choose a reason for hiding this comment

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

For example, this commit is not good. Because the document change has nothing to do with the code change.

We added the explanation for commits and commit messages:
See https://github.com/codeigniter4/CodeIgniter4/blob/develop/contributing/workflow.md#committing

@kenjis
Copy link
Member

kenjis commented Aug 30, 2022

It seems you used git merge, but we recommend to use git rebase when updating the PR branch.
See https://github.com/codeigniter4/CodeIgniter4/blob/develop/contributing/workflow.md#updating-your-branch

@kenjis kenjis merged commit 0fbecce into codeigniter4:4.3 Aug 30, 2022
@sclubricants sclubricants deleted the PostgreTextLength branch September 3, 2022 19:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
4.3 database Issues or pull requests that affect the database layer enhancement PRs that improve existing functionalities
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants