Skip to content
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

Extract txdb into common to make it reusable from other repositories. #914

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

pavel-raykov
Copy link
Contributor

@pavel-raykov pavel-raykov commented Nov 4, 2024

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant