@@ -70,12 +70,32 @@ var cleanupYugabyteDBCmd = &cli.Command{
7070 return fmt .Errorf ("creating yugabyte cluster: %w" , err )
7171 }
7272
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+ }
7886 }
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." )
7999
80100 // Create connection pool to postgres interface
81101 db , err := pgxpool .Connect (cctx .Context , settings .ConnectString )
0 commit comments