Skip to content
Open
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
2 changes: 1 addition & 1 deletion drivers/postgres/idxscan/idxscan.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func ParseCreateIndex(s string) IndexDef {
idxwith := strings.Index(utail, "WITH")
idxtbsp := strings.Index(utail, "TABLESPACE")
idxwhere := strings.Index(utail, "WHERE")
if idxwith >= 0 {
if idxwith >= 0 && (idxwith < idxwhere || idxwhere < 0) { // avoid matching timestamp with/without time zone in where clause
idxto := len(tail)
if idxwhere >= 0 {
idxto = idxwhere
Expand Down
104 changes: 89 additions & 15 deletions drivers/postgres/idxscan/idxscan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ import (
"testing"
)

const testingQ = ` CREATE UNiQUE
INDEX uk_candidate_recruiter_info_email ON public.candidate
USING btree
( sdih COLLATE "ru_RU" int4 ASC NULLS LAST,
asfg COLLATE "ru_RU" DESC,
ertny,
((recruiter_info ->>
'email'::text))
)

WITH a= b, c = d,e=f
TABLESPACE f
WHERE as;roug (hblkas) bfdvvkjhb (3li54f9q3er opfbg78 9)erg`

func TestParseCreateIndex(t *testing.T) {
ss := ParseCreateIndex(testingQ)
s := ` CREATE UNiQUE
INDEX uk_candidate_recruiter_info_email ON public.candidate
USING btree
( sdih COLLATE "ru_RU" int4 ASC NULLS LAST,
asfg COLLATE "ru_RU" DESC,
ertny,
((recruiter_info ->>
'email'::text))
)

WITH a= b, c = d,e=f
TABLESPACE f
WHERE as;roug (hblkas) bfdvvkjhb (3li54f9q3er opfbg78 9)erg`

ss := ParseCreateIndex(s)
eq := IndexDef{
Name: "uk_candidate_recruiter_info_email",
Table: "public.candidate",
Expand All @@ -32,6 +32,80 @@ func TestParseCreateIndex(t *testing.T) {
Tablespace: "f",
Where: "as;roug (hblkas) bfdvvkjhb (3li54f9q3er opfbg78 9)erg",
}

if !reflect.DeepEqual(ss, eq) {
t.Errorf("%#v", ss)
}
}

func TestParseCreateIndex_NoWithAndTimestampWithTimezone(t *testing.T) {
s := `CREATE UNIQUE INDEX \"IX_customer_external_id\"
ON public.customer
USING btree (external_id)
WHERE (created > '2020-09-27 20:00:00.000000'::timestamp with time zone)`

ss := ParseCreateIndex(s)
eq := IndexDef{
Name: "\\\"IX_customer_external_id\\\"",
Table: "public.customer",
Unique: true,
Concurrently: false,
UsingType: "btree",
ColDef: "(external_id)",
With: "",
Tablespace: "",
Where: "(created > '2020-09-27 20:00:00.000000'::timestamp with time zone)",
}

if !reflect.DeepEqual(ss, eq) {
t.Errorf("%#v", ss)
}
}

func TestParseCreateIndex_WithAndTimestampWithoutTimezone(t *testing.T) {
s := `CREATE UNIQUE INDEX \"IX_customer_external_id\"
ON public.customer
USING btree (external_id)
WITH a= b
WHERE (created > '2020-09-27 20:00:00.000000'::timestamp without time zone)`

ss := ParseCreateIndex(s)
eq := IndexDef{
Name: "\\\"IX_customer_external_id\\\"",
Table: "public.customer",
Unique: true,
Concurrently: false,
UsingType: "btree",
ColDef: "(external_id)",
With: "a= b",
Tablespace: "",
Where: "(created > '2020-09-27 20:00:00.000000'::timestamp without time zone)",
}

if !reflect.DeepEqual(ss, eq) {
t.Errorf("%#v", ss)
}
}

func TestParseCreateIndex_NoWhere(t *testing.T) {
s := `CREATE UNIQUE INDEX \"IX_customer_external_id\"
ON public.customer
USING btree (external_id)
WITH a= b`

ss := ParseCreateIndex(s)
eq := IndexDef{
Name: "\\\"IX_customer_external_id\\\"",
Table: "public.customer",
Unique: true,
Concurrently: false,
UsingType: "btree",
ColDef: "(external_id)",
With: "a= b",
Tablespace: "",
Where: "",
}

if !reflect.DeepEqual(ss, eq) {
t.Errorf("%#v", ss)
}
Expand Down