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

修复:建议主键命名为"PK_表名“,当未为主键命名的时候,规则不触发 #2048

Merged
merged 2 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions sqle/driver/mysql/audit_executed_sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ ALTER TABLE exist_db.not_exist_tb_1 ADD INDEX idx_2(b1);
ALTER TABLE exist_db.not_exist_tb_1 ADD COLUMN b2 blob UNIQUE KEY COMMENT "unit test";
ALTER TABLE exist_db.not_exist_tb_1 MODIFY COLUMN b1 blob UNIQUE KEY COMMENT "unit test";
`,
newTestResult().addResult(rulepkg.DDLCheckIndexedColumnWithBlob).
newTestResult().addResult(rulepkg.DDLCheckPKName).addResult(rulepkg.DDLCheckIndexedColumnWithBlob).
add(driverV2.RuleLevelWarn, "", "建议建表DDL包含CREATE_TIME字段且默认值为CURRENT_TIMESTAMP").
add(driverV2.RuleLevelWarn, "", "建表DDL需要包含UPDATE_TIME字段且默认值为CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP").
add(driverV2.RuleLevelWarn, "", "这些索引字段(b1)需要有非空约束"),
Expand Down Expand Up @@ -226,12 +226,12 @@ id bigint unsigned NOT NULL KEY DEFAULT "unit test" COMMENT "unit test",
v1 varchar(255) NOT NULL DEFAULT "unit test" COMMENT "unit test",
v2 varchar(255) NOT NULL DEFAULT "unit test" COMMENT "unit test"
)ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT="unit test";
ALTER TABLE exist_db.exist_tb_1 Add primary key(v1);
ALTER TABLE exist_db.exist_tb_1 Add primary key(v1);
`,
newTestResult().addResult(rulepkg.DDLCheckPKWithoutAutoIncrement).
add(driverV2.RuleLevelWarn, "", "建议建表DDL包含CREATE_TIME字段且默认值为CURRENT_TIMESTAMP").
add(driverV2.RuleLevelWarn, "", "建表DDL需要包含UPDATE_TIME字段且默认值为CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"),
newTestResult(),
newTestResult().addResult(rulepkg.DDLCheckPKName),
)
})

Expand All @@ -249,10 +249,10 @@ PRIMARY KEY (id)
)ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT="unit test";
ALTER TABLE exist_db.exist_tb_1 Add primary key(v1);
`,
newTestResult().addResult(rulepkg.DDLCheckPKWithoutBigintUnsigned).
newTestResult().addResult(rulepkg.DDLCheckPKName).addResult(rulepkg.DDLCheckPKWithoutBigintUnsigned).
add(driverV2.RuleLevelWarn, "", "建议建表DDL包含CREATE_TIME字段且默认值为CURRENT_TIMESTAMP").
add(driverV2.RuleLevelWarn, "", "建表DDL需要包含UPDATE_TIME字段且默认值为CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"),
newTestResult(),
newTestResult().addResult(rulepkg.DDLCheckPKName),
)
})

Expand Down
46 changes: 23 additions & 23 deletions sqle/driver/mysql/audit_offline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ update_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMEST
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT= "unit test";
`,
newTestResult().addResult(rulepkg.DDLCheckPKWithoutIfNotExists),
newTestResult().addResult(rulepkg.DDLCheckPKName).addResult(rulepkg.DDLCheckPKWithoutIfNotExists),
)
}

Expand All @@ -429,7 +429,7 @@ func TestCheckObjectNameUsingKeywordOffline(t *testing.T) {
"PRIMARY KEY (id),"+
"INDEX `show` (v1)"+
")ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT=\"unit test\";",
newTestResult().addResult(rulepkg.DDLCheckObjectNameUsingKeyword, "select, create, show").
newTestResult().addResult(rulepkg.DDLCheckPKName).addResult(rulepkg.DDLCheckObjectNameUsingKeyword, "select, create, show").
addResult(rulepkg.DDLCheckIndexPrefix, "idx_"),
)
}
Expand All @@ -448,7 +448,7 @@ update_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMEST
v2 varchar(255) NOT NULL DEFAULT "unit test" COMMENT "unit test",
PRIMARY KEY (id)
)ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT="unit test";`, length64),
newTestResult(),
newTestResult().addResult(rulepkg.DDLCheckPKName),
)

runDefaultRulesInspectCase(t, "create_table: table length > 64", DefaultMysqlInspectOffline(),
Expand All @@ -461,7 +461,7 @@ update_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMEST
v2 varchar(255) NOT NULL DEFAULT "unit test" COMMENT "unit test",
PRIMARY KEY (id)
)ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT="unit test";`, length65),
newTestResult().addResult(rulepkg.DDLCheckObjectNameLength, 64),
newTestResult().addResult(rulepkg.DDLCheckPKName).addResult(rulepkg.DDLCheckObjectNameLength, 64),
)

