Repository navigation
feat(ladder): preserve tranche amount provenance #886
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package config | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "math/big" | ||
| ) | ||
|
|
||
| type LadderAmountBasis string | ||
|
|
||
| const ( | ||
| LadderAmountOnDemand LadderAmountBasis = "on_demand_equivalent" | ||
| LadderAmountCommitment LadderAmountBasis = "commitment" | ||
| ) | ||
|
|
||
| func trancheCommitmentRate(tr *LadderTrancheDB) (*string, error) { | ||
| if tr.AmountBasis == nil { | ||
| if tr.CommitmentRate != nil { | ||
| return nil, fmt.Errorf("commitment rate requires a known amount basis") | ||
| } | ||
| return nil, nil | ||
| } | ||
| switch *tr.AmountBasis { | ||
| case LadderAmountOnDemand: | ||
| if tr.CommitmentRate != nil { | ||
| return nil, fmt.Errorf("on-demand-equivalent amount must not have a commitment rate") | ||
| } | ||
| return nil, nil | ||
| case LadderAmountCommitment: | ||
| if tr.CommitmentRate == nil { | ||
| return nil, fmt.Errorf("commitment amount requires its stored rate") | ||
| } | ||
| rate, ok := new(big.Rat).SetString(*tr.CommitmentRate) | ||
| if !ok || rate.Sign() <= 0 || rate.Cmp(big.NewRat(1, 1)) > 0 { | ||
| return nil, fmt.Errorf("stored commitment rate must be a rational in (0, 1]") | ||
| } | ||
| canonical := rate.RatString() | ||
| return &canonical, nil | ||
| default: | ||
| return nil, fmt.Errorf("unknown ladder amount basis %q", *tr.AmountBasis) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
internal/database/postgres/migrations/000121_ladder_tranche_amount_provenance.down.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| ALTER TABLE ladder_tranches DROP CONSTRAINT ladder_tranche_amount_provenance; | ||
| ALTER TABLE ladder_tranches | ||
| DROP COLUMN commitment_rate, | ||
| DROP COLUMN amount_basis; |
11 changes: 11 additions & 0 deletions
11
internal/database/postgres/migrations/000121_ladder_tranche_amount_provenance.up.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| ALTER TABLE ladder_tranches | ||
| ADD COLUMN amount_basis TEXT, | ||
| ADD COLUMN commitment_rate TEXT; | ||
|
|
||
| ALTER TABLE ladder_tranches ADD CONSTRAINT ladder_tranche_amount_provenance CHECK ( | ||
| (amount_basis IS NULL AND commitment_rate IS NULL) | ||
| OR (amount_basis IS NOT NULL AND ( | ||
| (amount_basis = 'on_demand_equivalent' AND commitment_rate IS NULL) | ||
| OR (amount_basis = 'commitment' AND commitment_rate IS NOT NULL AND commitment_rate <> '') | ||
| )) | ||
| ) NOT VALID; | ||
65 changes: 65 additions & 0 deletions
65
internal/database/postgres/migrations/000121_ladder_tranche_amount_provenance_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| //go:build integration | ||
|
|
||
| package migrations_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/LeanerCloud/cloud-commitments-platform/internal/database/postgres/migrations" | ||
| "github.com/LeanerCloud/cloud-commitments-platform/internal/database/postgres/testhelpers" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestMigration_LadderTrancheAmountProvenance(t *testing.T) { | ||
| ctx := context.Background() | ||
| container := testhelpers.RequirePostgresContainer(ctx, t) | ||
| t.Cleanup(func() { require.NoError(t, container.Cleanup(context.Background())) }) | ||
| pool, path := container.DB.Pool(), getMigrationsPath() | ||
| require.NoError(t, migrations.MigrateToVersion(ctx, pool, path, 120)) | ||
| var id string | ||
| require.NoError(t, pool.QueryRow(ctx, `INSERT INTO ladder_tranches | ||
| (layer_type,amount_usd_hr,term,payment_option,scheduled_date,status) | ||
| VALUES ('compute-sp',12.5,'1y','no_upfront',NOW(),'scheduled') RETURNING id`).Scan(&id)) | ||
| require.NoError(t, migrations.MigrateToVersion(ctx, pool, path, 121)) | ||
| var validated bool | ||
| require.NoError(t, pool.QueryRow(ctx, `SELECT convalidated FROM pg_constraint WHERE conname='ladder_tranche_amount_provenance'`).Scan(&validated)) | ||
| require.False(t, validated) | ||
| var amount string | ||
| var basis, rate *string | ||
| require.NoError(t, pool.QueryRow(ctx, `SELECT amount_usd_hr::text,amount_basis,commitment_rate | ||
| FROM ladder_tranches WHERE id=$1`, id).Scan(&amount, &basis, &rate)) | ||
| require.Equal(t, "12.500000", amount) | ||
| require.Nil(t, basis) | ||
| require.Nil(t, rate) | ||
| for _, pair := range [][2]any{{"on_demand_equivalent", nil}, {"commitment", "1/3"}, {nil, nil}} { | ||
| _, err := pool.Exec(ctx, `UPDATE ladder_tranches SET amount_basis=$2,commitment_rate=$3 WHERE id=$1`, id, pair[0], pair[1]) | ||
| require.NoError(t, err) | ||
| } | ||
| for _, pair := range [][2]any{{nil, "1/3"}, {"on_demand_equivalent", "1/3"}, {"commitment", nil}, {"commitment", ""}, {"unknown", nil}} { | ||
| _, err := pool.Exec(ctx, `UPDATE ladder_tranches SET amount_basis=$2,commitment_rate=$3 WHERE id=$1`, id, pair[0], pair[1]) | ||
| require.Error(t, err, "invalid provenance pair %v", pair) | ||
| } | ||
| require.NoError(t, migrations.MigrateToVersion(ctx, pool, path, 122)) | ||
| require.NoError(t, pool.QueryRow(ctx, `SELECT convalidated FROM pg_constraint WHERE conname='ladder_tranche_amount_provenance'`).Scan(&validated)) | ||
| require.True(t, validated) | ||
| require.NoError(t, migrations.MigrateToVersion(ctx, pool, path, 121)) | ||
| require.NoError(t, pool.QueryRow(ctx, `SELECT convalidated FROM pg_constraint WHERE conname='ladder_tranche_amount_provenance'`).Scan(&validated)) | ||
| require.False(t, validated) | ||
| _, err := pool.Exec(ctx, `UPDATE ladder_tranches SET amount_basis='commitment' WHERE id=$1`, id) | ||
| require.Error(t, err) | ||
| require.NoError(t, migrations.MigrateToVersion(ctx, pool, path, 120)) | ||
| var columns int | ||
| require.NoError(t, pool.QueryRow(ctx, `SELECT count(*) FROM information_schema.columns | ||
| WHERE table_name='ladder_tranches' AND column_name IN ('amount_basis','commitment_rate')`).Scan(&columns)) | ||
| require.Zero(t, columns) | ||
| require.NoError(t, pool.QueryRow(ctx, `SELECT amount_usd_hr::text FROM ladder_tranches WHERE id=$1`, id).Scan(&amount)) | ||
| require.Equal(t, "12.500000", amount) | ||
| require.NoError(t, migrations.MigrateToVersion(ctx, pool, path, 122)) | ||
| require.NoError(t, pool.QueryRow(ctx, `SELECT convalidated FROM pg_constraint WHERE conname='ladder_tranche_amount_provenance'`).Scan(&validated)) | ||
| require.True(t, validated) | ||
| require.NoError(t, pool.QueryRow(ctx, `SELECT amount_usd_hr::text,amount_basis,commitment_rate FROM ladder_tranches WHERE id=$1`, id).Scan(&amount, &basis, &rate)) | ||
| require.Equal(t, "12.500000", amount) | ||
| require.Nil(t, basis) | ||
| require.Nil(t, rate) | ||
| } |
8 changes: 8 additions & 0 deletions
8
...al/database/postgres/migrations/000122_validate_ladder_tranche_amount_provenance.down.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| ALTER TABLE ladder_tranches DROP CONSTRAINT ladder_tranche_amount_provenance; | ||
| ALTER TABLE ladder_tranches ADD CONSTRAINT ladder_tranche_amount_provenance CHECK ( | ||
| (amount_basis IS NULL AND commitment_rate IS NULL) | ||
| OR (amount_basis IS NOT NULL AND ( | ||
| (amount_basis = 'on_demand_equivalent' AND commitment_rate IS NULL) | ||
| OR (amount_basis = 'commitment' AND commitment_rate IS NOT NULL AND commitment_rate <> '') | ||
| )) | ||
| ) NOT VALID; |
1 change: 1 addition & 0 deletions
1
...rnal/database/postgres/migrations/000122_validate_ladder_tranche_amount_provenance.up.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ALTER TABLE ladder_tranches VALIDATE CONSTRAINT ladder_tranche_amount_provenance; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.