|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log/slog" |
| 7 | + |
| 8 | + "github.com/calvinmclean/automated-garden/garden-app/pkg/storage" |
| 9 | + "github.com/calvinmclean/babyapi" |
| 10 | + "github.com/spf13/cobra" |
| 11 | +) |
| 12 | + |
| 13 | +var storageMigrateCommand = &cobra.Command{ |
| 14 | + Use: "storage-migrate", |
| 15 | + Short: "Migrate all resources from KV storage to SQL storage", |
| 16 | + RunE: func(cmd *cobra.Command, _ []string) error { |
| 17 | + slog.Info("Starting storage migration from KV to SQL") |
| 18 | + |
| 19 | + // Parse source (KV) storage config |
| 20 | + sourceDriver, _ := cmd.Flags().GetString("source-driver") |
| 21 | + sourceDSN, _ := cmd.Flags().GetString("source-dsn") |
| 22 | + sourceFilename, _ := cmd.Flags().GetString("source-filename") |
| 23 | + |
| 24 | + sourceConfig := storage.Config{ |
| 25 | + Driver: sourceDriver, |
| 26 | + Options: map[string]any{}, |
| 27 | + } |
| 28 | + |
| 29 | + if sourceDSN != "" { |
| 30 | + sourceConfig.Options["data_source_name"] = sourceDSN |
| 31 | + } |
| 32 | + if sourceFilename != "" { |
| 33 | + sourceConfig.Options["filename"] = sourceFilename |
| 34 | + } |
| 35 | + |
| 36 | + // Parse destination (SQL) storage config |
| 37 | + destDSN, _ := cmd.Flags().GetString("dest-dsn") |
| 38 | + |
| 39 | + destConfig := storage.Config{ |
| 40 | + Driver: "sqlite", |
| 41 | + Options: map[string]any{ |
| 42 | + "data_source_name": destDSN, |
| 43 | + "disable_migrations": false, |
| 44 | + }, |
| 45 | + } |
| 46 | + |
| 47 | + // Create source (KV) client |
| 48 | + sourceClient, err := storage.NewClient(sourceConfig) |
| 49 | + if err != nil { |
| 50 | + return fmt.Errorf("error creating source storage client: %w", err) |
| 51 | + } |
| 52 | + |
| 53 | + // Create destination (SQL) client |
| 54 | + destClient, err := storage.NewClient(destConfig) |
| 55 | + if err != nil { |
| 56 | + return fmt.Errorf("error creating destination storage client: %w", err) |
| 57 | + } |
| 58 | + |
| 59 | + ctx := context.Background() |
| 60 | + |
| 61 | + // Migrate Gardens |
| 62 | + slog.Info("Migrating Gardens") |
| 63 | + gardens, err := sourceClient.Gardens.Search(ctx, "", babyapi.EndDatedQueryParam(true)) |
| 64 | + if err != nil { |
| 65 | + return fmt.Errorf("error getting Gardens from source: %w", err) |
| 66 | + } |
| 67 | + slog.Info(fmt.Sprintf("Found %d Gardens to migrate", len(gardens))) |
| 68 | + for _, g := range gardens { |
| 69 | + if err := destClient.Gardens.Set(ctx, g); err != nil { |
| 70 | + return fmt.Errorf("error saving Garden to destination: %w", err) |
| 71 | + } |
| 72 | + } |
| 73 | + slog.Info(fmt.Sprintf("Successfully migrated %d Gardens", len(gardens))) |
| 74 | + |
| 75 | + // Migrate Zones (need to get per-garden since zones are nested under gardens) |
| 76 | + slog.Info("Migrating Zones") |
| 77 | + zoneCount := 0 |
| 78 | + for _, g := range gardens { |
| 79 | + zones, err := sourceClient.Zones.Search(ctx, g.GetID(), babyapi.EndDatedQueryParam(true)) |
| 80 | + if err != nil { |
| 81 | + return fmt.Errorf("error getting Zones for garden %s: %w", g.GetID(), err) |
| 82 | + } |
| 83 | + for _, z := range zones { |
| 84 | + if err := destClient.Zones.Set(ctx, z); err != nil { |
| 85 | + return fmt.Errorf("error saving Zone to destination: %w", err) |
| 86 | + } |
| 87 | + } |
| 88 | + zoneCount += len(zones) |
| 89 | + } |
| 90 | + slog.Info(fmt.Sprintf("Successfully migrated %d Zones", zoneCount)) |
| 91 | + |
| 92 | + // Migrate WaterSchedules |
| 93 | + slog.Info("Migrating WaterSchedules") |
| 94 | + waterSchedules, err := sourceClient.WaterSchedules.Search(ctx, "", babyapi.EndDatedQueryParam(true)) |
| 95 | + if err != nil { |
| 96 | + return fmt.Errorf("error getting WaterSchedules from source: %w", err) |
| 97 | + } |
| 98 | + slog.Info(fmt.Sprintf("Found %d WaterSchedules to migrate", len(waterSchedules))) |
| 99 | + for _, ws := range waterSchedules { |
| 100 | + if err := destClient.WaterSchedules.Set(ctx, ws); err != nil { |
| 101 | + return fmt.Errorf("error saving WaterSchedule to destination: %w", err) |
| 102 | + } |
| 103 | + } |
| 104 | + slog.Info(fmt.Sprintf("Successfully migrated %d WaterSchedules", len(waterSchedules))) |
| 105 | + |
| 106 | + // Migrate WeatherClientConfigs |
| 107 | + slog.Info("Migrating WeatherClientConfigs") |
| 108 | + weatherClients, err := sourceClient.WeatherClientConfigs.Search(ctx, "", babyapi.EndDatedQueryParam(true)) |
| 109 | + if err != nil { |
| 110 | + return fmt.Errorf("error getting WeatherClientConfigs from source: %w", err) |
| 111 | + } |
| 112 | + slog.Info(fmt.Sprintf("Found %d WeatherClientConfigs to migrate", len(weatherClients))) |
| 113 | + for _, wc := range weatherClients { |
| 114 | + if err := destClient.WeatherClientConfigs.Set(ctx, wc); err != nil { |
| 115 | + return fmt.Errorf("error saving WeatherClientConfig to destination: %w", err) |
| 116 | + } |
| 117 | + } |
| 118 | + slog.Info(fmt.Sprintf("Successfully migrated %d WeatherClientConfigs", len(weatherClients))) |
| 119 | + |
| 120 | + // Migrate NotificationClientConfigs |
| 121 | + slog.Info("Migrating NotificationClientConfigs") |
| 122 | + notificationClients, err := sourceClient.NotificationClientConfigs.Search(ctx, "", babyapi.EndDatedQueryParam(true)) |
| 123 | + if err != nil { |
| 124 | + return fmt.Errorf("error getting NotificationClientConfigs from source: %w", err) |
| 125 | + } |
| 126 | + slog.Info(fmt.Sprintf("Found %d NotificationClientConfigs to migrate", len(notificationClients))) |
| 127 | + for _, nc := range notificationClients { |
| 128 | + if err := destClient.NotificationClientConfigs.Set(ctx, nc); err != nil { |
| 129 | + return fmt.Errorf("error saving NotificationClientConfig to destination: %w", err) |
| 130 | + } |
| 131 | + } |
| 132 | + slog.Info(fmt.Sprintf("Successfully migrated %d NotificationClientConfigs", len(notificationClients))) |
| 133 | + |
| 134 | + // Migrate WaterRoutines |
| 135 | + slog.Info("Migrating WaterRoutines") |
| 136 | + waterRoutines, err := sourceClient.WaterRoutines.Search(ctx, "", babyapi.EndDatedQueryParam(true)) |
| 137 | + if err != nil { |
| 138 | + return fmt.Errorf("error getting WaterRoutines from source: %w", err) |
| 139 | + } |
| 140 | + slog.Info(fmt.Sprintf("Found %d WaterRoutines to migrate", len(waterRoutines))) |
| 141 | + for _, wr := range waterRoutines { |
| 142 | + if err := destClient.WaterRoutines.Set(ctx, wr); err != nil { |
| 143 | + return fmt.Errorf("error saving WaterRoutine to destination: %w", err) |
| 144 | + } |
| 145 | + } |
| 146 | + slog.Info(fmt.Sprintf("Successfully migrated %d WaterRoutines", len(waterRoutines))) |
| 147 | + |
| 148 | + slog.Info("Storage migration completed successfully") |
| 149 | + return nil |
| 150 | + }, |
| 151 | +} |
| 152 | + |
| 153 | +func init() { |
| 154 | + storageMigrateCommand.Flags().String("source-driver", "hashmap", "Source storage driver (hashmap or redis)") |
| 155 | + storageMigrateCommand.Flags().String("source-dsn", "", "Source Redis DSN (e.g., 'localhost:6379')") |
| 156 | + storageMigrateCommand.Flags().String("source-filename", "", "Source hashmap filename (e.g., 'storage.json')") |
| 157 | + storageMigrateCommand.Flags().String("dest-dsn", "garden.db", "Destination SQLite DSN") |
| 158 | +} |
0 commit comments