Skip to content

Commit a3fdad7

Browse files
committed
Add IDColumn to table context and enhance WithTx tests
1 parent 95252fc commit a3fdad7

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

table.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,8 @@ func (t *Table[T, P, I]) WithTx(tx pgx.Tx) *Table[T, P, I] {
289289
SQL: t.DB.SQL,
290290
Query: t.DB.TxQuery(tx),
291291
},
292-
Name: t.Name,
292+
Name: t.Name,
293+
IDColumn: t.IDColumn,
293294
}
294295
}
295296

tests/table_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"testing"
88

99
sq "github.com/Masterminds/squirrel"
10+
"github.com/jackc/pgx/v5"
1011
"github.com/stretchr/testify/require"
1112
)
1213

@@ -193,6 +194,34 @@ func TestTable(t *testing.T) {
193194
})
194195
require.NoError(t, err, "SaveTx transaction failed")
195196
})
197+
198+
t.Run("WithTx keeps IDColumn", func(t *testing.T) {
199+
ctx := t.Context()
200+
201+
account := &Account{Name: "WithTx IDColumn Account"}
202+
err := db.Accounts.Save(ctx, account)
203+
require.NoError(t, err, "create account failed")
204+
205+
article := &Article{AccountID: account.ID, Author: "WithTx author"}
206+
err = db.Articles.Save(ctx, article)
207+
require.NoError(t, err, "create article failed")
208+
209+
err = pgx.BeginFunc(ctx, db.Conn, func(pgTx pgx.Tx) error {
210+
txTable := db.Articles.Table.WithTx(pgTx)
211+
if err := txTable.HardDeleteByID(ctx, article.ID); err != nil {
212+
return err
213+
}
214+
215+
_, err := txTable.GetByID(ctx, article.ID)
216+
require.Error(t, err, "article should be deleted inside tx")
217+
218+
return nil
219+
})
220+
require.NoError(t, err, "WithTx HardDeleteByID failed")
221+
222+
_, err = db.Articles.GetByID(ctx, article.ID)
223+
require.Error(t, err, "article should be deleted")
224+
})
196225
}
197226

198227
func TestLockForUpdates(t *testing.T) {

0 commit comments

Comments
 (0)