5
5
"strings"
6
6
7
7
"github.com/MakeNowJust/heredoc"
8
+ "github.com/algolia/algoliasearch-client-go/v3/algolia/opt"
8
9
"github.com/algolia/algoliasearch-client-go/v3/algolia/search"
9
10
"github.com/spf13/cobra"
10
11
@@ -105,7 +106,12 @@ func runDeleteCmd(opts *DeleteOptions) error {
105
106
106
107
for _ , index := range indices {
107
108
if _ , err := index .Delete (); err != nil {
108
- return fmt .Errorf ("failed to delete index %q: %w" , index .GetName (), err )
109
+ opts .IO .StartProgressIndicatorWithLabel (fmt .Sprint ("Deleting replica index " , index .GetName ()))
110
+ err := deleteReplicaIndex (client , index )
111
+ opts .IO .StopProgressIndicator ()
112
+ if err != nil {
113
+ return fmt .Errorf ("failed to delete index %q: %w" , index .GetName (), err )
114
+ }
109
115
}
110
116
}
111
117
@@ -116,3 +122,81 @@ func runDeleteCmd(opts *DeleteOptions) error {
116
122
117
123
return nil
118
124
}
125
+
126
+ // Delete a replica index.
127
+ func deleteReplicaIndex (client * search.Client , replicaIndex * search.Index ) error {
128
+ replicaName := replicaIndex .GetName ()
129
+ primaryName , err := findPrimaryIndex (replicaIndex )
130
+ if err != nil {
131
+ return fmt .Errorf ("can't find primary index for %q: %w" , replicaName , err )
132
+ }
133
+
134
+ err = detachReplicaIndex (replicaName , primaryName , client )
135
+ if err != nil {
136
+ return fmt .Errorf ("can't unlink replica index %s from primary index %s: %w" , replicaName , primaryName , err )
137
+ }
138
+
139
+ _ , err = replicaIndex .Delete ()
140
+ if err != nil {
141
+ return fmt .Errorf ("can't delete replica index %q: %w" , replicaName , err )
142
+ }
143
+
144
+ return nil
145
+ }
146
+
147
+ // Find the primary index of a replica index
148
+ func findPrimaryIndex (replicaIndex * search.Index ) (string , error ) {
149
+ replicaName := replicaIndex .GetName ()
150
+ settings , err := replicaIndex .GetSettings ()
151
+
152
+ if err != nil {
153
+ return "" , fmt .Errorf ("can't get settings of replica index %q: %w" , replicaName , err )
154
+ }
155
+
156
+ primary := settings .Primary
157
+ if primary == nil {
158
+ return "" , fmt .Errorf ("index %s doesn't have a primary" , replicaName )
159
+ }
160
+
161
+ return primary .Get (), nil
162
+ }
163
+
164
+ // Remove replica from `replicas` settings of the primary index
165
+ func detachReplicaIndex (replicaName string , primaryName string , client * search.Client ) error {
166
+ primaryIndex := client .InitIndex (primaryName )
167
+ settings , err := primaryIndex .GetSettings ()
168
+
169
+ if err != nil {
170
+ return fmt .Errorf ("can't get settings of primary index %q: %w" , primaryName , err )
171
+ }
172
+
173
+ replicas := settings .Replicas .Get ()
174
+ indexOfReplica := findIndex (replicas , replicaName )
175
+
176
+ // Delete the replica at position `indexOfReplica` from the array
177
+ replicas = append (replicas [:indexOfReplica ], replicas [indexOfReplica + 1 :]... )
178
+
179
+ res , err := primaryIndex .SetSettings (
180
+ search.Settings {
181
+ Replicas : opt .Replicas (replicas ... ),
182
+ },
183
+ )
184
+
185
+ if err != nil {
186
+ return fmt .Errorf ("can't update settings of index %q: %w" , primaryName , err )
187
+ }
188
+
189
+ // Wait until the settings are updated, else a subsequent `delete` will fail.
190
+ _ = res .Wait ()
191
+ return nil
192
+ }
193
+
194
+ // Find the index of the string `target` in the array `arr`
195
+ func findIndex (arr []string , target string ) int {
196
+ for i , v := range arr {
197
+ if v == target {
198
+ return i
199
+ }
200
+ }
201
+ return - 1
202
+ }
0 commit comments