Skip to content

Commit

Permalink
fix: operator payments migration (#402)
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-nicolas committed Jun 30, 2023
1 parent f043e23 commit d3784df
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 39 deletions.
6 changes: 3 additions & 3 deletions components/operator/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ require (
github.com/go-openapi/swag v0.22.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/gnostic v0.6.9 // indirect
github.com/google/gofuzz v1.2.0 // indirect
Expand Down Expand Up @@ -149,9 +149,9 @@ require (
go.uber.org/fx v1.19.1 // indirect
go.uber.org/multierr v1.9.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/crypto v0.6.0 // indirect
golang.org/x/crypto v0.7.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/oauth2 v0.5.0 // indirect
golang.org/x/oauth2 v0.6.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.7.0 // indirect
golang.org/x/term v0.6.0 // indirect
Expand Down
11 changes: 6 additions & 5 deletions components/operator/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,9 @@ github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QD
github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
Expand Down Expand Up @@ -576,8 +577,8 @@ golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.6.0 h1:qfktjS5LUO+fFKeJXZ+ikTRijMmljikvG68fpMMruSc=
golang.org/x/crypto v0.6.0/go.mod h1:OFC/31mSvZgRz0V1QTNCzfAI1aIRzbiufJtkMIlEp58=
golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A=
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
Expand Down Expand Up @@ -665,8 +666,8 @@ golang.org/x/oauth2 v0.0.0-20201208152858-08078c50e5b5/go.mod h1:KelEdhl1UZF7XfJ
golang.org/x/oauth2 v0.0.0-20210218202405-ba52d332ba99/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
golang.org/x/oauth2 v0.0.0-20210323180902-22b0adad7558/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
golang.org/x/oauth2 v0.5.0 h1:HuArIo48skDwlrvM3sEdHXElYslAMsf3KwRkkW4MC4s=
golang.org/x/oauth2 v0.5.0/go.mod h1:9/XBHVqLaWO3/BRHs5jbpYCnOZVjj5V0ndyaAM7KB4I=
golang.org/x/oauth2 v0.6.0 h1:Lh8GPgSKBfWSwFvtuWOfeI3aAAnbXTSutYxJiOJFgIw=
golang.org/x/oauth2 v0.6.0/go.mod h1:ycmewcwgD4Rpr3eZJLSB4Kyyljb3qDh40vJ8STE5HKw=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,11 @@ data:
"match": {
"indexed.provider": this.payload.connector
}
},
{
"match": {
"stack": env("STACK")
}
}
]
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apiVersion: stack.formance.com/v1beta3
kind: Migration
metadata:
annotations:
reloader.stakater.com/auto: "true"
generation: 1
labels:
stack: "true"
name: payments-v0.6.7-pre-upgrade
namespace: multipod-latest
spec:
configuration: multipod-latest
module: payments
postUpgrade: false
targetedVersion: v0.6.7
version: multipod-latest
status:
terminated: true
83 changes: 52 additions & 31 deletions components/operator/internal/handlers/handler_payments.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,7 @@ func init() {
},
"v0.6.5": {
PreUpgrade: func(ctx modules.Context) error {
postgresUri := fmt.Sprintf(
"postgresql://%s:%s@%s:%d/%s?sslmode=disable",
ctx.Configuration.Spec.Services.Payments.Postgres.Username,
ctx.Configuration.Spec.Services.Payments.Postgres.Password,
ctx.Configuration.Spec.Services.Payments.Postgres.Host,
ctx.Configuration.Spec.Services.Payments.Postgres.Port,
ctx.Stack.GetServiceName("payments"),
)

db, err := sql.Open("postgres", postgresUri)
if err != nil {
return err
}
bunDB := bun.NewDB(db, pgdialect.New())

return cmd.Migrate(ctx.Context, bunDB)
return paymentsPreUpgradeMigration(ctx)
},
PostUpgrade: func(ctx modules.PostInstallContext) error {
if err := resetConnectors(ctx, "stripe"); err != nil {
Expand All @@ -98,27 +83,63 @@ func init() {
return nil
},
Services: func(ctx modules.ModuleContext) modules.Services {
return modules.Services{{
InjectPostgresVariables: true,
HasVersionEndpoint: true,
ListenEnvVar: "LISTEN",
ExposeHTTP: true,
NeedTopic: true,
Liveness: modules.LivenessLegacy,
Container: func(resolveContext modules.ContainerResolutionContext) modules.Container {
return modules.Container{
Env: env(resolveContext),
Image: modules.GetImage("payments", resolveContext.Versions.Spec.Payments),
Resources: modules.ResourceSizeSmall(),
}
},
}}
return paymentsServices(ctx, env)
},
},
"v0.6.7": {
PreUpgrade: func(ctx modules.Context) error {
return paymentsPreUpgradeMigration(ctx)
},
Services: func(ctx modules.ModuleContext) modules.Services {
return paymentsServices(ctx, env)
},
},
},
})
}

func paymentsPreUpgradeMigration(ctx modules.Context) error {
postgresUri := fmt.Sprintf(
"postgresql://%s:%s@%s:%d/%s?sslmode=disable",
ctx.Configuration.Spec.Services.Payments.Postgres.Username,
ctx.Configuration.Spec.Services.Payments.Postgres.Password,
ctx.Configuration.Spec.Services.Payments.Postgres.Host,
ctx.Configuration.Spec.Services.Payments.Postgres.Port,
ctx.Stack.GetServiceName("payments"),
)

db, err := sql.Open("postgres", postgresUri)
if err != nil {
return err
}

bunDB := bun.NewDB(db, pgdialect.New())
defer bunDB.Close()

return cmd.Migrate(ctx.Context, bunDB)
}

func paymentsServices(
ctx modules.ModuleContext,
env func(resolveContext modules.ContainerResolutionContext) modules.ContainerEnv,
) modules.Services {
return modules.Services{{
InjectPostgresVariables: true,
HasVersionEndpoint: true,
ListenEnvVar: "LISTEN",
ExposeHTTP: true,
NeedTopic: true,
Liveness: modules.LivenessLegacy,
Container: func(resolveContext modules.ContainerResolutionContext) modules.Container {
return modules.Container{
Env: env(resolveContext),
Image: modules.GetImage("payments", resolveContext.Versions.Spec.Payments),
Resources: modules.ResourceSizeSmall(),
}
},
}}
}

func resetConnectors(ctx modules.PostInstallContext, connector string) error {
endpoint := fmt.Sprintf(
"http://payments.%s.svc:%d/connectors/%s/reset",
Expand Down

1 comment on commit d3784df

@vercel
Copy link

@vercel vercel bot commented on d3784df Jun 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.