Skip to content

Commit

Permalink
Fix migration for end-dated and mixed versions
Browse files Browse the repository at this point in the history
  • Loading branch information
calvinmclean committed Sep 3, 2024
1 parent e003da4 commit 5a85c71
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
4 changes: 2 additions & 2 deletions garden-app/gardens.yaml.example
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
Garden_chokmn1nhf81274ru2mg: '{"name":"Indoor Seed Starting","topic_prefix":"seed-garden","id":"chokmn1nhf81274ru2mg","max_zones":3,"created_at":"2023-05-27T00:14:20.324Z","light_schedule":{"duration":"16h0m0s","start_time":"06:00:00-07:00"},"version":1}'
Garden_cos1pt0n1e43o39cs40g: '{"name":"Front Yard","topic_prefix":"front-yard","id":"cos1pt0n1e43o39cs40g","max_zones":4,"created_at":"2024-05-05T16:58:10.206246-07:00","version":1}'
WaterSchedule_chokmq1nhf81274ru2n0: '{"id":"chokmq1nhf81274ru2n0","duration":"1s","interval":"30s","start_time":"15:00:00Z","end_date":"2024-05-05T17:01:10.335976-07:00","name":"WaterEvery30s"}'
WaterSchedule_chokmq1nhf81274ru2n0: '{"id":"chokmq1nhf81274ru2n0","duration":"1s","interval":"30s","start_date":null,"start_time":"15:00:00Z","end_date":"2024-05-05T17:01:10.335976-07:00","name":"WaterEvery30s","version":1}'
WaterSchedule_cii72s9nhf8f7gdpckug: '{"id":"cii72s9nhf8f7gdpckug","duration":"1h0m0s","interval":"480h0m0s","start_date":null,"start_time":"12:00:00Z","name":"Winter Trees","description":"Water deeply and infrequently in the winter","active_period":{"start_month":"October","end_month":"March"},"version":1}'
WaterSchedule_cjbg22a8tio6of9s8o0g: '{"id":"cjbg22a8tio6of9s8o0g","duration":"30s","interval":"24h0m0s","start_date":null,"start_time":"15:00:00Z","name":"Seedlings","description":"Water seedlings a bit every day","version":1}'
WaterSchedule_cos1s28n1e43sc2vb4k0: '{"id":"cos1s28n1e43sc2vb4k0","duration":"1h30m0s","interval":"240h0m0s","start_date":null,"start_time":"12:00:00Z","name":"Summer Trees","description":"Water deeply every 10 days","active_period":{"start_month":"April","end_month":"September"},"version":1}'
WaterSchedule_cos1suon1e43sc2vb4kg: '{"id":"cos1suon1e43sc2vb4kg","duration":"45m0s","interval":"120h0m0s","start_date":null,"start_time":"15:04:05Z","name":"Shrubs","description":"Water shrubs every 5 days","version":1}'
Zone_chokn19nhf81274ru2o0: '{"name":"Zone 1","id":"chokn19nhf81274ru2o0","garden_id":"chokmn1nhf81274ru2mg","position":0,"created_at":"2023-05-27T00:15:01.683Z","water_schedule_ids":["cjbg22a8tio6of9s8o0g"],"skip_count":null,"version":1}'
Zone_cij42vpnhf85d3acsgu0: '{"name":"Zone 2","id":"cij42vpnhf85d3acsgu0","garden_id":"chokmn1nhf81274ru2mg","position":0,"created_at":"2023-07-06T04:22:23.331Z","end_date":"2024-05-05T17:00:29.204187-07:00","water_schedule_ids":["chokmq1nhf81274ru2n0"],"skip_count":null}'
Zone_cij42vpnhf85d3acsgu0: '{"name":"Zone 2","id":"cij42vpnhf85d3acsgu0","garden_id":"chokmn1nhf81274ru2mg","position":0,"created_at":"2023-07-06T04:22:23.331Z","end_date":"2024-05-05T17:00:29.204187-07:00","water_schedule_ids":["chokmq1nhf81274ru2n0"],"skip_count":null,"version":1}'
Zone_cij436pnhf85d3acsgug: '{"name":"Zone 1","id":"cij436pnhf85d3acsgug","garden_id":"cihpp51nhf84tr94jtfg","position":0,"created_at":"2023-07-06T04:22:51.608Z","water_schedule_ids":["cii72s9nhf8f7gdpckug"],"skip_count":null,"version":1}'
Zone_cos1q8gn1e43o39cs410: '{"name":"Trees","details":{"description":"This zone controls watering to two trees that are watered deeply"},"id":"cos1q8gn1e43o39cs410","garden_id":"cos1pt0n1e43o39cs40g","position":0,"created_at":"2024-05-05T16:58:49.357958-07:00","water_schedule_ids":["cos1s28n1e43sc2vb4k0","cii72s9nhf8f7gdpckug"],"skip_count":null,"version":1}'
Zone_cos1qf0n1e43o39cs41g: '{"name":"Shrubs","details":{"description":"This zone has a few shrubs that need water more frequently"},"id":"cos1qf0n1e43o39cs41g","garden_id":"cos1pt0n1e43o39cs40g","position":2,"created_at":"2024-05-05T16:59:08.973836-07:00","water_schedule_ids":["cos1suon1e43sc2vb4kg"],"skip_count":null,"version":1}'
17 changes: 14 additions & 3 deletions garden-app/pkg/storage/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package storage