runDefaultRulesInspectCase(t, "create_table: columns length > 64", DefaultMysqlInspectOffline(),
Expand All @@ -474,7 +474,7 @@ update_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMEST
v2 varchar(255) NOT NULL DEFAULT "unit test" COMMENT "unit test",
PRIMARY KEY (id)
)ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT="unit test";`, length65),
newTestResult().addResult(rulepkg.DDLCheckObjectNameLength, 64),
newTestResult().addResult(rulepkg.DDLCheckPKName).addResult(rulepkg.DDLCheckObjectNameLength, 64),
)

runDefaultRulesInspectCase(t, "create_table: index length > 64", DefaultMysqlInspectOffline(),
Expand All @@ -488,7 +488,7 @@ update_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMEST
PRIMARY KEY (id),
INDEX idx_%s (v1)
)ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT="unit test";`, length65),
newTestResult().addResult(rulepkg.DDLCheckObjectNameLength, 64),
newTestResult().addResult(rulepkg.DDLCheckPKName).addResult(rulepkg.DDLCheckObjectNameLength, 64),
)

runDefaultRulesInspectCase(t, "alter_table: table length > 64", DefaultMysqlInspectOffline(),
Expand Down Expand Up @@ -573,7 +573,7 @@ v2 varchar(255) NOT NULL DEFAULT "unit test" COMMENT "unit test",
PRIMARY KEY (id)
)ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT="unit test";
`,
newTestResult().addResult(rulepkg.DDLCheckPKWithoutAutoIncrement),
newTestResult().addResult(rulepkg.DDLCheckPKName).addResult(rulepkg.DDLCheckPKWithoutAutoIncrement),
)

runDefaultRulesInspectCase(t, "create_table: primary key not bigint unsigned(1)", DefaultMysqlInspectOffline(),
Expand All @@ -600,7 +600,7 @@ v2 varchar(255) NOT NULL DEFAULT "unit test" COMMENT "unit test",
PRIMARY KEY (id)
)ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT="unit test";
`,
newTestResult().addResult(rulepkg.DDLCheckPKWithoutBigintUnsigned),
newTestResult().addResult(rulepkg.DDLCheckPKName).addResult(rulepkg.DDLCheckPKWithoutBigintUnsigned),
)
}

Expand All @@ -615,7 +615,7 @@ func TestCheckColumnCharLengthOffline(t *testing.T) {
PRIMARY KEY (id)
)ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT="unit test";
`,
newTestResult(),
newTestResult().addResult(rulepkg.DDLCheckPKName),
)

runDefaultRulesInspectCase(t, "create_table: check char(21)", DefaultMysqlInspectOffline(),
Expand All @@ -629,7 +629,7 @@ func TestCheckColumnCharLengthOffline(t *testing.T) {
PRIMARY KEY (id)
)ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT="unit test";
`,
newTestResult().addResult(rulepkg.DDLCheckColumnCharLength),
newTestResult().addResult(rulepkg.DDLCheckPKName).addResult(rulepkg.DDLCheckColumnCharLength),
)
}

Expand Down Expand Up @@ -760,7 +760,7 @@ PRIMARY KEY (id),
INDEX idx_b1 (b1)
)ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT="unit test";
`,
newTestResult().addResult(rulepkg.DDLCheckIndexedColumnWithBlob).add(driverV2.RuleLevelWarn, rulepkg.DDLCheckIndexNotNullConstraint, "这些索引字段(b1)需要有非空约束"),
newTestResult().addResult(rulepkg.DDLCheckPKName).addResult(rulepkg.DDLCheckIndexedColumnWithBlob).add(driverV2.RuleLevelWarn, rulepkg.DDLCheckIndexNotNullConstraint, "这些索引字段(b1)需要有非空约束"),
)

runDefaultRulesInspectCase(t, "create_table: disable index column blob (2)", DefaultMysqlInspectOffline(),
Expand All @@ -775,7 +775,7 @@ b1 blob UNIQUE KEY COMMENT "unit test",
PRIMARY KEY (id)
)ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT="unit test";
`,
newTestResult().addResult(rulepkg.DDLCheckIndexedColumnWithBlob).add(driverV2.RuleLevelWarn, rulepkg.DDLCheckIndexNotNullConstraint, "这些索引字段(b1)需要有非空约束"),
newTestResult().addResult(rulepkg.DDLCheckPKName).addResult(rulepkg.DDLCheckIndexedColumnWithBlob).add(driverV2.RuleLevelWarn, rulepkg.DDLCheckIndexNotNullConstraint, "这些索引字段(b1)需要有非空约束"),
)

