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

check Spatial column and Spatial index #2066

Merged
merged 5 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,6 @@ require (

replace (
github.com/pingcap/log => github.com/pingcap/log v0.0.0-20191012051959-b742a5d432e9
github.com/pingcap/parser => github.com/sjjian/parser v0.0.0-20231020015929-c5d7ca486d80
github.com/pingcap/parser => github.com/sjjian/parser v0.0.0-20231122073510-03e191068cf1
google.golang.org/grpc => google.golang.org/grpc v1.29.0
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -843,8 +843,8 @@ github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrf
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/sjjian/parser v0.0.0-20231020015929-c5d7ca486d80 h1:2pFDv4Gd1vVqHwPEzWiTN4vgP9jLKZDuVP/jEcC0SE0=
github.com/sjjian/parser v0.0.0-20231020015929-c5d7ca486d80/go.mod h1:Qq2tnreUXwVo7NAKAHmbWFsgqpDUkxwhJCClY+ZCudA=
github.com/sjjian/parser v0.0.0-20231122073510-03e191068cf1 h1:0TrsSYbQFneiCIbZF3YWS0WJD5MP4J/CEoglDGrADUg=
github.com/sjjian/parser v0.0.0-20231122073510-03e191068cf1/go.mod h1:Qq2tnreUXwVo7NAKAHmbWFsgqpDUkxwhJCClY+ZCudA=
github.com/skeema/knownhosts v1.2.0 h1:h9r9cf0+u7wSE+M183ZtMGgOJKiL96brpaz5ekfJCpM=
github.com/skeema/knownhosts v1.2.0/go.mod h1:g4fPeYpque7P0xefxtGzV81ihjC8sX2IqpAoNkjxbMo=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
Expand Down
7 changes: 6 additions & 1 deletion spelling_dict.txt
Original file line number Diff line number Diff line change
Expand Up @@ -441,4 +441,9 @@ datediff
xxx
sprintln
sessionctx
stmtctx
stmtctx
linestring
multipoint
multilinestring
multipolygon
geometrycollection
359 changes: 359 additions & 0 deletions sqle/driver/mysql/audit_offline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3061,3 +3061,362 @@ func TestDDLAvoidText(t *testing.T) {
newTestResult())
})
}

func TestDDLAvoidFullText(t *testing.T) {
rule := rulepkg.RuleHandlerMap[rulepkg.DDLAvoidFullText].Rule
// 全文索引
t.Run(`create table without fulltext index`, func(t *testing.T) {
runSingleRuleInspectCase(
rule,
t,
``,
DefaultMysqlInspectOffline(),
`CREATE TABLE example (
id INT primary key,
content TEXT,
INDEX idx_id_content (id, content)
);`,
newTestResult())
})

t.Run(`create common table`, func(t *testing.T) {
runSingleRuleInspectCase(
rule,
t,
``,
DefaultMysqlInspectOffline(),
`CREATE TABLE example (
id INT primary key,
content TEXT
);`,
newTestResult())
})

t.Run(`create fulltext index`, func(t *testing.T) {
runSingleRuleInspectCase(
rule,
t,
``,
DefaultMysqlInspectOffline(),
`CREATE FULLTEXT INDEX index_name
ON table_name (column_name);`,
newTestResult().addResult(rulepkg.DDLAvoidFullText))
})

t.Run(`create index`, func(t *testing.T) {
runSingleRuleInspectCase(
rule,
t,
``,
DefaultMysqlInspectOffline(),
`CREATE INDEX index_name ON table_name (column_name);`,
newTestResult())
})

t.Run(`create unique index`, func(t *testing.T) {
runSingleRuleInspectCase(
rule,
t,
``,
DefaultMysqlInspectOffline(),
`CREATE unique INDEX index_name ON table_name (column_name);`,
newTestResult())
})

t.Run(`create table with fulltext index`, func(t *testing.T) {
runSingleRuleInspectCase(
rule,
t,
``,
DefaultMysqlInspectOffline(),
`CREATE TABLE example (
id INT,
content TEXT,
FULLTEXT INDEX idx_content (content)
);`,
newTestResult().addResult(rulepkg.DDLAvoidFullText))
})

t.Run(`alter table add fulltext`, func(t *testing.T) {
runSingleRuleInspectCase(
rule,
t,
``,
DefaultMysqlInspectOffline(),
`ALTER TABLE your_table_name
ADD FULLTEXT INDEX idx_name (name);`,
newTestResult().addResult(rulepkg.DDLAvoidFullText))
})

t.Run(`alter table`, func(t *testing.T) {
runSingleRuleInspectCase(
rule,
t,
``,
DefaultMysqlInspectOffline(),
`ALTER TABLE example
ADD INDEX idx_name (name),
ADD UNIQUE INDEX idx_email (email);`,
newTestResult())
})
}