import (
"context"
"errors"
"fmt"

"github.com/calvinmclean/automated-garden/garden-app/pkg"
"github.com/calvinmclean/automated-garden/garden-app/pkg/storage/migrate"
"github.com/calvinmclean/babyapi"
)

var (
Expand Down Expand Up @@ -48,13 +50,16 @@ func (c *Client) RunMigrations(ctx context.Context) error {
}

func (c *Client) RunZoneMigrations(ctx context.Context) error {
zones, err := c.Zones.GetAll(ctx, nil)
zones, err := c.Zones.GetAll(ctx, babyapi.EndDatedQueryParam(true))
if err != nil {
return fmt.Errorf("error getting all Zones: %w", err)

Check warning on line 55 in garden-app/pkg/storage/migrations.go

View check run for this annotation

Codecov / codecov/patch

garden-app/pkg/storage/migrations.go#L55

Added line #L55 was not covered by tests
}

for zone, err := range migrate.Each[*pkg.Zone, *pkg.Zone](zoneMigrations, zones) {
if err != nil {
if errors.Is(err, migrate.ErrNotFound) {
continue

Check warning on line 61 in garden-app/pkg/storage/migrations.go

View check run for this annotation

Codecov / codecov/patch

garden-app/pkg/storage/migrations.go#L60-L61

Added lines #L60 - L61 were not covered by tests
}
return fmt.Errorf("error migrating Zone: %w", err)

Check warning on line 63 in garden-app/pkg/storage/migrations.go

View check run for this annotation

Codecov / codecov/patch

garden-app/pkg/storage/migrations.go#L63

Added line #L63 was not covered by tests
}

Expand All @@ -68,13 +73,16 @@ func (c *Client) RunZoneMigrations(ctx context.Context) error {
}

func (c *Client) RunGardenMigrations(ctx context.Context) error {
gardens, err := c.Gardens.GetAll(ctx, nil)
gardens, err := c.Gardens.GetAll(ctx, babyapi.EndDatedQueryParam(true))
if err != nil {
return fmt.Errorf("error getting all Gardens: %w", err)

Check warning on line 78 in garden-app/pkg/storage/migrations.go

View check run for this annotation

Codecov / codecov/patch

garden-app/pkg/storage/migrations.go#L78

Added line #L78 was not covered by tests
}

for garden, err := range migrate.Each[*pkg.Garden, *pkg.Garden](gardenMigrations, gardens) {
if err != nil {
if errors.Is(err, migrate.ErrNotFound) {
continue

Check warning on line 84 in garden-app/pkg/storage/migrations.go

View check run for this annotation

Codecov / codecov/patch

garden-app/pkg/storage/migrations.go#L83-L84

Added lines #L83 - L84 were not covered by tests
}
return fmt.Errorf("error migrating Garden: %w", err)

Check warning on line 86 in garden-app/pkg/storage/migrations.go

View check run for this annotation

Codecov / codecov/patch

garden-app/pkg/storage/migrations.go#L86

Added line #L86 was not covered by tests
}

Expand All @@ -88,13 +96,16 @@ func (c *Client) RunGardenMigrations(ctx context.Context) error {
}

func (c *Client) RunWaterScheduleMigrations(ctx context.Context) error {
waterSchedules, err := c.WaterSchedules.GetAll(ctx, nil)
waterSchedules, err := c.WaterSchedules.GetAll(ctx, babyapi.EndDatedQueryParam(true))
if err != nil {
return fmt.Errorf("error getting all WaterSchedules: %w", err)

Check warning on line 101 in garden-app/pkg/storage/migrations.go

View check run for this annotation

Codecov / codecov/patch

garden-app/pkg/storage/migrations.go#L101

Added line #L101 was not covered by tests
}

for waterSchedule, err := range migrate.Each[*pkg.WaterSchedule, *pkg.WaterSchedule](waterScheduleMigrations, waterSchedules) {
if err != nil {
if errors.Is(err, migrate.ErrNotFound) {
continue

Check warning on line 107 in garden-app/pkg/storage/migrations.go

View check run for this annotation

Codecov / codecov/patch

garden-app/pkg/storage/migrations.go#L106-L107

Added lines #L106 - L107 were not covered by tests
}
return fmt.Errorf("error migrating WaterSchedule: %w", err)

Check warning on line 109 in garden-app/pkg/storage/migrations.go

View check run for this annotation

Codecov / codecov/patch

garden-app/pkg/storage/migrations.go#L109

Added line #L109 was not covered by tests
}

Expand Down

0 comments on commit 5a85c71

Please sign in to comment.