handler := rulepkg.RuleHandlerMap[rulepkg.DDLCheckAlterTableNeedMerge]
Expand All @@ -799,7 +799,7 @@ PRIMARY KEY (id),
FOREIGN KEY (id) REFERENCES exist_tb_1(id)
)ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT="unit test";
`,
newTestResult().addResult(rulepkg.DDLDisableFK),
newTestResult().addResult(rulepkg.DDLCheckPKName).addResult(rulepkg.DDLDisableFK),
)
}

Expand Down Expand Up @@ -930,7 +930,7 @@ PRIMARY KEY (id),
INDEX index_1 (v1)
)ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT="unit test";
`,
newTestResult().addResult(rulepkg.DDLCheckIndexPrefix, "idx_"),
newTestResult().addResult(rulepkg.DDLCheckPKName).addResult(rulepkg.DDLCheckIndexPrefix, "idx_"),
)

runDefaultRulesInspectCase(t, "alter_table: index prefix not idx_", DefaultMysqlInspectOffline(),
Expand Down Expand Up @@ -1012,7 +1012,7 @@ v1 varchar(255) COMMENT "unit test",
PRIMARY KEY (id)
)ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT="unit test";
`,
newTestResult().addResult(rulepkg.DDLCheckColumnWithoutDefault),
newTestResult().addResult(rulepkg.DDLCheckPKName).addResult(rulepkg.DDLCheckColumnWithoutDefault),
)

runDefaultRulesInspectCase(t, "alter_table: column without default", DefaultMysqlInspectOffline(),
Expand Down Expand Up @@ -1055,7 +1055,7 @@ v1 timestamp COMMENT "unit test",
PRIMARY KEY (id)
)ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT="unit test";
`,
newTestResult().addResult(rulepkg.DDLCheckColumnTimestampWithoutDefault).addResult(rulepkg.DDLDisableTypeTimestamp),
newTestResult().addResult(rulepkg.DDLCheckPKName).addResult(rulepkg.DDLCheckColumnTimestampWithoutDefault).addResult(rulepkg.DDLDisableTypeTimestamp),
)

runDefaultRulesInspectCase(t, "alter_table: column timestamp without default", DefaultMysqlInspectOffline(),
Expand All @@ -1079,7 +1079,7 @@ v1 blob NOT NULL COMMENT "unit test",
PRIMARY KEY (id)
)ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT="unit test";
`,
newTestResult().addResult(rulepkg.DDLCheckColumnBlobWithNotNull).
newTestResult().addResult(rulepkg.DDLCheckPKName).addResult(rulepkg.DDLCheckColumnBlobWithNotNull).
addResult(rulepkg.DDLCheckFieldNotNUllMustContainDefaultValue, "v1"),
)

Expand All @@ -1102,7 +1102,7 @@ update_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMEST
PRIMARY KEY (id)
)ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT="unit test";
`,
newTestResult().addResult(rulepkg.DDLCheckColumnBlobDefaultIsNotNull),
newTestResult().addResult(rulepkg.DDLCheckPKName).addResult(rulepkg.DDLCheckColumnBlobDefaultIsNotNull),
)

runDefaultRulesInspectCase(t, "alter_table: column timestamp without default", DefaultMysqlInspectOffline(),
Expand Down Expand Up @@ -2109,7 +2109,7 @@ v2 varchar(255) NOT NULL DEFAULT "unit test" COMMENT "unit test",
PRIMARY KEY (id)
)ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT="unit test";
`,
newTestResult(),
newTestResult().addResult(rulepkg.DDLCheckPKName),
)
inspector.Ctx = session.NewContext(parent.Ctx)
}
Expand Down Expand Up @@ -2165,7 +2165,7 @@ v2 varchar(255) NOT NULL DEFAULT "unit test" COMMENT "unit test",
PRIMARY KEY (id)
)ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT="unit test";
`,
newTestResult(),
newTestResult().addResult(rulepkg.DDLCheckPKName),
)
inspector.Ctx = session.NewContext(parent.Ctx)
}
Expand Down Expand Up @@ -2377,15 +2377,15 @@ func Test_DDL_CHECK_PK_NAMEOffline(t *testing.T) {
for _, sql := range []string{
`create table t1(id int, primary key pk_t1(id))`,
`create table t1(id int, primary key PK_T1(id))`,
`create table t1(id int, primary key(id))`,
`alter table exist_db.exist_tb_2 Add primary key(id)`,
`alter table exist_db.exist_tb_2 Add primary key PK_EXIST_TB_2(id)`} {
runSingleRuleInspectCase(rulepkg.RuleHandlerMap[rulepkg.DDLCheckPKName].Rule, t, "", DefaultMysqlInspectOffline(), sql, newTestResult())
}

for _, sql := range []string{
`create table t1(id int, primary key wrongPK(id))`,
`alter table exist_db.exist_tb_2 Add primary key wrongPK(id)`} {
`alter table exist_db.exist_tb_2 Add primary key wrongPK(id)`,
`create table t1(id int, primary key(id))`,
`alter table exist_db.exist_tb_2 Add primary key(id)`} {
runSingleRuleInspectCase(rulepkg.RuleHandlerMap[rulepkg.DDLCheckPKName].Rule, t, "", DefaultMysqlInspectOffline(), sql, newTestResult().addResult(rulepkg.DDLCheckPKName))
}
}
Expand Down
Loading
Loading