Skip to content

fix: ensure proper type handling for column existence and index creation in MySQLRecordManager#5726

Open
prd-hoang-doan wants to merge 1 commit intoFlowiseAI:mainfrom
prd-hoang-doan:fix/mysql_record_manager_migration_issue
Open

fix: ensure proper type handling for column existence and index creation in MySQLRecordManager#5726
prd-hoang-doan wants to merge 1 commit intoFlowiseAI:mainfrom
prd-hoang-doan:fix/mysql_record_manager_migration_issue

Conversation

@prd-hoang-doan
Copy link
Contributor

fix: ensure proper type handling for column existence and index creation in MySQLRecordManager

Description

  • Loose Comparisons ("0", 0) when altering table by adding doc_id column.
  • Fixes a MySQL migration error that occurred when creating indexes on LONGTEXT columns in the MySQLRecordManager.

Ticket: #5574

Reproduce

image

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @prd-hoang-doan, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves critical MySQL migration errors by refining type handling and index creation within the MySQLRecordManager. It ensures that column existence checks are robust against loose type comparisons and correctly implements conditional index creation for TEXT and BLOB column types, which previously caused failures due to MySQL's requirement for prefix lengths on such indexes.

Highlights

  • Strict Type Comparison for Column Existence: The check for the existence of the 'doc_id' column now explicitly converts the query result to a number using Number(), ensuring a strict comparison (=== 0) and preventing potential issues with loose type comparisons (e.g., '0' vs 0).
  • Conditional Index Creation for TEXT/BLOB Columns: The index creation logic has been enhanced to first determine the data type of the column. If the column is identified as a TEXT or BLOB type, the index is created with a prefix length of 255, which is necessary in MySQL to index large text fields and resolves previous migration errors.
Changelog
  • packages/components/nodes/recordmanager/MySQLRecordManager/MySQLrecordManager.ts
    • Modified the ColumnExists check to use Number(checkColumn[0].ColumnExists) === 0 for strict type comparison.
    • Introduced a new query to fetch the DATA_TYPE of a column before index creation.
    • Implemented conditional logic to create an index with a prefix length of 255 for TEXT or BLOB column types, addressing MySQL limitations.
Activity
  • No specific human activity (comments, reviews, etc.) has been recorded for this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

The pull request effectively addresses two critical issues within the MySQLRecordManager. First, it corrects a type comparison bug when checking for column existence by explicitly converting the database result to a number. Second, and more significantly, it resolves a long-standing MySQL migration error by implementing logic to dynamically determine column types and apply appropriate index prefix lengths for TEXT and BLOB columns. These changes enhance the stability and reliability of the MySQL record manager, ensuring smoother database operations and migrations.

Comment on lines +238 to +256
if (Number(Check[0].IndexIsThere) === 0) {
// Check column data type to determine if prefix length is needed
const columnTypeCheck = await queryRunner.manager.query(
`SELECT DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema=DATABASE() AND table_name='${tableName}' AND column_name='${column}';`
)

// For TEXT/BLOB columns, use prefix length of 255
if (columnTypeCheck.length > 0) {
const dataType = columnTypeCheck[0].DATA_TYPE.toLowerCase()
if (dataType.includes('text') || dataType.includes('blob')) {
await queryRunner.manager.query(`CREATE INDEX \`${column}_index\`
ON \`${tableName}\` (\`${column}\`(255));`)
} else {
await queryRunner.manager.query(`CREATE INDEX \`${column}_index\`
ON \`${tableName}\` (\`${column}\`);`)
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

critical

The previous code attempted to create an index on LONGTEXT columns without specifying a prefix length, which is not allowed in MySQL and would lead to a migration error. The new logic correctly identifies TEXT or BLOB column types and applies a prefix length of 255, resolving this critical database migration issue. This is a significant improvement for database compatibility and stability.

                if (Number(Check[0].IndexIsThere) === 0) {
                    // Check column data type to determine if prefix length is needed
                    const columnTypeCheck = await queryRunner.manager.query(
                        `SELECT DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS 
                            WHERE table_schema=DATABASE() AND table_name='${tableName}' AND column_name='${column}';`
                    )

                    // For TEXT/BLOB columns, use prefix length of 255
                    if (columnTypeCheck.length > 0) {
                        const dataType = columnTypeCheck[0].DATA_TYPE.toLowerCase()
                        if (dataType.includes('text') || dataType.includes('blob')) {
                            await queryRunner.manager.query(`CREATE INDEX `${column}_index`
        ON `${tableName}` (`${column}`(255));`)
                        } else {
                            await queryRunner.manager.query(`CREATE INDEX `${column}_index`
        ON `${tableName}` (`${column}`);`)
                        }
                    }
                }

@Alessioforlante
Copy link

Any idea if and when this is going to be implemented and the issue fix?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants