Skip to content

Commit

Permalink
Add limit to rows for insert command
Browse files Browse the repository at this point in the history
Signed-off-by: Shaad7 <[email protected]>
  • Loading branch information
AbdullahAlShaad committed Aug 22, 2023
1 parent bbe4b2e commit 561de94
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 14 deletions.
4 changes: 4 additions & 0 deletions pkg/data/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ const (
const (
actor = "kubedb-cli"
)

const (
maxRows = 100000
)
5 changes: 2 additions & 3 deletions pkg/data/elasticsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,9 @@ func InsertElasticsearchDataCMD(f cmdutil.Factory) *cobra.Command {
log.Fatalln(err)
}

if rows <= 0 {
log.Fatal("rows need to be greater than 0")
if rows < 1 || rows > maxRows {
log.Fatalf("rows need to be between 1 and %d", maxRows)
}

err = opts.insertDataInDatabase(rows)
if err != nil {
log.Fatal(err)
Expand Down
7 changes: 3 additions & 4 deletions pkg/data/mariadb.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,16 @@ func InsertMariaDBDataCMD(f cmdutil.Factory) *cobra.Command {
log.Fatalln(err)
}

if rows <= 0 {
log.Fatal("Inserted rows must be greater than 0")
}

tunnel, err := lib.TunnelToDBService(opts.config, dbName, namespace, api.MySQLDatabasePort)
if err != nil {
log.Fatal("couldn't create tunnel, error: ", err)
}

defer tunnel.Close()

if rows < 1 || rows > maxRows {
log.Fatalf("rows need to be between 1 and %d", maxRows)
}
err = opts.insertDataExecCmd(tunnel, rows)
if err != nil {
log.Fatal(err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/data/mongodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ func InsertMongoDBDataCMD(f cmdutil.Factory) *cobra.Command {
log.Fatalln(err)
}

if rows <= 0 {
log.Fatal("rows need to be greater than 0")
if rows < 1 || rows > maxRows {
log.Fatalf("rows need to be between 1 and %d", maxRows)
}

command := fmt.Sprintf("for(var i=1;i<=%d;i++){db[\"%s\"].insert({_id:\"doc\"+i,actor:\"%s\"})}", rows, mgCollectionName, actor)
Expand Down
4 changes: 3 additions & 1 deletion pkg/data/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ func InsertMySQLDataCMD(f cmdutil.Factory) *cobra.Command {
}

defer tunnel.Close()

if rows < 1 || rows > maxRows {
log.Fatalf("rows need to be between 1 and %d", maxRows)
}
err = opts.insertDataExecCmd(tunnel, rows)
if err != nil {
log.Fatal(err)
Expand Down
7 changes: 3 additions & 4 deletions pkg/data/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ func InsertPostgresDataCMD(f cmdutil.Factory) *cobra.Command {
log.Fatal("couldn't create tunnel, error: ", err)
}
defer tunnel.Close()
if rows < 1 || rows > maxRows {
log.Fatalf("rows need to be between 1 and %d", maxRows)
}
err = opts.insertDataExecCmd(tunnel, rows)
if err != nil {
log.Fatal(err)
Expand All @@ -97,10 +100,6 @@ func InsertPostgresDataCMD(f cmdutil.Factory) *cobra.Command {
}

func (opts *postgresOpts) insertDataExecCmd(tunnel *portforward.Tunnel, rows int) error {
if rows <= 0 {
return fmt.Errorf("rows need to be greater than 0")
}

command := `
DO $$ BEGIN
IF NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'appscode_kubedb_postgres_test_table') THEN
Expand Down
3 changes: 3 additions & 0 deletions pkg/data/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ func InsertRedisDataCMD(f cmdutil.Factory) *cobra.Command {
if err != nil {
log.Fatalln(err)
}
if rows < 1 || rows > maxRows {
log.Fatalf("rows need to be between 1 and %d", maxRows)
}

err = opts.insertDataInDatabase(rows)
if err != nil {
Expand Down

0 comments on commit 561de94

Please sign in to comment.