|
| 1 | +package importcmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/spf13/cobra" |
| 7 | + |
| 8 | + ps "github.com/planetscale/planetscale-go/planetscale" |
| 9 | + |
| 10 | + "github.com/planetscale/cli/internal/cmdutil" |
| 11 | + "github.com/planetscale/cli/internal/import/d1" |
| 12 | + "github.com/planetscale/cli/internal/printer" |
| 13 | +) |
| 14 | + |
| 15 | +const defaultD1Branch = "main" |
| 16 | + |
| 17 | +var d1DatabaseBranchArgs = cobra.RangeArgs(1, 2) |
| 18 | + |
| 19 | +func parseDatabaseBranch(args []string) (database, branch string) { |
| 20 | + database = args[0] |
| 21 | + branch = defaultD1Branch |
| 22 | + if len(args) > 1 { |
| 23 | + branch = args[1] |
| 24 | + } |
| 25 | + return database, branch |
| 26 | +} |
| 27 | + |
| 28 | +func d1Org(ch *cmdutil.Helper) string { |
| 29 | + return ch.Config.Organization |
| 30 | +} |
| 31 | + |
| 32 | +func writeD1(ch *cmdutil.Helper, resp d1.Response) error { |
| 33 | + if resp.Status == "error" { |
| 34 | + switch ch.Printer.Format() { |
| 35 | + case printer.JSON: |
| 36 | + if err := ch.Printer.PrintJSON(resp); err != nil { |
| 37 | + return err |
| 38 | + } |
| 39 | + case printer.Human: |
| 40 | + d1.PrintHumanResponse(ch.Printer, resp) |
| 41 | + default: |
| 42 | + return fmt.Errorf(`import d1 does not support output format %q (use human or json)`, ch.Printer.Format()) |
| 43 | + } |
| 44 | + return d1CommandError(resp) |
| 45 | + } |
| 46 | + |
| 47 | + switch ch.Printer.Format() { |
| 48 | + case printer.JSON: |
| 49 | + return ch.Printer.PrintJSON(resp) |
| 50 | + case printer.Human: |
| 51 | + d1.PrintHumanResponse(ch.Printer, resp) |
| 52 | + return nil |
| 53 | + default: |
| 54 | + return fmt.Errorf(`import d1 does not support output format %q (use human or json)`, ch.Printer.Format()) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func d1CommandError(resp d1.Response) error { |
| 59 | + msg := "import d1 command failed" |
| 60 | + if resp.Error != nil { |
| 61 | + msg = resp.Error.Message |
| 62 | + if resp.Error.Remediation != "" { |
| 63 | + msg += "\n" + resp.Error.Remediation |
| 64 | + } |
| 65 | + } |
| 66 | + return &cmdutil.Error{ |
| 67 | + Msg: msg, |
| 68 | + ExitCode: cmdutil.ActionRequestedExitCode, |
| 69 | + Handled: true, |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func d1NotifyAPI(client *ps.Client) d1.NotifyAPIConfig { |
| 74 | + return d1.NotifyAPIConfig{Client: client} |
| 75 | +} |
| 76 | + |
| 77 | +func importTableCount(prepared *d1.ImportPrepareResult) int { |
| 78 | + if prepared == nil || prepared.Plan == nil { |
| 79 | + return 0 |
| 80 | + } |
| 81 | + return countDataTables(prepared.Plan.Tables) |
| 82 | +} |
| 83 | + |
| 84 | +func verifyTableCount(org, database, branch, migrationID, inputPath string) int { |
| 85 | + path := inputPath |
| 86 | + if path == "" && migrationID != "" { |
| 87 | + if state, err := d1.LoadState(org, database, branch, migrationID); err == nil { |
| 88 | + path = state.InputPath |
| 89 | + } |
| 90 | + } |
| 91 | + if path == "" { |
| 92 | + return 0 |
| 93 | + } |
| 94 | + tables, err := d1.ParseDump(path) |
| 95 | + if err != nil { |
| 96 | + return 0 |
| 97 | + } |
| 98 | + n := 0 |
| 99 | + for _, t := range tables { |
| 100 | + if !d1.IsORMMetadataTable(t.Name) { |
| 101 | + n++ |
| 102 | + } |
| 103 | + } |
| 104 | + return n |
| 105 | +} |
| 106 | + |
| 107 | +func countDataTables(tables []d1.TablePlan) int { |
| 108 | + n := 0 |
| 109 | + for _, table := range tables { |
| 110 | + if !d1.IsORMMetadataTable(table.Name) { |
| 111 | + n++ |
| 112 | + } |
| 113 | + } |
| 114 | + return n |
| 115 | +} |
| 116 | + |
| 117 | +// D1Cmd returns the import d1 subcommand group. |
| 118 | +func D1Cmd(ch *cmdutil.Helper) *cobra.Command { |
| 119 | + cmd := &cobra.Command{ |
| 120 | + Use: "d1 <command>", |
| 121 | + Short: "Import Cloudflare D1 into PlanetScale Postgres", |
| 122 | + Long: `Offline import from Cloudflare D1 (SQLite) to PlanetScale Postgres. |
| 123 | +
|
| 124 | +Export your D1 database with wrangler (wrangler d1 export <name> --remote --output ./d1-export.sql), |
| 125 | +lint the dump, then start the import (use --dry-run to preview). |
| 126 | +All commands support --format json for machine-readable output. |
| 127 | +
|
| 128 | +Branch-scoped commands use the same positional form as other PlanetScale CLI commands: |
| 129 | + pscale import d1 start <database> [branch] --input ./d1-export.sql |
| 130 | +Org comes from your pscale config (pscale org).`, |
| 131 | + } |
| 132 | + |
| 133 | + cmd.AddCommand(d1DoctorCmd(ch)) |
| 134 | + cmd.AddCommand(d1LintCmd(ch)) |
| 135 | + cmd.AddCommand(d1ConvertSchemaCmd(ch)) |
| 136 | + cmd.AddCommand(d1StartCmd(ch)) |
| 137 | + cmd.AddCommand(d1VerifyCmd(ch)) |
| 138 | + cmd.AddCommand(d1StatusCmd(ch)) |
| 139 | + cmd.AddCommand(d1CompleteCmd(ch)) |
| 140 | + |
| 141 | + return cmd |
| 142 | +} |
0 commit comments