Skip to content

Commit

Permalink
feat: prevent downgrades of application
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Oct 24, 2024
1 parent 716461d commit 32d2250
Show file tree
Hide file tree
Showing 22 changed files with 265 additions and 96 deletions.
2 changes: 1 addition & 1 deletion docs/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3277,7 +3277,7 @@ Authorization ( Scopes: ledger:write )

|Name|Type|Required|Restrictions|Description|
|---|---|---|---|---|
|version|integer(int64)|false|none|none|
|version|string|false|none|none|
|name|string|false|none|none|
|date|string(date-time)|false|none|none|
|state|string|false|none|none|
Expand Down
14 changes: 8 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module github.com/formancehq/ledger

go 1.22.1

toolchain go1.22.7
toolchain go1.23.2

replace github.com/formancehq/stack/ledger/client => ./pkg/client

Expand All @@ -13,23 +13,20 @@ require (
github.com/alitto/pond v1.9.2
github.com/antlr/antlr4/runtime/Go/antlr v1.4.10
github.com/bluele/gcache v0.0.2
github.com/formancehq/go-libs/v2 v2.0.1-0.20241022185745-110c95803b63
github.com/formancehq/numscript v0.0.9-0.20241009144012-1150c14a1417
github.com/formancehq/go-libs/v2 v2.0.1-0.20241023163904-e440de7907c7
github.com/formancehq/stack/ledger/client v0.0.0-00010101000000-000000000000
github.com/go-chi/chi/v5 v5.1.0
github.com/go-chi/cors v1.2.1
github.com/google/go-cmp v0.6.0
github.com/google/uuid v1.6.0
github.com/invopop/jsonschema v0.12.0
github.com/jackc/pgx/v5 v5.7.1
github.com/jamiealquiza/tachymeter v2.0.0+incompatible
github.com/logrusorgru/aurora v2.0.3+incompatible
github.com/nats-io/nats.go v1.37.0
github.com/onsi/ginkgo/v2 v2.20.2
github.com/onsi/gomega v1.34.2
github.com/ory/dockertest/v3 v3.11.0
github.com/pborman/uuid v1.2.1
github.com/pkg/errors v0.9.1
github.com/shomali11/xsql v0.0.0-20190608141458-bf76292144df
github.com/spf13/cobra v1.8.1
github.com/spf13/pflag v1.0.5
Expand All @@ -42,7 +39,7 @@ require (
go.opentelemetry.io/otel/metric v1.31.0
go.opentelemetry.io/otel/sdk/metric v1.31.0
go.opentelemetry.io/otel/trace v1.31.0
go.uber.org/fx v1.23.0
go.uber.org/fx v1.22.2
go.uber.org/mock v0.4.0
golang.org/x/oauth2 v0.23.0
golang.org/x/sync v0.8.0
Expand Down Expand Up @@ -91,6 +88,8 @@ require (
github.com/ebitengine/purego v0.8.0 // indirect
github.com/ericlagergren/decimal v0.0.0-20240411145413-00de7ca16731 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/formancehq/numscript v0.0.9-0.20241009144012-1150c14a1417
github.com/getsentry/sentry-go v0.28.1 // indirect
github.com/go-chi/chi v4.1.2+incompatible // indirect
github.com/go-chi/render v1.0.3 // indirect
github.com/go-logr/logr v1.4.2 // indirect
Expand All @@ -115,6 +114,7 @@ require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/pgx/v5 v5.7.1 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect
github.com/jcmturner/aescts/v2 v2.0.0 // indirect
github.com/jcmturner/dnsutils/v2 v2.0.0 // indirect
Expand All @@ -140,6 +140,7 @@ require (
github.com/opencontainers/image-spec v1.1.0 // indirect
github.com/opencontainers/runc v1.1.14 // indirect
github.com/pierrec/lz4/v4 v4.1.21 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect
github.com/puzpuzpuz/xsync/v3 v3.4.0 // indirect
Expand All @@ -149,6 +150,7 @@ require (
github.com/shirou/gopsutil/v4 v4.24.9 // indirect
github.com/shomali11/util v0.0.0-20180607005212-e0f70fd665ff // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/sourcegraph/jsonrpc2 v0.2.0 // indirect
github.com/tklauser/go-sysconf v0.3.14 // indirect
github.com/tklauser/numcpus v0.9.0 // indirect
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc // indirect
Expand Down
37 changes: 17 additions & 20 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,22 @@ github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4=
github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI=
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/formancehq/go-libs/v2 v2.0.1-0.20241017152835-2c30f563ab46 h1:8wZtnWSIYNV7DwD0Jr4HsbcRgezOrgDJ2Q0w9ABieKc=
github.com/formancehq/go-libs/v2 v2.0.1-0.20241017152835-2c30f563ab46/go.mod h1:LgxayMN6wgAQbkB3ioBDTHOVMKp1rC6Q55M1CvG44xY=
github.com/formancehq/go-libs/v2 v2.0.1-0.20241017153232-1a62cecf1a61 h1:GSIhsdo/YXuZXI4q8xA8IrdOkkjfFp6O+DiNywk8s8U=
github.com/formancehq/go-libs/v2 v2.0.1-0.20241017153232-1a62cecf1a61/go.mod h1:LgxayMN6wgAQbkB3ioBDTHOVMKp1rC6Q55M1CvG44xY=
github.com/formancehq/go-libs/v2 v2.0.1-0.20241022185745-110c95803b63 h1:DN6gDFwh3zO9VwV6Nt2tj4/BEecyfWfOdHp1YYJ5sBA=
github.com/formancehq/go-libs/v2 v2.0.1-0.20241022185745-110c95803b63/go.mod h1:LgxayMN6wgAQbkB3ioBDTHOVMKp1rC6Q55M1CvG44xY=
github.com/formancehq/numscript v0.0.9-0.20241009144012-1150c14a1417 h1:LOd5hxnXDIBcehFrpW1OnXk+VSs0yJXeu1iAOO+Hji4=
github.com/formancehq/numscript v0.0.9-0.20241009144012-1150c14a1417/go.mod h1:btuSv05cYwi9BvLRxVs5zrunU+O1vTgigG1T6UsawcY=
github.com/formancehq/go-libs/v2 v2.0.1-0.20241023163308-e06611e301d6 h1:on0q5s/WngVsTEllmADLdiAWwyG7qVAOzuqSBVs/mng=
github.com/formancehq/go-libs/v2 v2.0.1-0.20241023163308-e06611e301d6/go.mod h1:LgxayMN6wgAQbkB3ioBDTHOVMKp1rC6Q55M1CvG44xY=
github.com/formancehq/go-libs/v2 v2.0.1-0.20241023163904-e440de7907c7 h1:x8vIRM5+y01pLs2YqnYcoUsvFJ/6cP5qDtsM248OmWM=
github.com/formancehq/go-libs/v2 v2.0.1-0.20241023163904-e440de7907c7/go.mod h1:LgxayMN6wgAQbkB3ioBDTHOVMKp1rC6Q55M1CvG44xY=
github.com/formancehq/numscript v0.0.8 h1:YAHcgtGxd6DWqwwiFaxWRVuXuBaW5HFx+GRuyUidlxU=
github.com/formancehq/numscript v0.0.8/go.mod h1:P8qnq15PyWUuhskZdsrzTTqVS5CBkhakDKEPAyF4oYQ=
github.com/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8vw=
github.com/fortytw2/leaktest v1.3.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g=
github.com/gkampitakis/ciinfo v0.3.0 h1:gWZlOC2+RYYttL0hBqcoQhM7h1qNkVqvRCV1fOvpAv8=
github.com/gkampitakis/ciinfo v0.3.0/go.mod h1:1NIwaOcFChN4fa/B0hEBdAb6npDlFL8Bwx4dfRLRqAo=
github.com/gkampitakis/go-diff v1.3.2 h1:Qyn0J9XJSDTgnsgHRdz9Zp24RaJeKMUHg2+PDZZdC4M=
github.com/gkampitakis/go-diff v1.3.2/go.mod h1:LLgOrpqleQe26cte8s36HTWcTmMEur6OPYerdAAS9tk=
github.com/gkampitakis/go-snaps v0.5.4 h1:GX+dkKmVsRenz7SoTbdIEL4KQARZctkMiZ8ZKprRwT8=
github.com/gkampitakis/go-snaps v0.5.4/go.mod h1:ZABkO14uCuVxBHAXAfKG+bqNz+aa1bGPAg8jkI0Nk8Y=
github.com/getsentry/sentry-go v0.28.1 h1:zzaSm/vHmGllRM6Tpx1492r0YDzauArdBfkJRtY6P5k=
github.com/getsentry/sentry-go v0.28.1/go.mod h1:1fQZ+7l7eeJ3wYi82q5Hg8GqAPgefRq+FP/QhafYVgg=
github.com/go-chi/chi v4.1.2+incompatible h1:fGFk2Gmi/YKXk0OmGfBh0WgmN3XB8lVnEyNz34tQRec=
github.com/go-chi/chi v4.1.2+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ=
github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw=
Expand Down Expand Up @@ -157,6 +161,7 @@ github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+
github.com/gorilla/securecookie v1.1.2 h1:YCIWL56dvtr73r6715mJs5ZvhtnY73hBvEF8kXD8ePA=
github.com/gorilla/securecookie v1.1.2/go.mod h1:NfCASbcHqRSY+3a8tlWJwsQap2VX5pwzwo4h3eOamfo=
github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM=
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0 h1:asbCHRVmodnJTuQ3qamDwqVOIjwqUPTYmYuemVOx+Ys=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0/go.mod h1:ggCgvZ2r7uOoQjOyu2Y1NhHmEPPzzuhWgcza5M1Ji1I=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
Expand Down Expand Up @@ -222,8 +227,6 @@ github.com/lufia/plan9stats v0.0.0-20240909124753-873cd0166683 h1:7UMa6KCCMjZEMD
github.com/lufia/plan9stats v0.0.0-20240909124753-873cd0166683/go.mod h1:ilwx/Dta8jXAgpFYFvSWEMwxmbWXyiUHkd5FwyKhb5k=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/maruel/natural v1.1.1 h1:Hja7XhhmvEFhcByqDoHz9QZbkWey+COd9xWfCfn1ioo=
github.com/maruel/natural v1.1.1/go.mod h1:v+Rfd79xlw1AgVBjbO0BEQmptqb5HvL/k9GRHB7ZKEg=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
Expand Down Expand Up @@ -292,6 +295,8 @@ github.com/shomali11/xsql v0.0.0-20190608141458-bf76292144df h1:SVCDTuzM3KEk8WBw
github.com/shomali11/xsql v0.0.0-20190608141458-bf76292144df/go.mod h1:K8jR5lDI2MGs9Ky+X2jIF4MwIslI0L8o8ijIlEq7/Vw=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/sourcegraph/jsonrpc2 v0.2.0 h1:KjN/dC4fP6aN9030MZCJs9WQbTOjWHhrtKVpzzSrr/U=
github.com/sourcegraph/jsonrpc2 v0.2.0/go.mod h1:ZafdZgk/axhT1cvZAPOhw+95nz2I/Ra5qMlU4gTRwIo=
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
Expand All @@ -307,14 +312,6 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tidwall/gjson v1.17.1 h1:wlYEnwqAHgzmhNUFfw7Xalt2JzQvsMx2Se4PcoFCT/U=
github.com/tidwall/gjson v1.17.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
github.com/tklauser/go-sysconf v0.3.14 h1:g5vzr9iPFFz24v2KZXs/pvpvh8/V9Fw6vQK5ZZb78yU=
github.com/tklauser/go-sysconf v0.3.14/go.mod h1:1ym4lWMLUOhuBOPGtRcJm7tEGX4SCYNEEEtghGG/8uY=
github.com/tklauser/numcpus v0.9.0 h1:lmyCHtANi8aRUgkckBgoDk1nHCux3n2cgkJLXdQGPDo=
Expand Down Expand Up @@ -401,8 +398,8 @@ go.opentelemetry.io/proto/otlp v1.3.1 h1:TrMUixzpM0yuc/znrFTP9MMRh8trP93mkCiDVeX
go.opentelemetry.io/proto/otlp v1.3.1/go.mod h1:0X1WI4de4ZsLrrJNLAQbFeLCm3T7yBkR0XqQ7niQU+8=
go.uber.org/dig v1.18.0 h1:imUL1UiY0Mg4bqbFfsRQO5G4CGRBec/ZujWTvSVp3pw=
go.uber.org/dig v1.18.0/go.mod h1:Us0rSJiThwCv2GteUN0Q7OKvU7n5J4dxZ9JKUXozFdE=
go.uber.org/fx v1.23.0 h1:lIr/gYWQGfTwGcSXWXu4vP5Ws6iqnNEIY+F/aFzCKTg=
go.uber.org/fx v1.23.0/go.mod h1:o/D9n+2mLP6v1EG+qsdT1O8wKopYAsqZasju97SDFCU=
go.uber.org/fx v1.22.2 h1:iPW+OPxv0G8w75OemJ1RAnTUrF55zOJlXlo1TbJ0Buw=
go.uber.org/fx v1.22.2/go.mod h1:o/D9n+2mLP6v1EG+qsdT1O8wKopYAsqZasju97SDFCU=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
go.uber.org/mock v0.4.0 h1:VcM4ZOtdbR4f6VXfiOpwpVJDL6lCReaZ6mw31wqh7KU=
Expand Down
42 changes: 41 additions & 1 deletion internal/storage/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package driver
import (
"context"
"database/sql"
"errors"
"fmt"
"github.com/formancehq/go-libs/v2/collectionutils"
"github.com/formancehq/go-libs/v2/metadata"
Expand Down Expand Up @@ -128,10 +129,49 @@ func (d *Driver) OpenLedger(ctx context.Context, name string) (*ledgerstore.Stor

func (d *Driver) Initialize(ctx context.Context) error {
logging.FromContext(ctx).Debugf("Initialize driver")
err := Migrate(ctx, d.db)
err := d.detectRollbacks(ctx)
if err != nil {
return fmt.Errorf("detecting rollbacks: %w", err)
}

err = Migrate(ctx, d.db)
if err != nil {
return fmt.Errorf("migrating system store: %w", err)
}

return nil
}

func (d *Driver) detectRollbacks(ctx context.Context) error {

logging.FromContext(ctx).Debugf("Checking for downgrades on system schema")
if err := detectDowngrades(GetMigrator(), ctx, d.db); err != nil {
return fmt.Errorf("detecting rollbacks of system schema: %w", err)
}

type row struct {
Bucket string `bun:"bucket"`
}
rows := make([]row, 0)
if err := d.db.NewSelect().
DistinctOn("bucket").
ModelTableExpr("_system.ledgers").
Column("bucket").
Scan(ctx, &rows); err != nil {
err = postgres.ResolveError(err)
if errors.Is(err, postgres.ErrMissingTable) {
return nil
}
return err
}

for _, r := range rows {
logging.FromContext(ctx).Debugf("Checking for downgrades on bucket '%s'", r.Bucket)
if err := detectDowngrades(bucket.GetMigrator(r.Bucket), ctx, d.db); err != nil {
return fmt.Errorf("detecting rollbacks on bucket '%s': %w", r.Bucket, err)
}
}

return nil
}

Expand Down
24 changes: 24 additions & 0 deletions internal/storage/driver/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package driver

import "fmt"

type ErrRollbackDetected struct {
LastRegisterVersion int
LastAvailableVersion int
}

func (e ErrRollbackDetected) Error() string {
return fmt.Sprintf("rollback detected, last register version: %d, last available version: %d", e.LastRegisterVersion, e.LastAvailableVersion)
}

func (e ErrRollbackDetected) Is(err error) bool {
_, ok := err.(ErrRollbackDetected)
return ok
}

func newErrRollbackDetected(lastRegisterVersion, lastAvailableVersion int) ErrRollbackDetected {
return ErrRollbackDetected{
LastRegisterVersion: lastRegisterVersion,
LastAvailableVersion: lastAvailableVersion,
}
}
23 changes: 23 additions & 0 deletions internal/storage/driver/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package driver

import (
"context"
"errors"
"fmt"
"github.com/formancehq/go-libs/v2/time"

"github.com/formancehq/go-libs/v2/platform/postgres"
Expand Down Expand Up @@ -182,6 +184,27 @@ func Migrate(ctx context.Context, db bun.IDB) error {
return GetMigrator().Up(ctx, db)
}

func detectDowngrades(migrator *migrations.Migrator, ctx context.Context, db bun.IDB) error {
lastVersion, err := migrator.GetDBVersion(ctx, db)
if err != nil {
if !errors.Is(err, migrations.ErrMissingVersionTable) {
return fmt.Errorf("failed to get last version: %w", err)
}
}
if err == nil && lastVersion != -1 {
allMigrations, err := migrator.GetMigrations(ctx, db)
if err != nil {
return fmt.Errorf("failed to get all migrations: %w", err)
}

if len(allMigrations) < int(lastVersion) {
return newErrRollbackDetected(int(lastVersion), len(allMigrations))
}
}

return nil
}

const aggregateObjects = `
create or replace function public.jsonb_concat(a jsonb, b jsonb) returns jsonb
as 'select $1 || $2'
Expand Down
4 changes: 1 addition & 3 deletions openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3478,9 +3478,7 @@ components:
type: object
properties:
version:
type: integer
format: int64
minimum: 0
type: string
example: 11
name:
type: string
Expand Down
4 changes: 1 addition & 3 deletions openapi/v2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1748,9 +1748,7 @@ components:
type: object
properties:
version:
type: integer
format: int64
minimum: 0
type: string
example: 11
name:
type: string
Expand Down
6 changes: 3 additions & 3 deletions pkg/client/.speakeasy/gen.lock
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
lockVersion: 2.0.0
id: a9ac79e1-e429-4ee3-96c4-ec973f19bec3
management:
docChecksum: 043615f02d6da6be8aa8e5c006587e66
docChecksum: db305ef0d86f319f00b70be811eefeea
docVersion: v1
speakeasyVersion: 1.351.0
generationVersion: 2.384.1
releaseVersion: 0.4.16
configChecksum: 87ddc5f3dd8be9290d0bf5cabd849a48
releaseVersion: 0.4.17
configChecksum: 4844480c9de6ace6cb6abaa8ed6c2fe3
features:
go:
additionalDependencies: 0.1.0
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/.speakeasy/gen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ generation:
auth:
oAuth2ClientCredentialsEnabled: true
go:
version: 0.4.16
version: 0.4.17
additionalDependencies: {}
allowUnknownFieldsInWeakUnions: false
clientServerStatusCodesAsErrors: true
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/docs/models/components/v2migrationinfo.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

| Field | Type | Required | Description | Example |
| ----------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------- |
| `Version` | **int64* | :heavy_minus_sign: | N/A | 11 |
| `Version` | **string* | :heavy_minus_sign: | N/A | 11 |
| `Name` | **string* | :heavy_minus_sign: | N/A | migrations:001 |
| `Date` | [*time.Time](https://pkg.go.dev/time#Time) | :heavy_minus_sign: | N/A | |
| `State` | [*components.V2MigrationInfoState](../../models/components/v2migrationinfostate.md) | :heavy_minus_sign: | N/A | |
4 changes: 2 additions & 2 deletions pkg/client/formance.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ func New(opts ...SDKOption) *Formance {
sdkConfiguration: sdkConfiguration{
Language: "go",
OpenAPIDocVersion: "v1",
SDKVersion: "0.4.16",
SDKVersion: "0.4.17",
GenVersion: "2.384.1",
UserAgent: "speakeasy-sdk/go 0.4.16 2.384.1 v1 github.com/formancehq/stack/ledger/client",
UserAgent: "speakeasy-sdk/go 0.4.17 2.384.1 v1 github.com/formancehq/stack/ledger/client",
Hooks: hooks.New(),
},
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/client/models/components/v2migrationinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (e *V2MigrationInfoState) UnmarshalJSON(data []byte) error {
}

type V2MigrationInfo struct {
Version *int64 `json:"version,omitempty"`
Version *string `json:"version,omitempty"`
Name *string `json:"name,omitempty"`
Date *time.Time `json:"date,omitempty"`
State *V2MigrationInfoState `json:"state,omitempty"`
Expand All @@ -53,7 +53,7 @@ func (v *V2MigrationInfo) UnmarshalJSON(data []byte) error {
return nil
}

func (o *V2MigrationInfo) GetVersion() *int64 {
func (o *V2MigrationInfo) GetVersion() *string {
if o == nil {
return nil
}
Expand Down
Loading

0 comments on commit 32d2250

Please sign in to comment.