Back to Bytebase

MySQL Advisor Omni Migration Scenarios

docs/plans/SCENARIOS-mysql-advisor-omni.md

3.17.110.0 KB
Original Source

MySQL Advisor Omni Migration Scenarios

Goal: Migrate all 87 MySQL advisor rules from ANTLR tree walking to omni AST type switches Verification: go test -count=1 ./backend/plugin/advisor/mysql/... passes with identical advice output; grep -r "GetANTLRAST" backend/plugin/advisor/mysql/ returns zero matches after completion Reference sources: PG omni migration (backend/plugin/advisor/pg/generic_checker_omni.go, utils_omni.go), omni MySQL AST (github.com/bytebase/omni/mysql/ast)

Status: [ ] pending, [x] passing, [~] partial


Phase 1: Framework & Proof

Build the omni rule infrastructure and prove it works with one representative rule from each major category.

1.1 Core Framework

  • OmniRule interface with OnStatement(node ast.Node), Name(), GetAdviceList()
  • OmniBaseRule struct with Level, Title, Advice, BaseLine, StmtText fields
  • SetStatement(baseLine int, stmtText string) sets context for position calculations
  • AddAdvice() adds advice with BaseLine offset adjustment
  • LocToLine(loc ast.Loc) converts omni byte offset to 1-based line number
  • RunOmniRules() dispatcher iterates statements, extracts omni node, dispatches to rules
  • FindLineByName(name string) searches identifier in statement text, returns line

1.2 Utility Functions

  • omniTableName(ref *ast.TableRef) extracts table name
  • omniColumnNames(constraint *ast.Constraint) extracts column name list from constraint
  • omniIndexColumns(cols []*ast.IndexColumn) extracts column names from index column list
  • omniDataTypeName(dt *ast.DataType) extracts normalized type name string
  • omniIsNullable(col *ast.ColumnDef) checks if column allows NULL
  • omniHasDefault(col *ast.ColumnDef) checks if column has DEFAULT
  • omniIsAutoIncrement(col *ast.ColumnDef) checks if column is AUTO_INCREMENT
  • omniColumnComment(col *ast.ColumnDef) extracts column COMMENT string
  • omniTableOptionValue(opts []*ast.TableOption, name string) extracts table option by name
  • omniConstraintsByType(constraints []*ast.Constraint, typ ast.ConstraintType) filters constraints

1.3 Proof-of-Concept Rules (one per category)

  • rule_table_require_pk migrated — DDL-table representative (CreateTable + AlterTable + DropTable)
  • rule_column_no_null migrated — DDL-column representative (CreateTable + AlterTable column inspection)
  • rule_index_no_duplicate_column migrated — DDL-index representative (CreateTable + AlterTable + CreateIndex)
  • rule_naming_table migrated — naming representative (CreateTable + AlterTable + RenameTable)
  • rule_stmt_no_select_all migrated — DML representative (SelectStmt target list inspection)
  • All 5 proof rules produce identical advice output to ANTLR versions

Phase 2: DDL-Column Rules (21 rules)

2.1 Column Type & Attribute Rules

  • rule_column_auto_increment_initial_value — AUTO_INCREMENT initial value check
  • rule_column_auto_increment_must_integer — AUTO_INCREMENT must be integer type
  • rule_column_auto_increment_must_unsigned — AUTO_INCREMENT must be UNSIGNED
  • rule_column_current_time_count_limit — limit CURRENT_TIMESTAMP columns
  • rule_column_maximum_character_length — CHAR length limit
  • rule_column_maximum_varchar_length — VARCHAR length limit
  • rule_column_type_disallow_list — disallowed column types
  • rule_table_text_fields_total_length — total text field length limit

2.2 Column Constraint & Default Rules

  • rule_column_no_null — (already migrated in Phase 1, skip)
  • rule_column_require_default — columns must have DEFAULT
  • rule_column_set_default_for_not_null — NOT NULL columns must have DEFAULT
  • rule_column_required — required columns must exist
  • rule_column_comment_convention — column COMMENT convention

2.3 Column Modification Rules

  • rule_column_disallow_changing — disallow column changes
  • rule_column_disallow_changing_order — disallow column reordering
  • rule_column_disallow_changing_type — disallow column type changes
  • rule_column_disallow_drop — disallow column drops
  • rule_column_disallow_drop_in_index — disallow dropping indexed columns
  • rule_column_disallow_set_charset — disallow column-level charset
  • rule_column_require_charset — require column charset
  • rule_column_require_collation — require column collation

Phase 3: DDL-Table Rules (11 rules)

3.1 Table Structure Rules

  • rule_table_require_pk — (already migrated in Phase 1, skip)
  • rule_table_comment_convention — table COMMENT convention
  • rule_table_disallow_partition — disallow partitioning
  • rule_table_disallow_set_charset — disallow table-level charset
  • rule_table_require_charset — require table charset
  • rule_table_require_collation — require table collation
  • rule_table_limit_size — table size limit check