func TestDDLAvoidGeometry(t *testing.T) {
rule := rulepkg.RuleHandlerMap[rulepkg.DDLAvoidGeometry].Rule
// 空间字段
t.Run(`create table with column point`, func(t *testing.T) {
runSingleRuleInspectCase(
rule,
t,
``,
DefaultMysqlInspectOffline(),
`CREATE TABLE t (id INT PRIMARY KEY, g POINT);`,
newTestResult().addResult(rulepkg.DDLAvoidGeometry))
})
t.Run(`create table with column GEOMETRY`, func(t *testing.T) {
runSingleRuleInspectCase(
rule,
t,
``,
DefaultMysqlInspectOffline(),
`CREATE TABLE t (id INT PRIMARY KEY, g GEOMETRY);`,
newTestResult().addResult(rulepkg.DDLAvoidGeometry))
})
t.Run(`create table with column LINESTRING`, func(t *testing.T) {
runSingleRuleInspectCase(
rule,
t,
``,
DefaultMysqlInspectOffline(),
`CREATE TABLE t (id INT PRIMARY KEY, g LINESTRING);`,
newTestResult().addResult(rulepkg.DDLAvoidGeometry))
})
t.Run(`create table with column POLYGON`, func(t *testing.T) {
runSingleRuleInspectCase(
rule,
t,
``,
DefaultMysqlInspectOffline(),
`CREATE TABLE t (id INT PRIMARY KEY, g POLYGON);`,
newTestResult().addResult(rulepkg.DDLAvoidGeometry))
})
t.Run(`create table with column MULTIPOINT`, func(t *testing.T) {
runSingleRuleInspectCase(
rule,
t,
``,
DefaultMysqlInspectOffline(),
`CREATE TABLE t (id INT PRIMARY KEY, g MULTIPOINT);`,
newTestResult().addResult(rulepkg.DDLAvoidGeometry))
})
t.Run(`create table with column MULTILINESTRING`, func(t *testing.T) {
runSingleRuleInspectCase(
rule,
t,
``,
DefaultMysqlInspectOffline(),
`CREATE TABLE t (id INT PRIMARY KEY, g MULTILINESTRING);`,
newTestResult().addResult(rulepkg.DDLAvoidGeometry))
})
t.Run(`create table with column MULTIPOLYGON`, func(t *testing.T) {
runSingleRuleInspectCase(
rule,
t,
``,
DefaultMysqlInspectOffline(),
`CREATE TABLE t (id INT PRIMARY KEY, g MULTIPOLYGON);`,
newTestResult().addResult(rulepkg.DDLAvoidGeometry))
})
t.Run(`create table with column GEOMETRYCOLLECTION`, func(t *testing.T) {
runSingleRuleInspectCase(
rule,
t,
``,
DefaultMysqlInspectOffline(),
`CREATE TABLE t (id INT PRIMARY KEY, g GEOMETRYCOLLECTION);`,
newTestResult().addResult(rulepkg.DDLAvoidGeometry))
})
// alter add
t.Run(`alter table with column point`, func(t *testing.T) {
runSingleRuleInspectCase(
rule,
t,
``,
DefaultMysqlInspectOffline(),
`alter TABLE t add column t point;`,
newTestResult().addResult(rulepkg.DDLAvoidGeometry))
})
t.Run(`alter table with column GEOMETRY`, func(t *testing.T) {
runSingleRuleInspectCase(
rule,
t,
``,
DefaultMysqlInspectOffline(),
`alter TABLE t add column g GEOMETRY;`,
newTestResult().addResult(rulepkg.DDLAvoidGeometry))
})
t.Run(`alter table with column LINESTRING`, func(t *testing.T) {
runSingleRuleInspectCase(
rule,
t,
``,
DefaultMysqlInspectOffline(),
`alter TABLE t add column g LINESTRING;`,
newTestResult().addResult(rulepkg.DDLAvoidGeometry))
})
t.Run(`alter table with column POLYGON`, func(t *testing.T) {
runSingleRuleInspectCase(
rule,
t,
``,
DefaultMysqlInspectOffline(),
`alter TABLE t add column g POLYGON;`,
newTestResult().addResult(rulepkg.DDLAvoidGeometry))
})
t.Run(`alter table with column MULTIPOINT`, func(t *testing.T) {
runSingleRuleInspectCase(
rule,
t,
``,
DefaultMysqlInspectOffline(),
`alter TABLE t add column g MULTIPOINT;`,
newTestResult().addResult(rulepkg.DDLAvoidGeometry))
})
t.Run(`alter table with column MULTILINESTRING`, func(t *testing.T) {
runSingleRuleInspectCase(
rule,
t,
``,
DefaultMysqlInspectOffline(),
`alter TABLE t add column g MULTILINESTRING;`,
newTestResult().addResult(rulepkg.DDLAvoidGeometry))
})
t.Run(`alter table with column MULTIPOLYGON`, func(t *testing.T) {
runSingleRuleInspectCase(
rule,
t,
``,
DefaultMysqlInspectOffline(),
`alter TABLE t add column g MULTIPOLYGON;`,
newTestResult().addResult(rulepkg.DDLAvoidGeometry))
})
t.Run(`alter table with column GEOMETRYCOLLECTION`, func(t *testing.T) {
runSingleRuleInspectCase(
rule,
t,
``,
DefaultMysqlInspectOffline(),
`alter TABLE t add column g GEOMETRYCOLLECTION;`,
newTestResult().addResult(rulepkg.DDLAvoidGeometry))
})
// 空间索引
t.Run(`create table with GEOMETRY index`, func(t *testing.T) {
runSingleRuleInspectCase(
rule,
t,
``,
DefaultMysqlInspectOffline(),
`CREATE TABLE t (id INT PRIMARY KEY, g POINT, SPATIAL INDEX(g));`,
newTestResult().addResult(rulepkg.DDLAvoidGeometry))
})
t.Run(`Create a normal index`, func(t *testing.T) {
runSingleRuleInspectCase(
rule,
t,
``,
DefaultMysqlInspectOffline(),
`CREATE TABLE t (id INT PRIMARY KEY, name varchar(50), INDEX(name));`,
newTestResult())
})
t.Run(`alter table a GEOMETRY index`, func(t *testing.T) {
runSingleRuleInspectCase(
rule,
t,
``,
DefaultMysqlInspectOffline(),
`ALTER TABLE geom ADD SPATIAL INDEX(g);`,
newTestResult().addResult(rulepkg.DDLAvoidGeometry))
})
t.Run(`alter table a normal index`, func(t *testing.T) {
runSingleRuleInspectCase(
rule,
t,
``,
DefaultMysqlInspectOffline(),
`ALTER TABLE geom ADD INDEX(g);`,
newTestResult())
})
t.Run(`create a GEOMETRY index`, func(t *testing.T) {
runSingleRuleInspectCase(
rule,
t,
``,
DefaultMysqlInspectOffline(),
`CREATE SPATIAL INDEX g ON geom (g);`,
newTestResult().addResult(rulepkg.DDLAvoidGeometry))
})
t.Run(`create a normal index`, func(t *testing.T) {
runSingleRuleInspectCase(
rule,
t,
``,
DefaultMysqlInspectOffline(),
`CREATE INDEX g ON geom (g);`,
newTestResult())
})
t.Run(`alter table with geo index`, func(t *testing.T) {
runSingleRuleInspectCase(
rule,
t,
``,
DefaultMysqlInspectOffline(),
`alter table table_1 add SPATIAL index index_2(g);`,
newTestResult().addResult(rulepkg.DDLAvoidGeometry))
})
// 添加多个字段
t.Run(`alter table with column MULTIPOLYGON`, func(t *testing.T) {
runSingleRuleInspectCase(
rule,
t,
``,
DefaultMysqlInspectOffline(),
`alter TABLE t add column name varchar(20),add column g MULTIPOLYGON;`,
newTestResult().addResult(rulepkg.DDLAvoidGeometry))
})
t.Run(`alter table with normal columns`, func(t *testing.T) {
runSingleRuleInspectCase(
rule,
t,
``,
DefaultMysqlInspectOffline(),
`alter TABLE t add column name varchar(20),add column age int;`,
newTestResult())
})
t.Run(`alter table with geo column and normal index`, func(t *testing.T) {
runSingleRuleInspectCase(
rule,
t,
``,
DefaultMysqlInspectOffline(),
`alter table table_1 add column g point, add index index_1(column_name);`,
newTestResult().addResult(rulepkg.DDLAvoidGeometry))
})
t.Run(`alter table with normal column and geo index`, func(t *testing.T) {
runSingleRuleInspectCase(
rule,
t,
``,
DefaultMysqlInspectOffline(),
`alter table table_1 add column name varchar(20), add SPATIAL INDEX(g);`,
newTestResult().addResult(rulepkg.DDLAvoidGeometry))
})
t.Run(`alter table with normal column and geo index`, func(t *testing.T) {
runSingleRuleInspectCase(
rule,
t,
``,
DefaultMysqlInspectOffline(),
`alter table table_1 add SPATIAL index index_2(g);`,
newTestResult().addResult(rulepkg.DDLAvoidGeometry))
})
}
Loading
Loading