Skip to content

[Bug]: Boolean column unsigned #228

@puzzledpolymath

Description

@puzzledpolymath

No duplicates 🥲.

  • I have searched for a similar issue in our bug tracker and didn't find any solutions.

Describe the bug

I was in the process of planning out a schema for a project, I typically assign columns as unsigned when I know they'll never being going into negative.

This is where I discovered an issue which took some time to get my head around. I have limited knowledge of the code-base, but I'll do my best to explain where I think the problem lies.

To Reproduce

Take the following annotated column for example.

    #[Column(
        type: 'boolean',
        name: 'system',
        default: false,
        unsigned: true,
        comment: 'Whether a system account',
    )]
    private bool $system = false;

A migration for the corresponding column looks like this.

        ->addColumn('system', 'boolean', [
            'nullable'      => false,
            'defaultValue'  => false,
            'size'          => 1,
            'autoIncrement' => false,
            'unsigned'      => false,
            'zerofill'      => false,
            'comment'       => '',
        ])

Firstly you'll notice the comment is empty. While not the reason for raising this ticket, it is a bug I'd like to see it resolved. I believe the issue is that https://github.com/cycle/schema-builder/blob/2.x/src/Table/Column.php#L143 returns early when a default value is defined, never merging the attributes.

Back to the issue at hand, when running the migration everything works and the column looks as expected. But when you run the migration generator again, the Changes Collector wrongly determines alterations https://github.com/cycle/schema-migrations-generator/blob/2.x/src/Changes/Collector.php#L48

Oddly, when debugging the columns being compared it wasn't the unsigned property which was different, it was the size property, equaling 4 instead of 1. I think this is likely due to the MySQLColumn::createInstance method not knowing the userType at this point, or that the default size should be 1 (Default for boolean columns). Instead, the size is resolved as 4, default value when resolving a tinyint (DBMS specific column type).

While my theories may be wrong, I've created a new branch and test case which prove an issue does exist.

2.x...puzzledpolymath:cycle-database:bug/boolean-unsigned

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions