@@ -70,12 +70,32 @@ var cleanupYugabyteDBCmd = &cli.Command{
70
70
return fmt .Errorf ("creating yugabyte cluster: %w" , err )
71
71
}
72
72
73
- query := `DROP KEYSPACE idx`
74
- log .Debug (query )
75
- err = session .Query (query ).WithContext (cctx .Context ).Exec ()
76
- if err != nil {
77
- return err
73
+ keyspace := "idx"
74
+
75
+ // Step 1: Drop all indexes
76
+ var indexName string
77
+ indexesQuery := fmt .Sprintf ("SELECT index_name FROM system_schema.indexes WHERE keyspace_name='%s';" , keyspace )
78
+ iter := session .Query (indexesQuery ).Iter ()
79
+
80
+ for iter .Scan (& indexName ) {
81
+ dropIndexQuery := fmt .Sprintf ("DROP INDEX %s.%s;" , keyspace , indexName )
82
+ fmt .Println ("Executing:" , dropIndexQuery )
83
+ if err := session .Query (dropIndexQuery ).Exec (); err != nil {
84
+ return fmt .Errorf ("failed to drop index %s: %w" , indexName , err )
85
+ }
78
86
}
87
+ if err := iter .Close (); err != nil {
88
+ return fmt .Errorf ("failed to iterate over indexes: %w" , err )
89
+ }
90
+
91
+ // Step 2: Drop the keyspace
92
+ dropKeyspaceQuery := fmt .Sprintf ("DROP KEYSPACE %s;" , keyspace )
93
+ fmt .Println ("Executing:" , dropKeyspaceQuery )
94
+ if err := session .Query (dropKeyspaceQuery ).Exec (); err != nil {
95
+ return fmt .Errorf ("failed to drop keyspace: %w" , err )
96
+ }
97
+
98
+ fmt .Println ("Keyspace dropped successfully." )
79
99
80
100
// Create connection pool to postgres interface
81
101
db , err := pgxpool .Connect (cctx .Context , settings .ConnectString )
0 commit comments