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

增加在SQL审核阶段自动识别并合并相同表的alter table语句的功能 #669

Merged
merged 4 commits into from
Aug 25, 2024

Conversation

jiweixiao
Copy link
Contributor

@jiweixiao jiweixiao commented Aug 12, 2024

新增 check-merge参数,设置其为1后开启自动合并alter table语句功能(现在仅有命令行参数,后续将添加配置文件参数并将参数名修改为 alter-auto-merge)。在添加了check-merge参数后,goInc会检查用户提交的SQL,并将其中涉及相同表的alter table,create index,drop index语句统一合并为 alter table语句,然后返回SQL检查结果以及合并后新生成的SQL。

goInc校验的返回值增加了一个字段,显示对应的SQL是否为 (alter table|create index|drop index), 如果是的话,就会显示'alterTable'字样。对于合并后新生成的SQL,ErrorMessage字段显示"Merged"。以上两个字段可用于区分是原有SQL还是合并后的SQL

新增alterTableInfo结构体,记录每一个表被修改的信息,包括表名,所有alter语句的语法树,以及合并后的新SQL

在checkAlterTable方法中写入alterTableInfo,在提交阶段进行SQL的合并工作,通过合并语法树然后生成新的SQL

对于create index和drop index语句,使用convertCreateIndexToAddConstrain和convertDropIndexToDropIndex方法(已修改成convertCreateIndexToAlterTable和convertDropIndexToAlerTable)将其转换为alter table语句,然后进行合并。

checkAlterTable方法增加了mergeOnly参数,设置为true时不进行后续的检查。
去掉了alterTableInfo中的alterCount属性
@hanchuanchuan
Copy link
Owner

hanchuanchuan commented Aug 12, 2024

感谢PR!

  1. 建议将参数名改为alter-auto-merge参数, 以便和check-*参数区分开.
  2. 参数可以调到到config/config.go的结构体, 可以动态配置而非硬编码到调用选项, 这样会更加灵活.
  3. 获取语句type时可以尝试使用 fmt.Sprintf("%T",r.Type)返回所有语句的类型, 而非仅处理alter table/ add/drop index.

@hanchuanchuan hanchuanchuan marked this pull request as ready for review August 12, 2024 13:36
Copy link

@hanchuanchuan hanchuanchuan merged commit 7c6a1c0 into hanchuanchuan:master Aug 25, 2024
3 checks passed
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