|
| 1 | +package vtctld |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/planetscale/cli/internal/cmdutil" |
| 7 | + "github.com/planetscale/cli/internal/printer" |
| 8 | + ps "github.com/planetscale/planetscale-go/planetscale" |
| 9 | + "github.com/spf13/cobra" |
| 10 | +) |
| 11 | + |
| 12 | +func LookupVindexCmd(ch *cmdutil.Helper) *cobra.Command { |
| 13 | + cmd := &cobra.Command{ |
| 14 | + Use: "lookup-vindex <command>", |
| 15 | + Short: "Manage Lookup Vindex operations", |
| 16 | + } |
| 17 | + |
| 18 | + cmd.AddCommand(LookupVindexCreateCmd(ch)) |
| 19 | + cmd.AddCommand(LookupVindexShowCmd(ch)) |
| 20 | + cmd.AddCommand(LookupVindexExternalizeCmd(ch)) |
| 21 | + cmd.AddCommand(LookupVindexInternalizeCmd(ch)) |
| 22 | + cmd.AddCommand(LookupVindexCancelCmd(ch)) |
| 23 | + cmd.AddCommand(LookupVindexCompleteCmd(ch)) |
| 24 | + |
| 25 | + return cmd |
| 26 | +} |
| 27 | + |
| 28 | +func LookupVindexCreateCmd(ch *cmdutil.Helper) *cobra.Command { |
| 29 | + var flags struct { |
| 30 | + name string |
| 31 | + tableKeyspace string |
| 32 | + keyspace string |
| 33 | + vindexType string |
| 34 | + cells []string |
| 35 | + tabletTypes []string |
| 36 | + tableOwner string |
| 37 | + tableName string |
| 38 | + tableOwnerColumns []string |
| 39 | + tableVindexType string |
| 40 | + ignoreNulls bool |
| 41 | + tabletTypesInPreferenceOrder bool |
| 42 | + continueAfterCopyWithOwner bool |
| 43 | + } |
| 44 | + |
| 45 | + cmd := &cobra.Command{ |
| 46 | + Use: "create <database> <branch>", |
| 47 | + Short: "Create a Lookup Vindex", |
| 48 | + Args: cmdutil.RequiredArgs("database", "branch"), |
| 49 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 50 | + ctx := cmd.Context() |
| 51 | + database, branch := args[0], args[1] |
| 52 | + |
| 53 | + client, err := ch.Client() |
| 54 | + if err != nil { |
| 55 | + return err |
| 56 | + } |
| 57 | + |
| 58 | + end := ch.Printer.PrintProgress( |
| 59 | + fmt.Sprintf("Creating Lookup Vindex %s on %s/%s\u2026", |
| 60 | + printer.BoldBlue(flags.name), printer.BoldBlue(database), printer.BoldBlue(branch))) |
| 61 | + defer end() |
| 62 | + |
| 63 | + req := &ps.LookupVindexCreateRequest{ |
| 64 | + Organization: ch.Config.Organization, |
| 65 | + Database: database, |
| 66 | + Branch: branch, |
| 67 | + Name: flags.name, |
| 68 | + TableKeyspace: flags.tableKeyspace, |
| 69 | + Keyspace: flags.keyspace, |
| 70 | + Type: flags.vindexType, |
| 71 | + Cells: flags.cells, |
| 72 | + TabletTypes: flags.tabletTypes, |
| 73 | + TableOwner: flags.tableOwner, |
| 74 | + TableName: flags.tableName, |
| 75 | + TableOwnerColumns: flags.tableOwnerColumns, |
| 76 | + TableVindexType: flags.tableVindexType, |
| 77 | + } |
| 78 | + if cmd.Flags().Changed("ignore-nulls") { |
| 79 | + req.IgnoreNulls = &flags.ignoreNulls |
| 80 | + } |
| 81 | + if cmd.Flags().Changed("tablet-types-in-preference-order") { |
| 82 | + req.TabletTypesInPreferenceOrder = &flags.tabletTypesInPreferenceOrder |
| 83 | + } |
| 84 | + if cmd.Flags().Changed("continue-after-copy-with-owner") { |
| 85 | + req.ContinueAfterCopyWithOwner = &flags.continueAfterCopyWithOwner |
| 86 | + } |
| 87 | + |
| 88 | + data, err := client.LookupVindex.Create(ctx, req) |
| 89 | + if err != nil { |
| 90 | + return cmdutil.HandleError(err) |
| 91 | + } |
| 92 | + |
| 93 | + end() |
| 94 | + return ch.Printer.PrettyPrintJSON(data) |
| 95 | + }, |
| 96 | + } |
| 97 | + |
| 98 | + cmd.Flags().StringVar(&flags.name, "name", "", "Name of the Lookup Vindex") |
| 99 | + cmd.Flags().StringVar(&flags.tableKeyspace, "table-keyspace", "", "Keyspace of the table to create the vindex on") |
| 100 | + cmd.Flags().StringVar(&flags.keyspace, "keyspace", "", "Keyspace for the lookup table") |
| 101 | + cmd.Flags().StringVar(&flags.vindexType, "type", "", "Type of the vindex") |
| 102 | + cmd.Flags().StringSliceVar(&flags.cells, "cells", nil, "Cells to replicate from (comma-separated)") |
| 103 | + cmd.Flags().StringSliceVar(&flags.tabletTypes, "tablet-types", nil, "Tablet types to replicate from (comma-separated)") |
| 104 | + cmd.Flags().StringVar(&flags.tableOwner, "table-owner", "", "Owner table name") |
| 105 | + cmd.Flags().StringVar(&flags.tableName, "table-name", "", "Name of the lookup table") |
| 106 | + cmd.Flags().StringSliceVar(&flags.tableOwnerColumns, "table-owner-columns", nil, "Owner table columns (comma-separated)") |
| 107 | + cmd.Flags().StringVar(&flags.tableVindexType, "table-vindex-type", "", "Vindex type on the owner table") |
| 108 | + cmd.Flags().BoolVar(&flags.ignoreNulls, "ignore-nulls", false, "Ignore null values") |
| 109 | + cmd.Flags().BoolVar(&flags.tabletTypesInPreferenceOrder, "tablet-types-in-preference-order", false, "Use tablet types in preference order") |
| 110 | + cmd.Flags().BoolVar(&flags.continueAfterCopyWithOwner, "continue-after-copy-with-owner", false, "Continue after copy with owner") |
| 111 | + cmd.MarkFlagRequired("name") // nolint:errcheck |
| 112 | + cmd.MarkFlagRequired("table-keyspace") // nolint:errcheck |
| 113 | + |
| 114 | + return cmd |
| 115 | +} |
| 116 | + |
| 117 | +func LookupVindexShowCmd(ch *cmdutil.Helper) *cobra.Command { |
| 118 | + var flags struct { |
| 119 | + name string |
| 120 | + tableKeyspace string |
| 121 | + } |
| 122 | + |
| 123 | + cmd := &cobra.Command{ |
| 124 | + Use: "show <database> <branch>", |
| 125 | + Short: "Show details of a Lookup Vindex", |
| 126 | + Args: cmdutil.RequiredArgs("database", "branch"), |
| 127 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 128 | + ctx := cmd.Context() |
| 129 | + database, branch := args[0], args[1] |
| 130 | + |
| 131 | + client, err := ch.Client() |
| 132 | + if err != nil { |
| 133 | + return err |
| 134 | + } |
| 135 | + |
| 136 | + end := ch.Printer.PrintProgress( |
| 137 | + fmt.Sprintf("Fetching Lookup Vindex %s on %s/%s\u2026", |
| 138 | + printer.BoldBlue(flags.name), printer.BoldBlue(database), printer.BoldBlue(branch))) |
| 139 | + defer end() |
| 140 | + |
| 141 | + data, err := client.LookupVindex.Show(ctx, &ps.LookupVindexShowRequest{ |
| 142 | + Organization: ch.Config.Organization, |
| 143 | + Database: database, |
| 144 | + Branch: branch, |
| 145 | + Name: flags.name, |
| 146 | + TableKeyspace: flags.tableKeyspace, |
| 147 | + }) |
| 148 | + if err != nil { |
| 149 | + return cmdutil.HandleError(err) |
| 150 | + } |
| 151 | + |
| 152 | + end() |
| 153 | + return ch.Printer.PrettyPrintJSON(data) |
| 154 | + }, |
| 155 | + } |
| 156 | + |
| 157 | + cmd.Flags().StringVar(&flags.name, "name", "", "Name of the Lookup Vindex") |
| 158 | + cmd.Flags().StringVar(&flags.tableKeyspace, "table-keyspace", "", "Keyspace of the table") |
| 159 | + cmd.MarkFlagRequired("name") // nolint:errcheck |
| 160 | + cmd.MarkFlagRequired("table-keyspace") // nolint:errcheck |
| 161 | + |
| 162 | + return cmd |
| 163 | +} |
| 164 | + |
| 165 | +func LookupVindexExternalizeCmd(ch *cmdutil.Helper) *cobra.Command { |
| 166 | + var flags struct { |
| 167 | + name string |
| 168 | + tableKeyspace string |
| 169 | + keyspace string |
| 170 | + delete bool |
| 171 | + } |
| 172 | + |
| 173 | + cmd := &cobra.Command{ |
| 174 | + Use: "externalize <database> <branch>", |
| 175 | + Short: "Externalize a Lookup Vindex", |
| 176 | + Args: cmdutil.RequiredArgs("database", "branch"), |
| 177 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 178 | + ctx := cmd.Context() |
| 179 | + database, branch := args[0], args[1] |
| 180 | + |
| 181 | + client, err := ch.Client() |
| 182 | + if err != nil { |
| 183 | + return err |
| 184 | + } |
| 185 | + |
| 186 | + end := ch.Printer.PrintProgress( |
| 187 | + fmt.Sprintf("Externalizing Lookup Vindex %s on %s/%s\u2026", |
| 188 | + printer.BoldBlue(flags.name), printer.BoldBlue(database), printer.BoldBlue(branch))) |
| 189 | + defer end() |
| 190 | + |
| 191 | + req := &ps.LookupVindexExternalizeRequest{ |
| 192 | + Organization: ch.Config.Organization, |
| 193 | + Database: database, |
| 194 | + Branch: branch, |
| 195 | + Name: flags.name, |
| 196 | + TableKeyspace: flags.tableKeyspace, |
| 197 | + Keyspace: flags.keyspace, |
| 198 | + } |
| 199 | + |
| 200 | + if cmd.Flags().Changed("delete") { |
| 201 | + req.Delete = &flags.delete |
| 202 | + } |
| 203 | + |
| 204 | + data, err := client.LookupVindex.Externalize(ctx, req) |
| 205 | + if err != nil { |
| 206 | + return cmdutil.HandleError(err) |
| 207 | + } |
| 208 | + |
| 209 | + end() |
| 210 | + return ch.Printer.PrettyPrintJSON(data) |
| 211 | + }, |
| 212 | + } |
| 213 | + |
| 214 | + cmd.Flags().StringVar(&flags.name, "name", "", "Name of the Lookup Vindex") |
| 215 | + cmd.Flags().StringVar(&flags.tableKeyspace, "table-keyspace", "", "Keyspace of the table") |
| 216 | + cmd.Flags().StringVar(&flags.keyspace, "keyspace", "", "Keyspace for the lookup table") |
| 217 | + cmd.Flags().BoolVar(&flags.delete, "delete", false, "Delete the workflow after externalizing") |
| 218 | + cmd.MarkFlagRequired("name") // nolint:errcheck |
| 219 | + cmd.MarkFlagRequired("table-keyspace") // nolint:errcheck |
| 220 | + |
| 221 | + return cmd |
| 222 | +} |
| 223 | + |
| 224 | +func LookupVindexInternalizeCmd(ch *cmdutil.Helper) *cobra.Command { |
| 225 | + var flags struct { |
| 226 | + name string |
| 227 | + tableKeyspace string |
| 228 | + keyspace string |
| 229 | + } |
| 230 | + |
| 231 | + cmd := &cobra.Command{ |
| 232 | + Use: "internalize <database> <branch>", |
| 233 | + Short: "Internalize a Lookup Vindex", |
| 234 | + Args: cmdutil.RequiredArgs("database", "branch"), |
| 235 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 236 | + ctx := cmd.Context() |
| 237 | + database, branch := args[0], args[1] |
| 238 | + |
| 239 | + client, err := ch.Client() |
| 240 | + if err != nil { |
| 241 | + return err |
| 242 | + } |
| 243 | + |
| 244 | + end := ch.Printer.PrintProgress( |
| 245 | + fmt.Sprintf("Internalizing Lookup Vindex %s on %s/%s\u2026", |
| 246 | + printer.BoldBlue(flags.name), printer.BoldBlue(database), printer.BoldBlue(branch))) |
| 247 | + defer end() |
| 248 | + |
| 249 | + data, err := client.LookupVindex.Internalize(ctx, &ps.LookupVindexInternalizeRequest{ |
| 250 | + Organization: ch.Config.Organization, |
| 251 | + Database: database, |
| 252 | + Branch: branch, |
| 253 | + Name: flags.name, |
| 254 | + TableKeyspace: flags.tableKeyspace, |
| 255 | + Keyspace: flags.keyspace, |
| 256 | + }) |
| 257 | + if err != nil { |
| 258 | + return cmdutil.HandleError(err) |
| 259 | + } |
| 260 | + |
| 261 | + end() |
| 262 | + return ch.Printer.PrettyPrintJSON(data) |
| 263 | + }, |
| 264 | + } |
| 265 | + |
| 266 | + cmd.Flags().StringVar(&flags.name, "name", "", "Name of the Lookup Vindex") |
| 267 | + cmd.Flags().StringVar(&flags.tableKeyspace, "table-keyspace", "", "Keyspace of the table") |
| 268 | + cmd.Flags().StringVar(&flags.keyspace, "keyspace", "", "Keyspace for the lookup table") |
| 269 | + cmd.MarkFlagRequired("name") // nolint:errcheck |
| 270 | + cmd.MarkFlagRequired("table-keyspace") // nolint:errcheck |
| 271 | + |
| 272 | + return cmd |
| 273 | +} |
| 274 | + |
| 275 | +func LookupVindexCancelCmd(ch *cmdutil.Helper) *cobra.Command { |
| 276 | + var flags struct { |
| 277 | + name string |
| 278 | + tableKeyspace string |
| 279 | + } |
| 280 | + |
| 281 | + cmd := &cobra.Command{ |
| 282 | + Use: "cancel <database> <branch>", |
| 283 | + Short: "Cancel a Lookup Vindex creation", |
| 284 | + Args: cmdutil.RequiredArgs("database", "branch"), |
| 285 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 286 | + ctx := cmd.Context() |
| 287 | + database, branch := args[0], args[1] |
| 288 | + |
| 289 | + client, err := ch.Client() |
| 290 | + if err != nil { |
| 291 | + return err |
| 292 | + } |
| 293 | + |
| 294 | + end := ch.Printer.PrintProgress( |
| 295 | + fmt.Sprintf("Canceling Lookup Vindex %s on %s/%s\u2026", |
| 296 | + printer.BoldBlue(flags.name), printer.BoldBlue(database), printer.BoldBlue(branch))) |
| 297 | + defer end() |
| 298 | + |
| 299 | + data, err := client.LookupVindex.Cancel(ctx, &ps.LookupVindexCancelRequest{ |
| 300 | + Organization: ch.Config.Organization, |
| 301 | + Database: database, |
| 302 | + Branch: branch, |
| 303 | + Name: flags.name, |
| 304 | + TableKeyspace: flags.tableKeyspace, |
| 305 | + }) |
| 306 | + if err != nil { |
| 307 | + return cmdutil.HandleError(err) |
| 308 | + } |
| 309 | + |
| 310 | + end() |
| 311 | + return ch.Printer.PrettyPrintJSON(data) |
| 312 | + }, |
| 313 | + } |
| 314 | + |
| 315 | + cmd.Flags().StringVar(&flags.name, "name", "", "Name of the Lookup Vindex") |
| 316 | + cmd.Flags().StringVar(&flags.tableKeyspace, "table-keyspace", "", "Keyspace of the table") |
| 317 | + cmd.MarkFlagRequired("name") // nolint:errcheck |
| 318 | + cmd.MarkFlagRequired("table-keyspace") // nolint:errcheck |
| 319 | + |
| 320 | + return cmd |
| 321 | +} |
| 322 | + |
| 323 | +func LookupVindexCompleteCmd(ch *cmdutil.Helper) *cobra.Command { |
| 324 | + var flags struct { |
| 325 | + name string |
| 326 | + tableKeyspace string |
| 327 | + keyspace string |
| 328 | + } |
| 329 | + |
| 330 | + cmd := &cobra.Command{ |
| 331 | + Use: "complete <database> <branch>", |
| 332 | + Short: "Complete a Lookup Vindex creation", |
| 333 | + Args: cmdutil.RequiredArgs("database", "branch"), |
| 334 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 335 | + ctx := cmd.Context() |
| 336 | + database, branch := args[0], args[1] |
| 337 | + |
| 338 | + client, err := ch.Client() |
| 339 | + if err != nil { |
| 340 | + return err |
| 341 | + } |
| 342 | + |
| 343 | + end := ch.Printer.PrintProgress( |
| 344 | + fmt.Sprintf("Completing Lookup Vindex %s on %s/%s\u2026", |
| 345 | + printer.BoldBlue(flags.name), printer.BoldBlue(database), printer.BoldBlue(branch))) |
| 346 | + defer end() |
| 347 | + |
| 348 | + data, err := client.LookupVindex.Complete(ctx, &ps.LookupVindexCompleteRequest{ |
| 349 | + Organization: ch.Config.Organization, |
| 350 | + Database: database, |
| 351 | + Branch: branch, |
| 352 | + Name: flags.name, |
| 353 | + TableKeyspace: flags.tableKeyspace, |
| 354 | + Keyspace: flags.keyspace, |
| 355 | + }) |
| 356 | + if err != nil { |
| 357 | + return cmdutil.HandleError(err) |
| 358 | + } |
| 359 | + |
| 360 | + end() |
| 361 | + return ch.Printer.PrettyPrintJSON(data) |
| 362 | + }, |
| 363 | + } |
| 364 | + |
| 365 | + cmd.Flags().StringVar(&flags.name, "name", "", "Name of the Lookup Vindex") |
| 366 | + cmd.Flags().StringVar(&flags.tableKeyspace, "table-keyspace", "", "Keyspace of the table") |
| 367 | + cmd.Flags().StringVar(&flags.keyspace, "keyspace", "", "Keyspace for the lookup table") |
| 368 | + cmd.MarkFlagRequired("name") // nolint:errcheck |
| 369 | + cmd.MarkFlagRequired("table-keyspace") // nolint:errcheck |
| 370 | + |
| 371 | + return cmd |
| 372 | +} |
0 commit comments