Skip to content

Commit

Permalink
feat: migrate legacy data (null columns)
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Oct 24, 2024
1 parent a8442e0 commit 35b17a8
Show file tree
Hide file tree
Showing 45 changed files with 620 additions and 68 deletions.
38 changes: 6 additions & 32 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 h1:L/gRVlceqvL25
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
github.com/IBM/sarama v1.43.3 h1:Yj6L2IaNvb2mRBop39N7mmJAHBVY3dTPncr3qGVkxPA=
github.com/IBM/sarama v1.43.3/go.mod h1:FVIRaLrhK3Cla/9FfRF5X9Zua2KpS3SYIXxhac1H+FQ=
github.com/Masterminds/semver/v3 v3.2.1 h1:RN9w6+7QoMeJVGyfmbcgs28Br8cvmnucEXnY0rYXWg0=
github.com/Masterminds/semver/v3 v3.2.1/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ=
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 h1:TngWCqHvy9oXAN6lEVMRuU21PR1EtLVZJmdB18Gu3Rw=
Expand All @@ -26,8 +24,6 @@ github.com/alitto/pond v1.9.2 h1:9Qb75z/scEZVCoSU+osVmQ0I0JOeLfdTDafrbcJ8CLs=
github.com/alitto/pond v1.9.2/go.mod h1:xQn3P/sHTYcU/1BR3i86IGIrilcrGC2LiS+E2+CJWsI=
github.com/antlr/antlr4/runtime/Go/antlr v1.4.10 h1:yL7+Jz0jTC6yykIK/Wh74gnTJnrGr5AyrNMXuA0gves=
github.com/antlr/antlr4/runtime/Go/antlr v1.4.10/go.mod h1:F7bn7fEU90QkQ3tnmaTx3LTKLEDqnwWODIYppRQ5hnY=
github.com/antlr4-go/antlr/v4 v4.13.1 h1:SqQKkuVZ+zWkMMNkjy5FZe5mr5WURWnlpmOuzYWrPrQ=
github.com/antlr4-go/antlr/v4 v4.13.1/go.mod h1:GKmUxMtwp6ZgGwZSva4eWPC5mS6vUAmOABFgjdkM7Nw=
github.com/aws/aws-msk-iam-sasl-signer-go v1.0.0 h1:UyjtGmO0Uwl/K+zpzPwLoXzMhcN9xmnR2nrqJoBrg3c=
github.com/aws/aws-msk-iam-sasl-signer-go v1.0.0/go.mod h1:TJAXuFs2HcMib3sN5L0gUC+Q01Qvy3DemvA55WuC+iA=
github.com/aws/aws-sdk-go-v2 v1.32.2 h1:AkNLZEyYMLnx/Q/mSKkcMqwNFXMAvFto9bNsHqcTduI=
Expand Down Expand Up @@ -75,8 +71,6 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dlclark/regexp2 v1.11.4 h1:rPYF9/LECdNymJufQKmri9gV604RvvABwgOA8un7yAo=
github.com/dlclark/regexp2 v1.11.4/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/dnwe/otelsarama v0.0.0-20240308230250-9388d9d40bc0 h1:R2zQhFwSCyyd7L43igYjDrH0wkC/i+QBPELuY0HOu84=
github.com/dnwe/otelsarama v0.0.0-20240308230250-9388d9d40bc0/go.mod h1:2MqLKYJfjs3UriXXF9Fd0Qmh/lhxi/6tHXkqtXxyIHc=
github.com/docker/cli v27.3.1+incompatible h1:qEGdFBF3Xu6SCvCYhc7CzaQTlBmqDuzxPDpigSyeKQQ=
Expand All @@ -87,8 +81,6 @@ github.com/docker/go-connections v0.5.0 h1:USnMq7hx7gwdVZq1L49hLXaFtUdTADjXGp+uj
github.com/docker/go-connections v0.5.0/go.mod h1:ov60Kzw0kKElRwhNs9UlUHAE/F9Fe6GLaXnqyDdmEXc=
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/dop251/goja v0.0.0-20241009100908-5f46f2705ca3 h1:MXsAuToxwsTn5BEEYm2DheqIiC4jWGmkEJ1uy+KFhvQ=
github.com/dop251/goja v0.0.0-20241009100908-5f46f2705ca3/go.mod h1:MxLav0peU43GgvwVgNbLAj1s/bSGboKkhuULvq/7hx4=
github.com/eapache/go-resiliency v1.7.0 h1:n3NRTnBn5N0Cbi/IeOHuQn9s2UwVUH7Ga0ZWcP+9JTA=
github.com/eapache/go-resiliency v1.7.0/go.mod h1:5yPzW0MIvSe0JDsv0v+DvcjEv2FyD6iZYSs1ZI+iQho=
github.com/eapache/go-xerial-snappy v0.0.0-20230731223053-c322873962e3 h1:Oy0F4ALJ04o5Qqpdz8XLIpNA3WM/iSIXqxtqo7UGVws=
Expand All @@ -103,18 +95,14 @@ 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.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.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.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.20241021110602-fbd3b37b93f8 h1:q9mP1jT2q2/QxGdEg9mWOcsf/P7NNvC1vMmyDvj5EEY=
github.com/formancehq/go-libs/v2 v2.0.1-0.20241021110602-fbd3b37b93f8/go.mod h1:KO+eOrTVQ5tR3TZUAHapoQ+d7y2+Ie5Tg0QwfZHAK4k=
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/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 All @@ -131,8 +119,6 @@ github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
github.com/go-ole/go-ole v1.3.0 h1:Dt6ye7+vXGIKZ7Xtk4s6/xVdGDQynvom7xCFEdWr6uE=
github.com/go-ole/go-ole v1.3.0/go.mod h1:5LS6F96DhAwUc7C+1HLexzMXY1xGRSryjyPPKW6zv78=
github.com/go-sourcemap/sourcemap v2.1.3+incompatible h1:W1iEw64niKVGogNgBN3ePyLFfuisuzeidWPMPWmECqU=
github.com/go-sourcemap/sourcemap v2.1.3+incompatible/go.mod h1:F8jJfvm2KbVjc5NqelyYJmf/v5J0dwNLS2mL4sNA1Jg=
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
github.com/go-task/slim-sprig/v3 v3.0.0 h1:sUs3vkvUymDpBKi3qH1YSqBQk9+9D/8M2mN1vB6EwHI=
Expand Down Expand Up @@ -230,8 +216,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 @@ -315,14 +299,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 @@ -426,8 +402,6 @@ golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5y
golang.org/x/crypto v0.6.0/go.mod h1:OFC/31mSvZgRz0V1QTNCzfAI1aIRzbiufJtkMIlEp58=
golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw=
golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U=
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 h1:e66Fs6Z+fZTbFBAxKfP3PALWBtpfqks2bwGcexMxgtk=
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0/go.mod h1:2TbTHSBQa924w8M6Xs1QcRcFwyucIwBGpK1p2f1YFFY=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
Expand Down
2 changes: 1 addition & 1 deletion internal/storage/bucket/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Bucket struct {
}

