Skip to content

Commit

Permalink
chore: reorder migrations and clean next-minor todos
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Nov 8, 2024
1 parent e91cad2 commit f91b6c4
Show file tree
Hide file tree
Showing 32 changed files with 7 additions and 90 deletions.
2 changes: 1 addition & 1 deletion internal/storage/bucket/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

// stateless version (+1 regarding directory name, as migrations start from 1 in the lib)
const MinimalSchemaVersion = 12
const MinimalSchemaVersion = 15

type Bucket struct {
name string
Expand Down
3 changes: 0 additions & 3 deletions internal/storage/bucket/migrations/11-make-stateless/up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,6 @@ execute procedure set_compat_on_transactions_metadata();

alter table transactions
add column post_commit_volumes jsonb,
-- todo: set in subsequent migration `default transaction_date()`,
-- otherwise the function is called for every existing lines
add column inserted_at timestamp without time zone,
alter column timestamp set default transaction_date()
-- todo: we should change the type of this column, but actually it cause a full lock of the table
Expand Down Expand Up @@ -363,7 +361,6 @@ from (select row_number() over () as number, v.value
select null) v) data
$$ set search_path from current;

-- todo(next-minor): remove that on future version when the table will have this default value (need to fill nulls before)
create or replace function set_transaction_inserted_at() returns trigger
security definer
language plpgsql
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,11 @@ do $$
end loop;

drop table logs_transactions;

alter table transactions
alter column inserted_at set default transaction_date();

drop trigger set_transaction_inserted_at on transactions;
drop function set_transaction_inserted_at;
end
$$;
5 changes: 0 additions & 5 deletions internal/storage/ledger/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,11 +401,6 @@ func (s *Store) InsertTransaction(ctx context.Context, tx *ledger.Transaction) e
if err.(postgres.ErrConstraintsFailed).GetConstraint() == "transactions_reference" {
return nil, ledgercontroller.NewErrTransactionReferenceConflict(tx.Reference)
}
case errors.Is(err, postgres.ErrRaisedException{}):
// todo(next-minor): remove this test
if err.(postgres.ErrRaisedException).GetMessage() == "duplicate reference" {
return nil, ledgercontroller.NewErrTransactionReferenceConflict(tx.Reference)
}
default:
return nil, err
}
Expand Down
81 changes: 0 additions & 81 deletions internal/storage/ledger/transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import (
"database/sql"
"fmt"
"github.com/alitto/pond"
"github.com/formancehq/ledger/internal/storage/bucket"
ledgerstore "github.com/formancehq/ledger/internal/storage/ledger"
"github.com/google/uuid"
"math/big"
"slices"
"testing"
Expand Down Expand Up @@ -606,84 +603,6 @@ func TestTransactionsInsert(t *testing.T) {
require.Error(t, err)
require.True(t, errors.Is(err, ledgercontroller.ErrTransactionReferenceConflict{}))
})
// todo(next-minor): remove this test
t.Run("check reference conflict with minimal store version", func(t *testing.T) {
t.Parallel()

driver := newDriver(t)
ledgerName := uuid.NewString()[:8]

l := ledger.MustNewWithDefault(ledgerName)
l.Bucket = ledgerName

migrator := bucket.GetMigrator(driver.GetDB(), ledgerName)
for i := 0; i < bucket.MinimalSchemaVersion; i++ {
require.NoError(t, migrator.UpByOne(ctx))
}

b := bucket.New(driver.GetDB(), ledgerName)
err := b.AddLedger(ctx, l, driver.GetDB())
require.NoError(t, err)

store := ledgerstore.New(driver.GetDB(), b, l)

const nbTry = 100

for i := 0; i < nbTry; i++ {
errChan := make(chan error, 2)

// Create a simple tx
tx1 := ledger.Transaction{
TransactionData: ledger.TransactionData{
Timestamp: now,
Reference: fmt.Sprintf("foo:%d", i),
Postings: []ledger.Posting{
ledger.NewPosting("world", "bank", "USD/2", big.NewInt(100)),
},
},
}
go func() {
errChan <- store.InsertTransaction(ctx, &tx1)
}()

// Create another tx with the same reference
tx2 := ledger.Transaction{
TransactionData: ledger.TransactionData{
Timestamp: now,
Reference: fmt.Sprintf("foo:%d", i),
Postings: []ledger.Posting{
ledger.NewPosting("world", "bank", "USD/2", big.NewInt(100)),
},
},
}
go func() {
errChan <- store.InsertTransaction(ctx, &tx2)
}()

select {
case err1 := <-errChan:
if err1 != nil {
require.True(t, errors.Is(err1, ledgercontroller.ErrTransactionReferenceConflict{}))
select {
case err2 := <-errChan:
require.NoError(t, err2)
case <-time.After(time.Second):
require.Fail(t, "should have received an error")
}
} else {
select {
case err2 := <-errChan:
require.Error(t, err2)
require.True(t, errors.Is(err2, ledgercontroller.ErrTransactionReferenceConflict{}))
case <-time.After(time.Second):
require.Fail(t, "should have received an error")
}
}
case <-time.After(time.Second):
require.Fail(t, "should have received an error")
}
}
})
t.Run("check denormalization", func(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit f91b6c4

Please sign in to comment.