3.2 Table DDL Policy Rules

  • rule_table_disallow_ddl — disallow DDL on specific tables
  • rule_table_disallow_dml — disallow DML on specific tables
  • rule_table_drop_naming_convention — drop table naming convention
  • rule_use_innodb — require InnoDB engine

Phase 4: DDL-Index Rules (8 rules)

4.1 Index Rules

  • rule_index_no_duplicate_column — (already migrated in Phase 1, skip)
  • rule_index_key_number_limit — max columns per index
  • rule_index_pk_type — primary key type restrictions
  • rule_index_primary_key_type_allowlist — PK type allowlist
  • rule_index_total_number_limit — max indexes per table
  • rule_index_type_allow_list — index type allowlist
  • rule_index_type_no_blob — disallow BLOB in index
  • rule_table_no_duplicate_index — no duplicate indexes

Phase 5: DDL-Constraint & Database & View & Misc (12 rules)

5.1 Constraint & Charset Rules

  • rule_table_no_fk — disallow foreign keys
  • rule_charset_allowlist — charset allowlist
  • rule_collation_allowlist — collation allowlist

5.2 Database, View & System Object Rules

  • rule_database_drop_empty_db — only drop empty databases
  • rule_view_disallow_create — disallow view creation
  • rule_disallow_procedure — disallow procedure creation
  • rule_event_disallow_create — disallow event creation
  • rule_function_disallow_create — disallow function creation
  • rule_function_disallowed_list — disallowed function list
  • rule_table_disallow_trigger — disallow trigger creation
  • rule_migration_compatibility — backward compatibility checks
  • rule_online_migration — online migration checks

Phase 6: Naming Rules (7 rules)

6.1 Naming Convention Rules

  • rule_naming_table — (already migrated in Phase 1, skip)
  • rule_naming_column — column naming convention
  • rule_naming_auto_increment_column — auto-increment column naming
  • rule_naming_identifier_no_keyword — no reserved keywords as identifiers
  • rule_naming_index_convention — index naming convention
  • rule_naming_foreign_key_convention — foreign key naming convention
  • rule_naming_unique_key_convention — unique key naming convention

Phase 7: Statement Quality Rules (18 rules)

7.1 WHERE Clause Rules

  • rule_stmt_where_requirement_for_select — SELECT requires WHERE
  • rule_stmt_where_requirement_for_update_delete — UPDATE/DELETE requires WHERE
  • rule_stmt_no_leading_wildcard_like — no leading wildcard in LIKE
  • rule_statement_where_no_equal_null — no = NULL (use IS NULL)
  • rule_statement_where_disallow_using_function — no functions in WHERE
  • rule_statement_where_maximum_logical_operator_count — max logical operators in WHERE

7.2 Statement Structure Rules

  • rule_stmt_no_select_all — (already migrated in Phase 1, skip)
  • rule_stmt_disallow_limit — disallow LIMIT in DML
  • rule_stmt_disallow_order_by — disallow ORDER BY in DML
  • rule_stmt_disallow_commit — disallow COMMIT statement
  • rule_statement_merge_alter_table — merge multiple ALTER TABLE
  • rule_statement_maximum_limit_value — max LIMIT value
  • rule_statement_maximum_join_table_count — max JOIN table count
  • rule_statement_maximum_statements_in_transaction — max statements in transaction

7.3 Performance & Execution Rules

  • rule_stmt_max_execution_time — max execution time hint
  • rule_stmt_require_algorithm_or_lock_options — require ALGORITHM/LOCK in ALTER
  • rule_statement_add_column_without_position — ADD COLUMN without FIRST/AFTER
  • rule_statement_join_strict_column_attrs — strict JOIN column attribute matching

Phase 8: DML Rules (8 rules)

8.1 INSERT Rules

  • rule_insert_must_specify_column — INSERT must specify columns
  • rule_insert_row_limit — INSERT row count limit
  • rule_insert_disallow_order_by_rand — no ORDER BY RAND() in INSERT

8.2 DML Safety Rules

  • rule_statement_affected_row_limit — affected row limit
  • rule_statement_dml_dry_run — DML dry run check
  • rule_statement_select_full_table_scan — detect full table scans
  • rule_statement_disallow_using_filesort — detect filesort usage
  • rule_statement_disallow_using_temporary — detect temporary table usage

Phase 9: System Rule & Cleanup

9.1 System Rule

  • rule_builtin_prior_backup_check migrated — backup check before DDL
  • rule_statement_query_minimum_plan_level migrated — query plan level check

9.2 ANTLR Removal

  • Zero GetANTLRAST calls remain in backend/plugin/advisor/mysql/
  • GenericChecker ANTLR dispatcher removed or deprecated
  • AsANTLRAST() fallback removed from OmniAST
  • parseSingleStatementLenient() removed
  • ParseMySQL() callers migrated or removed
  • Build passes with no ANTLR imports in MySQL advisor package
  • All tests pass: advisor, schema, parser, integration