chainlink-common
chainlink-common copied to clipboard
Extract txdb into common to make it reusable from other repositories.
Txdb is used in tests as an SQL driver allowing to write tests which autorollback their transactions once they are finished.
Txdb has been part of the internal chainlink core node https://github.com/smartcontractkit/chainlink/blob/develop/core/internal/testutils/pgtest/txdb.go. This change makes it accessible from other smartcontractkit repositories.
The txdb.go file in this PR is identical to https://github.com/smartcontractkit/chainlink/blob/develop/core/internal/testutils/pgtest/txdb.go except of two changes:
- the init() part is removed: this part is responsible for parsing CL_DATABASE_URL in tests and is independent of the driver. The tests can figure out themselves which path to use.
- the dialects.Postgres is removed. Instead we parse the dialect directly from d.dbURL.
The txdb_test.go file in this PR is identical to https://github.com/smartcontractkit/chainlink/blob/develop/core/internal/testutils/pgtest/txdb_test.go except of the following two changes:
- instead of getting external db, we use in memory duckdb for testing:
duckdb, err := sql.Open("duckdb", "")
const driverName = "txdb"
sql.Register(driverName, &txDriver{
db: duckdb,
conns: make(map[string]*conn),
})
sqlx.BindDriver(driverName, sqlx.DOLLAR)
db, err := sqlx.Open(driverName, uuid.New().String())
require.NoError(t, err)
t.Cleanup(func() { assert.NoError(t, db.Close()) })
db.MapperFunc(reflectx.CamelToSnakeASCII)
- since duckdb takes milliseconds to setup, I have also reduced the timeout from 10 seconds to 2 when waiting for the transaction rollback to be executed:
time.Sleep(time.Second * 2)