Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 90077dd

Browse files
committedOct 1, 2019
Run all queries with CONCURRENTLY option; Remove concurrency option (redundant); go mod
1 parent b9e329c commit 90077dd

File tree

3 files changed

+31
-36
lines changed

3 files changed

+31
-36
lines changed
 

‎go.mod

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module github.com/joaodlf/pgpi
2+
3+
go 1.13
4+
5+
require (
6+
github.com/fatih/color v1.6.0
7+
github.com/lib/pq v0.0.0-20180201184707-88edab080323
8+
github.com/mattn/go-colorable v0.0.9 // indirect
9+
github.com/mattn/go-isatty v0.0.3 // indirect
10+
golang.org/x/crypto v0.0.0-20180228161326-91a49db82a88
11+
golang.org/x/sys v0.0.0-20180302081741-dd2ff4accc09 // indirect
12+
)

‎go.sum

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
github.com/fatih/color v1.6.0 h1:66qjqZk8kalYAvDRtM1AdAJQI0tj4Wrue3Eq3B3pmFU=
2+
github.com/fatih/color v1.6.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
3+
github.com/lib/pq v0.0.0-20180201184707-88edab080323 h1:Ou506ViB5uo2GloKFWIYi5hwRJn4AAOXuLVv8RMY9+4=
4+
github.com/lib/pq v0.0.0-20180201184707-88edab080323/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
5+
github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4=
6+
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
7+
github.com/mattn/go-isatty v0.0.3 h1:ns/ykhmWi7G9O+8a448SecJU3nSMBXJfqQkl0upE1jI=
8+
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
9+
golang.org/x/crypto v0.0.0-20180228161326-91a49db82a88 h1:jLkAo/qlT9whgCLYC5GAJ9kcKrv3Wj8VCc4N+KJ4wpw=
10+
golang.org/x/crypto v0.0.0-20180228161326-91a49db82a88/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
11+
golang.org/x/sys v0.0.0-20180302081741-dd2ff4accc09 h1:wNPZbZUOH0tyqngVRXeF2iQm19+ssqyebJTCFBvxsow=
12+
golang.org/x/sys v0.0.0-20180302081741-dd2ff4accc09/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=

‎main.go

+7-36
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"os"
99
"os/signal"
1010
"strconv"
11-
"sync"
1211
"syscall"
1312

1413
"github.com/fatih/color"
@@ -56,20 +55,6 @@ func validateConfirmation(value string) (bool, error) {
5655
return false, errors.New("invalid value supplied")
5756
}
5857

59-
// createPartition executes the CREATE INDEX queries.
60-
func createPartition(db *sql.DB, query string, wg *sync.WaitGroup, concurrency bool) {
61-
if concurrency == true {
62-
defer wg.Done()
63-
}
64-
65-
cyan.Println(fmt.Sprintf("Executing '%s'", query))
66-
_, err := db.Exec(query)
67-
68-
if err != nil {
69-
red.Println(err)
70-
}
71-
}
72-
7358
func main() {
7459
scanner := bufio.NewScanner(os.Stdin)
7560

@@ -150,7 +135,7 @@ func main() {
150135
yellow.Println("Connnected!")
151136

152137
tr:
153-
// TABLE NAME REGEX.
138+
// TABLE NAME REGEX.
154139
tableNameRegex := ""
155140
for {
156141
green.Print("Table name regex to apply index (E.g. tablename_.*_.*): ")
@@ -234,21 +219,10 @@ tr:
234219
}
235220
}
236221

237-
var concurrency bool
238-
for {
239-
green.Print("Execute queries concurrently? [y/n]: ")
240-
scanner.Scan()
241-
concurrency, err = validateConfirmation(scanner.Text())
242-
243-
if err == nil {
244-
break
245-
}
246-
}
247-
248222
var queries []string
249223
for _, table := range tables {
250224
fullIndexName := fmt.Sprintf("%s_%s", table, indexName)
251-
indexQuery := fmt.Sprintf(`CREATE %s INDEX %s ON %s (%s);`, uniqueIndexStr, fullIndexName, table, indexColumns)
225+
indexQuery := fmt.Sprintf(`CREATE %s INDEX CONCURRENTLY %s ON %s (%s);`, uniqueIndexStr, fullIndexName, table, indexColumns)
252226
queries = append(queries, indexQuery)
253227
cyan.Println(indexQuery)
254228
}
@@ -267,17 +241,14 @@ tr:
267241
if !execute {
268242
os.Exit(0)
269243
}
270-
271-
var wg sync.WaitGroup
272244
for _, query := range queries {
273-
if concurrency {
274-
wg.Add(1)
275-
go createPartition(db, query, &wg, concurrency)
276-
} else {
277-
createPartition(db, query, &wg, concurrency)
245+
cyan.Println(fmt.Sprintf("Executing '%s'", query))
246+
_, err := db.Exec(query)
247+
248+
if err != nil {
249+
red.Println(err)
278250
}
279251
}
280252

281-
wg.Wait()
282253
green.Println("All queries executed!")
283254
}

0 commit comments

Comments
 (0)
Please sign in to comment.