func (b *Bucket) Migrate(ctx context.Context, tracer trace.Tracer) error {
return Migrate(ctx, tracer, b.db, b.name)
return migrate(ctx, tracer, b.db, b.name)
}

func (b *Bucket) IsUpToDate(ctx context.Context) (bool, error) {
Expand Down
87 changes: 52 additions & 35 deletions internal/storage/bucket/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"embed"
"fmt"
"github.com/formancehq/go-libs/v2/logging"
"github.com/formancehq/go-libs/v2/migrations"
"github.com/ghodss/yaml"
"github.com/uptrace/bun"
Expand All @@ -19,7 +18,7 @@ import (
)

//go:embed migrations
var migrationsDir embed.FS
var MigrationsFS embed.FS

func GetMigrator(name string) *migrations.Migrator {
migrator := migrations.NewMigrator(migrations.WithSchema(name, true))
Expand All @@ -32,19 +31,46 @@ func GetMigrator(name string) *migrations.Migrator {
return migrator
}

func Migrate(ctx context.Context, tracer trace.Tracer, db bun.IDB, name string) error {
func migrate(ctx context.Context, tracer trace.Tracer, db bun.IDB, name string) error {
ctx, span := tracer.Start(ctx, "Migrate bucket")
defer span.End()

return GetMigrator(name).Up(ctx, db)
}

type Notes struct {
type notes struct {
Name string `yaml:"name"`
}

func collectMigrations(name string) ([]migrations.Migration, error) {
entries, err := migrationsDir.ReadDir("migrations")
return WalkMigrations(func(entry fs.DirEntry) (*migrations.Migration, error) {
rawNotes, err := MigrationsFS.ReadFile(filepath.Join("migrations", entry.Name(), "notes.yaml"))
if err != nil {
return nil, fmt.Errorf("failed to read notes.yaml: %w", err)
}

notes := &notes{}
if err := yaml.Unmarshal(rawNotes, notes); err != nil {
return nil, fmt.Errorf("failed to unmarshal notes.yaml: %w", err)
}

sqlFile, err := TemplateSQLFile(name, entry.Name(), "up.sql")
if err != nil {
return nil, fmt.Errorf("failed to template sql file: %w", err)
}

return &migrations.Migration{
Name: notes.Name,
Up: func(ctx context.Context, db bun.IDB) error {
_, err := db.ExecContext(ctx, sqlFile)
return err
},
}, nil
})
}

func WalkMigrations[T any](transformer func(entry fs.DirEntry) (*T, error)) ([]T, error) {
entries, err := MigrationsFS.ReadDir("migrations")
if err != nil {
return nil, err
}
Expand All @@ -65,42 +91,33 @@ func collectMigrations(name string) ([]migrations.Migration, error) {
return int(fileAVersion - fileBVersion)
})

ret := make([]migrations.Migration, len(entries))
ret := make([]T, len(entries))
for i, entry := range entries {
rawNotes, err := migrationsDir.ReadFile(filepath.Join("migrations", entry.Name(), "notes.yaml"))
transformed, err := transformer(entry)
if err != nil {
return nil, fmt.Errorf("failed to read notes.yaml: %w", err)
}

notes := &Notes{}
if err := yaml.Unmarshal(rawNotes, notes); err != nil {
return nil, fmt.Errorf("failed to unmarshal notes.yaml: %w", err)
return nil, fmt.Errorf("failed to transform entry: %w", err)
}
ret[i] = *transformed
}

rawSQL, err := migrationsDir.ReadFile(filepath.Join("migrations", entry.Name(), "migration.sql"))
if err != nil {
return nil, fmt.Errorf("failed to read migration.sql: %w", err)
}
return ret, nil
}

buf := bytes.NewBuffer(nil)
err = template.Must(template.New("migration").
Parse(string(rawSQL))).
Execute(buf, map[string]any{
"Bucket": name,
})
if err != nil {
return nil, fmt.Errorf("failed to execute template: %w", err)
}
func TemplateSQLFile(bucket, migrationDir, file string) (string, error) {
rawSQL, err := MigrationsFS.ReadFile(filepath.Join("migrations", migrationDir, file))
if err != nil {
return "", fmt.Errorf("failed to read file %s: %w", file, err)
}

ret[i] = migrations.Migration{
Name: notes.Name,
Up: func(ctx context.Context, db bun.IDB) error {
logging.FromContext(ctx).Infof("Applying migration %s", notes.Name)
_, err := db.ExecContext(ctx, buf.String())
return err
},
}
buf := bytes.NewBuffer(nil)
err = template.Must(template.New("migration").
Parse(string(rawSQL))).
Execute(buf, map[string]any{
"Bucket": bucket,
})
if err != nil {
panic(err)
}

return ret, nil
return buf.String(), nil
}
43 changes: 43 additions & 0 deletions internal/storage/bucket/migrations/0-init-schema/tests_after.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
set search_path = '{{.Bucket}}';

create extension if not exists "uuid-ossp";

insert into logs(seq, ledger, id, type, date, data, hash)
select
seq,
'ledger' || seq % 5,
(seq/5) + (seq % 5),
'NEW_TRANSACTION',
now(),
('{'
'"transaction": {'
'"id": ' || (seq/5) + (seq % 5) || ','
'"timestamp": "' || now() || '",'
'"postings": ['
'{'
'"source": "world",'
'"destination": "orders:' || seq || '",'
'"asset": "USD",'
'"amount": 100'
'},'
'{'
'"destination": "fees",'
'"source": "orders:' || seq || '",'
'"asset": "USD",'
'"amount": 1'
'},'
'{'
'"destination": "sellers:' || (seq % 5) || '",'
'"source": "orders:' || seq || '",'
'"asset": "USD",'
'"amount": 99'
'}'
'],'
'"metadata": { "tax": "1%" }'
'},'
'"accountMetadata": {'
'"orders:' || seq || '": { "tax": "1%" }'
'}'
'}')::jsonb,
'invalid-hash'
from generate_series(0, 100) as seq;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
name: Fill transaction ids of table moves
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
set search_path = '{{.Bucket}}';

do $$
begin
assert (
select count(*)
from moves
where transactions_id is null
) = 0, 'Still some rows with null transactions_id';
end$$;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
set search_path = '{{.Bucket}}';

do $$
begin
assert (
select count(*)
from moves
where transactions_id is null
) > 0, 'Should have some transactions with null transactions_id';
end$$;
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
set search_path = '{{.Bucket}}';

do $$
declare
_batch_size integer := 30;
begin
loop
with _outdated_moves as (
select *
from moves
where transactions_id is null
limit _batch_size
)
update moves
set transactions_id = (
select id
from transactions
where seq = moves.transactions_seq
)
from _outdated_moves
where moves.seq in (_outdated_moves.seq);

exit when not found;
end loop;
end
$$;

alter table moves
alter column transactions_id set not null;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
name: Fill inserted_at column of transactions table
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
set search_path = '{{.Bucket}}';

do $$
begin
assert (
select bool_and(log.date = transactions.inserted_at)
from logs log
join transactions on transactions.id = log.id
), 'Insertion dates of logs and transactions should not match';
end$$;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
set search_path = '{{.Bucket}}';

do $$
begin
assert (
select not bool_and(log.date = transactions.inserted_at)
from logs log
join transactions on transactions.id = log.id
), 'Insertion dates of logs and transactions should match';
end$$;
Loading

0 comments on commit 35b17a8

Please sign in to comment.